From e5e4b844250d36788f9b1bf71cfd36a6e14d5315 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Tue, 29 May 2012 18:26:58 +0900 Subject: [PATCH] ryu/app: convert existing application to new style ryu application Signed-off-by: Isaku Yamahata Signed-off-by: FUJITA Tomonori --- bin/ryu-manager | 10 ---------- ryu/app/cbench.py | 7 ++++--- ryu/app/event_dumper.py | 7 ++++--- ryu/app/rest.py | 10 ++++++++-- ryu/app/simple_isolation.py | 14 +++++++++++--- ryu/app/simple_switch.py | 12 ++++++++---- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/bin/ryu-manager b/bin/ryu-manager index a6944e2e..b1947680 100755 --- a/bin/ryu-manager +++ b/bin/ryu-manager @@ -31,8 +31,6 @@ from ryu import utils from ryu.app import wsapi from ryu.base.app_manager import AppManager from ryu.controller import controller -from ryu.controller import dpset -from ryu.controller import network FLAGS = gflags.FLAGS @@ -49,17 +47,9 @@ def main(): _args = FLAGS(sys.argv) log.init_log() - # to make non-converted apps work. Once all of them converted, - # this will be removed. - kwargs = { - 'network': network.Network(), - 'dpset': dpset.DPSet(), - } - app_mgr = AppManager() app_mgr.load_apps(FLAGS.app_lists) contexts = app_mgr.create_contexts() - contexts.update(kwargs) app_mgr.instantiate_apps(**contexts) services = [] diff --git a/ryu/app/cbench.py b/ryu/app/cbench.py index 81ed539c..c82ae538 100644 --- a/ryu/app/cbench.py +++ b/ryu/app/cbench.py @@ -15,15 +15,16 @@ # limitations under the License. +from ryu.base import app_manager from ryu.controller import ofp_event from ryu.controller.handler import MAIN_DISPATCHER from ryu.controller.handler import set_ev_cls from ryu.ofproto import nx_match -class Cbench(object): - def __init__(self, *_args, **kwargs): - pass +class Cbench(app_manager.RyuApp): + def __init__(self, *args, **kwargs): + super(Cbench, self).__init__(*args, **kwargs) @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) def packet_in_handler(self, ev): diff --git a/ryu/app/event_dumper.py b/ryu/app/event_dumper.py index 475e12ef..a7ffbbf5 100644 --- a/ryu/app/event_dumper.py +++ b/ryu/app/event_dumper.py @@ -18,6 +18,7 @@ import gflags import logging +from ryu.base import app_manager from ryu.controller import dispatcher from ryu.controller.handler import set_ev_cls @@ -30,9 +31,9 @@ gflags.DEFINE_multistring('dump_dispatcher', [], 'list of dispatcher name to dump event: default any') -class EventDumper(object): - def __init__(self, *_args, **_kwargs): - super(EventDumper, self).__init__() +class EventDumper(app_manager.RyuApp): + def __init__(self, *args, **kwargs): + super(EventDumper, self).__init__(*args, **kwargs) # EventDispatcher can be created and cloned before us. # So register it explicitly for ev_q in dispatcher.EventQueue.all_instances(): diff --git a/ryu/app/rest.py b/ryu/app/rest.py index 285cdbf0..762a2c01 100644 --- a/ryu/app/rest.py +++ b/ryu/app/rest.py @@ -21,6 +21,8 @@ from ryu.app.wsapi import WSPathComponent from ryu.app.wsapi import WSPathExtractResult from ryu.app.wsapi import WSPathStaticString from ryu.app.wsapi import wsapi +from ryu.base import app_manager +from ryu.controller import network # REST API @@ -93,9 +95,13 @@ class WSPathPort(WSPathComponent): return WSPathExtractResult(value={'dpid': dpid, 'port': port}) -class restapi: +class restapi(app_manager.RyuApp): + _CONTEXTS = { + 'network': network.Network, + } - def __init__(self, *_args, **kwargs): + def __init__(self, *args, **kwargs): + super(restapi, self).__init__(*args, **kwargs) self.ws = wsapi() self.api = self.ws.get_version("1.0") self.nw = kwargs['network'] diff --git a/ryu/app/simple_isolation.py b/ryu/app/simple_isolation.py index 1daf6be8..3535e73e 100644 --- a/ryu/app/simple_isolation.py +++ b/ryu/app/simple_isolation.py @@ -18,10 +18,13 @@ import logging import struct from ryu.app.rest_nw_id import NW_ID_UNKNOWN, NW_ID_EXTERNAL +from ryu.base import app_manager from ryu.exception import MacAddressDuplicated from ryu.exception import PortUnknown +from ryu.controller import dpset from ryu.controller import mac_to_network from ryu.controller import mac_to_port +from ryu.controller import network from ryu.controller import ofp_event from ryu.controller.handler import MAIN_DISPATCHER from ryu.controller.handler import CONFIG_DISPATCHER @@ -34,9 +37,14 @@ from ryu.lib import mac LOG = logging.getLogger('ryu.app.simple_isolation') -class SimpleIsolation(object): - def __init__(self, *_args, **kwargs): - super(SimpleIsolation, self).__init__() +class SimpleIsolation(app_manager.RyuApp): + _CONTEXTS = { + 'network': network.Network, + 'dpset': dpset.DPSet, + } + + def __init__(self, *args, **kwargs): + super(SimpleIsolation, self).__init__(*args, **kwargs) self.nw = kwargs['network'] self.dpset = kwargs['dpset'] self.mac2port = mac_to_port.MacToPortTable() diff --git a/ryu/app/simple_switch.py b/ryu/app/simple_switch.py index c95b96ca..ed664c6d 100644 --- a/ryu/app/simple_switch.py +++ b/ryu/app/simple_switch.py @@ -16,6 +16,7 @@ import logging import struct +from ryu.base import app_manager from ryu.controller import mac_to_port from ryu.controller import ofp_event from ryu.controller.handler import MAIN_DISPATCHER @@ -34,10 +35,13 @@ LOG = logging.getLogger('ryu.app.simple_switch') # TODO: we need to move the followings to something like db -class SimpleSwitch(object): - def __init__(self, *_args, **_kwargs): - super(SimpleSwitch, self).__init__() - self.mac2port = mac_to_port.MacToPortTable() +class SimpleSwitch(app_manager.RyuApp): + _CONTEXETS = { + 'mac2port': mac_to_port.MacToPortTable, + } + def __init__(self, *args, **kwargs): + super(SimpleSwitch, self).__init__(*args, **kwargs) + self.mac2port = kwargs['mac2port'] @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) def _packet_in_handler(self, ev):