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