1- """General Unpack utils for python-openflow."""
2- import pyof .v0x01 .common .header
3- import pyof .v0x01 .common .utils
4- import pyof .v0x04 .common .header
5- import pyof .v0x04 .common .utils
1+ """General Unpack utils for python-openflow.
2+
3+ This package was moved from kytos/of_core for the purpose of creating a generic
4+ method to perform package unpack independent of the OpenFlow version.
5+ """
6+ from pyof import v0x01 , v0x04
67from pyof .foundation .exceptions import UnpackException
78
8- pyof_version_libs = {0x01 : pyof . v0x01 ,
9- 0x04 : pyof . v0x04 }
9+ PYOF_VERSION_LIBS = {0x01 : v0x01 ,
10+ 0x04 : v0x04 }
1011
1112
1213def validate_packet (packet ):
13- """Check if packet is valid OF packet."""
14+ """Check if packet is valid OF packet.
15+
16+ Raises:
17+ UnpackException: If the packet is invalid.
18+ """
1419 if not isinstance (packet , bytes ):
1520 raise UnpackException ('invalid packet' )
1621
@@ -29,22 +34,32 @@ def validate_packet(packet):
2934
3035
3136def unpack (packet ):
32- """Unpack the OpenFlow Packet and returns a message."""
37+ """Unpack the OpenFlow Packet and returns a message.
38+
39+ Returns:
40+ GenericMessage: Message unpacked based on openflow packet.
41+
42+ Raises:
43+ UnpackException: if the packet can't be unpacked.
44+ """
3345 validate_packet (packet )
3446
3547 version = packet [0 ]
3648 try :
37- pyof_lib = pyof_version_libs [version ]
49+ pyof_lib = PYOF_VERSION_LIBS [version ]
3850 except KeyError :
3951 raise UnpackException ('Version not supported' )
4052
4153 try :
4254 header = pyof_lib .common .header .Header ()
43- header .unpack (packet [:8 ])
55+ header .unpack (packet [:header . get_size () ])
4456 message = pyof_lib .common .utils .new_message_from_header (header )
45- binary_data = packet [8 :]
46- if binary_data :
57+
58+ binary_data = packet [header .get_size ():]
59+ binary_data_size = header .length - header .get_size ()
60+
61+ if binary_data and len (binary_data ) == binary_data_size :
4762 message .unpack (binary_data )
4863 return message
49- except (UnpackException , ValueError ) as e :
50- raise UnpackException (e )
64+ except (UnpackException , ValueError ) as exception :
65+ raise UnpackException (exception )
0 commit comments