Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 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 | |
| 17 | package omci |
| 18 | |
| 19 | import ( |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 20 | "encoding/binary" |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 21 | "encoding/hex" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 22 | "errors" |
Matteo Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame] | 23 | "fmt" |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 24 | "strconv" |
| 25 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 26 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 27 | "github.com/opencord/omci-lib-go/v2" |
| 28 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 29 | log "github.com/sirupsen/logrus" |
| 30 | ) |
| 31 | |
| 32 | var omciLogger = log.WithFields(log.Fields{ |
| 33 | "module": "OMCI", |
| 34 | }) |
| 35 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 36 | // 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 Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 38 | func Serialize(msgType omci.MessageType, request gopacket.SerializableLayer, tid uint16) ([]byte, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 39 | 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 54 | func 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 Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 63 | func CreateMibResetRequest(tid uint16) ([]byte, error) { |
| 64 | |
| 65 | request := &omci.MibResetRequest{ |
| 66 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 67 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 68 | }, |
| 69 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 70 | pkt, err := Serialize(omci.MibResetRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 71 | if err != nil { |
| 72 | omciLogger.WithFields(log.Fields{ |
| 73 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 74 | }).Fatalf("Cannot Serialize MibResetRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 75 | return nil, err |
| 76 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 77 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 80 | func CreateMibResetResponse(tid uint16) ([]byte, error) { |
| 81 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 82 | request := &omci.MibResetResponse{ |
| 83 | MeBasePacket: omci.MeBasePacket{ |
| 84 | EntityClass: me.OnuDataClassID, |
| 85 | }, |
| 86 | Result: me.Success, |
| 87 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 88 | pkt, err := Serialize(omci.MibResetResponseType, request, tid) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 89 | if err != nil { |
| 90 | omciLogger.WithFields(log.Fields{ |
| 91 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 92 | }).Error("Cannot Serialize MibResetResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 93 | return nil, err |
| 94 | } |
| 95 | return pkt, nil |
| 96 | } |
| 97 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 98 | func CreateMibUploadRequest(tid uint16) ([]byte, error) { |
| 99 | request := &omci.MibUploadRequest{ |
| 100 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 101 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 102 | // Default Instance ID is 0 |
| 103 | }, |
| 104 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 105 | pkt, err := Serialize(omci.MibUploadRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 106 | if err != nil { |
| 107 | omciLogger.WithFields(log.Fields{ |
| 108 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 109 | }).Fatalf("Cannot Serialize MibUploadRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 110 | return nil, err |
| 111 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 112 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 115 | func 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 124 | MeBasePacket: omci.MeBasePacket{ |
| 125 | EntityClass: me.OnuDataClassID, |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 126 | Extended: isExtended, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 127 | }, |
| 128 | NumberOfCommands: numberOfCommands, |
| 129 | } |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 130 | omciLogger.WithFields(log.Fields{ |
| 131 | "NumberOfCommands": numberOfCommands, "isExtended": isExtended}).Debug("mib-upload-response") |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 132 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 133 | 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 140 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 141 | buffer := gopacket.NewSerializeBuffer() |
| 142 | err := gopacket.SerializeLayers(buffer, options, omciLayer, response) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 143 | if err != nil { |
| 144 | omciLogger.WithFields(log.Fields{ |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 145 | "Err": err, |
| 146 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 147 | }).Error("cannot-Serialize-MibUploadResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 148 | return nil, err |
| 149 | } |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 150 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 157 | return pkt, nil |
| 158 | } |
| 159 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 160 | func CreateMibUploadNextRequest(tid uint16, seqNumber uint16) ([]byte, error) { |
| 161 | |
| 162 | request := &omci.MibUploadNextRequest{ |
| 163 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 164 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 165 | // Default Instance ID is 0 |
| 166 | }, |
| 167 | CommandSequenceNumber: seqNumber, |
| 168 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 169 | pkt, err := Serialize(omci.MibUploadNextRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 170 | |
| 171 | if err != nil { |
| 172 | omciLogger.WithFields(log.Fields{ |
| 173 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 174 | }).Fatalf("Cannot Serialize MibUploadNextRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 175 | return nil, err |
| 176 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 177 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 180 | func 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 Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 186 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 187 | 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 Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 196 | func CreateMibUploadNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, mibDb *MibDb) ([]byte, error) { |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 197 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 198 | msgObj, err := ParseMibUploadNextRequest(omciPkt) |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 199 | if err != nil { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 200 | err := "omci Msg layer could not be assigned for LayerTypeGetRequest" |
| 201 | omciLogger.Error(err) |
| 202 | return nil, errors.New(err) |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 205 | omciLogger.WithFields(log.Fields{ |
| 206 | "EntityClass": msgObj.EntityClass, |
| 207 | "EntityInstance": msgObj.EntityInstance, |
| 208 | "CommandSequenceNumber": msgObj.CommandSequenceNumber, |
| 209 | }).Trace("received-omci-mibUploadNext-request") |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 210 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 211 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 218 | omciLogger.WithFields(log.Fields{ |
| 219 | "CommandSequenceNumber": msgObj.CommandSequenceNumber, |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 220 | "MibDbNumberOfCommands": numberOfCommands, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 221 | }).Error("mibdb-does-not-contain-item") |
| 222 | return nil, fmt.Errorf("mibdb-does-not-contain-item") |
| 223 | } |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 224 | 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 229 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 230 | // 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 259 | omciLogger.WithFields(log.Fields{ |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 260 | "reportedMe": reportedMe, |
| 261 | }).Trace("created-omci-mibUploadNext-response") |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 262 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 263 | 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 271 | |
| 272 | return pkt, nil |
| 273 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 274 | } |