-
Notifications
You must be signed in to change notification settings - Fork 35
implement general unpack method (moved from of_core.utils) #397
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""General Unpack utils for python-openflow.""" | ||
import pyof.v0x01.common.header | ||
import pyof.v0x01.common.utils | ||
import pyof.v0x04.common.header | ||
import pyof.v0x04.common.utils | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erickvermot you are not using this imported modules anywhere, so you should not import them here. from pyof import v0x01
from pyof import v0x04
pyof_version_libs = {0x01: v0x01,
0x04: v0x04} Note that I'm using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even better: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anywhere = line 25 for example. Those imports are needed since importing pyof does not import the child modules by default. The from statement is better suited when one tries to import a Class, function or variable from a given module, or when one wants to place the imported module in the current namespace. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked this out and, unfortunately, Diraol suggestion doesn't work because this is a special case like Erick said. Linters can't detect unused import statements like these, but I can't think of a better way to do it right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
from pyof.foundation.exceptions import UnpackException | ||
|
||
pyof_version_libs = {0x01: pyof.v0x01, | ||
0x04: pyof.v0x04} | ||
|
||
|
||
def validate_packet(packet): | ||
"""Check if packet is valid OF packet.""" | ||
if not isinstance(packet, bytes): | ||
raise UnpackException('invalid packet') | ||
|
||
packet_length = len(packet) | ||
|
||
if packet_length < 8 or packet_length > 2**16: | ||
raise UnpackException('invalid packet') | ||
|
||
if packet_length != int.from_bytes(packet[2:4], | ||
byteorder='big'): | ||
raise UnpackException('invalid packet') | ||
|
||
version = packet[0] | ||
if version == 0 or version >= 128: | ||
raise UnpackException('invalid packet') | ||
|
||
|
||
def unpack(packet): | ||
"""Unpack the OpenFlow Packet and returns a message.""" | ||
validate_packet(packet) | ||
|
||
version = packet[0] | ||
try: | ||
pyof_lib = pyof_version_libs[version] | ||
except KeyError: | ||
raise UnpackException('Version not supported') | ||
|
||
try: | ||
header = pyof_lib.common.header.Header() | ||
header.unpack(packet[:8]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erickvermot the header will always have length 8 [just to make sure that on OF 1.3.5 and newer they still keep this behavior]? If my comment make any sense, then we should "apply" it below also. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. headers always have length 8 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok then! |
||
message = pyof_lib.common.utils.new_message_from_header(header) | ||
binary_data = packet[8:] | ||
if binary_data: | ||
message.unpack(binary_data) | ||
return message | ||
except (UnpackException, ValueError) as e: | ||
raise UnpackException(e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add a comment stating that you are copying the code from of_core, specifying the filename from its repo root. Also write that this is because you want to have the exact same behavior for testing purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is already clear in the corresponding issues mentioned here:
#396 and #381