1 | """Helper python-openflow functions.""" |
||
2 | |||
3 | # System imports |
||
4 | |||
5 | # Third-party imports |
||
6 | |||
7 | # Local source tree imports |
||
8 | # Importing asynchronous messages |
||
9 | from pyof.v0x01.asynchronous.error_msg import ErrorMsg |
||
10 | from pyof.v0x01.asynchronous.flow_removed import FlowRemoved |
||
11 | from pyof.v0x01.asynchronous.packet_in import PacketIn |
||
12 | from pyof.v0x01.asynchronous.port_status import PortStatus |
||
13 | # Importing controller2switch messages |
||
14 | from pyof.v0x01.common.header import Header, Type |
||
15 | from pyof.v0x01.controller2switch.barrier_reply import BarrierReply |
||
16 | from pyof.v0x01.controller2switch.barrier_request import BarrierRequest |
||
17 | from pyof.v0x01.controller2switch.features_reply import FeaturesReply |
||
18 | from pyof.v0x01.controller2switch.features_request import FeaturesRequest |
||
19 | from pyof.v0x01.controller2switch.flow_mod import FlowMod |
||
20 | from pyof.v0x01.controller2switch.get_config_reply import GetConfigReply |
||
21 | from pyof.v0x01.controller2switch.get_config_request import GetConfigRequest |
||
22 | from pyof.v0x01.controller2switch.packet_out import PacketOut |
||
23 | from pyof.v0x01.controller2switch.port_mod import PortMod |
||
24 | from pyof.v0x01.controller2switch.queue_get_config_reply import ( |
||
25 | QueueGetConfigReply) |
||
26 | from pyof.v0x01.controller2switch.queue_get_config_request import ( |
||
27 | QueueGetConfigRequest) |
||
28 | from pyof.v0x01.controller2switch.set_config import SetConfig |
||
29 | from pyof.v0x01.controller2switch.stats_reply import StatsReply |
||
30 | from pyof.v0x01.controller2switch.stats_request import StatsRequest |
||
31 | # Importing symmetric messages |
||
32 | from pyof.v0x01.symmetric.echo_reply import EchoReply |
||
33 | from pyof.v0x01.symmetric.echo_request import EchoRequest |
||
34 | from pyof.v0x01.symmetric.hello import Hello |
||
35 | from pyof.v0x01.symmetric.vendor_header import VendorHeader |
||
36 | |||
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 | } |
||
64 | |||
65 | |||
66 | def new_message_from_message_type(message_type): |
||
67 | """Given an OpenFlow Message Type, return an empty message of that type. |
||
68 | |||
69 | Args: |
||
70 | messageType (:class:`~pyof.v0x01.common.header.Type`): |
||
71 | Python-openflow message. |
||
72 | |||
73 | Returns: |
||
74 | Empty OpenFlow message of the requested message type. |
||
75 | |||
76 | Raises: |
||
77 | KytosUndefinedMessageType: Unkown Message_Type. |
||
78 | |||
79 | """ |
||
80 | message_type = str(message_type) |
||
81 | |||
82 | if message_type not in MESSAGE_TYPES: |
||
83 | raise ValueError('"{}" is not known.'.format(message_type)) |
||
84 | |||
85 | message_class = MESSAGE_TYPES.get(message_type) |
||
86 | message_instance = message_class() |
||
87 | |||
88 | return message_instance |
||
89 | |||
90 | |||
91 | View Code Duplication | def new_message_from_header(header): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
92 | """Given an OF Header, return an empty message of header's message_type. |
||
93 | |||
94 | Args: |
||
95 | header (~pyof.v0x01.common.header.Header): Unpacked OpenFlow Header. |
||
96 | |||
97 | Returns: |
||
98 | Empty OpenFlow message of the same type of message_type attribute from |
||
99 | the given header. |
||
100 | The header attribute of the message will be populated. |
||
101 | |||
102 | Raises: |
||
103 | KytosUndefinedMessageType: Unkown Message_Type. |
||
104 | |||
105 | """ |
||
106 | message_type = header.message_type |
||
107 | if not isinstance(message_type, Type): |
||
108 | try: |
||
109 | if isinstance(message_type, str): |
||
110 | message_type = Type[message_type] |
||
111 | elif isinstance(message_type, int): |
||
112 | message_type = Type(message_type) |
||
113 | except ValueError: |
||
114 | raise ValueError |
||
115 | |||
116 | message = new_message_from_message_type(message_type) |
||
117 | message.header.xid = header.xid |
||
118 | message.header.length = header.length |
||
119 | |||
120 | return message |
||
121 | |||
122 | |||
123 | View Code Duplication | def unpack_message(buffer): |
|
0 ignored issues
–
show
|
|||
124 | """Unpack the whole buffer, including header pack. |
||
125 | |||
126 | Args: |
||
127 | buffer (bytes): Bytes representation of a openflow message. |
||
128 | |||
129 | Returns: |
||
130 | object: Instance of openflow message. |
||
131 | |||
132 | """ |
||
133 | hdr_size = Header().get_size() |
||
134 | hdr_buff, msg_buff = buffer[:hdr_size], buffer[hdr_size:] |
||
135 | header = Header() |
||
136 | header.unpack(hdr_buff) |
||
137 | message = new_message_from_header(header) |
||
138 | message.unpack(msg_buff) |
||
139 | return message |
||
140 |