1 # Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 OpenFlow event definitions.
24 from ryu.controller import handler
25 from ryu import ofproto
32 class EventOFPMsgBase(event.EventBase):
34 The base class of OpenFlow event class.
36 OpenFlow event classes have at least the following attributes.
38 .. tabularcolumns:: |l|L|
40 ============ ==============================================================
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 ============ ==============================================================
50 The msg object has some more additional members whose values are extracted
51 from the original OpenFlow message.
54 def __init__(self, msg):
55 self.timestamp = time.time()
56 super(EventOFPMsgBase, self).__init__()
61 # Create ofp_event type corresponding to OFP Msg
67 def _ofp_msg_name_to_ev_name(msg_name):
68 return 'Event' + msg_name
71 def ofp_msg_to_ev(msg):
72 return ofp_msg_to_ev_cls(msg.__class__)(msg)
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]
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
84 if name in _OFP_MSG_EVENTS:
87 cls = type(name, (EventOFPMsgBase,),
88 dict(__init__=lambda self, msg:
89 super(self.__class__, self).__init__(msg)))
91 _OFP_MSG_EVENTS[name] = cls
94 def _create_ofp_msg_ev_from_module(ofp_parser):
96 for _k, cls in inspect.getmembers(ofp_parser, inspect.isclass):
97 if not hasattr(cls, 'cls_msg_type'):
99 _create_ofp_msg_ev_class(cls)
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)
108 class EventOFPStateChange(event.EventBase):
110 An event class for negotiation phase change notification.
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.
116 ========= =================================================================
117 Attribute Description
118 ========= =================================================================
119 datapath ryu.controller.controller.Datapath instance of the switch
120 ========= =================================================================
123 def __init__(self, dp):
124 super(EventOFPStateChange, self).__init__()
128 class EventOFPPortStateChange(event.EventBase):
130 An event class to notify the port state changes of Dtatapath instance.
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.
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 ========= =================================================================
145 def __init__(self, dp, reason, port_no):
146 super(EventOFPPortStateChange, self).__init__()
149 self.port_no = port_no
152 handler.register_service('ryu.controller.ofp_handler')