Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 8f36308

Browse files
erickvermotmacartur
authored andcommitted
introduce general unpack method (comes from of_core.utils)
1 parent f92a11c commit 8f36308

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

pyof/utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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
6+
from pyof.foundation.exceptions import UnpackException
7+
8+
pyof_version_libs = {0x01: pyof.v0x01,
9+
0x04: pyof.v0x04}
10+
11+
12+
def unpack(packet):
13+
"""Unpack the OpenFlow Packet and returns a message."""
14+
try:
15+
version = packet[0]
16+
except IndexError:
17+
raise UnpackException('null packet')
18+
19+
try:
20+
pyof_lib = pyof_version_libs[version]
21+
except KeyError:
22+
raise UnpackException('Version not supported')
23+
24+
try:
25+
header = pyof_lib.common.header.Header()
26+
header.unpack(packet[:8])
27+
message = pyof_lib.common.utils.new_message_from_header(header)
28+
binary_data = packet[8:]
29+
if binary_data:
30+
if len(binary_data) == header.length - 8:
31+
message.unpack(binary_data)
32+
else:
33+
raise UnpackException(
34+
'packet size does not match packet length field')
35+
return message
36+
except (UnpackException, ValueError) as e:
37+
raise UnpackException(e)

0 commit comments

Comments
 (0)