1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 from ryu.base import app_manager
21 from ryu.controller import handler
22 from ryu.lib import hub
23 from ryu.topology import event
24 from ryu.topology import switches
26 LOG = logging.getLogger(__name__)
29 class DiscoveryEventDumper(app_manager.RyuApp):
30 ''' This app dumps discovery events
33 'switches': switches.Switches,
36 def __init__(self, *args, **kwargs):
37 super(DiscoveryEventDumper, self).__init__(*args, **kwargs)
39 # For testing when sync and async request.
40 # self.threads.append(
41 # hub.spawn(self._switch_request_sync, 5))
42 # self.threads.append(
43 # hub.spawn(self._switch_request_async, 10))
45 # self.threads.append(
46 # hub.spawn(self._link_request_sync, 5))
47 # self.threads.append(
48 # hub.spawn(self._link_request_async, 10))
52 @handler.set_ev_cls(event.EventSwitchEnter)
53 def switch_enter_handler(self, ev):
56 @handler.set_ev_cls(event.EventSwitchLeave)
57 def switch_leave_handler(self, ev):
60 @handler.set_ev_cls(event.EventPortAdd)
61 def port_add_handler(self, ev):
64 @handler.set_ev_cls(event.EventPortDelete)
65 def port_delete_handler(self, ev):
68 @handler.set_ev_cls(event.EventPortModify)
69 def port_modify_handler(self, ev):
72 @handler.set_ev_cls(event.EventLinkAdd)
73 def link_add_handler(self, ev):
76 @handler.set_ev_cls(event.EventLinkDelete)
77 def link_del_handler(self, ev):
80 def _switch_request_sync(self, interval):
82 request = event.EventSwitchRequest()
83 LOG.debug('switch_request sync %s thread(%s)',
84 request, id(hub.getcurrent()))
85 reply = self.send_request(request)
86 LOG.debug('switch_reply sync %s', reply)
87 if len(reply.switches) > 0:
88 for sw in reply.switches:
92 def _switch_request_async(self, interval):
94 request = event.EventSwitchRequest()
95 LOG.debug('switch_request async %s thread(%s)',
96 request, id(hub.getcurrent()))
97 self.send_event(request.dst, request)
103 if time.time() > start + i:
105 LOG.debug(' thread is busy... %s/%s thread(%s)',
106 i, busy, id(hub.getcurrent()))
107 LOG.debug(' thread yield to switch_reply handler. thread(%s)',
108 id(hub.getcurrent()))
113 LOG.debug(' thread get back. thread(%s)',
114 id(hub.getcurrent()))
115 hub.sleep(interval - busy)
117 @handler.set_ev_cls(event.EventSwitchReply)
118 def switch_reply_handler(self, reply):
119 LOG.debug('switch_reply async %s', reply)
120 if len(reply.switches) > 0:
121 for sw in reply.switches:
124 def _link_request_sync(self, interval):
125 while self.is_active:
126 request = event.EventLinkRequest()
127 LOG.debug('link_request sync %s thread(%s)',
128 request, id(hub.getcurrent()))
129 reply = self.send_request(request)
130 LOG.debug('link_reply sync %s', reply)
131 if len(reply.links) > 0:
132 for link in reply.links:
133 LOG.debug(' %s', link)
136 def _link_request_async(self, interval):
137 while self.is_active:
138 request = event.EventLinkRequest()
139 LOG.debug('link_request async %s thread(%s)',
140 request, id(hub.getcurrent()))
141 self.send_event(request.dst, request)
147 if time.time() > start + i:
149 LOG.debug(' thread is busy... %s/%s thread(%s)',
150 i, busy, id(hub.getcurrent()))
151 LOG.debug(' thread yield to link_reply handler. thread(%s)',
152 id(hub.getcurrent()))
157 LOG.debug(' thread get back. thread(%s)',
158 id(hub.getcurrent()))
159 hub.sleep(interval - busy)
161 @handler.set_ev_cls(event.EventLinkReply)
162 def link_reply_handler(self, reply):
163 LOG.debug('link_reply async %s', reply)
164 if len(reply.links) > 0:
165 for link in reply.links:
166 LOG.debug(' %s', link)