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>
This commit is contained in:
Yusuke Iwase 2014-11-19 09:25:22 +09:00 committed by FUJITA Tomonori
parent 828b6f48c4
commit b1b02cec00
2 changed files with 15 additions and 4 deletions

View File

@ -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

View File

@ -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