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 | "errors" |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 21 | "fmt" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 22 | "github.com/google/gopacket" |
| 23 | "github.com/opencord/omci-lib-go" |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 24 | me "github.com/opencord/omci-lib-go/generated" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | "math/rand" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 29 | func ParseOpticalLineSupervisionRequest(omciPkt gopacket.Packet) (*omci.OpticalLineSupervisionTestRequest, error) { |
| 30 | msgLayer := omciPkt.Layer(omci.LayerTypeTestRequest) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 31 | if msgLayer == nil { |
| 32 | err := "omci Msg layer could not be detected for LayerTypeTestRequest" |
| 33 | omciLogger.Error(err) |
| 34 | return nil, errors.New(err) |
| 35 | } |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 36 | msgObj, msgOk := msgLayer.(*omci.OpticalLineSupervisionTestRequest) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 37 | if !msgOk { |
| 38 | err := "omci Msg layer could not be assigned for LayerTypeTestRequest" |
| 39 | omciLogger.Error(err) |
| 40 | return nil, errors.New(err) |
| 41 | } |
| 42 | return msgObj, nil |
| 43 | } |
| 44 | |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 45 | func CreateTestResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error, me.ClassID, uint16, me.Results) { |
| 46 | // TODO: currently supports only OpticalLineSupervisionRequest |
| 47 | optSupReq, err := ParseOpticalLineSupervisionRequest(omciPkt) |
| 48 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 49 | if err != nil { |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 50 | return nil, err, 0, 0, me.ParameterError |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 53 | omciLogger.WithFields(log.Fields{ |
| 54 | "EntityClass": optSupReq.EntityClass, |
| 55 | "EntityInstance": optSupReq.EntityInstance, |
| 56 | }).Trace("received-test-request") |
| 57 | |
| 58 | omciResult := me.Success |
| 59 | response := &omci.TestResponse{ |
| 60 | MeBasePacket: omci.MeBasePacket{ |
| 61 | EntityClass: optSupReq.EntityClass, |
| 62 | EntityInstance: optSupReq.EntityInstance, |
| 63 | }, |
| 64 | Result: omciResult, |
| 65 | } |
| 66 | |
| 67 | pkt, err := Serialize(omci.TestResponseType, response, omciMsg.TransactionID) |
| 68 | if err != nil { |
| 69 | omciLogger.WithFields(log.Fields{ |
| 70 | "Err": err, |
| 71 | }).Error("cannot-Serialize-test-response") |
| 72 | return nil, err, 0, 0, me.ParameterError |
| 73 | } |
| 74 | |
| 75 | return pkt, nil, optSupReq.EntityClass, optSupReq.EntityInstance, omciResult |
| 76 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 79 | func CreateTestResult(classID me.ClassID, instID uint16, tid uint16) ([]byte, error) { |
| 80 | var result gopacket.SerializableLayer |
| 81 | switch classID { |
| 82 | case me.AniGClassID: |
| 83 | result = &omci.OpticalLineSupervisionTestResult{ |
| 84 | MeBasePacket: omci.MeBasePacket{ |
| 85 | EntityClass: classID, |
| 86 | EntityInstance: instID, |
| 87 | }, |
| 88 | PowerFeedVoltageType: uint8(1), |
| 89 | PowerFeedVoltage: uint16(163), |
| 90 | ReceivedOpticalPowerType: uint8(3), |
| 91 | ReceivedOpticalPower: uint16(rand.Intn(65000)), |
| 92 | MeanOpticalLaunchType: uint8(5), |
| 93 | MeanOpticalLaunch: uint16(rand.Intn(3000)), |
| 94 | LaserBiasCurrentType: uint8(9), |
| 95 | LaserBiasCurrent: uint16(rand.Intn(10000)), |
| 96 | TemperatureType: uint8(12), |
| 97 | Temperature: uint16(rand.Intn(20000)), |
| 98 | GeneralPurposeBuffer: uint16(0), |
| 99 | } |
| 100 | default: |
| 101 | return nil, fmt.Errorf("unsupported-class-id-for-test-result--class-id-%v", classID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 104 | pkt, err := Serialize(omci.TestResultType, result, tid) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 105 | if err != nil { |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 106 | omciLogger.WithFields(log.Fields{ |
| 107 | "Err": err, |
| 108 | }).Error("cannot-Serialize-test-result") |
| 109 | return nil, err |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 110 | } |
Girish Gowdra | 161d27a | 2021-05-05 12:01:44 -0700 | [diff] [blame] | 111 | return pkt, nil |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 112 | } |