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" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame^] | 22 | "github.com/opencord/omci-lib-go/v2" |
| 23 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 24 | log "github.com/sirupsen/logrus" |
| 25 | ) |
| 26 | |
| 27 | func ParseDeleteRequest(omciPkt gopacket.Packet) (*omci.DeleteRequest, error) { |
| 28 | msgLayer := omciPkt.Layer(omci.LayerTypeDeleteRequest) |
| 29 | if msgLayer == nil { |
| 30 | err := "omci Msg layer could not be detected for LayerTypeDeleteRequest" |
| 31 | omciLogger.Error(err) |
| 32 | return nil, errors.New(err) |
| 33 | } |
| 34 | msgObj, msgOk := msgLayer.(*omci.DeleteRequest) |
| 35 | if !msgOk { |
| 36 | err := "omci Msg layer could not be assigned for LayerTypeDeleteRequest" |
| 37 | omciLogger.Error(err) |
| 38 | return nil, errors.New(err) |
| 39 | } |
| 40 | return msgObj, nil |
| 41 | } |
| 42 | |
| 43 | func CreateDeleteResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 44 | msgObj, err := ParseDeleteRequest(omciPkt) |
| 45 | |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | omciLogger.WithFields(log.Fields{ |
| 51 | "EntityClass": msgObj.EntityClass, |
| 52 | "EntityInstance": msgObj.EntityInstance, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 53 | }).Trace("received-omci-delete-request") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 54 | |
| 55 | response := &omci.DeleteResponse{ |
| 56 | MeBasePacket: omci.MeBasePacket{ |
| 57 | EntityClass: msgObj.EntityClass, |
| 58 | EntityInstance: msgObj.EntityInstance, |
| 59 | }, |
| 60 | Result: me.Success, |
| 61 | } |
| 62 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 63 | pkt, err := Serialize(omci.DeleteResponseType, response, omciMsg.TransactionID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 64 | if err != nil { |
| 65 | omciLogger.WithFields(log.Fields{ |
| 66 | "Err": err, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 67 | }).Error("cannot-Serialize-DeleteResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | return pkt, nil |
| 72 | } |