From aecc7b0af53c3d8c0136373b254c1b674bcf28ef Mon Sep 17 00:00:00 2001 From: Satoshi Kobayashi Date: Wed, 21 May 2014 09:12:03 +0000 Subject: [PATCH] rest_topology: use decorator api Signed-off-by: Satoshi Kobayashi Reviewed-by: YAMADA Hideki Signed-off-by: FUJITA Tomonori --- ryu/app/rest_topology.py | 86 +++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/ryu/app/rest_topology.py b/ryu/app/rest_topology.py index 82a17557..5eed243d 100644 --- a/ryu/app/rest_topology.py +++ b/ryu/app/rest_topology.py @@ -16,10 +16,9 @@ import json from webob import Response -from ryu.app.wsgi import ControllerBase, WSGIApplication +from ryu.app.wsgi import ControllerBase, WSGIApplication, route from ryu.base import app_manager from ryu.lib import dpid as dpid_lib -from ryu.lib import port_no as port_no_lib from ryu.topology.api import get_switch, get_link # REST API for switch configuration @@ -40,28 +39,6 @@ from ryu.topology.api import get_switch, get_link # : datapath id in 16 hex -class TopologyController(ControllerBase): - def __init__(self, req, link, data, **config): - super(TopologyController, self).__init__(req, link, data, **config) - self.topology_api_app = data['topology_api_app'] - - def list_switches(self, req, **kwargs): - dpid = None - if 'dpid' in kwargs: - dpid = dpid_lib.str_to_dpid(kwargs['dpid']) - switches = get_switch(self.topology_api_app, dpid) - body = json.dumps([switch.to_dict() for switch in switches]) - return Response(content_type='application/json', body=body) - - def list_links(self, req, **kwargs): - dpid = None - if 'dpid' in kwargs: - dpid = dpid_lib.str_to_dpid(kwargs['dpid']) - links = get_link(self.topology_api_app, dpid) - body = json.dumps([link.to_dict() for link in links]) - return Response(content_type='application/json', body=body) - - class TopologyAPI(app_manager.RyuApp): _CONTEXTS = { 'wsgi': WSGIApplication @@ -69,31 +46,48 @@ class TopologyAPI(app_manager.RyuApp): def __init__(self, *args, **kwargs): super(TopologyAPI, self).__init__(*args, **kwargs) + wsgi = kwargs['wsgi'] - mapper = wsgi.mapper + wsgi.register(TopologyController, {'topology_api_app': self}) - controller = TopologyController - wsgi.registory[controller.__name__] = {'topology_api_app': self} - route_name = 'topology' - uri = '/v1.0/topology/switches' - mapper.connect(route_name, uri, controller=controller, - action='list_switches', - conditions=dict(method=['GET'])) +class TopologyController(ControllerBase): + def __init__(self, req, link, data, **config): + super(TopologyController, self).__init__(req, link, data, **config) + self.topology_api_app = data['topology_api_app'] - uri = '/v1.0/topology/switches/{dpid}' - requirements = {'dpid': dpid_lib.DPID_PATTERN} - s = mapper.submapper(controller=controller, requirements=requirements) - s.connect(route_name, uri, action='list_switches', - conditions=dict(method=['GET'])) + @route('topology', '/v1.0/topology/switches', + methods=['GET']) + def list_switches(self, req, **kwargs): + return self._switches(req, **kwargs) - uri = '/v1.0/topology/links' - mapper.connect(route_name, uri, controller=controller, - action='list_links', - conditions=dict(method=['GET'])) + @route('topology', '/v1.0/topology/switches/{dpid}', + methods=['GET'], requirements={'dpid': dpid_lib.DPID_PATTERN}) + def get_switch(self, req, **kwargs): + return self._switches(req, **kwargs) - uri = '/v1.0/topology/links/{dpid}' - requirements = {'dpid': dpid_lib.DPID_PATTERN} - s = mapper.submapper(controller=controller, requirements=requirements) - s.connect(route_name, uri, action='list_links', - conditions=dict(method=['GET'])) + @route('topology', '/v1.0/topology/links', + methods=['GET']) + def list_links(self, req, **kwargs): + return self._links(req, **kwargs) + + @route('topology', '/v1.0/topology/links/{dpid}', + methods=['GET'], requirements={'dpid': dpid_lib.DPID_PATTERN}) + def get_links(self, req, **kwargs): + return self._links(req, **kwargs) + + def _switches(self, req, **kwargs): + dpid = None + if 'dpid' in kwargs: + dpid = dpid_lib.str_to_dpid(kwargs['dpid']) + switches = get_switch(self.topology_api_app, dpid) + body = json.dumps([switch.to_dict() for switch in switches]) + return Response(content_type='application/json', body=body) + + def _links(self, req, **kwargs): + dpid = None + if 'dpid' in kwargs: + dpid = dpid_lib.str_to_dpid(kwargs['dpid']) + links = get_link(self.topology_api_app, dpid) + body = json.dumps([link.to_dict() for link in links]) + return Response(content_type='application/json', body=body)