-
Notifications
You must be signed in to change notification settings - Fork 35
implement general unpack method (moved from of_core.utils) #397
Changes from 1 commit
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,37 @@ | ||
"""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 unpack(packet): | ||
"""Unpack the OpenFlow Packet and returns a message.""" | ||
try: | ||
version = packet[0] | ||
except IndexError: | ||
raise UnpackException('null packet') | ||
|
||
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: | ||
if len(binary_data) == header.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. Is there a reason for adding this check to the of_core code? 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. packet (not message) validation. |
||
message.unpack(binary_data) | ||
else: | ||
raise UnpackException( | ||
'packet size does not match packet length field') | ||
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. Shouldn't we always call If the specific Message does have a "body" it may always wait for its body. If no body was passed, then the unpack will fail - and it should! Eventually, if the "body" is optional (or some attributes are), then the unpack should handle it naturally without raising an exception. If the specific message does not have a body, then the On the current code we are "ignoring" when the Message (class) does have a mandatory body and the message was sent through the network just with the header and without the body, which shouldn't be accepted while doing the unpack, right? Does it make sense? Considering my comment, it seems that this block can be something like message.unpack(binary_data) And I would also do the check 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. This unpack function unpacks all the data there is. About what the block does:
+ this PR is about bringing the method from of_core to pyof. We need this to have more reliable tests. There are still many improvements to this function, but I plan to make them in a different PR (regarding version agnostic messages for ex like the hello msg, as we have discussed before). 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. @diraol, @erickvermot told me something before that you might not be aware (see my review comment to write it in the module docstring): the purpose of this PR is to provide the same unpack of of_core to python-openflow tests. Thus, my review is based on copying the unpack method. Maybe we should move discussions about changing the current of_code code to another issue, wherever it will be (we must remove this duplication). Regarding Erick's 2 bullets, I see those issues in a different way, respectively:
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. About the first parag: About the bullets, you are mixing message validation with packet validation.
|
||
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