blob: 1e0b686d1e35dcaa3a20e7ba2f4eec6e9613de2f [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
Chip Bolingc15f61d2021-05-13 09:07:17 -050052 // Extended Request 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
Chip Bolingc15f61d2021-05-13 09:07:17 -050083 // Extended Response/Notification Message Types
Chip Boling00a30d22021-05-04 13:31:52 -050084 LayerTypeGetResponseExtended gopacket.LayerType
85 LayerTypeDownloadSectionResponseExtended gopacket.LayerType
Chip Bolingc15f61d2021-05-13 09:07:17 -050086 LayerTypeAlarmNotificationExtended gopacket.LayerType
87 LayerTypeAttributeValueChangeExtended gopacket.LayerType
88 LayerTypeTestResultExtended gopacket.LayerType
Chip Boling6e27b352020-02-14 09:10:01 -060089)
90
91func mkReqLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType {
92 return gopacket.RegisterLayerType(1000+(int(mt)|int(me.AR)),
93 gopacket.LayerTypeMetadata{Name: mts, Decoder: decode})
94}
95
96func mkRespLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType {
97 return gopacket.RegisterLayerType(1000+(int(mt)|int(me.AK)),
98 gopacket.LayerTypeMetadata{Name: mts, Decoder: decode})
99}
100
101func mkLayer(mt me.MsgType, mts string, decode gopacket.DecodeFunc) gopacket.LayerType {
102 return gopacket.RegisterLayerType(1000+(int(mt)),
103 gopacket.LayerTypeMetadata{Name: mts, Decoder: decode})
104}
105
106func init() {
107 // Create layers for message_type & action
108 LayerTypeCreateRequest = mkReqLayer(me.Create, "CreateRequest", gopacket.DecodeFunc(decodeCreateRequest))
109 LayerTypeDeleteRequest = mkReqLayer(me.Delete, "DeleteRequest", gopacket.DecodeFunc(decodeDeleteRequest))
110 LayerTypeSetRequest = mkReqLayer(me.Set, "SetRequest", gopacket.DecodeFunc(decodeSetRequest))
111 LayerTypeGetRequest = mkReqLayer(me.Get, "GetRequest", gopacket.DecodeFunc(decodeGetRequest))
112 LayerTypeGetAllAlarmsRequest = mkReqLayer(me.GetAllAlarms, "GetAllAlarmsRequest", gopacket.DecodeFunc(decodeGetAllAlarmsRequest))
113 LayerTypeGetAllAlarmsNextRequest = mkReqLayer(me.GetAllAlarmsNext, "GetAllAlarmsNextRequest", gopacket.DecodeFunc(decodeGetAllAlarmsNextRequest))
114 LayerTypeMibUploadRequest = mkReqLayer(me.MibUpload, "MibUploadRequest", gopacket.DecodeFunc(decodeMibUploadRequest))
115 LayerTypeMibUploadNextRequest = mkReqLayer(me.MibUploadNext, "MibUploadNextRequest", gopacket.DecodeFunc(decodeMibUploadNextRequest))
116 LayerTypeMibResetRequest = mkReqLayer(me.MibReset, "MibResetRequest", gopacket.DecodeFunc(decodeMibResetRequest))
117 LayerTypeTestRequest = mkReqLayer(me.Test, "TestRequest", gopacket.DecodeFunc(decodeTestRequest))
118 LayerTypeStartSoftwareDownloadRequest = mkReqLayer(me.StartSoftwareDownload, "StartSoftwareDownloadRequest", gopacket.DecodeFunc(decodeStartSoftwareDownloadRequest))
119
120 // For Download section, AR=0 if not response expected, AR=1 if response expected (last section of a window)
121 LayerTypeDownloadSectionRequest = mkLayer(me.DownloadSection, "DownloadSectionRequest", gopacket.DecodeFunc(decodeDownloadSectionRequest))
122 LayerTypeDownloadSectionLastRequest = mkReqLayer(me.DownloadSection, "DownloadLastSectionRequest", gopacket.DecodeFunc(decodeDownloadSectionRequest))
123 LayerTypeEndSoftwareDownloadRequest = mkReqLayer(me.EndSoftwareDownload, "EndSoftwareDownloadRequest", gopacket.DecodeFunc(decodeEndSoftwareDownloadRequest))
124 LayerTypeActivateSoftwareRequest = mkReqLayer(me.ActivateSoftware, "ActivateSoftwareRequest", gopacket.DecodeFunc(decodeActivateSoftwareRequest))
125 LayerTypeCommitSoftwareRequest = mkReqLayer(me.CommitSoftware, "CommitSoftwareRequest", gopacket.DecodeFunc(decodeCommitSoftwareRequest))
126 LayerTypeSynchronizeTimeRequest = mkReqLayer(me.SynchronizeTime, "SynchronizeTimeRequest", gopacket.DecodeFunc(decodeSynchronizeTimeRequest))
127 LayerTypeRebootRequest = mkReqLayer(me.Reboot, "RebootRequest", gopacket.DecodeFunc(decodeRebootRequest))
128 LayerTypeGetNextRequest = mkReqLayer(me.GetNext, "GetNextRequest", gopacket.DecodeFunc(decodeGetNextRequest))
129 LayerTypeGetCurrentDataRequest = mkReqLayer(me.GetCurrentData, "GetCurrentDataRequest", gopacket.DecodeFunc(decodeGetCurrentDataRequest))
130 LayerTypeSetTableRequest = mkReqLayer(me.SetTable, "SetTableRequest", gopacket.DecodeFunc(decodeSetTableRequest))
131
132 LayerTypeCreateResponse = mkRespLayer(me.Create, "CreateResponse", gopacket.DecodeFunc(decodeCreateResponse))
133 LayerTypeDeleteResponse = mkRespLayer(me.Delete, "DeleteResponse", gopacket.DecodeFunc(decodeDeleteResponse))
134 LayerTypeSetResponse = mkRespLayer(me.Set, "SetResponse", gopacket.DecodeFunc(decodeSetResponse))
135 LayerTypeGetResponse = mkRespLayer(me.Get, "GetResponse", gopacket.DecodeFunc(decodeGetResponse))
136 LayerTypeGetAllAlarmsResponse = mkRespLayer(me.GetAllAlarms, "GetAllAlarmsResponse", gopacket.DecodeFunc(decodeGetAllAlarmsResponse))
137 LayerTypeGetAllAlarmsNextResponse = mkRespLayer(me.GetAllAlarmsNext, "GetAllAlarmsNextResponse", gopacket.DecodeFunc(decodeGetAllAlarmsNextResponse))
138 LayerTypeMibUploadResponse = mkRespLayer(me.MibUpload, "MibUploadResponse", gopacket.DecodeFunc(decodeMibUploadResponse))
139 LayerTypeMibUploadNextResponse = mkRespLayer(me.MibUploadNext, "MibUploadNextResponse", gopacket.DecodeFunc(decodeMibUploadNextResponse))
140 LayerTypeMibResetResponse = mkRespLayer(me.MibReset, "MibResetResponse", gopacket.DecodeFunc(decodeMibResetResponse))
141 LayerTypeAlarmNotification = mkLayer(me.AlarmNotification, "AlarmNotification", gopacket.DecodeFunc(decodeAlarmNotification))
142 LayerTypeAttributeValueChange = mkLayer(me.AttributeValueChange, "AttributeValueChange", gopacket.DecodeFunc(decodeAttributeValueChange))
143 LayerTypeTestResponse = mkRespLayer(me.Test, "TestResponse", gopacket.DecodeFunc(decodeTestResponse))
144 LayerTypeStartSoftwareDownloadResponse = mkRespLayer(me.StartSoftwareDownload, "StartSoftwareDownloadResponse", gopacket.DecodeFunc(decodeStartSoftwareDownloadResponse))
145 LayerTypeDownloadSectionResponse = mkRespLayer(me.DownloadSection, "DownloadSectionResponse", gopacket.DecodeFunc(decodeDownloadSectionResponse))
146 LayerTypeEndSoftwareDownloadResponse = mkRespLayer(me.EndSoftwareDownload, "EndSoftwareDownloadResponse", gopacket.DecodeFunc(decodeEndSoftwareDownloadResponse))
147 LayerTypeActivateSoftwareResponse = mkRespLayer(me.ActivateSoftware, "ActivateSoftwareResponse", gopacket.DecodeFunc(decodeActivateSoftwareResponse))
148 LayerTypeCommitSoftwareResponse = mkRespLayer(me.CommitSoftware, "CommitSoftwareResponse", gopacket.DecodeFunc(decodeCommitSoftwareResponse))
149 LayerTypeSynchronizeTimeResponse = mkRespLayer(me.SynchronizeTime, "SynchronizeTimeResponse", gopacket.DecodeFunc(decodeSynchronizeTimeResponse))
150 LayerTypeRebootResponse = mkRespLayer(me.Reboot, "RebootResponse", gopacket.DecodeFunc(decodeRebootResponse))
151 LayerTypeGetNextResponse = mkRespLayer(me.GetNext, "GetNextResponse", gopacket.DecodeFunc(decodeGetNextResponse))
152 LayerTypeTestResult = mkRespLayer(me.TestResult, "TestResult", gopacket.DecodeFunc(decodeTestResult))
153 LayerTypeGetCurrentDataResponse = mkRespLayer(me.GetCurrentData, "GetCurrentDataResponse", gopacket.DecodeFunc(decodeGetCurrentDataResponse))
154 LayerTypeSetTableResponse = mkRespLayer(me.SetTable, "SetTableResponse", gopacket.DecodeFunc(decodeSetTableResponse))
155
Chip Boling157c9b92021-04-21 09:58:36 -0500156 // Extended message set support
157
158 LayerTypeGetRequestExtended = mkReqLayer(me.Get|me.ExtendedOffset, "GetRequest-Ext", gopacket.DecodeFunc(decodeGetRequestExtended))
159 LayerTypeGetResponseExtended = mkRespLayer(me.Get|me.ExtendedOffset, "GetResponse-Ext", gopacket.DecodeFunc(decodeGetResponseExtended))
Chip Boling00a30d22021-05-04 13:31:52 -0500160 LayerTypeDownloadSectionRequestExtended = mkLayer(me.DownloadSection|me.ExtendedOffset, "DownloadSectionRequest-Ext", gopacket.DecodeFunc(decodeDownloadSectionRequestExtended))
161 LayerTypeDownloadSectionLastRequestExtended = mkReqLayer(me.DownloadSection|me.ExtendedOffset, "DownloadLastSectionRequest-Ext", gopacket.DecodeFunc(decodeDownloadSectionRequestExtended))
162 LayerTypeDownloadSectionResponseExtended = mkRespLayer(me.DownloadSection|me.ExtendedOffset, "DownloadSectionResponse-Ext", gopacket.DecodeFunc(decodeDownloadSectionResponseExtended))
Chip Boling157c9b92021-04-21 09:58:36 -0500163
Chip Bolingc15f61d2021-05-13 09:07:17 -0500164 LayerTypeAlarmNotificationExtended = mkLayer(me.AlarmNotification|me.ExtendedOffset, "AlarmNotification-Ext", gopacket.DecodeFunc(decodeAlarmNotificationExtended))
165 LayerTypeAttributeValueChangeExtended = mkLayer(me.AttributeValueChange|me.ExtendedOffset, "AttributeValueChange-Ext", gopacket.DecodeFunc(decodeAttributeValueChangeExtended))
166 LayerTypeTestResultExtended = mkLayer(me.TestResult|me.ExtendedOffset, "TestResult-Ext", gopacket.DecodeFunc(decodeTestResultExtended))
167
Chip Boling6e27b352020-02-14 09:10:01 -0600168 // Map message_type and action to layer
169 nextLayerMapping = make(map[MessageType]gopacket.LayerType)
170
171 nextLayerMapping[CreateRequestType] = LayerTypeCreateRequest
172 nextLayerMapping[DeleteRequestType] = LayerTypeDeleteRequest
173 nextLayerMapping[SetRequestType] = LayerTypeSetRequest
174 nextLayerMapping[GetRequestType] = LayerTypeGetRequest
175 nextLayerMapping[GetAllAlarmsRequestType] = LayerTypeGetAllAlarmsRequest
176 nextLayerMapping[GetAllAlarmsNextRequestType] = LayerTypeGetAllAlarmsNextRequest
177 nextLayerMapping[MibUploadRequestType] = LayerTypeMibUploadRequest
178 nextLayerMapping[MibUploadNextRequestType] = LayerTypeMibUploadNextRequest
179 nextLayerMapping[MibResetRequestType] = LayerTypeMibResetRequest
180 nextLayerMapping[TestRequestType] = LayerTypeTestRequest
181 nextLayerMapping[StartSoftwareDownloadRequestType] = LayerTypeStartSoftwareDownloadRequest
182 nextLayerMapping[DownloadSectionRequestType] = LayerTypeDownloadSectionRequest
Chip Boling8c8018e2021-02-22 15:56:00 -0600183 nextLayerMapping[DownloadSectionRequestWithResponseType] = LayerTypeDownloadSectionRequest
Chip Boling6e27b352020-02-14 09:10:01 -0600184 nextLayerMapping[EndSoftwareDownloadRequestType] = LayerTypeEndSoftwareDownloadRequest
185 nextLayerMapping[ActivateSoftwareRequestType] = LayerTypeActivateSoftwareRequest
186 nextLayerMapping[CommitSoftwareRequestType] = LayerTypeCommitSoftwareRequest
187 nextLayerMapping[SynchronizeTimeRequestType] = LayerTypeSynchronizeTimeRequest
188 nextLayerMapping[RebootRequestType] = LayerTypeRebootRequest
189 nextLayerMapping[GetNextRequestType] = LayerTypeGetNextRequest
190 nextLayerMapping[GetCurrentDataRequestType] = LayerTypeGetCurrentDataRequest
191 nextLayerMapping[SetTableRequestType] = LayerTypeSetTableRequest
192
193 nextLayerMapping[CreateResponseType] = LayerTypeCreateResponse
194 nextLayerMapping[DeleteResponseType] = LayerTypeDeleteResponse
195 nextLayerMapping[SetResponseType] = LayerTypeSetResponse
196 nextLayerMapping[GetResponseType] = LayerTypeGetResponse
197 nextLayerMapping[GetAllAlarmsResponseType] = LayerTypeGetAllAlarmsResponse
198 nextLayerMapping[GetAllAlarmsNextResponseType] = LayerTypeGetAllAlarmsNextResponse
199 nextLayerMapping[MibUploadResponseType] = LayerTypeMibUploadResponse
200 nextLayerMapping[MibUploadNextResponseType] = LayerTypeMibUploadNextResponse
201 nextLayerMapping[MibResetResponseType] = LayerTypeMibResetResponse
202 nextLayerMapping[TestResponseType] = LayerTypeTestResponse
203 nextLayerMapping[StartSoftwareDownloadResponseType] = LayerTypeStartSoftwareDownloadResponse
204 nextLayerMapping[DownloadSectionResponseType] = LayerTypeDownloadSectionResponse
205 nextLayerMapping[EndSoftwareDownloadResponseType] = LayerTypeEndSoftwareDownloadResponse
206 nextLayerMapping[ActivateSoftwareResponseType] = LayerTypeActivateSoftwareResponse
207 nextLayerMapping[CommitSoftwareResponseType] = LayerTypeCommitSoftwareResponse
208 nextLayerMapping[SynchronizeTimeResponseType] = LayerTypeSynchronizeTimeResponse
209 nextLayerMapping[RebootResponseType] = LayerTypeRebootResponse
210 nextLayerMapping[GetNextResponseType] = LayerTypeGetNextResponse
211 nextLayerMapping[GetCurrentDataResponseType] = LayerTypeGetCurrentDataResponse
212 nextLayerMapping[SetTableResponseType] = LayerTypeSetTableResponse
213
214 nextLayerMapping[AttributeValueChangeType] = LayerTypeAttributeValueChange
215 nextLayerMapping[AlarmNotificationType] = LayerTypeAlarmNotification
216 nextLayerMapping[TestResultType] = LayerTypeTestResult
Chip Boling157c9b92021-04-21 09:58:36 -0500217
218 // Extended message support
219 nextLayerMapping[GetRequestType+ExtendedTypeDecodeOffset] = LayerTypeGetRequestExtended
220 nextLayerMapping[GetResponseType+ExtendedTypeDecodeOffset] = LayerTypeGetResponseExtended
Chip Boling00a30d22021-05-04 13:31:52 -0500221
222 // For Download section, AR=0 if not response expected, AR=1 if response expected (last section of a window)
223 nextLayerMapping[DownloadSectionRequestType+ExtendedTypeDecodeOffset] = LayerTypeDownloadSectionRequestExtended
224 nextLayerMapping[DownloadSectionRequestWithResponseType+ExtendedTypeDecodeOffset] = LayerTypeDownloadSectionLastRequestExtended
225 nextLayerMapping[DownloadSectionResponseType+ExtendedTypeDecodeOffset] = LayerTypeDownloadSectionResponseExtended
Chip Bolingc15f61d2021-05-13 09:07:17 -0500226
227 nextLayerMapping[AlarmNotificationType+ExtendedTypeDecodeOffset] = LayerTypeAlarmNotificationExtended
228 nextLayerMapping[AttributeValueChangeType+ExtendedTypeDecodeOffset] = LayerTypeAttributeValueChangeExtended
229 nextLayerMapping[TestResultType+ExtendedTypeDecodeOffset] = LayerTypeTestResultExtended
Chip Boling6e27b352020-02-14 09:10:01 -0600230}
231
Chip Boling157c9b92021-04-21 09:58:36 -0500232func MsgTypeToNextLayer(mt MessageType, isExtended bool) (gopacket.LayerType, error) {
233 if isExtended {
234 mt |= ExtendedTypeDecodeOffset
235 }
Chip Boling6e27b352020-02-14 09:10:01 -0600236 nextLayer, ok := nextLayerMapping[mt]
237 if ok {
238 return nextLayer, nil
239 }
Chip Boling157c9b92021-04-21 09:58:36 -0500240 return gopacket.LayerTypeZero, errors.New("unknown/unsupported message type")
Chip Boling6e27b352020-02-14 09:10:01 -0600241}