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