backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / topology / dumper.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 import logging
18 import time
19
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
25
26 LOG = logging.getLogger(__name__)
27
28
29 class DiscoveryEventDumper(app_manager.RyuApp):
30     ''' This app dumps discovery events
31     '''
32     _CONTEXTS = {
33         'switches': switches.Switches,
34     }
35
36     def __init__(self, *args, **kwargs):
37         super(DiscoveryEventDumper, self).__init__(*args, **kwargs)
38
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))
44 #
45 #        self.threads.append(
46 #            hub.spawn(self._link_request_sync, 5))
47 #        self.threads.append(
48 #            hub.spawn(self._link_request_async, 10))
49
50         self.is_active = True
51
52     @handler.set_ev_cls(event.EventSwitchEnter)
53     def switch_enter_handler(self, ev):
54         LOG.debug(ev)
55
56     @handler.set_ev_cls(event.EventSwitchLeave)
57     def switch_leave_handler(self, ev):
58         LOG.debug(ev)
59
60     @handler.set_ev_cls(event.EventPortAdd)
61     def port_add_handler(self, ev):
62         LOG.debug(ev)
63
64     @handler.set_ev_cls(event.EventPortDelete)
65     def port_delete_handler(self, ev):
66         LOG.debug(ev)
67
68     @handler.set_ev_cls(event.EventPortModify)
69     def port_modify_handler(self, ev):
70         LOG.debug(ev)
71
72     @handler.set_ev_cls(event.EventLinkAdd)
73     def link_add_handler(self, ev):
74         LOG.debug(ev)
75
76     @handler.set_ev_cls(event.EventLinkDelete)
77     def link_del_handler(self, ev):
78         LOG.debug(ev)
79
80     def _switch_request_sync(self, interval):
81         while self.is_active:
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:
89                     LOG.debug('  %s', sw)
90             hub.sleep(interval)
91
92     def _switch_request_async(self, interval):
93         while self.is_active:
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)
98
99             start = time.time()
100             busy = interval / 2
101             i = 0
102             while i < busy:
103                 if time.time() > start + i:
104                     i += 1
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()))
109
110             # yield
111             hub.sleep(0)
112
113             LOG.debug('  thread get back. thread(%s)',
114                       id(hub.getcurrent()))
115             hub.sleep(interval - busy)
116
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:
122                 LOG.debug('  %s', sw)
123
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)
134             hub.sleep(interval)
135
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)
142
143             start = time.time()
144             busy = interval / 2
145             i = 0
146             while i < busy:
147                 if time.time() > start + i:
148                     i += 1
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()))
153
154             # yield
155             hub.sleep(0)
156
157             LOG.debug('  thread get back. thread(%s)',
158                       id(hub.getcurrent()))
159             hub.sleep(interval - busy)
160
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)