blob: d22908dcd9c9178af79109abf482d8ba46e3c60f [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package omci
18
19import (
Matteo Scandolocfedba42022-02-14 16:08:54 -080020 "encoding/binary"
Matteo Scandolof9d43412021-01-12 11:11:34 -080021 "errors"
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080022 "fmt"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070023 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020024 "github.com/opencord/omci-lib-go/v2"
25 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070026 log "github.com/sirupsen/logrus"
27)
28
29var omciLogger = log.WithFields(log.Fields{
30 "module": "OMCI",
31})
32
Matteo Scandolof9d43412021-01-12 11:11:34 -080033// 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
34// we should probably move it in "omci-lib-go"
Matteo Scandolo992a23e2021-02-04 15:35:04 -080035func Serialize(msgType omci.MessageType, request gopacket.SerializableLayer, tid uint16) ([]byte, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070036 omciLayer := &omci.OMCI{
37 TransactionID: tid,
38 MessageType: msgType,
39 }
40 var options gopacket.SerializeOptions
41 options.FixLengths = true
42
43 buffer := gopacket.NewSerializeBuffer()
44 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
45 if err != nil {
46 return nil, err
47 }
48 return buffer.Bytes(), nil
49}
50
Matteo Scandolocfedba42022-02-14 16:08:54 -080051func SetTxIdInEncodedPacket(packet []byte, txId uint16) []byte {
52 valid := packet[2:]
53
54 b := make([]byte, 2)
55 binary.BigEndian.PutUint16(b, txId)
56
57 return append(b, valid...)
58}
59
Matteo Scandolo40e067f2019-10-16 16:59:41 -070060func CreateMibResetRequest(tid uint16) ([]byte, error) {
61
62 request := &omci.MibResetRequest{
63 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -080064 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -070065 },
66 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080067 pkt, err := Serialize(omci.MibResetRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070068 if err != nil {
69 omciLogger.WithFields(log.Fields{
70 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080071 }).Fatalf("Cannot Serialize MibResetRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070072 return nil, err
73 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080074 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070075}
76
Matteo Scandolof9d43412021-01-12 11:11:34 -080077func CreateMibResetResponse(tid uint16) ([]byte, error) {
78
Matteo Scandolof9d43412021-01-12 11:11:34 -080079 request := &omci.MibResetResponse{
80 MeBasePacket: omci.MeBasePacket{
81 EntityClass: me.OnuDataClassID,
82 },
83 Result: me.Success,
84 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080085 pkt, err := Serialize(omci.MibResetResponseType, request, tid)
Matteo Scandolof9d43412021-01-12 11:11:34 -080086 if err != nil {
87 omciLogger.WithFields(log.Fields{
88 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080089 }).Error("Cannot Serialize MibResetResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -080090 return nil, err
91 }
92 return pkt, nil
93}
94
Matteo Scandolo40e067f2019-10-16 16:59:41 -070095func CreateMibUploadRequest(tid uint16) ([]byte, error) {
96 request := &omci.MibUploadRequest{
97 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -080098 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -070099 // Default Instance ID is 0
100 },
101 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800102 pkt, err := Serialize(omci.MibUploadRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700103 if err != nil {
104 omciLogger.WithFields(log.Fields{
105 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800106 }).Fatalf("Cannot Serialize MibUploadRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700107 return nil, err
108 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800109 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700110}
111
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700112func CreateMibUploadResponse(tid uint16, numberOfCommands uint16) ([]byte, error) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800113 request := &omci.MibUploadResponse{
114 MeBasePacket: omci.MeBasePacket{
115 EntityClass: me.OnuDataClassID,
116 },
117 NumberOfCommands: numberOfCommands,
118 }
Matteo Scandolocfedba42022-02-14 16:08:54 -0800119
120 omciLogger.WithFields(log.Fields{"NumberOfCommands": numberOfCommands}).Debug("mib-upload-response")
121
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800122 pkt, err := Serialize(omci.MibUploadResponseType, request, tid)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800123 if err != nil {
124 omciLogger.WithFields(log.Fields{
125 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800126 }).Error("Cannot Serialize MibUploadResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800127 return nil, err
128 }
129 return pkt, nil
130}
131
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700132func CreateMibUploadNextRequest(tid uint16, seqNumber uint16) ([]byte, error) {
133
134 request := &omci.MibUploadNextRequest{
135 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800136 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700137 // Default Instance ID is 0
138 },
139 CommandSequenceNumber: seqNumber,
140 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800141 pkt, err := Serialize(omci.MibUploadNextRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700142
143 if err != nil {
144 omciLogger.WithFields(log.Fields{
145 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800146 }).Fatalf("Cannot Serialize MibUploadNextRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700147 return nil, err
148 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800149 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700150}
151
Matteo Scandolof9d43412021-01-12 11:11:34 -0800152func ParseMibUploadNextRequest(omciPkt gopacket.Packet) (*omci.MibUploadNextRequest, error) {
153 msgLayer := omciPkt.Layer(omci.LayerTypeMibUploadNextRequest)
154 if msgLayer == nil {
155 err := "omci Msg layer could not be detected for LayerTypeMibUploadNextRequest"
156 omciLogger.Error(err)
157 return nil, errors.New(err)
Scott Bakerb90c4312020-03-12 21:33:25 -0700158 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800159 msgObj, msgOk := msgLayer.(*omci.MibUploadNextRequest)
160 if !msgOk {
161 err := "omci Msg layer could not be assigned for MibUploadNextRequest"
162 omciLogger.Error(err)
163 return nil, errors.New(err)
164 }
165 return msgObj, nil
Scott Bakerb90c4312020-03-12 21:33:25 -0700166}
167
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700168func CreateMibUploadNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, mds uint8, mibDb *MibDb) ([]byte, error) {
Scott Bakerb90c4312020-03-12 21:33:25 -0700169
Matteo Scandolof9d43412021-01-12 11:11:34 -0800170 msgObj, err := ParseMibUploadNextRequest(omciPkt)
Scott Bakerb90c4312020-03-12 21:33:25 -0700171 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800172 err := "omci Msg layer could not be assigned for LayerTypeGetRequest"
173 omciLogger.Error(err)
174 return nil, errors.New(err)
Scott Bakerb90c4312020-03-12 21:33:25 -0700175 }
176
Matteo Scandolof9d43412021-01-12 11:11:34 -0800177 omciLogger.WithFields(log.Fields{
178 "EntityClass": msgObj.EntityClass,
179 "EntityInstance": msgObj.EntityInstance,
180 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
181 }).Trace("received-omci-mibUploadNext-request")
Scott Bakerb90c4312020-03-12 21:33:25 -0700182
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700183 if msgObj.CommandSequenceNumber > mibDb.NumberOfCommands {
184 omciLogger.WithFields(log.Fields{
185 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
186 "MibDbNumberOfCommands": mibDb.NumberOfCommands,
187 }).Error("mibdb-does-not-contain-item")
188 return nil, fmt.Errorf("mibdb-does-not-contain-item")
189 }
190 currentEntry := mibDb.items[int(msgObj.CommandSequenceNumber)]
Matteo Scandolocfedba42022-02-14 16:08:54 -0800191
192 // if packet is set then we don't need to serialize the packet, it's already done
193 if currentEntry.packet != nil {
194 omciLogger.WithFields(log.Fields{
195 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
196 "MibDbNumberOfCommands": mibDb.NumberOfCommands,
197 "packet": currentEntry.packet,
198 "request-txid": omciMsg.TransactionID,
199 }).Info("sending-custom-packet")
200
201 // NOTE we need to replace the first two bytes of the packet with the correct transactionId
202 pkt := SetTxIdInEncodedPacket(currentEntry.packet, omciMsg.TransactionID)
203
204 return pkt, nil
205 }
206
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700207 reportedMe, meErr := me.LoadManagedEntityDefinition(currentEntry.classId, me.ParamData{
208 EntityID: currentEntry.entityId.ToUint16(),
209 Attributes: currentEntry.params,
210 })
Matteo Scandolo9a1842d2021-02-08 16:00:17 -0800211
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700212 if meErr.GetError() != nil {
213 omciLogger.Errorf("Error while generating %s: %v", currentEntry.classId.String(), meErr.Error())
214 }
Scott Bakerb90c4312020-03-12 21:33:25 -0700215
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700216 if reportedMe.GetClassID() == me.OnuDataClassID {
217 // if this is ONU-Data we need to replace the MDS
218 if err := reportedMe.SetAttribute("MibDataSync", mds); err.GetError() != nil {
219 omciLogger.Errorf("Error while setting mds in %s: %v", currentEntry.classId.String(), meErr.Error())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800220 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700221 }
222
Matteo Scandolof9d43412021-01-12 11:11:34 -0800223 response := &omci.MibUploadNextResponse{
224 MeBasePacket: omci.MeBasePacket{
225 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700226 },
Matteo Scandolof9d43412021-01-12 11:11:34 -0800227 ReportedME: *reportedMe,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700228 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700229
Matteo Scandolof9d43412021-01-12 11:11:34 -0800230 omciLogger.WithFields(log.Fields{
231 "reportedMe": reportedMe,
232 }).Trace("created-omci-mibUploadNext-response")
233
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800234 pkt, err := Serialize(omci.MibUploadNextResponseType, response, omciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800235
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700236 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800237 omciLogger.WithFields(log.Fields{
238 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800239 }).Fatalf("Cannot Serialize MibUploadNextRequest")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800240 return nil, err
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700241 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700242
Matteo Scandolof9d43412021-01-12 11:11:34 -0800243 return pkt, nil
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700244}