summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYusuke Iwase <iwase.yusuke0@gmail.com>2014-11-19 09:25:22 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-11-07 14:43:20 +0900
commitb1b02cec0060ee8051fe28d22a0c9caff8df8d15 (patch)
tree4772af2fde30fb87950a9da6b842e056e4efae29
parent828b6f48c446cdfdc49e114d99a88c5c47d8ee68 (diff)
utils: Fix bytearray conversion
The parameter buf is an instance of bytearray, but Ryu tries to convert it as string, and outputs the error messages as a result. This patch fixes 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/ofproto/ofproto_parser.py2
-rw-r--r--ryu/utils.py17
2 files changed, 15 insertions, 4 deletions
diff --git a/ryu/ofproto/ofproto_parser.py b/ryu/ofproto/ofproto_parser.py
index 41ecc068..b883addd 100644
--- a/ryu/ofproto/ofproto_parser.py
+++ b/ryu/ofproto/ofproto_parser.py
@@ -60,7 +60,7 @@ def msg(datapath, version, msg_type, msg_len, xid, buf):
'Encounter an error during parsing OpenFlow packet from switch.'
'This implies switch sending a malformed OpenFlow packet.'
'version 0x%02x msg_type %d msg_len %d xid %d buf %s',
- version, msg_type, msg_len, xid, utils.bytearray_to_hex(buf))
+ version, msg_type, msg_len, xid, utils.hex_array(buf))
return None
diff --git a/ryu/utils.py b/ryu/utils.py
index a66b7fee..f0d2e3a9 100644
--- a/ryu/utils.py
+++ b/ryu/utils.py
@@ -93,14 +93,25 @@ def round_up(x, y):
return ((x + y - 1) / y) * y
-def hex_array(data):
+def _str_to_hex(data):
"""Convert string into array of hexes to be printed."""
return ' '.join(hex(ord(char)) for char in data)
-def bytearray_to_hex(data):
+def _bytearray_to_hex(data):
"""Convert bytearray into array of hexes to be printed."""
- return ' '.join(hex(ord(byte)) for byte in data)
+ return ' '.join(hex(byte) for byte in data)
+
+
+def hex_array(data):
+ """Convert string or bytearray into array of hexes to be printed."""
+ to_hex = {str: _str_to_hex,
+ bytearray: _bytearray_to_hex}
+ try:
+ return to_hex[type(data)](data)
+ except KeyError:
+ LOG.exception('%s is invalid data type' % type(data))
+ return None
# the following functions are taken from OpenStack