blob: 8b7afcb14123cfdfba5c54bea6bc01a82afce0ec [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 Scandolof9d43412021-01-12 11:11:34 -080020 "errors"
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080021 "fmt"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070022 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020023 "github.com/opencord/omci-lib-go/v2"
24 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070025 log "github.com/sirupsen/logrus"
26)
27
28var omciLogger = log.WithFields(log.Fields{
29 "module": "OMCI",
30})
31
Matteo Scandolof9d43412021-01-12 11:11:34 -080032// 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
33// we should probably move it in "omci-lib-go"
Matteo Scandolo992a23e2021-02-04 15:35:04 -080034func Serialize(msgType omci.MessageType, request gopacket.SerializableLayer, tid uint16) ([]byte, error) {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070035 omciLayer := &omci.OMCI{
36 TransactionID: tid,
37 MessageType: msgType,
38 }
39 var options gopacket.SerializeOptions
40 options.FixLengths = true
41
42 buffer := gopacket.NewSerializeBuffer()
43 err := gopacket.SerializeLayers(buffer, options, omciLayer, request)
44 if err != nil {
45 return nil, err
46 }
47 return buffer.Bytes(), nil
48}
49
Matteo Scandolo40e067f2019-10-16 16:59:41 -070050func CreateMibResetRequest(tid uint16) ([]byte, error) {
51
52 request := &omci.MibResetRequest{
53 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -080054 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -070055 },
56 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080057 pkt, err := Serialize(omci.MibResetRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070058 if err != nil {
59 omciLogger.WithFields(log.Fields{
60 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080061 }).Fatalf("Cannot Serialize MibResetRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070062 return nil, err
63 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080064 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070065}
66
Matteo Scandolof9d43412021-01-12 11:11:34 -080067func CreateMibResetResponse(tid uint16) ([]byte, error) {
68
Matteo Scandolof9d43412021-01-12 11:11:34 -080069 request := &omci.MibResetResponse{
70 MeBasePacket: omci.MeBasePacket{
71 EntityClass: me.OnuDataClassID,
72 },
73 Result: me.Success,
74 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080075 pkt, err := Serialize(omci.MibResetResponseType, request, tid)
Matteo Scandolof9d43412021-01-12 11:11:34 -080076 if err != nil {
77 omciLogger.WithFields(log.Fields{
78 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080079 }).Error("Cannot Serialize MibResetResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -080080 return nil, err
81 }
82 return pkt, nil
83}
84
Matteo Scandolo40e067f2019-10-16 16:59:41 -070085func CreateMibUploadRequest(tid uint16) ([]byte, error) {
86 request := &omci.MibUploadRequest{
87 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -080088 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -070089 // Default Instance ID is 0
90 },
91 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080092 pkt, err := Serialize(omci.MibUploadRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -070093 if err != nil {
94 omciLogger.WithFields(log.Fields{
95 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080096 }).Fatalf("Cannot Serialize MibUploadRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -070097 return nil, err
98 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -080099 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700100}
101
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700102func CreateMibUploadResponse(tid uint16, numberOfCommands uint16) ([]byte, error) {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800103 request := &omci.MibUploadResponse{
104 MeBasePacket: omci.MeBasePacket{
105 EntityClass: me.OnuDataClassID,
106 },
107 NumberOfCommands: numberOfCommands,
108 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800109 pkt, err := Serialize(omci.MibUploadResponseType, request, tid)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800110 if err != nil {
111 omciLogger.WithFields(log.Fields{
112 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800113 }).Error("Cannot Serialize MibUploadResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800114 return nil, err
115 }
116 return pkt, nil
117}
118
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700119func CreateMibUploadNextRequest(tid uint16, seqNumber uint16) ([]byte, error) {
120
121 request := &omci.MibUploadNextRequest{
122 MeBasePacket: omci.MeBasePacket{
Matteo Scandolof9d43412021-01-12 11:11:34 -0800123 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700124 // Default Instance ID is 0
125 },
126 CommandSequenceNumber: seqNumber,
127 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800128 pkt, err := Serialize(omci.MibUploadNextRequestType, request, tid)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700129
130 if err != nil {
131 omciLogger.WithFields(log.Fields{
132 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800133 }).Fatalf("Cannot Serialize MibUploadNextRequest")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700134 return nil, err
135 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800136 return HexEncode(pkt)
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700137}
138
Matteo Scandolof9d43412021-01-12 11:11:34 -0800139func ParseMibUploadNextRequest(omciPkt gopacket.Packet) (*omci.MibUploadNextRequest, error) {
140 msgLayer := omciPkt.Layer(omci.LayerTypeMibUploadNextRequest)
141 if msgLayer == nil {
142 err := "omci Msg layer could not be detected for LayerTypeMibUploadNextRequest"
143 omciLogger.Error(err)
144 return nil, errors.New(err)
Scott Bakerb90c4312020-03-12 21:33:25 -0700145 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800146 msgObj, msgOk := msgLayer.(*omci.MibUploadNextRequest)
147 if !msgOk {
148 err := "omci Msg layer could not be assigned for MibUploadNextRequest"
149 omciLogger.Error(err)
150 return nil, errors.New(err)
151 }
152 return msgObj, nil
Scott Bakerb90c4312020-03-12 21:33:25 -0700153}
154
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700155func CreateMibUploadNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, mds uint8, mibDb *MibDb) ([]byte, error) {
Scott Bakerb90c4312020-03-12 21:33:25 -0700156
Matteo Scandolof9d43412021-01-12 11:11:34 -0800157 msgObj, err := ParseMibUploadNextRequest(omciPkt)
Scott Bakerb90c4312020-03-12 21:33:25 -0700158 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800159 err := "omci Msg layer could not be assigned for LayerTypeGetRequest"
160 omciLogger.Error(err)
161 return nil, errors.New(err)
Scott Bakerb90c4312020-03-12 21:33:25 -0700162 }
163
Matteo Scandolof9d43412021-01-12 11:11:34 -0800164 omciLogger.WithFields(log.Fields{
165 "EntityClass": msgObj.EntityClass,
166 "EntityInstance": msgObj.EntityInstance,
167 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
168 }).Trace("received-omci-mibUploadNext-request")
Scott Bakerb90c4312020-03-12 21:33:25 -0700169
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700170 if msgObj.CommandSequenceNumber > mibDb.NumberOfCommands {
171 omciLogger.WithFields(log.Fields{
172 "CommandSequenceNumber": msgObj.CommandSequenceNumber,
173 "MibDbNumberOfCommands": mibDb.NumberOfCommands,
174 }).Error("mibdb-does-not-contain-item")
175 return nil, fmt.Errorf("mibdb-does-not-contain-item")
176 }
177 currentEntry := mibDb.items[int(msgObj.CommandSequenceNumber)]
178 reportedMe, meErr := me.LoadManagedEntityDefinition(currentEntry.classId, me.ParamData{
179 EntityID: currentEntry.entityId.ToUint16(),
180 Attributes: currentEntry.params,
181 })
Matteo Scandolo9a1842d2021-02-08 16:00:17 -0800182
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700183 if meErr.GetError() != nil {
184 omciLogger.Errorf("Error while generating %s: %v", currentEntry.classId.String(), meErr.Error())
185 }
Scott Bakerb90c4312020-03-12 21:33:25 -0700186
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700187 if reportedMe.GetClassID() == me.OnuDataClassID {
188 // if this is ONU-Data we need to replace the MDS
189 if err := reportedMe.SetAttribute("MibDataSync", mds); err.GetError() != nil {
190 omciLogger.Errorf("Error while setting mds in %s: %v", currentEntry.classId.String(), meErr.Error())
Matteo Scandolof9d43412021-01-12 11:11:34 -0800191 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700192 }
193
Matteo Scandolof9d43412021-01-12 11:11:34 -0800194 response := &omci.MibUploadNextResponse{
195 MeBasePacket: omci.MeBasePacket{
196 EntityClass: me.OnuDataClassID,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700197 },
Matteo Scandolof9d43412021-01-12 11:11:34 -0800198 ReportedME: *reportedMe,
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700199 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700200
Matteo Scandolof9d43412021-01-12 11:11:34 -0800201 omciLogger.WithFields(log.Fields{
202 "reportedMe": reportedMe,
203 }).Trace("created-omci-mibUploadNext-response")
204
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800205 pkt, err := Serialize(omci.MibUploadNextResponseType, response, omciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800206
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700207 if err != nil {
Matteo Scandolof9d43412021-01-12 11:11:34 -0800208 omciLogger.WithFields(log.Fields{
209 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800210 }).Fatalf("Cannot Serialize MibUploadNextRequest")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800211 return nil, err
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700212 }
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700213
Matteo Scandolof9d43412021-01-12 11:11:34 -0800214 return pkt, nil
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700215}