This patch adds requirements to path component.
And simplify with submapper.

Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/app/rest.py |   66 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/ryu/app/rest.py b/ryu/app/rest.py
index c834888..c9e649c 100644
--- a/ryu/app/rest.py
+++ b/ryu/app/rest.py
@@ -15,13 +15,15 @@
 # limitations under the License.
 
 import json
-from webob import Request, Response
+from webob import Response
 
+from ryu.app import wsgi_path
+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': wsgi_path.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']))
-- 
1.7.1.1


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to