blob: c09b13fe8925782471f82386eacb38d322bb908d [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolof9d43412021-01-12 11:11:34 -08003
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"
Elia Battiston9bfe1102022-02-03 10:38:03 +010021
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"
Matteo Scandolof9d43412021-01-12 11:11:34 -080025 log "github.com/sirupsen/logrus"
26)
27
28func ParseSetRequest(omciPkt gopacket.Packet) (*omci.SetRequest, error) {
29 msgLayer := omciPkt.Layer(omci.LayerTypeSetRequest)
30 if msgLayer == nil {
31 err := "omci Msg layer could not be detected for LayerTypeSetRequest"
32 omciLogger.Error(err)
33 return nil, errors.New(err)
34 }
35 msgObj, msgOk := msgLayer.(*omci.SetRequest)
36 if !msgOk {
37 err := "omci Msg layer could not be assigned for LayerTypeSetRequest"
38 omciLogger.Error(err)
39 return nil, errors.New(err)
40 }
41 return msgObj, nil
42}
43
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080044func CreateSetResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, result me.Results) ([]byte, error) {
Matteo Scandolof9d43412021-01-12 11:11:34 -080045
46 msgObj, err := ParseSetRequest(omciPkt)
47
48 if err != nil {
49 return nil, err
50 }
51
52 omciLogger.WithFields(log.Fields{
53 "EntityClass": msgObj.EntityClass,
54 "EntityInstance": msgObj.EntityInstance,
55 "AttributeMask": msgObj.AttributeMask,
56 }).Trace("received-omci-set-request")
57
58 response := &omci.SetResponse{
59 MeBasePacket: omci.MeBasePacket{
60 EntityClass: msgObj.EntityClass,
61 EntityInstance: msgObj.EntityInstance,
62 },
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080063 Result: result,
Matteo Scandolof9d43412021-01-12 11:11:34 -080064 }
65
Matteo Scandolo992a23e2021-02-04 15:35:04 -080066 pkt, err := Serialize(omci.SetResponseType, response, omciMsg.TransactionID)
Matteo Scandolof9d43412021-01-12 11:11:34 -080067 if err != nil {
68 omciLogger.WithFields(log.Fields{
69 "Err": err,
Matteo Scandolo992a23e2021-02-04 15:35:04 -080070 }).Error("cannot-Serialize-SetResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -080071 return nil, err
72 }
73
74 return pkt, nil
75
76}
Matteo Scandolo8a574812021-05-20 15:18:53 -070077
78func CreateSetRequest(managedEntity *me.ManagedEntity, tid uint16) ([]byte, error) {
79
80 // TODO
81 // why can't we create the SetRequest as we do for all other omci Requests (eg: MibResetRequest)?
82 // if we do the Attributes are not sent
83
84 request := &omci.SetRequest{
85 MeBasePacket: omci.MeBasePacket{
86 EntityClass: managedEntity.GetClassID(),
87 EntityInstance: managedEntity.GetEntityID(),
88 },
89 Attributes: managedEntity.GetAttributeValueMap(),
90 AttributeMask: 0x800, // FIXME how can we generate this based on managedEntity.AttributeValueMap?
91 }
92 omciLogger.Info(request)
93
94 pkt, err := Serialize(omci.SetRequestType, request, tid)
95
96 if err != nil {
97 omciLogger.WithFields(log.Fields{
98 "Err": err,
99 }).Fatalf("Cannot Serialize SetRequest")
100 return nil, err
101 }
102 return HexEncode(pkt)
103}