Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [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 ( |
| 20 | "encoding/hex" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "github.com/google/gopacket" |
| 24 | "github.com/opencord/omci-lib-go" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v4/go/openolt" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 27 | log "github.com/sirupsen/logrus" |
| 28 | "strconv" |
| 29 | ) |
| 30 | |
| 31 | func ParseGetRequest(omciPkt gopacket.Packet) (*omci.GetRequest, error) { |
| 32 | msgLayer := omciPkt.Layer(omci.LayerTypeGetRequest) |
| 33 | if msgLayer == nil { |
| 34 | err := "omci Msg layer could not be detected for LayerTypeGetRequest" |
| 35 | omciLogger.Error(err) |
| 36 | return nil, errors.New(err) |
| 37 | } |
| 38 | msgObj, msgOk := msgLayer.(*omci.GetRequest) |
| 39 | if !msgOk { |
| 40 | err := "omci Msg layer could not be assigned for LayerTypeGetRequest" |
| 41 | omciLogger.Error(err) |
| 42 | return nil, errors.New(err) |
| 43 | } |
| 44 | return msgObj, nil |
| 45 | } |
| 46 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 47 | func CreateGetResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, onuSn *openolt.SerialNumber, mds uint8) ([]byte, error) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 48 | |
| 49 | msgObj, err := ParseGetRequest(omciPkt) |
| 50 | |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | omciLogger.WithFields(log.Fields{ |
| 56 | "EntityClass": msgObj.EntityClass, |
| 57 | "EntityInstance": msgObj.EntityInstance, |
| 58 | "AttributeMask": fmt.Sprintf("%x", msgObj.AttributeMask), |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 59 | }).Trace("received-omci-get-request") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 60 | |
| 61 | var response *omci.GetResponse |
| 62 | switch msgObj.EntityClass { |
| 63 | case me.Onu2GClassID: |
| 64 | response = createOnu2gResponse(msgObj.AttributeMask, msgObj.EntityInstance) |
| 65 | case me.OnuGClassID: |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 66 | response = createOnugResponse(msgObj.AttributeMask, msgObj.EntityInstance, onuSn) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 67 | case me.SoftwareImageClassID: |
| 68 | response = createSoftwareImageResponse(msgObj.AttributeMask, msgObj.EntityInstance) |
| 69 | case me.IpHostConfigDataClassID: |
| 70 | response = createIpHostResponse(msgObj.AttributeMask, msgObj.EntityInstance) |
| 71 | case me.UniGClassID: |
| 72 | response = createUnigResponse(msgObj.AttributeMask, msgObj.EntityInstance) |
| 73 | case me.PhysicalPathTerminationPointEthernetUniClassID: |
| 74 | response = createPptpResponse(msgObj.AttributeMask, msgObj.EntityInstance) |
| 75 | case me.AniGClassID: |
| 76 | response = createAnigResponse(msgObj.AttributeMask, msgObj.EntityInstance) |
| 77 | case me.OnuDataClassID: |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 78 | response = createOnuDataResponse(msgObj.AttributeMask, msgObj.EntityInstance, mds) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 79 | default: |
| 80 | omciLogger.WithFields(log.Fields{ |
| 81 | "EntityClass": msgObj.EntityClass, |
| 82 | "EntityInstance": msgObj.EntityInstance, |
| 83 | "AttributeMask": fmt.Sprintf("%x", msgObj.AttributeMask), |
| 84 | }).Warnf("do-not-know-how-to-handle-get-request-for-me-class") |
| 85 | return nil, nil |
| 86 | } |
| 87 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 88 | pkt, err := Serialize(omci.GetResponseType, response, omciMsg.TransactionID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 89 | if err != nil { |
| 90 | omciLogger.WithFields(log.Fields{ |
| 91 | "Err": err, |
| 92 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 93 | }).Error("cannot-Serialize-Onu2gResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | log.WithFields(log.Fields{ |
| 98 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 99 | "pkt": hex.EncodeToString(pkt), |
| 100 | }).Trace("omci-get-response") |
| 101 | |
| 102 | return pkt, nil |
| 103 | } |
| 104 | |
| 105 | func createOnu2gResponse(attributeMask uint16, entityID uint16) *omci.GetResponse { |
| 106 | |
| 107 | managedEntity, meErr := me.NewOnu2G(me.ParamData{ |
| 108 | EntityID: entityID, |
| 109 | Attributes: me.AttributeValueMap{ |
| 110 | "ManagedEntityId": entityID, |
Matteo Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame^] | 111 | "EquipmentId": toOctets("12345123451234512345", 20), |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 112 | "OpticalNetworkUnitManagementAndControlChannelOmccVersion": 180, |
| 113 | "VendorProductCode": 0, |
| 114 | "SecurityCapability": 1, |
| 115 | "SecurityMode": 1, |
| 116 | "TotalPriorityQueueNumber": 1, |
| 117 | "TotalTrafficSchedulerNumber": 1, |
| 118 | "Deprecated": 1, |
| 119 | "TotalGemPortIdNumber": 32, |
| 120 | "Sysuptime": 319389947, // NOTE need to be smarter? |
| 121 | "ConnectivityCapability": 127, |
| 122 | "CurrentConnectivityMode": 5, |
| 123 | "QualityOfServiceQosConfigurationFlexibility": 48, |
| 124 | "PriorityQueueScaleFactor": 1, |
| 125 | }, |
| 126 | }) |
| 127 | |
| 128 | if meErr.GetError() != nil { |
| 129 | omciLogger.Errorf("NewOnu2G %v", meErr.Error()) |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | return &omci.GetResponse{ |
| 134 | MeBasePacket: omci.MeBasePacket{ |
| 135 | EntityClass: me.Onu2GClassID, |
| 136 | }, |
| 137 | Attributes: managedEntity.GetAttributeValueMap(), |
| 138 | AttributeMask: attributeMask, |
| 139 | Result: me.Success, |
| 140 | } |
| 141 | } |
| 142 | |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 143 | func createOnugResponse(attributeMask uint16, entityID uint16, onuSn *openolt.SerialNumber) *omci.GetResponse { |
| 144 | |
| 145 | managedEntity, meErr := me.NewOnuG(me.ParamData{ |
| 146 | EntityID: entityID, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 147 | Attributes: me.AttributeValueMap{ |
| 148 | "ManagedEntityId": entityID, |
| 149 | "VendorId": toOctets("BBSM", 4), |
| 150 | "Version": toOctets("v0.0.1", 14), |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 151 | "SerialNumber": append(onuSn.VendorId, onuSn.VendorSpecific...), |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 152 | "TrafficManagementOption": 0, |
| 153 | "Deprecated": 0, |
| 154 | "BatteryBackup": 0, |
| 155 | "AdministrativeState": 0, |
| 156 | "OperationalState": 0, |
| 157 | "OnuSurvivalTime": 10, |
| 158 | "LogicalOnuId": toOctets("BBSM", 24), |
| 159 | "LogicalPassword": toOctets("BBSM", 12), |
| 160 | "CredentialsStatus": 0, |
| 161 | "ExtendedTcLayerOptions": 0, |
| 162 | }, |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 163 | }) |
| 164 | |
| 165 | if meErr.GetError() != nil { |
| 166 | omciLogger.Errorf("NewOnu2G %v", meErr.Error()) |
| 167 | return nil |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 168 | } |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 169 | |
| 170 | return &omci.GetResponse{ |
| 171 | MeBasePacket: omci.MeBasePacket{ |
| 172 | EntityClass: me.OnuGClassID, |
| 173 | }, |
| 174 | Attributes: managedEntity.GetAttributeValueMap(), |
| 175 | AttributeMask: attributeMask, |
| 176 | Result: me.Success, |
| 177 | } |
| 178 | |
| 179 | //return &omci.GetResponse{ |
| 180 | // MeBasePacket: omci.MeBasePacket{ |
| 181 | // EntityClass: me.OnuGClassID, |
| 182 | // EntityInstance: entityID, |
| 183 | // }, |
| 184 | // Attributes: me.AttributeValueMap{ |
| 185 | // |
| 186 | // }, |
| 187 | // Result: me.Success, |
| 188 | // AttributeMask: attributeMask, |
| 189 | //} |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | func createSoftwareImageResponse(attributeMask uint16, entityInstance uint16) *omci.GetResponse { |
| 193 | // NOTE that we need send the response for the correct ME Instance or the adapter won't process it |
| 194 | return &omci.GetResponse{ |
| 195 | MeBasePacket: omci.MeBasePacket{ |
| 196 | EntityClass: me.SoftwareImageClassID, |
| 197 | EntityInstance: entityInstance, |
| 198 | }, |
| 199 | Attributes: me.AttributeValueMap{ |
| 200 | "ManagedEntityId": 0, |
Matteo Scandolo | 9a1842d | 2021-02-08 16:00:17 -0800 | [diff] [blame^] | 201 | "Version": toOctets("00000000000001", 14), |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 202 | "IsCommitted": 1, |
| 203 | "IsActive": 1, |
| 204 | "IsValid": 1, |
| 205 | "ProductCode": toOctets("product-code", 25), |
| 206 | "ImageHash": toOctets("broadband-sim", 16), |
| 207 | }, |
| 208 | Result: me.Success, |
| 209 | AttributeMask: attributeMask, |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func createIpHostResponse(attributeMask uint16, entityInstance uint16) *omci.GetResponse { |
| 214 | return &omci.GetResponse{ |
| 215 | MeBasePacket: omci.MeBasePacket{ |
| 216 | EntityClass: me.IpHostConfigDataClassID, |
| 217 | EntityInstance: entityInstance, |
| 218 | }, |
| 219 | Attributes: me.AttributeValueMap{ |
| 220 | "ManagedEntityId": 0, |
| 221 | "MacAddress": toOctets("aabbcc", 6), |
| 222 | }, |
| 223 | Result: me.Success, |
| 224 | AttributeMask: attributeMask, |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | func createUnigResponse(attributeMask uint16, entityID uint16) *omci.GetResponse { |
| 229 | managedEntity, meErr := me.NewUniG(me.ParamData{ |
| 230 | EntityID: entityID, |
| 231 | Attributes: me.AttributeValueMap{ |
| 232 | "ManagedEntityId": entityID, |
| 233 | "Deprecated": 0, |
| 234 | "AdministrativeState": 0, |
| 235 | "ManagementCapability": 0, |
| 236 | "NonOmciManagementIdentifier": 1, |
| 237 | "RelayAgentOptions": 1, |
| 238 | }, |
| 239 | }) |
| 240 | |
| 241 | if meErr.GetError() != nil { |
| 242 | omciLogger.Errorf("NewUniG %v", meErr.Error()) |
| 243 | return nil |
| 244 | } |
| 245 | |
| 246 | return &omci.GetResponse{ |
| 247 | MeBasePacket: omci.MeBasePacket{ |
| 248 | EntityClass: me.UniGClassID, |
| 249 | }, |
| 250 | Attributes: managedEntity.GetAttributeValueMap(), |
| 251 | AttributeMask: attributeMask, |
| 252 | Result: me.Success, |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | func createPptpResponse(attributeMask uint16, entityID uint16) *omci.GetResponse { |
| 257 | managedEntity, meErr := me.NewPhysicalPathTerminationPointEthernetUni(me.ParamData{ |
| 258 | EntityID: entityID, |
| 259 | Attributes: me.AttributeValueMap{ |
| 260 | "ManagedEntityId": entityID, |
| 261 | "ExpectedType": 0, |
| 262 | "SensedType": 0, |
| 263 | "AutoDetectionConfiguration": 0, |
| 264 | "EthernetLoopbackConfiguration": 0, |
| 265 | "AdministrativeState": 0, |
| 266 | "OperationalState": 0, |
| 267 | "ConfigurationInd": 0, |
| 268 | "MaxFrameSize": 0, |
| 269 | "DteOrDceInd": 0, |
| 270 | "PauseTime": 0, |
| 271 | "BridgedOrIpInd": 0, |
| 272 | "Arc": 0, |
| 273 | "ArcInterval": 0, |
| 274 | "PppoeFilter": 0, |
| 275 | "PowerControl": 0, |
| 276 | }, |
| 277 | }) |
| 278 | |
| 279 | if meErr.GetError() != nil { |
| 280 | omciLogger.Errorf("NewPhysicalPathTerminationPointEthernetUni %v", meErr.Error()) |
| 281 | return nil |
| 282 | } |
| 283 | |
| 284 | return &omci.GetResponse{ |
| 285 | MeBasePacket: omci.MeBasePacket{ |
| 286 | EntityClass: me.PhysicalPathTerminationPointEthernetUniClassID, |
| 287 | }, |
| 288 | Attributes: managedEntity.GetAttributeValueMap(), |
| 289 | AttributeMask: attributeMask, |
| 290 | Result: me.Success, |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | func createAnigResponse(attributeMask uint16, entityID uint16) *omci.GetResponse { |
| 295 | managedEntity, meErr := me.NewAniG(me.ParamData{ |
| 296 | EntityID: entityID, |
| 297 | Attributes: me.AttributeValueMap{ |
| 298 | "ManagedEntityId": entityID, |
| 299 | "SrIndication": 0, |
| 300 | "TotalTcontNumber": 0, |
| 301 | "GemBlockLength": 0, |
| 302 | "PiggybackDbaReporting": 0, |
| 303 | "Deprecated": 0, |
| 304 | "SignalFailThreshold": 0, |
| 305 | "SignalDegradeThreshold": 0, |
| 306 | "Arc": 0, |
| 307 | "ArcInterval": 0, |
| 308 | "OpticalSignalLevel": 0, |
| 309 | "LowerOpticalThreshold": 0, |
| 310 | "UpperOpticalThreshold": 0, |
| 311 | "OnuResponseTime": 0, |
| 312 | "TransmitOpticalLevel": 0, |
| 313 | "LowerTransmitPowerThreshold": 0, |
| 314 | "UpperTransmitPowerThreshold": 0, |
| 315 | }, |
| 316 | }) |
| 317 | |
| 318 | if meErr.GetError() != nil { |
| 319 | omciLogger.Errorf("NewAniG %v", meErr.Error()) |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | return &omci.GetResponse{ |
| 324 | MeBasePacket: omci.MeBasePacket{ |
| 325 | EntityClass: me.AniGClassID, |
| 326 | }, |
| 327 | Attributes: managedEntity.GetAttributeValueMap(), |
| 328 | AttributeMask: attributeMask, |
| 329 | Result: me.Success, |
| 330 | } |
| 331 | } |
| 332 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 333 | func createOnuDataResponse(attributeMask uint16, entityID uint16, mds uint8) *omci.GetResponse { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 334 | managedEntity, meErr := me.NewOnuData(me.ParamData{ |
| 335 | EntityID: entityID, |
| 336 | Attributes: me.AttributeValueMap{ |
| 337 | "ManagedEntityId": entityID, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 338 | "MibDataSync": mds, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 339 | }, |
| 340 | }) |
| 341 | |
| 342 | if meErr.GetError() != nil { |
| 343 | omciLogger.Errorf("NewOnuData %v", meErr.Error()) |
| 344 | return nil |
| 345 | } |
| 346 | |
| 347 | return &omci.GetResponse{ |
| 348 | MeBasePacket: omci.MeBasePacket{ |
| 349 | EntityClass: me.OnuDataClassID, |
| 350 | }, |
| 351 | Attributes: managedEntity.GetAttributeValueMap(), |
| 352 | AttributeMask: attributeMask, |
| 353 | Result: me.Success, |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | func toOctets(str string, size int) []byte { |
| 358 | asciiBytes := []byte(str) |
| 359 | |
| 360 | if len(asciiBytes) < size { |
| 361 | missing := size - len(asciiBytes) |
| 362 | for i := 0; i < missing; i++ { |
| 363 | asciiBytes = append(asciiBytes, []byte{0x00}[0]) |
| 364 | } |
| 365 | } |
| 366 | return asciiBytes |
| 367 | } |