diff --git a/pyof/utils.py b/pyof/utils.py index 392129a95..04240d5d4 100644 --- a/pyof/utils.py +++ b/pyof/utils.py @@ -3,11 +3,12 @@ This package was moved from kytos/of_core for the purpose of creating a generic method to perform package unpack independent of the OpenFlow version. """ -from pyof import v0x01, v0x04 +from pyof.v0x01.common.utils import Header0x01, unpack_message_v0x01 +from pyof.v0x04.common.utils import unpack_message as unpack_message_v0x04 from pyof.foundation.exceptions import UnpackException -PYOF_VERSION_LIBS = {0x01: v0x01, - 0x04: v0x04} +PYOF_UNPACK_MESSAGE = {0x01: unpack_message_v0x01, + 0x04: unpack_message_v0x04} def validate_packet(packet): @@ -45,23 +46,15 @@ def unpack(packet): """ validate_packet(packet) - version = packet[0] + try: - pyof_lib = PYOF_VERSION_LIBS[version] + unpack_message = PYOF_UNPACK_MESSAGE[version] except KeyError: raise UnpackException('Version not supported') try: - header = pyof_lib.common.header.Header() - header.unpack(packet[:header.get_size()]) - message = pyof_lib.common.utils.new_message_from_header(header) - - binary_data = packet[header.get_size():] - binary_data_size = header.length - header.get_size() - - if binary_data and len(binary_data) == binary_data_size: - message.unpack(binary_data) - return message + message = unpack_message(packet) except (UnpackException, ValueError) as exception: raise UnpackException(exception) + return message diff --git a/pyof/v0x04/common/utils.py b/pyof/v0x04/common/utils.py index 4149c41a2..be470b78b 100644 --- a/pyof/v0x04/common/utils.py +++ b/pyof/v0x04/common/utils.py @@ -166,7 +166,15 @@ def new_message_from_header(header): def unpack_message(buffer): - """Unpack the whole buffer, including header pack.""" + """Unpack the whole buffer, including header pack. + + Args: + buffer (bytes): Bytes representation of a openflow message. + + Returns: + object: Instance of openflow message. + + """ hdr_size = Header().get_size() hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:] header = Header()