From b1b02cec0060ee8051fe28d22a0c9caff8df8d15 Mon Sep 17 00:00:00 2001 From: Yusuke Iwase Date: Wed, 19 Nov 2014 09:25:22 +0900 Subject: 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 Signed-off-by: FUJITA Tomonori --- ryu/ofproto/ofproto_parser.py | 2 +- ryu/utils.py | 17 ++++++++++++++--- 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 -- cgit v1.2.3