backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / controller / ofp_event.py
1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """
18 OpenFlow event definitions.
19 """
20
21 import inspect
22 import time
23
24 from ryu.controller import handler
25 from ryu import ofproto
26 from . import event
27
28
29 NAME = 'ofp_event'
30
31
32 class EventOFPMsgBase(event.EventBase):
33     """
34     The base class of OpenFlow event class.
35
36     OpenFlow event classes have at least the following attributes.
37
38     .. tabularcolumns:: |l|L|
39
40     ============ ==============================================================
41     Attribute    Description
42     ============ ==============================================================
43     msg          An object which describes the corresponding OpenFlow message.
44     msg.datapath A ryu.controller.controller.Datapath instance
45                  which describes an OpenFlow switch from which we received
46                  this OpenFlow message.
47     timestamp    Timestamp when Datapath instance generated this event.
48     ============ ==============================================================
49
50     The msg object has some more additional members whose values are extracted
51     from the original OpenFlow message.
52     """
53
54     def __init__(self, msg):
55         self.timestamp = time.time()
56         super(EventOFPMsgBase, self).__init__()
57         self.msg = msg
58
59
60 #
61 # Create ofp_event type corresponding to OFP Msg
62 #
63
64 _OFP_MSG_EVENTS = {}
65
66
67 def _ofp_msg_name_to_ev_name(msg_name):
68     return 'Event' + msg_name
69
70
71 def ofp_msg_to_ev(msg):
72     return ofp_msg_to_ev_cls(msg.__class__)(msg)
73
74
75 def ofp_msg_to_ev_cls(msg_cls):
76     name = _ofp_msg_name_to_ev_name(msg_cls.__name__)
77     return _OFP_MSG_EVENTS[name]
78
79
80 def _create_ofp_msg_ev_class(msg_cls):
81     name = _ofp_msg_name_to_ev_name(msg_cls.__name__)
82     # print 'creating ofp_event %s' % name
83
84     if name in _OFP_MSG_EVENTS:
85         return
86
87     cls = type(name, (EventOFPMsgBase,),
88                dict(__init__=lambda self, msg:
89                     super(self.__class__, self).__init__(msg)))
90     globals()[name] = cls
91     _OFP_MSG_EVENTS[name] = cls
92
93
94 def _create_ofp_msg_ev_from_module(ofp_parser):
95     # print mod
96     for _k, cls in inspect.getmembers(ofp_parser, inspect.isclass):
97         if not hasattr(cls, 'cls_msg_type'):
98             continue
99         _create_ofp_msg_ev_class(cls)
100
101
102 for ofp_mods in ofproto.get_ofp_modules().values():
103     ofp_parser = ofp_mods[1]
104     # print 'loading module %s' % ofp_parser
105     _create_ofp_msg_ev_from_module(ofp_parser)
106
107
108 class EventOFPStateChange(event.EventBase):
109     """
110     An event class for negotiation phase change notification.
111
112     An instance of this class is sent to observer after changing
113     the negotiation phase.
114     An instance has at least the following attributes.
115
116     ========= =================================================================
117     Attribute Description
118     ========= =================================================================
119     datapath  ryu.controller.controller.Datapath instance of the switch
120     ========= =================================================================
121     """
122
123     def __init__(self, dp):
124         super(EventOFPStateChange, self).__init__()
125         self.datapath = dp
126
127
128 class EventOFPPortStateChange(event.EventBase):
129     """
130     An event class to notify the port state changes of Dtatapath instance.
131
132     This event performs like EventOFPPortStatus, but Ryu will
133     send this event after updating ``ports`` dict of Datapath instances.
134     An instance has at least the following attributes.
135
136     ========= =================================================================
137     Attribute Description
138     ========= =================================================================
139     datapath  ryu.controller.controller.Datapath instance of the switch
140     reason    one of OFPPR_*
141     port_no   Port number which state was changed
142     ========= =================================================================
143     """
144
145     def __init__(self, dp, reason, port_no):
146         super(EventOFPPortStateChange, self).__init__()
147         self.datapath = dp
148         self.reason = reason
149         self.port_no = port_no
150
151
152 handler.register_service('ryu.controller.ofp_handler')