summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIWASE Yusuke <iwase.yusuke0@gmail.com>2016-05-10 14:29:39 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-05-13 22:25:09 +0900
commitc1047818d3ce907b8a606719d650a0a982a5645e (patch)
tree83be2b0e6df81a823848fdcb7f2365c81a574c3a
parent326b46bf6d72e201fa8667f757ac5d2cef18885a (diff)
REST Apps: Adopt to Python 3
In Python 3, webob.Request.body is a byte type value and json.loads() cannot parse it, because json.loads() suppose a str type value. This patch fixes to use webob.Request.json to parse request body and avoids this problem. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/app/rest_conf_switch.py6
-rw-r--r--ryu/app/rest_firewall.py8
-rw-r--r--ryu/app/rest_qos.py4
-rw-r--r--ryu/app/rest_router.py19
4 files changed, 22 insertions, 15 deletions
diff --git a/ryu/app/rest_conf_switch.py b/ryu/app/rest_conf_switch.py
index e397a37b..9767f363 100644
--- a/ryu/app/rest_conf_switch.py
+++ b/ryu/app/rest_conf_switch.py
@@ -111,7 +111,11 @@ class ConfSwitchController(ControllerBase):
def set_key(self, req, dpid, key, **_kwargs):
def _set_val(dpid, key):
- val = json.loads(req.body)
+ try:
+ val = req.json if req.body else {}
+ except ValueError:
+ return Response(status=http_client.BAD_REQUEST,
+ body='invalid syntax %s' % req.body)
self.conf_switch.set_key(dpid, key, val)
return None
diff --git a/ryu/app/rest_firewall.py b/ryu/app/rest_firewall.py
index 322ddb1d..a04525f7 100644
--- a/ryu/app/rest_firewall.py
+++ b/ryu/app/rest_firewall.py
@@ -492,8 +492,8 @@ class FirewallController(ControllerBase):
def _set_rule(self, req, switchid, vlan_id=VLANID_NONE):
try:
- rule = json.loads(req.body)
- except SyntaxError:
+ rule = req.json if req.body else {}
+ except ValueError:
FirewallController._LOGGER.debug('invalid syntax %s', req.body)
return Response(status=400)
@@ -516,8 +516,8 @@ class FirewallController(ControllerBase):
def _delete_rule(self, req, switchid, vlan_id=VLANID_NONE):
try:
- ruleid = json.loads(req.body)
- except SyntaxError:
+ ruleid = req.json if req.body else {}
+ except ValueError:
FirewallController._LOGGER.debug('invalid syntax %s', req.body)
return Response(status=400)
diff --git a/ryu/app/rest_qos.py b/ryu/app/rest_qos.py
index 051c96b3..89185a6e 100644
--- a/ryu/app/rest_qos.py
+++ b/ryu/app/rest_qos.py
@@ -506,8 +506,8 @@ class QoSController(ControllerBase):
def _access_switch(self, req, switchid, vlan_id, func, waiters):
try:
- rest = json.loads(req.body) if req.body else {}
- except SyntaxError:
+ rest = req.json if req.body else {}
+ except ValueError:
QoSController._LOGGER.debug('invalid syntax %s', req.body)
return Response(status=400)
diff --git a/ryu/app/rest_router.py b/ryu/app/rest_router.py
index 100d565e..2098ffae 100644
--- a/ryu/app/rest_router.py
+++ b/ryu/app/rest_router.py
@@ -376,42 +376,45 @@ class RouterController(ControllerBase):
@rest_command
def get_data(self, req, switch_id, **_kwargs):
return self._access_router(switch_id, VLANID_NONE,
- 'get_data', req.body)
+ 'get_data', req)
# GET /router/{switch_id}/{vlan_id}
@rest_command
def get_vlan_data(self, req, switch_id, vlan_id, **_kwargs):
return self._access_router(switch_id, vlan_id,
- 'get_data', req.body)
+ 'get_data', req)
# POST /router/{switch_id}
@rest_command
def set_data(self, req, switch_id, **_kwargs):
return self._access_router(switch_id, VLANID_NONE,
- 'set_data', req.body)
+ 'set_data', req)
# POST /router/{switch_id}/{vlan_id}
@rest_command
def set_vlan_data(self, req, switch_id, vlan_id, **_kwargs):
return self._access_router(switch_id, vlan_id,
- 'set_data', req.body)
+ 'set_data', req)
# DELETE /router/{switch_id}
@rest_command
def delete_data(self, req, switch_id, **_kwargs):
return self._access_router(switch_id, VLANID_NONE,
- 'delete_data', req.body)
+ 'delete_data', req)
# DELETE /router/{switch_id}/{vlan_id}
@rest_command
def delete_vlan_data(self, req, switch_id, vlan_id, **_kwargs):
return self._access_router(switch_id, vlan_id,
- 'delete_data', req.body)
+ 'delete_data', req)
- def _access_router(self, switch_id, vlan_id, func, rest_param):
+ def _access_router(self, switch_id, vlan_id, func, req):
rest_message = []
routers = self._get_router(switch_id)
- param = json.loads(rest_param) if rest_param else {}
+ try:
+ param = req.json if req.body else {}
+ except ValueError:
+ raise SyntaxError('invalid syntax %s', req.body)
for router in routers.values():
function = getattr(router, func)
data = function(vlan_id, param, self.waiters)