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 | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 20 | "errors" |
Matteo Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame] | 21 | "fmt" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 22 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 23 | "github.com/opencord/omci-lib-go/v2" |
| 24 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 25 | log "github.com/sirupsen/logrus" |
| 26 | ) |
| 27 | |
| 28 | var omciLogger = log.WithFields(log.Fields{ |
| 29 | "module": "OMCI", |
| 30 | }) |
| 31 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 32 | // 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 Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 34 | func Serialize(msgType omci.MessageType, request gopacket.SerializableLayer, tid uint16) ([]byte, error) { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 35 | 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 Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 50 | func CreateMibResetRequest(tid uint16) ([]byte, error) { |
| 51 | |
| 52 | request := &omci.MibResetRequest{ |
| 53 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 54 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 55 | }, |
| 56 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 57 | pkt, err := Serialize(omci.MibResetRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 58 | if err != nil { |
| 59 | omciLogger.WithFields(log.Fields{ |
| 60 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 61 | }).Fatalf("Cannot Serialize MibResetRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 62 | return nil, err |
| 63 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 64 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 67 | func CreateMibResetResponse(tid uint16) ([]byte, error) { |
| 68 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 69 | request := &omci.MibResetResponse{ |
| 70 | MeBasePacket: omci.MeBasePacket{ |
| 71 | EntityClass: me.OnuDataClassID, |
| 72 | }, |
| 73 | Result: me.Success, |
| 74 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 75 | pkt, err := Serialize(omci.MibResetResponseType, request, tid) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 76 | if err != nil { |
| 77 | omciLogger.WithFields(log.Fields{ |
| 78 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 79 | }).Error("Cannot Serialize MibResetResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 80 | return nil, err |
| 81 | } |
| 82 | return pkt, nil |
| 83 | } |
| 84 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 85 | func CreateMibUploadRequest(tid uint16) ([]byte, error) { |
| 86 | request := &omci.MibUploadRequest{ |
| 87 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 88 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 89 | // Default Instance ID is 0 |
| 90 | }, |
| 91 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 92 | pkt, err := Serialize(omci.MibUploadRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 93 | if err != nil { |
| 94 | omciLogger.WithFields(log.Fields{ |
| 95 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 96 | }).Fatalf("Cannot Serialize MibUploadRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 97 | return nil, err |
| 98 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 99 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 102 | func CreateMibUploadResponse(tid uint16, numberOfCommands uint16) ([]byte, error) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 103 | request := &omci.MibUploadResponse{ |
| 104 | MeBasePacket: omci.MeBasePacket{ |
| 105 | EntityClass: me.OnuDataClassID, |
| 106 | }, |
| 107 | NumberOfCommands: numberOfCommands, |
| 108 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 109 | pkt, err := Serialize(omci.MibUploadResponseType, request, tid) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 110 | if err != nil { |
| 111 | omciLogger.WithFields(log.Fields{ |
| 112 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 113 | }).Error("Cannot Serialize MibUploadResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 114 | return nil, err |
| 115 | } |
| 116 | return pkt, nil |
| 117 | } |
| 118 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 119 | func CreateMibUploadNextRequest(tid uint16, seqNumber uint16) ([]byte, error) { |
| 120 | |
| 121 | request := &omci.MibUploadNextRequest{ |
| 122 | MeBasePacket: omci.MeBasePacket{ |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 123 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 124 | // Default Instance ID is 0 |
| 125 | }, |
| 126 | CommandSequenceNumber: seqNumber, |
| 127 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 128 | pkt, err := Serialize(omci.MibUploadNextRequestType, request, tid) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 129 | |
| 130 | if err != nil { |
| 131 | omciLogger.WithFields(log.Fields{ |
| 132 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 133 | }).Fatalf("Cannot Serialize MibUploadNextRequest") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 134 | return nil, err |
| 135 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 136 | return HexEncode(pkt) |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 139 | func 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 Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 145 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 146 | 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 Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 155 | 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] | 156 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 157 | msgObj, err := ParseMibUploadNextRequest(omciPkt) |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 158 | if err != nil { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 159 | err := "omci Msg layer could not be assigned for LayerTypeGetRequest" |
| 160 | omciLogger.Error(err) |
| 161 | return nil, errors.New(err) |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 164 | omciLogger.WithFields(log.Fields{ |
| 165 | "EntityClass": msgObj.EntityClass, |
| 166 | "EntityInstance": msgObj.EntityInstance, |
| 167 | "CommandSequenceNumber": msgObj.CommandSequenceNumber, |
| 168 | }).Trace("received-omci-mibUploadNext-request") |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 169 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 170 | 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 Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame] | 182 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 183 | if meErr.GetError() != nil { |
| 184 | omciLogger.Errorf("Error while generating %s: %v", currentEntry.classId.String(), meErr.Error()) |
| 185 | } |
Scott Baker | b90c431 | 2020-03-12 21:33:25 -0700 | [diff] [blame] | 186 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 187 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 191 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 194 | response := &omci.MibUploadNextResponse{ |
| 195 | MeBasePacket: omci.MeBasePacket{ |
| 196 | EntityClass: me.OnuDataClassID, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 197 | }, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 198 | ReportedME: *reportedMe, |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 199 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 200 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 201 | omciLogger.WithFields(log.Fields{ |
| 202 | "reportedMe": reportedMe, |
| 203 | }).Trace("created-omci-mibUploadNext-response") |
| 204 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 205 | pkt, err := Serialize(omci.MibUploadNextResponseType, response, omciMsg.TransactionID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 206 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 207 | if err != nil { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 208 | omciLogger.WithFields(log.Fields{ |
| 209 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 210 | }).Fatalf("Cannot Serialize MibUploadNextRequest") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 211 | return nil, err |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 212 | } |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 213 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 214 | return pkt, nil |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 215 | } |