When BGP session goes up/down, BGPSpeaker can detect changing the session.

Signed-off-by: Toshiki Tsuboi <[email protected]>
---
 doc/source/library_bgp_speaker.rst       |  6 +++++-
 ryu/services/protocols/bgp/bgpspeaker.py | 36 ++++++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/doc/source/library_bgp_speaker.rst 
b/doc/source/library_bgp_speaker.rst
index e17d824..cf80926 100644
--- a/doc/source/library_bgp_speaker.rst
+++ b/doc/source/library_bgp_speaker.rst
@@ -38,9 +38,13 @@ instance advertizes some prefixes.
         print 'the best path changed:', event.remote_as, event.prefix,\
             event.nexthop, event.is_withdraw
 
+    def detect_peer_down(remote_ip, remote_as):
+        print 'Peer down:', remote_ip, remote_as
+
     if __name__ == "__main__":
         speaker = BGPSpeaker(as_number=64512, router_id='10.0.0.1',
-                             
best_path_change_handler=dump_remote_best_path_change)
+                             
best_path_change_handler=dump_remote_best_path_change,
+                             peer_down_handler=detect_peer_down)
 
         speaker.neighbor_add('192.168.177.32', 64513)
        # uncomment the below line if the speaker needs to talk with a bmp 
server.
diff --git a/ryu/services/protocols/bgp/bgpspeaker.py 
b/ryu/services/protocols/bgp/bgpspeaker.py
index 87fdfe5..6479bf8 100644
--- a/ryu/services/protocols/bgp/bgpspeaker.py
+++ b/ryu/services/protocols/bgp/bgpspeaker.py
@@ -108,6 +108,8 @@ class BGPSpeaker(object):
                  refresh_stalepath_time=DEFAULT_REFRESH_STALEPATH_TIME,
                  refresh_max_eor_time=DEFAULT_REFRESH_MAX_EOR_TIME,
                  best_path_change_handler=None,
+                 peer_down_handler=None,
+                 peer_up_handler=None,
                  ssh_console=False,
                  label_range=DEFAULT_LABEL_RANGE):
         """Create a new BGPSpeaker object with as_number and router_id to
@@ -137,6 +139,12 @@ class BGPSpeaker(object):
         peer down. The handler is supposed to take one argument, the
         instance of an EventPrefix class instance.
 
+        ``peer_down_handler``, if specified, is called when BGP peering
+        session goes down.
+
+        ``peer_up_handler``, if specified, is called when BGP peering
+        session goes up.
+
         """
         super(BGPSpeaker, self).__init__()
 
@@ -150,9 +158,23 @@ class BGPSpeaker(object):
         self._core_start(settings)
         self._init_signal_listeners()
         self._best_path_change_handler = best_path_change_handler
+        self._peer_down_handler = peer_down_handler
+        self._peer_up_handler = peer_up_handler
         if ssh_console:
             hub.spawn(ssh.SSH_CLI_CONTROLLER.start)
 
+    def _notify_peer_down(self, peer):
+        remote_ip = peer.protocol.recv_open_msg.bgp_identifier
+        remote_as = peer.protocol.recv_open_msg.my_as
+        if self._peer_down_handler:
+            self._peer_down_handler(remote_ip, remote_as)
+
+    def _notify_peer_up(self, peer):
+        remote_ip = peer.protocol.recv_open_msg.bgp_identifier
+        remote_as = peer.protocol.recv_open_msg.my_as
+        if self._peer_up_handler:
+            self._peer_up_handler(remote_ip, remote_as)
+
     def _notify_best_path_changed(self, path, is_withdraw):
         if path.source:
             nexthop = path.nexthop
@@ -182,8 +204,18 @@ class BGPSpeaker(object):
         CORE_MANAGER.get_core_service()._signal_bus.register_listener(
             BgpSignalBus.BGP_BEST_PATH_CHANGED,
             lambda _, info:
-                self._notify_best_path_changed(info['path'],
-                                               info['is_withdraw'])
+            self._notify_best_path_changed(info['path'],
+                                           info['is_withdraw'])
+        )
+        CORE_MANAGER.get_core_service()._signal_bus.register_listener(
+            BgpSignalBus.BGP_ADJ_DOWN,
+            lambda _, info:
+            self._notify_peer_down(info['peer'])
+        )
+        CORE_MANAGER.get_core_service()._signal_bus.register_listener(
+            BgpSignalBus.BGP_ADJ_UP,
+            lambda _, info:
+            self._notify_peer_up(info['peer'])
         )
 
     def _core_start(self, settings):
-- 
1.9.3 (Apple Git-50)


------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to