app/rest: add requirements to path component

This patch adds requirements to path component so that URL is verified.
And simplify with submapper.

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-08-07 17:34:29 +09:00 committed by FUJITA Tomonori
parent 7578e7d602
commit 60b6d099bf

View File

@ -15,13 +15,15 @@
# limitations under the License.
import json
from webob import Request, Response
from webob import Response
from ryu.app import wsgi as app_wsgi
from ryu.app.wsgi import ControllerBase, WSGIApplication
from ryu.base import app_manager
from ryu.controller import network
from ryu.exception import NetworkNotFound, NetworkAlreadyExist
from ryu.exception import PortNotFound, PortAlreadyExist
from ryu.app.wsgi import ControllerBase, WSGIApplication
from ryu.lib import dpid as dpid_lib
## TODO:XXX
## define db interface and store those information into db
@ -100,8 +102,10 @@ class PortController(ControllerBase):
self.nw = data
def create(self, req, network_id, dpid, port_id, **_kwargs):
dpid = dpid_lib.str_to_dpid(dpid)
port_id = int(port_id)
try:
self.nw.create_port(network_id, int(dpid, 16), int(port_id))
self.nw.create_port(network_id, dpid, port_id)
except NetworkNotFound:
return Response(status=404)
except PortAlreadyExist:
@ -110,8 +114,10 @@ class PortController(ControllerBase):
return Response(status=200)
def update(self, req, network_id, dpid, port_id, **_kwargs):
dpid = dpid_lib.str_to_dpid(dpid)
port_id = int(port_id)
try:
self.nw.update_port(network_id, int(dpid, 16), int(port_id))
self.nw.update_port(network_id, dpid, port_id)
except NetworkNotFound:
return Response(status=404)
@ -126,58 +132,58 @@ class PortController(ControllerBase):
return Response(content_type='application/json', body=body)
def delete(self, req, network_id, dpid, port_id, **_kwargs):
dpid = dpid_lib.str_to_dpid(dpid)
port_id = int(port_id)
try:
self.nw.remove_port(network_id, int(dpid, 16), int(port_id))
self.nw.remove_port(network_id, dpid, port_id)
except (NetworkNotFound, PortNotFound):
return Response(status=404)
return Response(status=200)
class restapi(app_manager.RyuApp):
class RestAPI(app_manager.RyuApp):
_CONTEXTS = {
'network': network.Network,
'wsgi': WSGIApplication
}
def __init__(self, *args, **kwargs):
super(restapi, self).__init__(*args, **kwargs)
super(RestAPI, self).__init__(*args, **kwargs)
self.nw = kwargs['network']
wsgi = kwargs['wsgi']
mapper = wsgi.mapper
wsgi.registory['NetworkController'] = self.nw
route_name = 'networks'
uri = '/v1.0/networks'
mapper.connect('networks', uri,
mapper.connect(route_name, uri,
controller=NetworkController, action='lists',
conditions=dict(method=['GET', 'HEAD']))
uri += '/{network_id}'
mapper.connect('networks', uri,
controller=NetworkController, action='create',
conditions=dict(method=['POST']))
mapper.connect('networks', uri,
controller=NetworkController, action='update',
conditions=dict(method=['PUT']))
mapper.connect('networks', uri,
controller=NetworkController, action='delete',
conditions=dict(method=['DELETE']))
s = mapper.submapper(controller=NetworkController)
s.connect(route_name, uri, action='create',
conditions=dict(method=['POST']))
s.connect(route_name, uri, action='update',
conditions=dict(method=['PUT']))
s.connect(route_name, uri, action='delete',
conditions=dict(method=['DELETE']))
wsgi.registory['PortController'] = self.nw
mapper.connect('networks', uri,
route_name = 'ports'
mapper.connect(route_name, uri,
controller=PortController, action='lists',
conditions=dict(method=['GET']))
uri += '/{dpid}_{port_id}'
mapper.connect('ports', uri,
controller=PortController, action='create',
conditions=dict(method=['POST']))
mapper.connect('ports', uri,
controller=PortController, action='update',
conditions=dict(method=['PUT']))
mapper.connect('ports', uri,
controller=PortController, action='delete',
conditions=dict(method=['DELETE']))
requirements = {'dpid': dpid_lib.DPID_PATTERN,
'port_id': app_wsgi.DIGIT_PATTERN}
s = mapper.submapper(controller=PortController,
requirements=requirements)
s.connect(route_name, uri, action='create',
conditions=dict(method=['POST']))
s.connect(route_name, uri, action='update',
conditions=dict(method=['PUT']))
s.connect(route_name, uri, action='delete',
conditions=dict(method=['DELETE']))