Shad Ansari | 4dade92 | 2017-12-13 19:06:49 +0000 | [diff] [blame] | 1 | # |
| 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 | # |
| 16 | from scapy.fields import ByteField, PacketField, IntField |
| 17 | from scapy.fields import ShortField, ConditionalField |
| 18 | from scapy.packet import Packet |
| 19 | |
| 20 | from voltha.extensions.omci.omci_defs import FixedLenField |
| 21 | from 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 |
| 27 | from voltha.extensions.omci.omci_messages import OmciCreateResponse |
| 28 | |
| 29 | |
| 30 | class OmciFrame(Packet): |
| 31 | name = "OmciFrame" |
| 32 | fields_desc = [ |
| 33 | ShortField("transaction_id", 0), |
| 34 | ByteField("message_type", None), |
| 35 | ByteField("omci", 0x0a), |
| 36 | ConditionalField(FixedLenField( |
| 37 | PacketField("omci_message", None, OmciCreate), align=36), |
| 38 | lambda pkt: pkt.message_type == OmciCreate.message_id), |
| 39 | ConditionalField(FixedLenField( |
| 40 | PacketField("omci_message", None, OmciCreateResponse), align=36), |
| 41 | lambda pkt: pkt.message_type == OmciCreateResponse.message_id), |
| 42 | ConditionalField(FixedLenField( |
| 43 | PacketField("omci_message", None, OmciDelete), align=36), |
| 44 | lambda pkt: pkt.message_type == OmciDelete.message_id), |
| 45 | ConditionalField(FixedLenField( |
| 46 | PacketField("omci_message", None, OmciDeleteResponse), align=36), |
| 47 | lambda pkt: pkt.message_type == OmciDeleteResponse.message_id), |
| 48 | ConditionalField(FixedLenField( |
| 49 | PacketField("omci_message", None, OmciSet), align=36), |
| 50 | lambda pkt: pkt.message_type == OmciSet.message_id), |
| 51 | ConditionalField(FixedLenField( |
| 52 | PacketField("omci_message", None, OmciSetResponse), align=36), |
| 53 | lambda pkt: pkt.message_type == OmciSetResponse.message_id), |
| 54 | ConditionalField(FixedLenField( |
| 55 | PacketField("omci_message", None, OmciGet), align=36), |
| 56 | lambda pkt: pkt.message_type == OmciGet.message_id), |
| 57 | ConditionalField(FixedLenField( |
| 58 | PacketField("omci_message", None, OmciGetResponse), align=36), |
| 59 | lambda pkt: pkt.message_type == OmciGetResponse.message_id), |
| 60 | ConditionalField(FixedLenField( |
| 61 | PacketField("omci_message", None, OmciGetAllAlarms), align=36), |
| 62 | lambda pkt: pkt.message_type == OmciGetAllAlarms.message_id), |
| 63 | ConditionalField(FixedLenField( |
| 64 | PacketField( |
| 65 | "omci_message", None, OmciGetAllAlarmsResponse), align=36), |
| 66 | lambda pkt: |
| 67 | pkt.message_type == OmciGetAllAlarmsResponse.message_id), |
| 68 | ConditionalField(FixedLenField( |
| 69 | PacketField("omci_message", None, OmciGetAllAlarmsNext), align=36), |
| 70 | lambda pkt: pkt.message_type == OmciGetAllAlarmsNext.message_id), |
| 71 | ConditionalField(FixedLenField( |
| 72 | PacketField( |
| 73 | "omci_message", None, OmciGetAllAlarmsNextResponse), align=36), |
| 74 | lambda pkt: |
| 75 | pkt.message_type == OmciGetAllAlarmsNextResponse.message_id), |
| 76 | |
| 77 | ConditionalField(FixedLenField( |
| 78 | PacketField("omci_message", None, OmciMibUpload), align=36), |
| 79 | lambda pkt: pkt.message_type == OmciMibUpload.message_id), |
| 80 | ConditionalField(FixedLenField( |
| 81 | PacketField("omci_message", None, OmciMibUploadResponse), align=36), |
| 82 | lambda pkt: pkt.message_type == OmciMibUploadResponse.message_id), |
| 83 | ConditionalField(FixedLenField( |
| 84 | PacketField("omci_message", None, OmciMibUploadNext), align=36), |
| 85 | lambda pkt: |
| 86 | pkt.message_type == OmciMibUploadNext.message_id), |
| 87 | ConditionalField(FixedLenField( |
| 88 | PacketField("omci_message", None, OmciMibUploadNextResponse), align=36), |
| 89 | lambda pkt: pkt.message_type == OmciMibUploadNextResponse.message_id), |
| 90 | |
| 91 | ConditionalField(FixedLenField( |
| 92 | PacketField("omci_message", None, OmciMibReset), align=36), |
| 93 | lambda pkt: pkt.message_type == OmciMibReset.message_id), |
| 94 | ConditionalField(FixedLenField( |
| 95 | PacketField("omci_message", None, OmciMibResetResponse), align=36), |
| 96 | lambda pkt: pkt.message_type == OmciMibResetResponse.message_id), |
| 97 | |
| 98 | # TODO add entries for remaining OMCI message types |
| 99 | |
| 100 | IntField("omci_trailer", 0x00000028) |
| 101 | ] |
| 102 | |
| 103 | # We needed to patch the do_dissect(...) method of Packet, because |
| 104 | # it wiped out already dissected conditional fields with None if they |
| 105 | # referred to the same field name. We marked the only new line of code |
| 106 | # with "Extra condition added". |
| 107 | def do_dissect(self, s): |
| 108 | raw = s |
| 109 | self.raw_packet_cache_fields = {} |
| 110 | for f in self.fields_desc: |
| 111 | if not s: |
| 112 | break |
| 113 | s, fval = f.getfield(self, s) |
| 114 | # We need to track fields with mutable values to discard |
| 115 | # .raw_packet_cache when needed. |
| 116 | if f.islist or f.holds_packets: |
| 117 | self.raw_packet_cache_fields[f.name] = f.do_copy(fval) |
| 118 | # Extra condition added |
| 119 | if fval is not None or f.name not in self.fields: |
| 120 | self.fields[f.name] = fval |
| 121 | assert(raw.endswith(s)) |
| 122 | self.raw_packet_cache = raw[:-len(s)] if s else raw |
| 123 | self.explicit = 1 |
| 124 | return s |