Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 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 | */ |
| 17 | package omci |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | me "github.com/cboling/omci/generated" |
| 22 | "github.com/google/gopacket" |
| 23 | ) |
| 24 | |
| 25 | var nextLayerMapping map[MessageType]gopacket.LayerType |
| 26 | |
| 27 | var ( |
| 28 | LayerTypeCreateRequest gopacket.LayerType |
| 29 | LayerTypeDeleteRequest gopacket.LayerType |
| 30 | LayerTypeSetRequest gopacket.LayerType |
| 31 | LayerTypeGetRequest gopacket.LayerType |
| 32 | LayerTypeGetAllAlarmsRequest gopacket.LayerType |
| 33 | LayerTypeGetAllAlarmsNextRequest gopacket.LayerType |
| 34 | LayerTypeMibUploadRequest gopacket.LayerType |
| 35 | LayerTypeMibUploadNextRequest gopacket.LayerType |
| 36 | LayerTypeMibResetRequest gopacket.LayerType |
| 37 | LayerTypeTestRequest gopacket.LayerType |
| 38 | LayerTypeStartSoftwareDownloadRequest gopacket.LayerType |
| 39 | LayerTypeDownloadSectionRequest gopacket.LayerType |
| 40 | LayerTypeDownloadSectionLastRequest gopacket.LayerType |
| 41 | LayerTypeEndSoftwareDownloadRequest gopacket.LayerType |
| 42 | LayerTypeActivateSoftwareRequest gopacket.LayerType |
| 43 | LayerTypeCommitSoftwareRequest gopacket.LayerType |
| 44 | LayerTypeSynchronizeTimeRequest gopacket.LayerType |
| 45 | LayerTypeRebootRequest gopacket.LayerType |
| 46 | LayerTypeGetNextRequest gopacket.LayerType |
| 47 | LayerTypeGetCurrentDataRequest gopacket.LayerType |
| 48 | LayerTypeSetTableRequest gopacket.LayerType |
| 49 | ) |
| 50 | var ( |
| 51 | LayerTypeCreateResponse gopacket.LayerType |
| 52 | LayerTypeDeleteResponse gopacket.LayerType |
| 53 | LayerTypeSetResponse gopacket.LayerType |
| 54 | LayerTypeGetResponse gopacket.LayerType |
| 55 | LayerTypeGetAllAlarmsResponse gopacket.LayerType |
| 56 | LayerTypeGetAllAlarmsNextResponse gopacket.LayerType |
| 57 | LayerTypeMibUploadResponse gopacket.LayerType |
| 58 | LayerTypeMibUploadNextResponse gopacket.LayerType |
| 59 | LayerTypeMibResetResponse gopacket.LayerType |
| 60 | LayerTypeAlarmNotification gopacket.LayerType |
| 61 | LayerTypeAttributeValueChange gopacket.LayerType |
| 62 | LayerTypeTestResponse gopacket.LayerType |
| 63 | LayerTypeStartSoftwareDownloadResponse gopacket.LayerType |
| 64 | LayerTypeDownloadSectionResponse gopacket.LayerType |
| 65 | LayerTypeEndSoftwareDownloadResponse gopacket.LayerType |
| 66 | LayerTypeActivateSoftwareResponse gopacket.LayerType |
| 67 | LayerTypeCommitSoftwareResponse gopacket.LayerType |
| 68 | LayerTypeSynchronizeTimeResponse gopacket.LayerType |
| 69 | LayerTypeRebootResponse gopacket.LayerType |
| 70 | LayerTypeGetNextResponse gopacket.LayerType |
| 71 | LayerTypeTestResult gopacket.LayerType |
| 72 | LayerTypeGetCurrentDataResponse gopacket.LayerType |
| 73 | LayerTypeSetTableResponse gopacket.LayerType |
| 74 | ) |
| 75 | |
| 76 | func mkReqLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType { |
| 77 | return gopacket.RegisterLayerType(1000+(int(mt)|int(me.AR)), |
| 78 | gopacket.LayerTypeMetadata{Name: mts, Decoder: decode}) |
| 79 | } |
| 80 | |
| 81 | func mkRespLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType { |
| 82 | return gopacket.RegisterLayerType(1000+(int(mt)|int(me.AK)), |
| 83 | gopacket.LayerTypeMetadata{Name: mts, Decoder: decode}) |
| 84 | } |
| 85 | |
| 86 | func mkLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType { |
| 87 | return gopacket.RegisterLayerType(1000+(int(mt)), |
| 88 | gopacket.LayerTypeMetadata{Name: mts, Decoder: decode}) |
| 89 | } |
| 90 | |
| 91 | func init() { |
| 92 | // Create layers for message_type & action |
| 93 | LayerTypeCreateRequest = mkReqLayer(me.Create, "CreateRequest", gopacket.DecodeFunc(decodeCreateRequest)) |
| 94 | LayerTypeDeleteRequest = mkReqLayer(me.Delete, "DeleteRequest", gopacket.DecodeFunc(decodeDeleteRequest)) |
| 95 | LayerTypeSetRequest = mkReqLayer(me.Set, "SetRequest", gopacket.DecodeFunc(decodeSetRequest)) |
| 96 | LayerTypeGetRequest = mkReqLayer(me.Get, "GetRequest", gopacket.DecodeFunc(decodeGetRequest)) |
| 97 | LayerTypeGetAllAlarmsRequest = mkReqLayer(me.GetAllAlarms, "GetAllAlarmsRequest", gopacket.DecodeFunc(decodeGetAllAlarmsRequest)) |
| 98 | LayerTypeGetAllAlarmsNextRequest = mkReqLayer(me.GetAllAlarmsNext, "GetAllAlarmsNextRequest", gopacket.DecodeFunc(decodeGetAllAlarmsNextRequest)) |
| 99 | LayerTypeMibUploadRequest = mkReqLayer(me.MibUpload, "MibUploadRequest", gopacket.DecodeFunc(decodeMibUploadRequest)) |
| 100 | LayerTypeMibUploadNextRequest = mkReqLayer(me.MibUploadNext, "MibUploadNextRequest", gopacket.DecodeFunc(decodeMibUploadNextRequest)) |
| 101 | LayerTypeMibResetRequest = mkReqLayer(me.MibReset, "MibResetRequest", gopacket.DecodeFunc(decodeMibResetRequest)) |
| 102 | LayerTypeTestRequest = mkReqLayer(me.Test, "TestRequest", gopacket.DecodeFunc(decodeTestRequest)) |
| 103 | LayerTypeStartSoftwareDownloadRequest = mkReqLayer(me.StartSoftwareDownload, "StartSoftwareDownloadRequest", gopacket.DecodeFunc(decodeStartSoftwareDownloadRequest)) |
| 104 | |
| 105 | // For Download section, AR=0 if not response expected, AR=1 if response expected (last section of a window) |
| 106 | LayerTypeDownloadSectionRequest = mkLayer(me.DownloadSection, "DownloadSectionRequest", gopacket.DecodeFunc(decodeDownloadSectionRequest)) |
| 107 | LayerTypeDownloadSectionLastRequest = mkReqLayer(me.DownloadSection, "DownloadLastSectionRequest", gopacket.DecodeFunc(decodeDownloadSectionRequest)) |
| 108 | LayerTypeEndSoftwareDownloadRequest = mkReqLayer(me.EndSoftwareDownload, "EndSoftwareDownloadRequest", gopacket.DecodeFunc(decodeEndSoftwareDownloadRequest)) |
| 109 | LayerTypeActivateSoftwareRequest = mkReqLayer(me.ActivateSoftware, "ActivateSoftwareRequest", gopacket.DecodeFunc(decodeActivateSoftwareRequest)) |
| 110 | LayerTypeCommitSoftwareRequest = mkReqLayer(me.CommitSoftware, "CommitSoftwareRequest", gopacket.DecodeFunc(decodeCommitSoftwareRequest)) |
| 111 | LayerTypeSynchronizeTimeRequest = mkReqLayer(me.SynchronizeTime, "SynchronizeTimeRequest", gopacket.DecodeFunc(decodeSynchronizeTimeRequest)) |
| 112 | LayerTypeRebootRequest = mkReqLayer(me.Reboot, "RebootRequest", gopacket.DecodeFunc(decodeRebootRequest)) |
| 113 | LayerTypeGetNextRequest = mkReqLayer(me.GetNext, "GetNextRequest", gopacket.DecodeFunc(decodeGetNextRequest)) |
| 114 | LayerTypeGetCurrentDataRequest = mkReqLayer(me.GetCurrentData, "GetCurrentDataRequest", gopacket.DecodeFunc(decodeGetCurrentDataRequest)) |
| 115 | LayerTypeSetTableRequest = mkReqLayer(me.SetTable, "SetTableRequest", gopacket.DecodeFunc(decodeSetTableRequest)) |
| 116 | |
| 117 | LayerTypeCreateResponse = mkRespLayer(me.Create, "CreateResponse", gopacket.DecodeFunc(decodeCreateResponse)) |
| 118 | LayerTypeDeleteResponse = mkRespLayer(me.Delete, "DeleteResponse", gopacket.DecodeFunc(decodeDeleteResponse)) |
| 119 | LayerTypeSetResponse = mkRespLayer(me.Set, "SetResponse", gopacket.DecodeFunc(decodeSetResponse)) |
| 120 | LayerTypeGetResponse = mkRespLayer(me.Get, "GetResponse", gopacket.DecodeFunc(decodeGetResponse)) |
| 121 | LayerTypeGetAllAlarmsResponse = mkRespLayer(me.GetAllAlarms, "GetAllAlarmsResponse", gopacket.DecodeFunc(decodeGetAllAlarmsResponse)) |
| 122 | LayerTypeGetAllAlarmsNextResponse = mkRespLayer(me.GetAllAlarmsNext, "GetAllAlarmsNextResponse", gopacket.DecodeFunc(decodeGetAllAlarmsNextResponse)) |
| 123 | LayerTypeMibUploadResponse = mkRespLayer(me.MibUpload, "MibUploadResponse", gopacket.DecodeFunc(decodeMibUploadResponse)) |
| 124 | LayerTypeMibUploadNextResponse = mkRespLayer(me.MibUploadNext, "MibUploadNextResponse", gopacket.DecodeFunc(decodeMibUploadNextResponse)) |
| 125 | LayerTypeMibResetResponse = mkRespLayer(me.MibReset, "MibResetResponse", gopacket.DecodeFunc(decodeMibResetResponse)) |
| 126 | LayerTypeAlarmNotification = mkLayer(me.AlarmNotification, "AlarmNotification", gopacket.DecodeFunc(decodeAlarmNotification)) |
| 127 | LayerTypeAttributeValueChange = mkLayer(me.AttributeValueChange, "AttributeValueChange", gopacket.DecodeFunc(decodeAttributeValueChange)) |
| 128 | LayerTypeTestResponse = mkRespLayer(me.Test, "TestResponse", gopacket.DecodeFunc(decodeTestResponse)) |
| 129 | LayerTypeStartSoftwareDownloadResponse = mkRespLayer(me.StartSoftwareDownload, "StartSoftwareDownloadResponse", gopacket.DecodeFunc(decodeStartSoftwareDownloadResponse)) |
| 130 | LayerTypeDownloadSectionResponse = mkRespLayer(me.DownloadSection, "DownloadSectionResponse", gopacket.DecodeFunc(decodeDownloadSectionResponse)) |
| 131 | LayerTypeEndSoftwareDownloadResponse = mkRespLayer(me.EndSoftwareDownload, "EndSoftwareDownloadResponse", gopacket.DecodeFunc(decodeEndSoftwareDownloadResponse)) |
| 132 | LayerTypeActivateSoftwareResponse = mkRespLayer(me.ActivateSoftware, "ActivateSoftwareResponse", gopacket.DecodeFunc(decodeActivateSoftwareResponse)) |
| 133 | LayerTypeCommitSoftwareResponse = mkRespLayer(me.CommitSoftware, "CommitSoftwareResponse", gopacket.DecodeFunc(decodeCommitSoftwareResponse)) |
| 134 | LayerTypeSynchronizeTimeResponse = mkRespLayer(me.SynchronizeTime, "SynchronizeTimeResponse", gopacket.DecodeFunc(decodeSynchronizeTimeResponse)) |
| 135 | LayerTypeRebootResponse = mkRespLayer(me.Reboot, "RebootResponse", gopacket.DecodeFunc(decodeRebootResponse)) |
| 136 | LayerTypeGetNextResponse = mkRespLayer(me.GetNext, "GetNextResponse", gopacket.DecodeFunc(decodeGetNextResponse)) |
| 137 | LayerTypeTestResult = mkRespLayer(me.TestResult, "TestResult", gopacket.DecodeFunc(decodeTestResult)) |
| 138 | LayerTypeGetCurrentDataResponse = mkRespLayer(me.GetCurrentData, "GetCurrentDataResponse", gopacket.DecodeFunc(decodeGetCurrentDataResponse)) |
| 139 | LayerTypeSetTableResponse = mkRespLayer(me.SetTable, "SetTableResponse", gopacket.DecodeFunc(decodeSetTableResponse)) |
| 140 | |
| 141 | // Map message_type and action to layer |
| 142 | nextLayerMapping = make(map[MessageType]gopacket.LayerType) |
| 143 | |
| 144 | nextLayerMapping[CreateRequestType] = LayerTypeCreateRequest |
| 145 | nextLayerMapping[DeleteRequestType] = LayerTypeDeleteRequest |
| 146 | nextLayerMapping[SetRequestType] = LayerTypeSetRequest |
| 147 | nextLayerMapping[GetRequestType] = LayerTypeGetRequest |
| 148 | nextLayerMapping[GetAllAlarmsRequestType] = LayerTypeGetAllAlarmsRequest |
| 149 | nextLayerMapping[GetAllAlarmsNextRequestType] = LayerTypeGetAllAlarmsNextRequest |
| 150 | nextLayerMapping[MibUploadRequestType] = LayerTypeMibUploadRequest |
| 151 | nextLayerMapping[MibUploadNextRequestType] = LayerTypeMibUploadNextRequest |
| 152 | nextLayerMapping[MibResetRequestType] = LayerTypeMibResetRequest |
| 153 | nextLayerMapping[TestRequestType] = LayerTypeTestRequest |
| 154 | nextLayerMapping[StartSoftwareDownloadRequestType] = LayerTypeStartSoftwareDownloadRequest |
| 155 | nextLayerMapping[DownloadSectionRequestType] = LayerTypeDownloadSectionRequest |
| 156 | nextLayerMapping[EndSoftwareDownloadRequestType] = LayerTypeEndSoftwareDownloadRequest |
| 157 | nextLayerMapping[ActivateSoftwareRequestType] = LayerTypeActivateSoftwareRequest |
| 158 | nextLayerMapping[CommitSoftwareRequestType] = LayerTypeCommitSoftwareRequest |
| 159 | nextLayerMapping[SynchronizeTimeRequestType] = LayerTypeSynchronizeTimeRequest |
| 160 | nextLayerMapping[RebootRequestType] = LayerTypeRebootRequest |
| 161 | nextLayerMapping[GetNextRequestType] = LayerTypeGetNextRequest |
| 162 | nextLayerMapping[GetCurrentDataRequestType] = LayerTypeGetCurrentDataRequest |
| 163 | nextLayerMapping[SetTableRequestType] = LayerTypeSetTableRequest |
| 164 | |
| 165 | nextLayerMapping[CreateResponseType] = LayerTypeCreateResponse |
| 166 | nextLayerMapping[DeleteResponseType] = LayerTypeDeleteResponse |
| 167 | nextLayerMapping[SetResponseType] = LayerTypeSetResponse |
| 168 | nextLayerMapping[GetResponseType] = LayerTypeGetResponse |
| 169 | nextLayerMapping[GetAllAlarmsResponseType] = LayerTypeGetAllAlarmsResponse |
| 170 | nextLayerMapping[GetAllAlarmsNextResponseType] = LayerTypeGetAllAlarmsNextResponse |
| 171 | nextLayerMapping[MibUploadResponseType] = LayerTypeMibUploadResponse |
| 172 | nextLayerMapping[MibUploadNextResponseType] = LayerTypeMibUploadNextResponse |
| 173 | nextLayerMapping[MibResetResponseType] = LayerTypeMibResetResponse |
| 174 | nextLayerMapping[TestResponseType] = LayerTypeTestResponse |
| 175 | nextLayerMapping[StartSoftwareDownloadResponseType] = LayerTypeStartSoftwareDownloadResponse |
| 176 | nextLayerMapping[DownloadSectionResponseType] = LayerTypeDownloadSectionResponse |
| 177 | nextLayerMapping[EndSoftwareDownloadResponseType] = LayerTypeEndSoftwareDownloadResponse |
| 178 | nextLayerMapping[ActivateSoftwareResponseType] = LayerTypeActivateSoftwareResponse |
| 179 | nextLayerMapping[CommitSoftwareResponseType] = LayerTypeCommitSoftwareResponse |
| 180 | nextLayerMapping[SynchronizeTimeResponseType] = LayerTypeSynchronizeTimeResponse |
| 181 | nextLayerMapping[RebootResponseType] = LayerTypeRebootResponse |
| 182 | nextLayerMapping[GetNextResponseType] = LayerTypeGetNextResponse |
| 183 | nextLayerMapping[GetCurrentDataResponseType] = LayerTypeGetCurrentDataResponse |
| 184 | nextLayerMapping[SetTableResponseType] = LayerTypeSetTableResponse |
| 185 | |
| 186 | nextLayerMapping[AttributeValueChangeType] = LayerTypeAttributeValueChange |
| 187 | nextLayerMapping[AlarmNotificationType] = LayerTypeAlarmNotification |
| 188 | nextLayerMapping[TestResultType] = LayerTypeTestResult |
| 189 | } |
| 190 | |
| 191 | func MsgTypeToNextLayer(mt MessageType) (gopacket.LayerType, error) { |
| 192 | nextLayer, ok := nextLayerMapping[mt] |
| 193 | if ok { |
| 194 | return nextLayer, nil |
| 195 | } |
| 196 | return gopacket.LayerTypeZero, errors.New("unknown message type") |
| 197 | } |