Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package omci |
| 18 | |
| 19 | import ( |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 20 | "encoding/binary" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 21 | "errors" |
Matteo Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame] | 22 | "fmt" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 23 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 24 | "github.com/opencord/omci-lib-go/v2" |
| 25 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 26 | log "github.com/sirupsen/logrus" |
| 27 | ) |
| 28 | |
| 29 | var omciLogger = log.WithFields(log.Fields{ |
| 30 | "module": "OMCI", |
| 31 | }) |
| 32 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 33 | // 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 Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 35 | func Serialize(msgType omci.MessageType, request gopacket.SerializableLayer, tid uint16) ([]byte, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 36 | 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 51 | func 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 Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 60 | func CreateMibResetRequest(tid uint16) ([]byte, error) { |
| 61 | |
| 62 | request := &omci.MibResetRequest{ |
| 63 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 64 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 65 | }, |
| 66 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 67 | pkt, err := Serialize(omci.MibResetRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 68 | if err != nil { |
| 69 | omciLogger.WithFields(log.Fields{ |
| 70 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 71 | }).Fatalf("Cannot Serialize MibResetRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 72 | return nil, err |
| 73 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 74 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 77 | func CreateMibResetResponse(tid uint16) ([]byte, error) { |
| 78 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 79 | request := &omci.MibResetResponse{ |
| 80 | MeBasePacket: omci.MeBasePacket{ |
| 81 | EntityClass: me.OnuDataClassID, |
| 82 | }, |
| 83 | Result: me.Success, |
| 84 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 85 | pkt, err := Serialize(omci.MibResetResponseType, request, tid) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 86 | if err != nil { |
| 87 | omciLogger.WithFields(log.Fields{ |
| 88 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 89 | }).Error("Cannot Serialize MibResetResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 90 | return nil, err |
| 91 | } |
| 92 | return pkt, nil |
| 93 | } |
| 94 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 95 | func CreateMibUploadRequest(tid uint16) ([]byte, error) { |
| 96 | request := &omci.MibUploadRequest{ |
| 97 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 98 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 99 | // Default Instance ID is 0 |
| 100 | }, |
| 101 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 102 | pkt, err := Serialize(omci.MibUploadRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 103 | if err != nil { |
| 104 | omciLogger.WithFields(log.Fields{ |
| 105 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 106 | }).Fatalf("Cannot Serialize MibUploadRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 107 | return nil, err |
| 108 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 109 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 112 | func CreateMibUploadResponse(tid uint16, numberOfCommands uint16) ([]byte, error) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 113 | request := &omci.MibUploadResponse{ |
| 114 | MeBasePacket: omci.MeBasePacket{ |
| 115 | EntityClass: me.OnuDataClassID, |
| 116 | }, |
| 117 | NumberOfCommands: numberOfCommands, |
| 118 | } |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 119 | |
| 120 | omciLogger.WithFields(log.Fields{"NumberOfCommands": numberOfCommands}).Debug("mib-upload-response") |
| 121 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 122 | pkt, err := Serialize(omci.MibUploadResponseType, request, tid) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 123 | if err != nil { |
| 124 | omciLogger.WithFields(log.Fields{ |
| 125 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 126 | }).Error("Cannot Serialize MibUploadResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 127 | return nil, err |
| 128 | } |
| 129 | return pkt, nil |
| 130 | } |
| 131 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 132 | func CreateMibUploadNextRequest(tid uint16, seqNumber uint16) ([]byte, error) { |
| 133 | |
| 134 | request := &omci.MibUploadNextRequest{ |
| 135 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 136 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 137 | // Default Instance ID is 0 |
| 138 | }, |
| 139 | CommandSequenceNumber: seqNumber, |
| 140 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 141 | pkt, err := Serialize(omci.MibUploadNextRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 142 | |
| 143 | if err != nil { |
| 144 | omciLogger.WithFields(log.Fields{ |
| 145 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 146 | }).Fatalf("Cannot Serialize MibUploadNextRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 147 | return nil, err |
| 148 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 149 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 152 | func 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 Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 158 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 159 | 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 Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 168 | func CreateMibUploadNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, mds uint8, mibDb *MibDb) ([]byte, error) { |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 169 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 170 | msgObj, err := ParseMibUploadNextRequest(omciPkt) |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 171 | if err != nil { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 172 | err := "omci Msg layer could not be assigned for LayerTypeGetRequest" |
| 173 | omciLogger.Error(err) |
| 174 | return nil, errors.New(err) |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 177 | omciLogger.WithFields(log.Fields{ |
| 178 | "EntityClass": msgObj.EntityClass, |
| 179 | "EntityInstance": msgObj.EntityInstance, |
| 180 | "CommandSequenceNumber": msgObj.CommandSequenceNumber, |
| 181 | }).Trace("received-omci-mibUploadNext-request") |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 182 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 183 | 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 191 | |
| 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 207 | reportedMe, meErr := me.LoadManagedEntityDefinition(currentEntry.classId, me.ParamData{ |
| 208 | EntityID: currentEntry.entityId.ToUint16(), |
| 209 | Attributes: currentEntry.params, |
| 210 | }) |
Matteo Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame] | 211 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 212 | if meErr.GetError() != nil { |
| 213 | omciLogger.Errorf("Error while generating %s: %v", currentEntry.classId.String(), meErr.Error()) |
| 214 | } |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 215 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 216 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 220 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 223 | response := &omci.MibUploadNextResponse{ |
| 224 | MeBasePacket: omci.MeBasePacket{ |
| 225 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 226 | }, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 227 | ReportedME: *reportedMe, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 228 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 229 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 230 | omciLogger.WithFields(log.Fields{ |
| 231 | "reportedMe": reportedMe, |
| 232 | }).Trace("created-omci-mibUploadNext-response") |
| 233 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 234 | pkt, err := Serialize(omci.MibUploadNextResponseType, response, omciMsg.TransactionID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 235 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 236 | if err != nil { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 237 | omciLogger.WithFields(log.Fields{ |
| 238 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 239 | }).Fatalf("Cannot Serialize MibUploadNextRequest") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 240 | return nil, err |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 241 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 242 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 243 | return pkt, nil |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 244 | } |