blob: 80ebd43db95c852389fc8d15b5b9a771ea821656 [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo40e067f2019-10-16 16:59:41 -07003
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
17package omci
18
19import (
Matteo Scandolocfedba42022-02-14 16:08:54 -080020 "encoding/binary"
Holger Hildebrandt236e3742022-05-04 14:07:27 +000021 "encoding/hex"
Matteo Scandolof9d43412021-01-12 11:11:34 -080022 "errors"
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080023 "fmt"
Holger Hildebrandt236e3742022-05-04 14:07:27 +000024 "strconv"
25
Matteo Scandolo40e067f2019-10-16 16:59:41 -070026 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020027 "github.com/opencord/omci-lib-go/v2"
28 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070029 log "github.com/sirupsen/logrus"
30)
31
32var omciLogger = log.WithFields(log.Fields{
33 "module": "OMCI",
34})
35
Matteo Scandolof9d43412021-01-12 11:11:34 -080036// NOTE this is basically the same as https://github.com/opencord/voltha-openonu-adapter-go/blob/master/internal/pkg/onuadaptercore/omci_cc.go#L545-L564
37// we should probably move it in "omci-lib-go"
Matteo Scandolo992a23e2021-02-04 15:35:04 -080038func Serialize(msgType omci.MessageType, request gopacket.SerializableLayer, tid uint16) ([]byte, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070039 omciLayer := &omci.OMCI{
40 TransactionID: tid,
41 MessageType: msgType,
42 }
43 var options gopacket.SerializeOptions
44 options.FixLengths = true
45
46 buffer := gopacket.NewSerializeBuffer()
47 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
48 if err != nil {
49 return nil, err
50 }
51 return buffer.Bytes(), nil
52}
53
Matteo Scandolocfedba42022-02-14 16:08:54 -080054func SetTxIdInEncodedPacket(packet []byte, txId uint16) []byte {
55 valid := packet[2:]
56
57 b := make([]byte, 2)
58 binary.BigEndian.PutUint16(b, txId)
59
60 return append(b, valid...)
61}
62
Matteo Scandolo40e067f2019-10-16 16:59:41 -070063func CreateMibResetRequest(tid uint16) ([]byte, error) {
64
65 request := &omci.MibResetRequest{
66 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -080067 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -070068 },
69 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080070 pkt, err := Serialize(omci.MibResetRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070071 if err != nil {
72 omciLogger.WithFields(log.Fields{
73 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080074 }).Fatalf("Cannot Serialize MibResetRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070075 return nil, err
76 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080077 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070078}
79
Matteo Scandolof9d43412021-01-12 11:11:34 -080080func CreateMibResetResponse(tid uint16) ([]byte, error) {
81
Matteo Scandolof9d43412021-01-12 11:11:34 -080082 request := &omci.MibResetResponse{
83 MeBasePacket: omci.MeBasePacket{
84 EntityClass: me.OnuDataClassID,
85 },
86 Result: me.Success,
87 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080088 pkt, err := Serialize(omci.MibResetResponseType, request, tid)
Matteo Scandolof9d43412021-01-12 11:11:34 -080089 if err != nil {
90 omciLogger.WithFields(log.Fields{
91 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080092 }).Error("Cannot Serialize MibResetResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -080093 return nil, err
94 }
95 return pkt, nil
96}
97
Matteo Scandolo40e067f2019-10-16 16:59:41 -070098func CreateMibUploadRequest(tid uint16) ([]byte, error) {
99 request := &omci.MibUploadRequest{
100 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800101 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700102 // Default Instance ID is 0
103 },
104 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800105 pkt, err := Serialize(omci.MibUploadRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700106 if err != nil {
107 omciLogger.WithFields(log.Fields{
108 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800109 }).Fatalf("Cannot Serialize MibUploadRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700110 return nil, err
111 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800112 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700113}
114
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000115func CreateMibUploadResponse(omciMsg *omci.OMCI, mibDb *MibDb) ([]byte, error) {
116
117 isExtended := false
118 numberOfCommands := mibDb.NumberOfBaselineCommands
119 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
120 isExtended = true
121 numberOfCommands = mibDb.NumberOfExtendedCommands
122 }
123 response := &omci.MibUploadResponse{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800124 MeBasePacket: omci.MeBasePacket{
125 EntityClass: me.OnuDataClassID,
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000126 Extended: isExtended,
Matteo Scandolof9d43412021-01-12 11:11:34 -0800127 },
128 NumberOfCommands: numberOfCommands,
129 }
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000130 omciLogger.WithFields(log.Fields{
131 "NumberOfCommands": numberOfCommands, "isExtended": isExtended}).Debug("mib-upload-response")
Matteo Scandolocfedba42022-02-14 16:08:54 -0800132
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000133 omciLayer := &omci.OMCI{
134 TransactionID: omciMsg.TransactionID,
135 MessageType: omci.MibUploadResponseType,
136 DeviceIdentifier: omciMsg.DeviceIdentifier,
137 }
138 var options gopacket.SerializeOptions
139 options.FixLengths = true
Matteo Scandolocfedba42022-02-14 16:08:54 -0800140
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000141 buffer := gopacket.NewSerializeBuffer()
142 err := gopacket.SerializeLayers(buffer, options, omciLayer, response)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800143 if err != nil {
144 omciLogger.WithFields(log.Fields{
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000145 "Err": err,
146 "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
147 }).Error("cannot-Serialize-MibUploadResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800148 return nil, err
149 }
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000150 pkt := buffer.Bytes()
151
152 log.WithFields(log.Fields{
153 "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
154 "pkt": hex.EncodeToString(pkt),
155 }).Trace("omci-mib-upload-response")
156
Matteo Scandolof9d43412021-01-12 11:11:34 -0800157 return pkt, nil
158}
159
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700160func CreateMibUploadNextRequest(tid uint16, seqNumber uint16) ([]byte, error) {
161
162 request := &omci.MibUploadNextRequest{
163 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800164 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700165 // Default Instance ID is 0
166 },
167 CommandSequenceNumber: seqNumber,
168 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800169 pkt, err := Serialize(omci.MibUploadNextRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700170
171 if err != nil {
172 omciLogger.WithFields(log.Fields{
173 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800174 }).Fatalf("Cannot Serialize MibUploadNextRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700175 return nil, err
176 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800177 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700178}
179
Matteo Scandolof9d43412021-01-12 11:11:34 -0800180func ParseMibUploadNextRequest(omciPkt gopacket.Packet) (*omci.MibUploadNextRequest, error) {
181 msgLayer := omciPkt.Layer(omci.LayerTypeMibUploadNextRequest)
182 if msgLayer == nil {
183 err := "omci Msg layer could not be detected for LayerTypeMibUploadNextRequest"
184 omciLogger.Error(err)
185 return nil, errors.New(err)
Scott Bakerb90c4312020-03-12 21:33:25 -0700186 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800187 msgObj, msgOk := msgLayer.(*omci.MibUploadNextRequest)
188 if !msgOk {
189 err := "omci Msg layer could not be assigned for MibUploadNextRequest"
190 omciLogger.Error(err)
191 return nil, errors.New(err)
192 }
193 return msgObj, nil
Scott Bakerb90c4312020-03-12 21:33:25 -0700194}
195
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000196func CreateMibUploadNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, mibDb *MibDb) ([]byte, error) {
Scott Bakerb90c4312020-03-12 21:33:25 -0700197
Matteo Scandolof9d43412021-01-12 11:11:34 -0800198 msgObj, err := ParseMibUploadNextRequest(omciPkt)
Scott Bakerb90c4312020-03-12 21:33:25 -0700199 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800200 err := "omci Msg layer could not be assigned for LayerTypeGetRequest"
201 omciLogger.Error(err)
202 return nil, errors.New(err)
Scott Bakerb90c4312020-03-12 21:33:25 -0700203 }
204
Matteo Scandolof9d43412021-01-12 11:11:34 -0800205 omciLogger.WithFields(log.Fields{
206 "EntityClass": msgObj.EntityClass,
207 "EntityInstance": msgObj.EntityInstance,
208 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
209 }).Trace("received-omci-mibUploadNext-request")
Scott Bakerb90c4312020-03-12 21:33:25 -0700210
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000211 isExtended := false
212 numberOfCommands := mibDb.NumberOfBaselineCommands
213 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
214 isExtended = true
215 numberOfCommands = mibDb.NumberOfExtendedCommands
216 }
217 if msgObj.CommandSequenceNumber > numberOfCommands {
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700218 omciLogger.WithFields(log.Fields{
219 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000220 "MibDbNumberOfCommands": numberOfCommands,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700221 }).Error("mibdb-does-not-contain-item")
222 return nil, fmt.Errorf("mibdb-does-not-contain-item")
223 }
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000224 if isExtended {
225 pkt := SetTxIdInEncodedPacket(mibDb.extendedResponses[int(msgObj.CommandSequenceNumber)], omciMsg.TransactionID)
226 return pkt, nil
227 } else {
228 currentEntry := mibDb.baselineItems[int(msgObj.CommandSequenceNumber)]
Matteo Scandolocfedba42022-02-14 16:08:54 -0800229
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000230 // if packet is set then we don't need to serialize the packet, it's already done
231 if currentEntry.packet != nil {
232 omciLogger.WithFields(log.Fields{
233 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
234 "MibDbNumberOfCommands": numberOfCommands,
235 "packet": currentEntry.packet,
236 "request-txid": omciMsg.TransactionID,
237 }).Info("sending-custom-packet")
238
239 // NOTE we need to replace the first two bytes of the packet with the correct transactionId
240 pkt := SetTxIdInEncodedPacket(currentEntry.packet, omciMsg.TransactionID)
241
242 return pkt, nil
243 }
244 reportedMe, meErr := me.LoadManagedEntityDefinition(currentEntry.classId, me.ParamData{
245 EntityID: currentEntry.entityId.ToUint16(),
246 Attributes: currentEntry.params,
247 })
248
249 if meErr.GetError() != nil {
250 omciLogger.Errorf("Error while generating %s: %v", currentEntry.classId.String(), meErr.Error())
251 }
252 response := &omci.MibUploadNextResponse{
253 MeBasePacket: omci.MeBasePacket{
254 EntityClass: me.OnuDataClassID,
255 },
256 ReportedME: *reportedMe,
257 }
258
Matteo Scandolocfedba42022-02-14 16:08:54 -0800259 omciLogger.WithFields(log.Fields{
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000260 "reportedMe": reportedMe,
261 }).Trace("created-omci-mibUploadNext-response")
Matteo Scandolocfedba42022-02-14 16:08:54 -0800262
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000263 pkt, err := Serialize(omci.MibUploadNextResponseType, response, omciMsg.TransactionID)
264
265 if err != nil {
266 omciLogger.WithFields(log.Fields{
267 "Err": err,
268 }).Fatalf("Cannot Serialize MibUploadNextRequest")
269 return nil, err
270 }
Matteo Scandolocfedba42022-02-14 16:08:54 -0800271
272 return pkt, nil
273 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700274}