controller: factor out ofp message event from event.py

Move out ofp msg event from event.py into ofp_event.py

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Isaku Yamahata 2012-02-02 14:48:35 +09:00 committed by FUJITA Tomonori
parent b3002281e9
commit c22ef47a9c
6 changed files with 95 additions and 79 deletions

@ -19,9 +19,9 @@ import struct
from ryu.app.rest_nw_id import NW_ID_UNKNOWN, NW_ID_EXTERNAL
from ryu.exception import MacAddressDuplicated
from ryu.exception import PortUnknown
from ryu.controller import event
from ryu.controller import mac_to_network
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
@ -36,12 +36,12 @@ class SimpleIsolation(object):
self.mac2port = mac_to_port.MacToPortTable()
self.mac2net = mac_to_network.MacToNetwork(self.nw)
@set_ev_cls(event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
self.mac2port.dpid_add(ev.msg.datapath_id)
self.nw.add_datapath(ev.msg)
@set_ev_cls(event.EventOFPBarrierReply)
@set_ev_cls(ofp_event.EventOFPBarrierReply)
def barrier_reply_handler(self, ev):
LOG.debug('barrier reply ev %s msg %s', ev, ev.msg)
@ -106,7 +106,7 @@ class SimpleIsolation(object):
else:
self._flood_to_nw_id(msg, src, dst, dst_nw_id)
@set_ev_cls(event.EventOFPPacketIn, MAIN_DISPATCHER)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
# LOG.debug('packet in ev %s msg %s', ev, ev.msg)
msg = ev.msg
@ -238,6 +238,6 @@ class SimpleIsolation(object):
datapath.send_delete_all_flows()
datapath.send_barrier()
@set_ev_cls(event.EventOFPBarrierReply, MAIN_DISPATCHER)
@set_ev_cls(ofp_event.EventOFPBarrierReply, MAIN_DISPATCHER)
def barrier_replay_handler(self, ev):
pass

@ -15,8 +15,8 @@
import logging
import struct
from ryu.controller import event
from ryu.controller import mac_to_port
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.lib.mac import haddr_to_str
@ -36,7 +36,7 @@ class SimpleSwitch(object):
def __init__(self, *_args, **_kwargs):
self.mac2port = mac_to_port.MacToPortTable()
@set_ev_cls(event.EventOFPPacketIn, MAIN_DISPATCHER)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
@ -75,7 +75,7 @@ class SimpleSwitch(object):
datapath.send_packet_out(msg.buffer_id, msg.in_port, actions)
@set_ev_cls(event.EventOFPPortStatus, MAIN_DISPATCHER)
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def _port_status_handler(self, ev):
msg = ev.msg
reason = msg.reason

@ -27,8 +27,8 @@ from ryu.ofproto import ofproto_v1_0
from ryu.ofproto import ofproto_v1_0_parser
from ryu.controller import dispatcher
from ryu.controller import event
from ryu.controller import handler
from ryu.controller import ofp_event
LOG = logging.getLogger('ryu.controller.controller')
@ -165,7 +165,7 @@ class Datapath(object):
while self.is_active:
msg = self.recv_q.get()
#LOG.debug('_event_loop ev %s cls %s', msg, msg.__class__)
self.ev_q.queue(event.ofp_msg_to_ev(msg))
self.ev_q.queue(ofp_event.ofp_msg_to_ev(msg))
def send_ev(self, ev):
#LOG.debug('send_ev %s', ev)

@ -13,65 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import inspect
class EventBase(object):
# Nothing yet
pass
class EventOFPMsgBase(EventBase):
def __init__(self, msg):
super(EventOFPMsgBase, self).__init__()
self.msg = msg
#
# Create event type corresponding to OFP Msg
#
_OFP_MSG_EVENTS = {}
def _ofp_msg_name_to_ev_name(msg_name):
return 'Event' + msg_name
def ofp_msg_to_ev(msg):
name = _ofp_msg_name_to_ev_name(msg.__class__.__name__)
return _OFP_MSG_EVENTS[name](msg)
def _create_ofp_msg_ev_class(msg_cls):
name = _ofp_msg_name_to_ev_name(msg_cls.__name__)
# print 'creating event %s' % name
if name in _OFP_MSG_EVENTS:
return
cls = type(name, (EventOFPMsgBase,),
dict(__init__=lambda self, msg:
super(self.__class__, self).__init__(msg)))
globals()[name] = cls
_OFP_MSG_EVENTS[name] = cls
def _create_ofp_msg_ev_from_module(modname):
(f, _s, _t) = modname.rpartition('.')
mod = __import__(modname, fromlist=[f])
print mod
for _k, cls in mod.__dict__.items():
if not inspect.isclass(cls):
continue
if 'cls_msg_type' not in cls.__dict__:
continue
_create_ofp_msg_ev_class(cls)
# TODO:XXX
_PARSER_MODULE_LIST = ['ryu.ofproto.ofproto_v1_0_parser']
for m in _PARSER_MODULE_LIST:
# print 'loading module %s' % m
_create_ofp_msg_ev_from_module(m)

@ -17,8 +17,8 @@ import copy
import inspect
import logging
from ryu.controller import event
from ryu.controller import dispatcher
from ryu.controller import ofp_event
LOG = logging.getLogger('ryu.controller.handler')
@ -105,7 +105,7 @@ def register_instance(i, dispatchers=None):
@register_cls([HANDSHAKE_DISPATCHER, CONFIG_DISPATCHER, MAIN_DISPATCHER])
class EchoHandler(object):
@staticmethod
@set_ev_cls(event.EventOFPEchoRequest)
@set_ev_cls(ofp_event.EventOFPEchoRequest)
def echo_request_handler(ev):
msg = ev.msg
# LOG.debug('echo request msg %s %s', msg, str(msg.data))
@ -116,7 +116,7 @@ class EchoHandler(object):
datapath.send_msg(echo_reply)
@staticmethod
@set_ev_cls(event.EventOFPEchoReply)
@set_ev_cls(ofp_event.EventOFPEchoReply)
def echo_reply_handler(ev):
# do nothing
# msg = ev.msg
@ -127,7 +127,7 @@ class EchoHandler(object):
@register_cls([HANDSHAKE_DISPATCHER, CONFIG_DISPATCHER, MAIN_DISPATCHER])
class ErrorMsgHandler(object):
@staticmethod
@set_ev_cls(event.EventOFPErrorMsg)
@set_ev_cls(ofp_event.EventOFPErrorMsg)
def error_msg_handler(ev):
msg = ev.msg
LOG.debug('error msg ev %s type 0x%x code 0x%x %s',
@ -138,7 +138,7 @@ class ErrorMsgHandler(object):
@register_cls(HANDSHAKE_DISPATCHER)
class HandShakeHandler(object):
@staticmethod
@set_ev_cls(event.EventOFPHello)
@set_ev_cls(ofp_event.EventOFPHello)
def hello_handler(ev):
LOG.debug('hello ev %s', ev)
msg = ev.msg
@ -170,7 +170,7 @@ class HandShakeHandler(object):
@register_cls(CONFIG_DISPATCHER)
class ConfigHandler(object):
@staticmethod
@set_ev_cls(event.EventOFPSwitchFeatures)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures)
def switch_features_handler(ev):
msg = ev.msg
datapath = msg.datapath
@ -194,15 +194,15 @@ class ConfigHandler(object):
datapath.send_barrier()
# The above OFPC_DELETE request may trigger flow removed event.
# The above OFPC_DELETE request may trigger flow removed ofp_event.
# Just ignore them.
@staticmethod
@set_ev_cls(event.EventOFPFlowRemoved)
@set_ev_cls(ofp_event.EventOFPFlowRemoved)
def flow_removed_handler(ev):
LOG.debug("flow removed ev %s msg %s", ev, ev.msg)
@staticmethod
@set_ev_cls(event.EventOFPBarrierReply)
@set_ev_cls(ofp_event.EventOFPBarrierReply)
def barrier_reply_handler(ev):
LOG.debug('barrier reply ev %s msg %s', ev, ev.msg)
@ -214,12 +214,12 @@ class ConfigHandler(object):
@register_cls(MAIN_DISPATCHER)
class MainHandler(object):
@staticmethod
@set_ev_cls(event.EventOFPFlowRemoved)
@set_ev_cls(ofp_event.EventOFPFlowRemoved)
def flow_removed_handler(ev):
pass
@staticmethod
@set_ev_cls(event.EventOFPPortStatus)
@set_ev_cls(ofp_event.EventOFPPortStatus)
def port_status_handler(ev):
msg = ev.msg
LOG.debug('port status %s', msg.reason)

@ -0,0 +1,74 @@
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import inspect
from . import event
class EventOFPMsgBase(event.EventBase):
def __init__(self, msg):
super(EventOFPMsgBase, self).__init__()
self.msg = msg
#
# Create ofp_event type corresponding to OFP Msg
#
_OFP_MSG_EVENTS = {}
def _ofp_msg_name_to_ev_name(msg_name):
return 'Event' + msg_name
def ofp_msg_to_ev(msg):
name = _ofp_msg_name_to_ev_name(msg.__class__.__name__)
return _OFP_MSG_EVENTS[name](msg)
def _create_ofp_msg_ev_class(msg_cls):
name = _ofp_msg_name_to_ev_name(msg_cls.__name__)
# print 'creating ofp_event %s' % name
if name in _OFP_MSG_EVENTS:
return
cls = type(name, (EventOFPMsgBase,),
dict(__init__=lambda self, msg:
super(self.__class__, self).__init__(msg)))
globals()[name] = cls
_OFP_MSG_EVENTS[name] = cls
def _create_ofp_msg_ev_from_module(modname):
(f, _s, _t) = modname.rpartition('.')
mod = __import__(modname, fromlist=[f])
print mod
for _k, cls in mod.__dict__.items():
if not inspect.isclass(cls):
continue
if 'cls_msg_type' not in cls.__dict__:
continue
_create_ofp_msg_ev_class(cls)
# TODO:XXX
_PARSER_MODULE_LIST = ['ryu.ofproto.ofproto_v1_0_parser']
for m in _PARSER_MODULE_LIST:
# print 'loading module %s' % m
_create_ofp_msg_ev_from_module(m)