blob: c0d7d4aeefa7dbf54a75e2f15d19a0611a5bc6ff [file] [log] [blame]
Chip Boling32aab302019-01-23 10:50:18 -06001#
2# Copyright 2017 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16from scapy.fields import ByteField, PacketField, IntField
17from scapy.fields import ShortField, ConditionalField
18from scapy.packet import Packet
19
20from voltha.extensions.omci.omci_fields import FixedLenField
21from voltha.extensions.omci.omci_messages import OmciCreate, OmciDelete, \
22 OmciDeleteResponse, OmciSet, OmciSetResponse, OmciGet, OmciGetResponse, \
23 OmciGetAllAlarms, OmciGetAllAlarmsResponse, OmciGetAllAlarmsNext, \
24 OmciMibResetResponse, OmciMibReset, OmciMibUploadNextResponse, \
25 OmciMibUploadNext, OmciMibUploadResponse, OmciMibUpload, \
26 OmciGetAllAlarmsNextResponse, OmciAttributeValueChange, \
27 OmciTestResult, OmciAlarmNotification, \
28 OmciReboot, OmciRebootResponse, OmciGetNext, OmciGetNextResponse, \
29 OmciSynchronizeTime, OmciSynchronizeTimeResponse, OmciGetCurrentData, \
30 OmciGetCurrentDataResponse, OmciStartSoftwareDownload, OmciStartSoftwareDownloadResponse, \
31 OmciDownloadSection, OmciDownloadSectionLast, OmciDownloadSectionResponse, \
32 OmciEndSoftwareDownload, OmciEndSoftwareDownloadResponse, \
33 OmciActivateImage, OmciActivateImageResponse, \
34 OmciCommitImage, OmciCommitImageResponse
35
36from voltha.extensions.omci.omci_messages import OmciCreateResponse
37
38
39class OmciFrame(Packet):
40 name = "OmciFrame"
41 fields_desc = [
42 ShortField("transaction_id", 0),
43 ByteField("message_type", None),
44 ByteField("omci", 0x0a),
45 ConditionalField(FixedLenField(
46 PacketField("omci_message", None, OmciCreate), align=36),
47 lambda pkt: pkt.message_type == OmciCreate.message_id),
48 ConditionalField(FixedLenField(
49 PacketField("omci_message", None, OmciCreateResponse), align=36),
50 lambda pkt: pkt.message_type == OmciCreateResponse.message_id),
51 ConditionalField(FixedLenField(
52 PacketField("omci_message", None, OmciDelete), align=36),
53 lambda pkt: pkt.message_type == OmciDelete.message_id),
54 ConditionalField(FixedLenField(
55 PacketField("omci_message", None, OmciDeleteResponse), align=36),
56 lambda pkt: pkt.message_type == OmciDeleteResponse.message_id),
57 ConditionalField(FixedLenField(
58 PacketField("omci_message", None, OmciSet), align=36),
59 lambda pkt: pkt.message_type == OmciSet.message_id),
60 ConditionalField(FixedLenField(
61 PacketField("omci_message", None, OmciSetResponse), align=36),
62 lambda pkt: pkt.message_type == OmciSetResponse.message_id),
63 ConditionalField(FixedLenField(
64 PacketField("omci_message", None, OmciGet), align=36),
65 lambda pkt: pkt.message_type == OmciGet.message_id),
66 ConditionalField(FixedLenField(
67 PacketField("omci_message", None, OmciGetResponse), align=36),
68 lambda pkt: pkt.message_type == OmciGetResponse.message_id),
69 ConditionalField(FixedLenField(
70 PacketField("omci_message", None, OmciGetAllAlarms), align=36),
71 lambda pkt: pkt.message_type == OmciGetAllAlarms.message_id),
72 ConditionalField(FixedLenField(
73 PacketField(
74 "omci_message", None, OmciGetAllAlarmsResponse), align=36),
75 lambda pkt:
76 pkt.message_type == OmciGetAllAlarmsResponse.message_id),
77 ConditionalField(FixedLenField(
78 PacketField("omci_message", None, OmciGetAllAlarmsNext), align=36),
79 lambda pkt: pkt.message_type == OmciGetAllAlarmsNext.message_id),
80 ConditionalField(FixedLenField(
81 PacketField(
82 "omci_message", None, OmciGetAllAlarmsNextResponse), align=36),
83 lambda pkt:
84 pkt.message_type == OmciGetAllAlarmsNextResponse.message_id),
85
86 ConditionalField(FixedLenField(
87 PacketField("omci_message", None, OmciMibUpload), align=36),
88 lambda pkt: pkt.message_type == OmciMibUpload.message_id),
89 ConditionalField(FixedLenField(
90 PacketField("omci_message", None, OmciMibUploadResponse), align=36),
91 lambda pkt: pkt.message_type == OmciMibUploadResponse.message_id),
92 ConditionalField(FixedLenField(
93 PacketField("omci_message", None, OmciMibUploadNext), align=36),
94 lambda pkt:
95 pkt.message_type == OmciMibUploadNext.message_id),
96 ConditionalField(FixedLenField(
97 PacketField("omci_message", None, OmciMibUploadNextResponse), align=36),
98 lambda pkt: pkt.message_type == OmciMibUploadNextResponse.message_id),
99
100 ConditionalField(FixedLenField(
101 PacketField("omci_message", None, OmciMibReset), align=36),
102 lambda pkt: pkt.message_type == OmciMibReset.message_id),
103 ConditionalField(FixedLenField(
104 PacketField("omci_message", None, OmciMibResetResponse), align=36),
105 lambda pkt: pkt.message_type == OmciMibResetResponse.message_id),
106
107 ConditionalField(FixedLenField(
108 PacketField("omci_message", None, OmciAlarmNotification), align=36),
109 lambda pkt: pkt.message_type == OmciAlarmNotification.message_id),
110 ConditionalField(FixedLenField(
111 PacketField("omci_message", None, OmciAttributeValueChange), align=36),
112 lambda pkt: pkt.message_type == OmciAttributeValueChange.message_id),
113 ConditionalField(FixedLenField(
114 PacketField("omci_message", None, OmciTestResult), align=36),
115 lambda pkt: pkt.message_type == OmciTestResult.message_id),
116
117 ConditionalField(FixedLenField(
118 PacketField("omci_message", None, OmciReboot), align=36),
119 lambda pkt: pkt.message_type == OmciReboot.message_id),
120 ConditionalField(FixedLenField(
121 PacketField("omci_message", None, OmciRebootResponse), align=36),
122 lambda pkt: pkt.message_type == OmciRebootResponse.message_id),
123
124 ConditionalField(FixedLenField(
125 PacketField("omci_message", None, OmciGetNext), align=36),
126 lambda pkt: pkt.message_type == OmciGetNext.message_id),
127 ConditionalField(FixedLenField(
128 PacketField("omci_message", None, OmciGetNextResponse), align=36),
129 lambda pkt: pkt.message_type == OmciGetNextResponse.message_id),
130
131 ConditionalField(FixedLenField(
132 PacketField("omci_message", None, OmciSynchronizeTime), align=36),
133 lambda pkt: pkt.message_type == OmciSynchronizeTime.message_id),
134 ConditionalField(FixedLenField(
135 PacketField("omci_message", None, OmciSynchronizeTimeResponse), align=36),
136 lambda pkt: pkt.message_type == OmciSynchronizeTimeResponse.message_id),
137
138 ConditionalField(FixedLenField(
139 PacketField("omci_message", None, OmciGetCurrentData), align=36),
140 lambda pkt: pkt.message_type == OmciGetCurrentData.message_id),
141 ConditionalField(FixedLenField(
142 PacketField("omci_message", None, OmciGetCurrentDataResponse), align=36),
143 lambda pkt: pkt.message_type == OmciGetCurrentDataResponse.message_id),
144
145 ConditionalField(FixedLenField(
146 PacketField("omci_message", None, OmciStartSoftwareDownload), align=36),
147 lambda pkt: pkt.message_type == OmciStartSoftwareDownload.message_id),
148 ConditionalField(FixedLenField(
149 PacketField("omci_message", None, OmciStartSoftwareDownloadResponse), align=36),
150 lambda pkt: pkt.message_type == OmciStartSoftwareDownloadResponse.message_id),
151 ConditionalField(FixedLenField(
152 PacketField("omci_message", None, OmciDownloadSection), align=36),
153 lambda pkt: pkt.message_type == OmciDownloadSection.message_id),
154 ConditionalField(FixedLenField(
155 PacketField("omci_message", None, OmciDownloadSectionLast), align=36),
156 lambda pkt: pkt.message_type == OmciDownloadSectionLast.message_id),
157 ConditionalField(FixedLenField(
158 PacketField("omci_message", None, OmciDownloadSectionResponse), align=36),
159 lambda pkt: pkt.message_type == OmciDownloadSectionResponse.message_id),
160 ConditionalField(FixedLenField(
161 PacketField("omci_message", None, OmciEndSoftwareDownload), align=36),
162 lambda pkt: pkt.message_type == OmciEndSoftwareDownload.message_id),
163 ConditionalField(FixedLenField(
164 PacketField("omci_message", None, OmciEndSoftwareDownloadResponse), align=36),
165 lambda pkt: pkt.message_type == OmciEndSoftwareDownloadResponse.message_id),
166
167 ConditionalField(FixedLenField(
168 PacketField("omci_message", None, OmciActivateImage), align=36),
169 lambda pkt: pkt.message_type == OmciActivateImage.message_id),
170 ConditionalField(FixedLenField(
171 PacketField("omci_message", None, OmciActivateImageResponse), align=36),
172 lambda pkt: pkt.message_type == OmciActivateImageResponse.message_id),
173
174 ConditionalField(FixedLenField(
175 PacketField("omci_message", None, OmciCommitImage), align=36),
176 lambda pkt: pkt.message_type == OmciCommitImage.message_id),
177 ConditionalField(FixedLenField(
178 PacketField("omci_message", None, OmciCommitImageResponse), align=36),
179 lambda pkt: pkt.message_type == OmciCommitImageResponse.message_id),
180
181 # TODO add entries for remaining OMCI message types
182
183 IntField("omci_trailer", 0x00000028)
184 ]
185
186 # We needed to patch the do_dissect(...) method of Packet, because
187 # it wiped out already dissected conditional fields with None if they
188 # referred to the same field name. We marked the only new line of code
189 # with "Extra condition added".
190 def do_dissect(self, s):
191 raw = s
192 self.raw_packet_cache_fields = {}
193 for f in self.fields_desc:
194 if not s:
195 break
196 s, fval = f.getfield(self, s)
197 # We need to track fields with mutable values to discard
198 # .raw_packet_cache when needed.
199 if f.islist or f.holds_packets:
200 self.raw_packet_cache_fields[f.name] = f.do_copy(fval)
201 # Extra condition added
202 if fval is not None or f.name not in self.fields:
203 self.fields[f.name] = fval
204 assert(raw.endswith(s))
205 self.raw_packet_cache = raw[:-len(s)] if s else raw
206 self.explicit = 1
207 return s