blob: 4a527bf5394cd74c002960ba17dbc84639121452 [file] [log] [blame]
Chip Boling6e27b352020-02-14 09:10:01 -06001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
Andrea Campanella7167ebb2020-02-24 09:56:38 +01003 * Copyright 2020-present Open Networking Foundation
4
Chip Boling6e27b352020-02-14 09:10:01 -06005 * 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 Campanella7167ebb2020-02-24 09:56:38 +01008
Chip Boling6e27b352020-02-14 09:10:01 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Andrea Campanella7167ebb2020-02-24 09:56:38 +010010
Chip Boling6e27b352020-02-14 09:10:01 -060011 * 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 Boling6e27b352020-02-14 09:10:01 -060016 */
17
18package omci
19
20import (
21 "errors"
Chip Boling6e27b352020-02-14 09:10:01 -060022 "github.com/google/gopacket"
David K. Bainbridgeadf422d2021-04-09 16:06:41 +000023 me "github.com/opencord/omci-lib-go/generated"
Chip Boling6e27b352020-02-14 09:10:01 -060024)
25
26var nextLayerMapping map[MessageType]gopacket.LayerType
27
28var (
Chip Boling157c9b92021-04-21 09:58:36 -050029 // Baseline Message Types
Chip Boling6e27b352020-02-14 09:10:01 -060030 LayerTypeCreateRequest gopacket.LayerType
31 LayerTypeDeleteRequest gopacket.LayerType
32 LayerTypeSetRequest gopacket.LayerType
33 LayerTypeGetRequest gopacket.LayerType
34 LayerTypeGetAllAlarmsRequest gopacket.LayerType
35 LayerTypeGetAllAlarmsNextRequest gopacket.LayerType
36 LayerTypeMibUploadRequest gopacket.LayerType
37 LayerTypeMibUploadNextRequest gopacket.LayerType
38 LayerTypeMibResetRequest gopacket.LayerType
39 LayerTypeTestRequest gopacket.LayerType
40 LayerTypeStartSoftwareDownloadRequest gopacket.LayerType
41 LayerTypeDownloadSectionRequest gopacket.LayerType
42 LayerTypeDownloadSectionLastRequest gopacket.LayerType
43 LayerTypeEndSoftwareDownloadRequest gopacket.LayerType
44 LayerTypeActivateSoftwareRequest gopacket.LayerType
45 LayerTypeCommitSoftwareRequest gopacket.LayerType
46 LayerTypeSynchronizeTimeRequest gopacket.LayerType
47 LayerTypeRebootRequest gopacket.LayerType
48 LayerTypeGetNextRequest gopacket.LayerType
49 LayerTypeGetCurrentDataRequest gopacket.LayerType
50 LayerTypeSetTableRequest gopacket.LayerType
Chip Boling157c9b92021-04-21 09:58:36 -050051
52 // Extended Message Types
Chip Boling00a30d22021-05-04 13:31:52 -050053 LayerTypeGetRequestExtended gopacket.LayerType
54 LayerTypeDownloadSectionRequestExtended gopacket.LayerType
55 LayerTypeDownloadSectionLastRequestExtended gopacket.LayerType
Chip Boling6e27b352020-02-14 09:10:01 -060056)
57var (
Chip Boling157c9b92021-04-21 09:58:36 -050058 // Baseline Message Types
Chip Boling6e27b352020-02-14 09:10:01 -060059 LayerTypeCreateResponse gopacket.LayerType
60 LayerTypeDeleteResponse gopacket.LayerType
61 LayerTypeSetResponse gopacket.LayerType
62 LayerTypeGetResponse gopacket.LayerType
63 LayerTypeGetAllAlarmsResponse gopacket.LayerType
64 LayerTypeGetAllAlarmsNextResponse gopacket.LayerType
65 LayerTypeMibUploadResponse gopacket.LayerType
66 LayerTypeMibUploadNextResponse gopacket.LayerType
67 LayerTypeMibResetResponse gopacket.LayerType
68 LayerTypeAlarmNotification gopacket.LayerType
69 LayerTypeAttributeValueChange gopacket.LayerType
70 LayerTypeTestResponse gopacket.LayerType
71 LayerTypeStartSoftwareDownloadResponse gopacket.LayerType
72 LayerTypeDownloadSectionResponse gopacket.LayerType
73 LayerTypeEndSoftwareDownloadResponse gopacket.LayerType
74 LayerTypeActivateSoftwareResponse gopacket.LayerType
75 LayerTypeCommitSoftwareResponse gopacket.LayerType
76 LayerTypeSynchronizeTimeResponse gopacket.LayerType
77 LayerTypeRebootResponse gopacket.LayerType
78 LayerTypeGetNextResponse gopacket.LayerType
79 LayerTypeTestResult gopacket.LayerType
80 LayerTypeGetCurrentDataResponse gopacket.LayerType
81 LayerTypeSetTableResponse gopacket.LayerType
Chip Boling157c9b92021-04-21 09:58:36 -050082
83 // Extended Message Types
Chip Boling00a30d22021-05-04 13:31:52 -050084 LayerTypeGetResponseExtended gopacket.LayerType
85 LayerTypeDownloadSectionResponseExtended gopacket.LayerType
Chip Boling6e27b352020-02-14 09:10:01 -060086)
87
88func mkReqLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType {
89 return gopacket.RegisterLayerType(1000+(int(mt)|int(me.AR)),
90 gopacket.LayerTypeMetadata{Name: mts, Decoder: decode})
91}
92
93func mkRespLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType {
94 return gopacket.RegisterLayerType(1000+(int(mt)|int(me.AK)),
95 gopacket.LayerTypeMetadata{Name: mts, Decoder: decode})
96}
97
98func mkLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType {
99 return gopacket.RegisterLayerType(1000+(int(mt)),
100 gopacket.LayerTypeMetadata{Name: mts, Decoder: decode})
101}
102
103func init() {
104 // Create layers for message_type & action
105 LayerTypeCreateRequest = mkReqLayer(me.Create, "CreateRequest", gopacket.DecodeFunc(decodeCreateRequest))
106 LayerTypeDeleteRequest = mkReqLayer(me.Delete, "DeleteRequest", gopacket.DecodeFunc(decodeDeleteRequest))
107 LayerTypeSetRequest = mkReqLayer(me.Set, "SetRequest", gopacket.DecodeFunc(decodeSetRequest))
108 LayerTypeGetRequest = mkReqLayer(me.Get, "GetRequest", gopacket.DecodeFunc(decodeGetRequest))
109 LayerTypeGetAllAlarmsRequest = mkReqLayer(me.GetAllAlarms, "GetAllAlarmsRequest", gopacket.DecodeFunc(decodeGetAllAlarmsRequest))
110 LayerTypeGetAllAlarmsNextRequest = mkReqLayer(me.GetAllAlarmsNext, "GetAllAlarmsNextRequest", gopacket.DecodeFunc(decodeGetAllAlarmsNextRequest))
111 LayerTypeMibUploadRequest = mkReqLayer(me.MibUpload, "MibUploadRequest", gopacket.DecodeFunc(decodeMibUploadRequest))
112 LayerTypeMibUploadNextRequest = mkReqLayer(me.MibUploadNext, "MibUploadNextRequest", gopacket.DecodeFunc(decodeMibUploadNextRequest))
113 LayerTypeMibResetRequest = mkReqLayer(me.MibReset, "MibResetRequest", gopacket.DecodeFunc(decodeMibResetRequest))
114 LayerTypeTestRequest = mkReqLayer(me.Test, "TestRequest", gopacket.DecodeFunc(decodeTestRequest))
115 LayerTypeStartSoftwareDownloadRequest = mkReqLayer(me.StartSoftwareDownload, "StartSoftwareDownloadRequest", gopacket.DecodeFunc(decodeStartSoftwareDownloadRequest))
116
117 // For Download section, AR=0 if not response expected, AR=1 if response expected (last section of a window)
118 LayerTypeDownloadSectionRequest = mkLayer(me.DownloadSection, "DownloadSectionRequest", gopacket.DecodeFunc(decodeDownloadSectionRequest))
119 LayerTypeDownloadSectionLastRequest = mkReqLayer(me.DownloadSection, "DownloadLastSectionRequest", gopacket.DecodeFunc(decodeDownloadSectionRequest))
120 LayerTypeEndSoftwareDownloadRequest = mkReqLayer(me.EndSoftwareDownload, "EndSoftwareDownloadRequest", gopacket.DecodeFunc(decodeEndSoftwareDownloadRequest))
121 LayerTypeActivateSoftwareRequest = mkReqLayer(me.ActivateSoftware, "ActivateSoftwareRequest", gopacket.DecodeFunc(decodeActivateSoftwareRequest))
122 LayerTypeCommitSoftwareRequest = mkReqLayer(me.CommitSoftware, "CommitSoftwareRequest", gopacket.DecodeFunc(decodeCommitSoftwareRequest))
123 LayerTypeSynchronizeTimeRequest = mkReqLayer(me.SynchronizeTime, "SynchronizeTimeRequest", gopacket.DecodeFunc(decodeSynchronizeTimeRequest))
124 LayerTypeRebootRequest = mkReqLayer(me.Reboot, "RebootRequest", gopacket.DecodeFunc(decodeRebootRequest))
125 LayerTypeGetNextRequest = mkReqLayer(me.GetNext, "GetNextRequest", gopacket.DecodeFunc(decodeGetNextRequest))
126 LayerTypeGetCurrentDataRequest = mkReqLayer(me.GetCurrentData, "GetCurrentDataRequest", gopacket.DecodeFunc(decodeGetCurrentDataRequest))
127 LayerTypeSetTableRequest = mkReqLayer(me.SetTable, "SetTableRequest", gopacket.DecodeFunc(decodeSetTableRequest))
128
129 LayerTypeCreateResponse = mkRespLayer(me.Create, "CreateResponse", gopacket.DecodeFunc(decodeCreateResponse))
130 LayerTypeDeleteResponse = mkRespLayer(me.Delete, "DeleteResponse", gopacket.DecodeFunc(decodeDeleteResponse))
131 LayerTypeSetResponse = mkRespLayer(me.Set, "SetResponse", gopacket.DecodeFunc(decodeSetResponse))
132 LayerTypeGetResponse = mkRespLayer(me.Get, "GetResponse", gopacket.DecodeFunc(decodeGetResponse))
133 LayerTypeGetAllAlarmsResponse = mkRespLayer(me.GetAllAlarms, "GetAllAlarmsResponse", gopacket.DecodeFunc(decodeGetAllAlarmsResponse))
134 LayerTypeGetAllAlarmsNextResponse = mkRespLayer(me.GetAllAlarmsNext, "GetAllAlarmsNextResponse", gopacket.DecodeFunc(decodeGetAllAlarmsNextResponse))
135 LayerTypeMibUploadResponse = mkRespLayer(me.MibUpload, "MibUploadResponse", gopacket.DecodeFunc(decodeMibUploadResponse))
136 LayerTypeMibUploadNextResponse = mkRespLayer(me.MibUploadNext, "MibUploadNextResponse", gopacket.DecodeFunc(decodeMibUploadNextResponse))
137 LayerTypeMibResetResponse = mkRespLayer(me.MibReset, "MibResetResponse", gopacket.DecodeFunc(decodeMibResetResponse))
138 LayerTypeAlarmNotification = mkLayer(me.AlarmNotification, "AlarmNotification", gopacket.DecodeFunc(decodeAlarmNotification))
139 LayerTypeAttributeValueChange = mkLayer(me.AttributeValueChange, "AttributeValueChange", gopacket.DecodeFunc(decodeAttributeValueChange))
140 LayerTypeTestResponse = mkRespLayer(me.Test, "TestResponse", gopacket.DecodeFunc(decodeTestResponse))
141 LayerTypeStartSoftwareDownloadResponse = mkRespLayer(me.StartSoftwareDownload, "StartSoftwareDownloadResponse", gopacket.DecodeFunc(decodeStartSoftwareDownloadResponse))
142 LayerTypeDownloadSectionResponse = mkRespLayer(me.DownloadSection, "DownloadSectionResponse", gopacket.DecodeFunc(decodeDownloadSectionResponse))
143 LayerTypeEndSoftwareDownloadResponse = mkRespLayer(me.EndSoftwareDownload, "EndSoftwareDownloadResponse", gopacket.DecodeFunc(decodeEndSoftwareDownloadResponse))
144 LayerTypeActivateSoftwareResponse = mkRespLayer(me.ActivateSoftware, "ActivateSoftwareResponse", gopacket.DecodeFunc(decodeActivateSoftwareResponse))
145 LayerTypeCommitSoftwareResponse = mkRespLayer(me.CommitSoftware, "CommitSoftwareResponse", gopacket.DecodeFunc(decodeCommitSoftwareResponse))
146 LayerTypeSynchronizeTimeResponse = mkRespLayer(me.SynchronizeTime, "SynchronizeTimeResponse", gopacket.DecodeFunc(decodeSynchronizeTimeResponse))
147 LayerTypeRebootResponse = mkRespLayer(me.Reboot, "RebootResponse", gopacket.DecodeFunc(decodeRebootResponse))
148 LayerTypeGetNextResponse = mkRespLayer(me.GetNext, "GetNextResponse", gopacket.DecodeFunc(decodeGetNextResponse))
149 LayerTypeTestResult = mkRespLayer(me.TestResult, "TestResult", gopacket.DecodeFunc(decodeTestResult))
150 LayerTypeGetCurrentDataResponse = mkRespLayer(me.GetCurrentData, "GetCurrentDataResponse", gopacket.DecodeFunc(decodeGetCurrentDataResponse))
151 LayerTypeSetTableResponse = mkRespLayer(me.SetTable, "SetTableResponse", gopacket.DecodeFunc(decodeSetTableResponse))
152
Chip Boling157c9b92021-04-21 09:58:36 -0500153 // Extended message set support
154
155 LayerTypeGetRequestExtended = mkReqLayer(me.Get|me.ExtendedOffset, "GetRequest-Ext", gopacket.DecodeFunc(decodeGetRequestExtended))
156 LayerTypeGetResponseExtended = mkRespLayer(me.Get|me.ExtendedOffset, "GetResponse-Ext", gopacket.DecodeFunc(decodeGetResponseExtended))
Chip Boling00a30d22021-05-04 13:31:52 -0500157 LayerTypeDownloadSectionRequestExtended = mkLayer(me.DownloadSection|me.ExtendedOffset, "DownloadSectionRequest-Ext", gopacket.DecodeFunc(decodeDownloadSectionRequestExtended))
158 LayerTypeDownloadSectionLastRequestExtended = mkReqLayer(me.DownloadSection|me.ExtendedOffset, "DownloadLastSectionRequest-Ext", gopacket.DecodeFunc(decodeDownloadSectionRequestExtended))
159 LayerTypeDownloadSectionResponseExtended = mkRespLayer(me.DownloadSection|me.ExtendedOffset, "DownloadSectionResponse-Ext", gopacket.DecodeFunc(decodeDownloadSectionResponseExtended))
Chip Boling157c9b92021-04-21 09:58:36 -0500160
Chip Boling6e27b352020-02-14 09:10:01 -0600161 // Map message_type and action to layer
162 nextLayerMapping = make(map[MessageType]gopacket.LayerType)
163
164 nextLayerMapping[CreateRequestType] = LayerTypeCreateRequest
165 nextLayerMapping[DeleteRequestType] = LayerTypeDeleteRequest
166 nextLayerMapping[SetRequestType] = LayerTypeSetRequest
167 nextLayerMapping[GetRequestType] = LayerTypeGetRequest
168 nextLayerMapping[GetAllAlarmsRequestType] = LayerTypeGetAllAlarmsRequest
169 nextLayerMapping[GetAllAlarmsNextRequestType] = LayerTypeGetAllAlarmsNextRequest
170 nextLayerMapping[MibUploadRequestType] = LayerTypeMibUploadRequest
171 nextLayerMapping[MibUploadNextRequestType] = LayerTypeMibUploadNextRequest
172 nextLayerMapping[MibResetRequestType] = LayerTypeMibResetRequest
173 nextLayerMapping[TestRequestType] = LayerTypeTestRequest
174 nextLayerMapping[StartSoftwareDownloadRequestType] = LayerTypeStartSoftwareDownloadRequest
175 nextLayerMapping[DownloadSectionRequestType] = LayerTypeDownloadSectionRequest
Chip Boling8c8018e2021-02-22 15:56:00 -0600176 nextLayerMapping[DownloadSectionRequestWithResponseType] = LayerTypeDownloadSectionRequest
Chip Boling6e27b352020-02-14 09:10:01 -0600177 nextLayerMapping[EndSoftwareDownloadRequestType] = LayerTypeEndSoftwareDownloadRequest
178 nextLayerMapping[ActivateSoftwareRequestType] = LayerTypeActivateSoftwareRequest
179 nextLayerMapping[CommitSoftwareRequestType] = LayerTypeCommitSoftwareRequest
180 nextLayerMapping[SynchronizeTimeRequestType] = LayerTypeSynchronizeTimeRequest
181 nextLayerMapping[RebootRequestType] = LayerTypeRebootRequest
182 nextLayerMapping[GetNextRequestType] = LayerTypeGetNextRequest
183 nextLayerMapping[GetCurrentDataRequestType] = LayerTypeGetCurrentDataRequest
184 nextLayerMapping[SetTableRequestType] = LayerTypeSetTableRequest
185
186 nextLayerMapping[CreateResponseType] = LayerTypeCreateResponse
187 nextLayerMapping[DeleteResponseType] = LayerTypeDeleteResponse
188 nextLayerMapping[SetResponseType] = LayerTypeSetResponse
189 nextLayerMapping[GetResponseType] = LayerTypeGetResponse
190 nextLayerMapping[GetAllAlarmsResponseType] = LayerTypeGetAllAlarmsResponse
191 nextLayerMapping[GetAllAlarmsNextResponseType] = LayerTypeGetAllAlarmsNextResponse
192 nextLayerMapping[MibUploadResponseType] = LayerTypeMibUploadResponse
193 nextLayerMapping[MibUploadNextResponseType] = LayerTypeMibUploadNextResponse
194 nextLayerMapping[MibResetResponseType] = LayerTypeMibResetResponse
195 nextLayerMapping[TestResponseType] = LayerTypeTestResponse
196 nextLayerMapping[StartSoftwareDownloadResponseType] = LayerTypeStartSoftwareDownloadResponse
197 nextLayerMapping[DownloadSectionResponseType] = LayerTypeDownloadSectionResponse
198 nextLayerMapping[EndSoftwareDownloadResponseType] = LayerTypeEndSoftwareDownloadResponse
199 nextLayerMapping[ActivateSoftwareResponseType] = LayerTypeActivateSoftwareResponse
200 nextLayerMapping[CommitSoftwareResponseType] = LayerTypeCommitSoftwareResponse
201 nextLayerMapping[SynchronizeTimeResponseType] = LayerTypeSynchronizeTimeResponse
202 nextLayerMapping[RebootResponseType] = LayerTypeRebootResponse
203 nextLayerMapping[GetNextResponseType] = LayerTypeGetNextResponse
204 nextLayerMapping[GetCurrentDataResponseType] = LayerTypeGetCurrentDataResponse
205 nextLayerMapping[SetTableResponseType] = LayerTypeSetTableResponse
206
207 nextLayerMapping[AttributeValueChangeType] = LayerTypeAttributeValueChange
208 nextLayerMapping[AlarmNotificationType] = LayerTypeAlarmNotification
209 nextLayerMapping[TestResultType] = LayerTypeTestResult
Chip Boling157c9b92021-04-21 09:58:36 -0500210
211 // Extended message support
212 nextLayerMapping[GetRequestType+ExtendedTypeDecodeOffset] = LayerTypeGetRequestExtended
213 nextLayerMapping[GetResponseType+ExtendedTypeDecodeOffset] = LayerTypeGetResponseExtended
Chip Boling00a30d22021-05-04 13:31:52 -0500214
215 // For Download section, AR=0 if not response expected, AR=1 if response expected (last section of a window)
216 nextLayerMapping[DownloadSectionRequestType+ExtendedTypeDecodeOffset] = LayerTypeDownloadSectionRequestExtended
217 nextLayerMapping[DownloadSectionRequestWithResponseType+ExtendedTypeDecodeOffset] = LayerTypeDownloadSectionLastRequestExtended
218 nextLayerMapping[DownloadSectionResponseType+ExtendedTypeDecodeOffset] = LayerTypeDownloadSectionResponseExtended
Chip Boling6e27b352020-02-14 09:10:01 -0600219}
220
Chip Boling157c9b92021-04-21 09:58:36 -0500221func MsgTypeToNextLayer(mt MessageType, isExtended bool) (gopacket.LayerType, error) {
222 if isExtended {
223 mt |= ExtendedTypeDecodeOffset
224 }
Chip Boling6e27b352020-02-14 09:10:01 -0600225 nextLayer, ok := nextLayerMapping[mt]
226 if ok {
227 return nextLayer, nil
228 }
Chip Boling157c9b92021-04-21 09:58:36 -0500229 return gopacket.LayerTypeZero, errors.New("unknown/unsupported message type")
Chip Boling6e27b352020-02-14 09:10:01 -0600230}