|
5 | 5 |
|
6 | 6 | # Local source tree imports |
7 | 7 | from pyof.foundation.base import GenericMessage, GenericStruct |
8 | | -from pyof.foundation.basic_types import (BinaryData, Pad, |
| 8 | +from pyof.foundation.basic_types import (BinaryData, FixedTypeList, Pad, |
9 | 9 | UBInt8, UBInt16, UBInt32, UBInt64) |
10 | 10 | from pyof.v0x04.common.flow_match import Match |
11 | 11 | from pyof.v0x04.common.header import Header, Type |
12 | | -from pyof.v0x04.controller2switch.common import MultipartTypes |
| 12 | +from pyof.v0x04.controller2switch.common import (ExperimenterMultipartHeader, |
| 13 | + MultipartTypes, TableFeatures) |
13 | 14 |
|
14 | 15 | # Third-party imports |
15 | 16 |
|
16 | | - |
17 | 17 | __all__ = ('MultipartRequest', 'MultipartRequestFlags', |
18 | | - 'AggregateStatsRequest', 'FlowStatsRequest', 'PortStatsRequest', |
19 | | - 'QueueStatsRequest', 'GroupStatsRequest', 'MeterMultipartRequest') |
| 18 | + 'AggregateStatsRequest', 'FlowStatsRequest', |
| 19 | + 'PortStatsRequest', 'QueueStatsRequest', |
| 20 | + 'GroupStatsRequest', 'MeterMultipartRequest', |
| 21 | + 'TableFeatures') |
20 | 22 |
|
21 | 23 | # Enum |
22 | 24 |
|
@@ -63,8 +65,87 @@ def __init__(self, xid=None, multipart_type=None, flags=None, body=b''): |
63 | 65 | self.flags = flags |
64 | 66 | self.body = body |
65 | 67 |
|
| 68 | + def pack(self, value=None): |
| 69 | + """Pack a MultipartRequest using the object's attributes. |
| 70 | +
|
| 71 | + This method will pack the attribute body and multipart_type before pack |
| 72 | + the MultipartRequest object, then will return this struct as a |
| 73 | + binary data. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + multiparty_packed (bytes): Binary data with MultipartRequest |
| 77 | + packed. |
| 78 | + """ |
| 79 | + buff = self.body |
| 80 | + if not value: |
| 81 | + value = self.body |
| 82 | + |
| 83 | + if value: |
| 84 | + if isinstance(value, (list, FixedTypeList)): |
| 85 | + obj = self._get_body_instance() |
| 86 | + obj.extend(value) |
| 87 | + elif hasattr(value, 'pack'): |
| 88 | + obj = value |
| 89 | + |
| 90 | + self.body = obj.pack() |
| 91 | + |
| 92 | + multiparty_packed = super().pack() |
| 93 | + self.body = buff |
| 94 | + |
| 95 | + return multiparty_packed |
| 96 | + |
| 97 | + def unpack(self, buff, offset=0): |
| 98 | + """Unpack a binary message into this object's attributes. |
| 99 | +
|
| 100 | + Unpack the binary value *buff* and update this object attributes based |
| 101 | + on the results. It is an inplace method and it receives the binary data |
| 102 | + of the message **without the header**. |
| 103 | +
|
| 104 | + This class' unpack method is like the :meth:`.GenericMessage.unpack` |
| 105 | + one, except for the ``body`` attribute which has its type determined |
| 106 | + by the ``multipart_type`` attribute. |
| 107 | +
|
| 108 | + Args: |
| 109 | + buff (bytes): Binary data package to be unpacked, without the |
| 110 | + header. |
| 111 | + """ |
| 112 | + super().unpack(buff[offset:]) |
| 113 | + self._unpack_body() |
| 114 | + |
| 115 | + def _unpack_body(self): |
| 116 | + """Unpack `body` replace it by the result.""" |
| 117 | + obj = self._get_body_instance() |
| 118 | + obj.unpack(self.body.value) |
| 119 | + self.body = obj |
| 120 | + |
| 121 | + def _get_body_instance(self): |
| 122 | + """Method used to return the body instance.""" |
| 123 | + simple_body = { |
| 124 | + MultipartTypes.OFPMP_FLOW: FlowStatsRequest, |
| 125 | + MultipartTypes.OFPMP_AGGREGATE: AggregateStatsRequest, |
| 126 | + MultipartTypes.OFPMP_PORT_STATS: PortStatsRequest, |
| 127 | + MultipartTypes.OFPMP_QUEUE: QueueStatsRequest, |
| 128 | + MultipartTypes.OFPMP_GROUP: GroupStatsRequest, |
| 129 | + MultipartTypes.OFPMP_METER: MeterMultipartRequest, |
| 130 | + MultipartTypes.OFPMP_EXPERIMENTER: ExperimenterMultipartHeader |
| 131 | + } |
| 132 | + |
| 133 | + array_of_bodies = {MultipartTypes.OFPMP_TABLE_FEATURES: TableFeatures} |
| 134 | + |
| 135 | + if isinstance(self.multipart_type, (int, UBInt16)): |
| 136 | + self.multipart_type = self.multipart_type.enum_ref( |
| 137 | + self.multipart_type.value) |
| 138 | + |
| 139 | + pyof_class = simple_body.get(self.multipart_type, None) |
| 140 | + if pyof_class: |
| 141 | + return pyof_class() |
| 142 | + |
| 143 | + array_of_class = array_of_bodies.get(self.multipart_type, None) |
| 144 | + if array_of_class: |
| 145 | + return FixedTypeList(pyof_class=array_of_class) |
| 146 | + |
| 147 | + return BinaryData(b'') |
66 | 148 |
|
67 | | -# MultipartRequest body |
68 | 149 |
|
69 | 150 | class AggregateStatsRequest(GenericStruct): |
70 | 151 | """Body for ofp_stats_request of type OFPST_AGGREGATE.""" |
|
0 commit comments