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" |
| 21 | "github.com/google/gopacket" |
| 22 | "github.com/opencord/omci-lib-go" |
| 23 | me "github.com/opencord/omci-lib-go/generated" |
| 24 | log "github.com/sirupsen/logrus" |
| 25 | ) |
| 26 | |
| 27 | func ParseSetRequest(omciPkt gopacket.Packet) (*omci.SetRequest, error) { |
| 28 | msgLayer := omciPkt.Layer(omci.LayerTypeSetRequest) |
| 29 | if msgLayer == nil { |
| 30 | err := "omci Msg layer could not be detected for LayerTypeSetRequest" |
| 31 | omciLogger.Error(err) |
| 32 | return nil, errors.New(err) |
| 33 | } |
| 34 | msgObj, msgOk := msgLayer.(*omci.SetRequest) |
| 35 | if !msgOk { |
| 36 | err := "omci Msg layer could not be assigned for LayerTypeSetRequest" |
| 37 | omciLogger.Error(err) |
| 38 | return nil, errors.New(err) |
| 39 | } |
| 40 | return msgObj, nil |
| 41 | } |
| 42 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 43 | 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] | 44 | |
| 45 | msgObj, err := ParseSetRequest(omciPkt) |
| 46 | |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | omciLogger.WithFields(log.Fields{ |
| 52 | "EntityClass": msgObj.EntityClass, |
| 53 | "EntityInstance": msgObj.EntityInstance, |
| 54 | "AttributeMask": msgObj.AttributeMask, |
| 55 | }).Trace("received-omci-set-request") |
| 56 | |
| 57 | response := &omci.SetResponse{ |
| 58 | MeBasePacket: omci.MeBasePacket{ |
| 59 | EntityClass: msgObj.EntityClass, |
| 60 | EntityInstance: msgObj.EntityInstance, |
| 61 | }, |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 62 | Result: result, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 65 | pkt, err := Serialize(omci.SetResponseType, response, omciMsg.TransactionID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 66 | if err != nil { |
| 67 | omciLogger.WithFields(log.Fields{ |
| 68 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 69 | }).Error("cannot-Serialize-SetResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | return pkt, nil |
| 74 | |
| 75 | } |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 76 | |
| 77 | func CreateSetRequest(managedEntity *me.ManagedEntity, tid uint16) ([]byte, error) { |
| 78 | |
| 79 | // TODO |
| 80 | // why can't we create the SetRequest as we do for all other omci Requests (eg: MibResetRequest)? |
| 81 | // if we do the Attributes are not sent |
| 82 | |
| 83 | request := &omci.SetRequest{ |
| 84 | MeBasePacket: omci.MeBasePacket{ |
| 85 | EntityClass: managedEntity.GetClassID(), |
| 86 | EntityInstance: managedEntity.GetEntityID(), |
| 87 | }, |
| 88 | Attributes: managedEntity.GetAttributeValueMap(), |
| 89 | AttributeMask: 0x800, // FIXME how can we generate this based on managedEntity.AttributeValueMap? |
| 90 | } |
| 91 | omciLogger.Info(request) |
| 92 | |
| 93 | pkt, err := Serialize(omci.SetRequestType, request, tid) |
| 94 | |
| 95 | if err != nil { |
| 96 | omciLogger.WithFields(log.Fields{ |
| 97 | "Err": err, |
| 98 | }).Fatalf("Cannot Serialize SetRequest") |
| 99 | return nil, err |
| 100 | } |
| 101 | return HexEncode(pkt) |
| 102 | } |