blob: 7d786b9018b985686d16da8a573e96f59ebbe937 [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
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
17package omci
18
19import (
20 "errors"
Girish Gowdra161d27a2021-05-05 12:01:44 -070021 "fmt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080022 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020023 "github.com/opencord/omci-lib-go/v2"
24 me "github.com/opencord/omci-lib-go/v2/generated"
Girish Gowdra161d27a2021-05-05 12:01:44 -070025 log "github.com/sirupsen/logrus"
26 "math/rand"
Matteo Scandolof9d43412021-01-12 11:11:34 -080027)
28
Girish Gowdra161d27a2021-05-05 12:01:44 -070029func ParseOpticalLineSupervisionRequest(omciPkt gopacket.Packet) (*omci.OpticalLineSupervisionTestRequest, error) {
30 msgLayer := omciPkt.Layer(omci.LayerTypeTestRequest)
Matteo Scandolof9d43412021-01-12 11:11:34 -080031 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 Gowdra161d27a2021-05-05 12:01:44 -070036 msgObj, msgOk := msgLayer.(*omci.OpticalLineSupervisionTestRequest)
Matteo Scandolof9d43412021-01-12 11:11:34 -080037 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 Gowdra161d27a2021-05-05 12:01:44 -070045func 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 Scandolof9d43412021-01-12 11:11:34 -080049 if err != nil {
Girish Gowdra161d27a2021-05-05 12:01:44 -070050 return nil, err, 0, 0, me.ParameterError
Matteo Scandolof9d43412021-01-12 11:11:34 -080051 }
52
Girish Gowdra161d27a2021-05-05 12:01:44 -070053 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 Scandolof9d43412021-01-12 11:11:34 -080077}
78
Girish Gowdra161d27a2021-05-05 12:01:44 -070079func 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 Scandolof9d43412021-01-12 11:11:34 -0800102 }
103
Girish Gowdra161d27a2021-05-05 12:01:44 -0700104 pkt, err := Serialize(omci.TestResultType, result, tid)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800105 if err != nil {
Girish Gowdra161d27a2021-05-05 12:01:44 -0700106 omciLogger.WithFields(log.Fields{
107 "Err": err,
108 }).Error("cannot-Serialize-test-result")
109 return nil, err
Matteo Scandolof9d43412021-01-12 11:11:34 -0800110 }
Girish Gowdra161d27a2021-05-05 12:01:44 -0700111 return pkt, nil
Matteo Scandolof9d43412021-01-12 11:11:34 -0800112}