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

Commit f2d0381

Browse files
committed
Improve unpack method in pyof/utils
Reuse the methods unpack_message from pyof.[version].common.utils Create a constant called MESSAGE_TYPES Fix #344
1 parent 42d1821 commit f2d0381

3 files changed

Lines changed: 110 additions & 106 deletions

File tree

pyof/utils.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"""
66
from pyof import v0x01, v0x04
77
from pyof.foundation.exceptions import UnpackException
8+
from pyof.v0x01.common import utils as u_v0x01 # pylint: disable=unused-import
9+
from pyof.v0x04.common import utils as u_v0x04 # pylint: disable=unused-import
810

911
PYOF_VERSION_LIBS = {0x01: v0x01,
1012
0x04: v0x04}
@@ -25,8 +27,7 @@ def validate_packet(packet):
2527
if packet_length < 8 or packet_length > 2**16:
2628
raise UnpackException('invalid packet')
2729

28-
if packet_length != int.from_bytes(packet[2:4],
29-
byteorder='big'):
30+
if packet_length != int.from_bytes(packet[2:4], byteorder='big'):
3031
raise UnpackException('invalid packet')
3132

3233
version = packet[0]
@@ -37,6 +38,9 @@ def validate_packet(packet):
3738
def unpack(packet):
3839
"""Unpack the OpenFlow Packet and returns a message.
3940
41+
Args:
42+
packet: buffer with the openflow packet.
43+
4044
Returns:
4145
GenericMessage: Message unpacked based on openflow packet.
4246
@@ -53,15 +57,7 @@ def unpack(packet):
5357
raise UnpackException('Version not supported')
5458

5559
try:
56-
header = pyof_lib.common.header.Header()
57-
header.unpack(packet[:header.get_size()])
58-
message = pyof_lib.common.utils.new_message_from_header(header)
59-
60-
binary_data = packet[header.get_size():]
61-
binary_data_size = header.length - header.get_size()
62-
63-
if binary_data and len(binary_data) == binary_data_size:
64-
message.unpack(binary_data)
60+
message = pyof_lib.common.utils.unpack_message(packet)
6561
return message
6662
except (UnpackException, ValueError) as exception:
6763
raise UnpackException(exception)

pyof/v0x01/common/utils.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,33 @@
3434
from pyof.v0x01.symmetric.hello import Hello
3535
from pyof.v0x01.symmetric.vendor_header import VendorHeader
3636

37-
__all__ = ('new_message_from_header', 'new_message_from_message_type',
38-
'unpack_message')
37+
__all__ = ('MESSAGE_TYPES', 'new_message_from_header',
38+
'new_message_from_message_type', 'unpack_message')
39+
40+
MESSAGE_TYPES = {
41+
str(Type.OFPT_HELLO): Hello,
42+
str(Type.OFPT_ERROR): ErrorMsg,
43+
str(Type.OFPT_ECHO_REQUEST): EchoRequest,
44+
str(Type.OFPT_ECHO_REPLY): EchoReply,
45+
str(Type.OFPT_VENDOR): VendorHeader,
46+
str(Type.OFPT_FEATURES_REQUEST): FeaturesRequest,
47+
str(Type.OFPT_FEATURES_REPLY): FeaturesReply,
48+
str(Type.OFPT_GET_CONFIG_REQUEST): GetConfigRequest,
49+
str(Type.OFPT_GET_CONFIG_REPLY): GetConfigReply,
50+
str(Type.OFPT_SET_CONFIG): SetConfig,
51+
str(Type.OFPT_PACKET_IN): PacketIn,
52+
str(Type.OFPT_FLOW_REMOVED): FlowRemoved,
53+
str(Type.OFPT_PORT_STATUS): PortStatus,
54+
str(Type.OFPT_PACKET_OUT): PacketOut,
55+
str(Type.OFPT_FLOW_MOD): FlowMod,
56+
str(Type.OFPT_PORT_MOD): PortMod,
57+
str(Type.OFPT_STATS_REQUEST): StatsRequest,
58+
str(Type.OFPT_STATS_REPLY): StatsReply,
59+
str(Type.OFPT_BARRIER_REQUEST): BarrierRequest,
60+
str(Type.OFPT_BARRIER_REPLY): BarrierReply,
61+
str(Type.OFPT_QUEUE_GET_CONFIG_REQUEST): QueueGetConfigRequest,
62+
str(Type.OFPT_QUEUE_GET_CONFIG_REPLY): QueueGetConfigReply
63+
}
3964

4065

4166
def new_message_from_message_type(message_type):
@@ -54,35 +79,10 @@ def new_message_from_message_type(message_type):
5479
"""
5580
message_type = str(message_type)
5681

57-
available_classes = {
58-
str(Type.OFPT_HELLO): Hello,
59-
str(Type.OFPT_ERROR): ErrorMsg,
60-
str(Type.OFPT_ECHO_REQUEST): EchoRequest,
61-
str(Type.OFPT_ECHO_REPLY): EchoReply,
62-
str(Type.OFPT_VENDOR): VendorHeader,
63-
str(Type.OFPT_FEATURES_REQUEST): FeaturesRequest,
64-
str(Type.OFPT_FEATURES_REPLY): FeaturesReply,
65-
str(Type.OFPT_GET_CONFIG_REQUEST): GetConfigRequest,
66-
str(Type.OFPT_GET_CONFIG_REPLY): GetConfigReply,
67-
str(Type.OFPT_SET_CONFIG): SetConfig,
68-
str(Type.OFPT_PACKET_IN): PacketIn,
69-
str(Type.OFPT_FLOW_REMOVED): FlowRemoved,
70-
str(Type.OFPT_PORT_STATUS): PortStatus,
71-
str(Type.OFPT_PACKET_OUT): PacketOut,
72-
str(Type.OFPT_FLOW_MOD): FlowMod,
73-
str(Type.OFPT_PORT_MOD): PortMod,
74-
str(Type.OFPT_STATS_REQUEST): StatsRequest,
75-
str(Type.OFPT_STATS_REPLY): StatsReply,
76-
str(Type.OFPT_BARRIER_REQUEST): BarrierRequest,
77-
str(Type.OFPT_BARRIER_REPLY): BarrierReply,
78-
str(Type.OFPT_QUEUE_GET_CONFIG_REQUEST): QueueGetConfigRequest,
79-
str(Type.OFPT_QUEUE_GET_CONFIG_REPLY): QueueGetConfigReply
80-
}
81-
82-
if message_type not in available_classes:
82+
if message_type not in MESSAGE_TYPES:
8383
raise ValueError('"{}" is not known.'.format(message_type))
8484

85-
message_class = available_classes.get(message_type)
85+
message_class = MESSAGE_TYPES.get(message_type)
8686
message_instance = message_class()
8787

8888
return message_instance

pyof/v0x04/common/utils.py

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,69 @@
4242
from pyof.v0x04.symmetric.experimenter import ExperimenterHeader
4343
from pyof.v0x04.symmetric.hello import Hello
4444

45-
__all__ = ('new_message_from_header', 'new_message_from_message_type')
45+
__all__ = ('MESSAGE_TYPES', 'new_message_from_header',
46+
'new_message_from_message_type', 'unpack_message')
47+
48+
MESSAGE_TYPES = {
49+
50+
# Symetric/Immutable messages
51+
str(Type.OFPT_HELLO): Hello,
52+
str(Type.OFPT_ERROR): ErrorMsg,
53+
str(Type.OFPT_ECHO_REQUEST): EchoRequest,
54+
str(Type.OFPT_ECHO_REPLY): EchoReply,
55+
str(Type.OFPT_EXPERIMENTER): ExperimenterHeader,
56+
57+
# Switch configuration messages
58+
# Controller/Switch messages
59+
str(Type.OFPT_FEATURES_REQUEST): FeaturesRequest,
60+
str(Type.OFPT_FEATURES_REPLY): FeaturesReply,
61+
str(Type.OFPT_GET_CONFIG_REQUEST): GetConfigRequest,
62+
str(Type.OFPT_GET_CONFIG_REPLY): GetConfigReply,
63+
str(Type.OFPT_SET_CONFIG): SetConfig,
64+
65+
# Async messages
66+
str(Type.OFPT_PACKET_IN): PacketIn,
67+
str(Type.OFPT_FLOW_REMOVED): FlowRemoved,
68+
str(Type.OFPT_PORT_STATUS): PortStatus,
69+
70+
# Controller command messages
71+
# Controller/Switch message
72+
str(Type.OFPT_PACKET_OUT): PacketOut,
73+
str(Type.OFPT_FLOW_MOD): FlowMod,
74+
str(Type.OFPT_GROUP_MOD): GroupMod,
75+
str(Type.OFPT_PORT_MOD): PortMod,
76+
str(Type.OFPT_TABLE_MOD): TableMod,
77+
78+
# Multipart messages.
79+
# Controller/Switch message
80+
str(Type.OFPT_MULTIPART_REPLY): MultipartReply,
81+
str(Type.OFPT_MULTIPART_REQUEST): MultipartRequest,
82+
83+
# Barrier messages
84+
# Controller/Switch message
85+
str(Type.OFPT_BARRIER_REQUEST): BarrierRequest,
86+
str(Type.OFPT_BARRIER_REPLY): BarrierReply,
87+
88+
# Queue Configuration messages
89+
# Controller/Switch message
90+
str(Type.OFPT_QUEUE_GET_CONFIG_REQUEST): QueueGetConfigRequest,
91+
str(Type.OFPT_QUEUE_GET_CONFIG_REPLY): QueueGetConfigReply,
92+
93+
# Controller role change request message
94+
# Controller/Switch message
95+
str(Type.OFPT_ROLE_REQUEST): RoleRequest,
96+
str(Type.OFPT_ROLE_REPLY): RoleReply,
97+
98+
# Asynchronous message configuration
99+
# Controller/Switch message
100+
str(Type.OFPT_GET_ASYNC_REQUEST): GetAsyncRequest,
101+
str(Type.OFPT_GET_ASYNC_REPLY): GetAsyncReply,
102+
str(Type.OFPT_SET_ASYNC): SetAsync,
103+
104+
# Meters and rate limiters configuration messages
105+
# Controller/Switch message
106+
str(Type.OFPT_METER_MOD): MeterMod,
107+
}
46108

47109

48110
def new_message_from_message_type(message_type):
@@ -60,73 +122,11 @@ def new_message_from_message_type(message_type):
60122
61123
"""
62124
message_type = str(message_type)
63-
64-
available_classes = {
65-
66-
# Symetric/Immutable messages
67-
str(Type.OFPT_HELLO): Hello,
68-
str(Type.OFPT_ERROR): ErrorMsg,
69-
str(Type.OFPT_ECHO_REQUEST): EchoRequest,
70-
str(Type.OFPT_ECHO_REPLY): EchoReply,
71-
str(Type.OFPT_EXPERIMENTER): ExperimenterHeader,
72-
73-
# Switch configuration messages
74-
# Controller/Switch messages
75-
str(Type.OFPT_FEATURES_REQUEST): FeaturesRequest,
76-
str(Type.OFPT_FEATURES_REPLY): FeaturesReply,
77-
str(Type.OFPT_GET_CONFIG_REQUEST): GetConfigRequest,
78-
str(Type.OFPT_GET_CONFIG_REPLY): GetConfigReply,
79-
str(Type.OFPT_SET_CONFIG): SetConfig,
80-
81-
# Async messages
82-
str(Type.OFPT_PACKET_IN): PacketIn,
83-
str(Type.OFPT_FLOW_REMOVED): FlowRemoved,
84-
str(Type.OFPT_PORT_STATUS): PortStatus,
85-
86-
# Controller command messages
87-
# Controller/Switch message
88-
str(Type.OFPT_PACKET_OUT): PacketOut,
89-
str(Type.OFPT_FLOW_MOD): FlowMod,
90-
str(Type.OFPT_GROUP_MOD): GroupMod,
91-
str(Type.OFPT_PORT_MOD): PortMod,
92-
str(Type.OFPT_TABLE_MOD): TableMod,
93-
94-
# Multipart messages.
95-
# Controller/Switch message
96-
str(Type.OFPT_MULTIPART_REPLY): MultipartReply,
97-
str(Type.OFPT_MULTIPART_REQUEST): MultipartRequest,
98-
99-
# Barrier messages
100-
# Controller/Switch message
101-
str(Type.OFPT_BARRIER_REQUEST): BarrierRequest,
102-
str(Type.OFPT_BARRIER_REPLY): BarrierReply,
103-
104-
# Queue Configuration messages
105-
# Controller/Switch message
106-
str(Type.OFPT_QUEUE_GET_CONFIG_REQUEST): QueueGetConfigRequest,
107-
str(Type.OFPT_QUEUE_GET_CONFIG_REPLY): QueueGetConfigReply,
108-
109-
# Controller role change request message
110-
# Controller/Switch message
111-
str(Type.OFPT_ROLE_REQUEST): RoleRequest,
112-
str(Type.OFPT_ROLE_REPLY): RoleReply,
113-
114-
# Asynchronous message configuration
115-
# Controller/Switch message
116-
str(Type.OFPT_GET_ASYNC_REQUEST): GetAsyncRequest,
117-
str(Type.OFPT_GET_ASYNC_REPLY): GetAsyncReply,
118-
str(Type.OFPT_SET_ASYNC): SetAsync,
119-
120-
# Meters and rate limiters configuration messages
121-
# Controller/Switch message
122-
str(Type.OFPT_METER_MOD): MeterMod,
123-
}
124-
125-
if message_type not in available_classes:
125+
if message_type not in MESSAGE_TYPES:
126126
msg = "Define class for {} in {}".format(message_type, __file__)
127127
raise ValueError(msg)
128128

129-
message_class = available_classes.get(message_type)
129+
message_class = MESSAGE_TYPES.get(message_type)
130130
message_instance = message_class()
131131

132132
return message_instance
@@ -166,7 +166,15 @@ def new_message_from_header(header):
166166

167167

168168
def unpack_message(buffer):
169-
"""Unpack the whole buffer, including header pack."""
169+
"""Unpack the whole buffer, including header pack.
170+
171+
Args:
172+
buffer (bytes): Bytes representation of a openflow message.
173+
174+
Returns:
175+
object: Instance of openflow message.
176+
177+
"""
170178
hdr_size = Header().get_size()
171179
hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:]
172180
header = Header()

0 commit comments

Comments
 (0)