Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 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" |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 21 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 22 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 23 | "github.com/opencord/omci-lib-go/v2" |
| 24 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 25 | log "github.com/sirupsen/logrus" |
| 26 | ) |
| 27 | |
| 28 | func 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 Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 44 | func CreateSetResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, result me.Results) ([]byte, error) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 45 | |
| 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 Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 63 | Result: result, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 66 | pkt, err := Serialize(omci.SetResponseType, response, omciMsg.TransactionID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 67 | if err != nil { |
| 68 | omciLogger.WithFields(log.Fields{ |
| 69 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 70 | }).Error("cannot-Serialize-SetResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 71 | return nil, err |
| 72 | } |
| 73 | |
| 74 | return pkt, nil |
| 75 | |
| 76 | } |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 77 | |
| 78 | func 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 | } |