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 | "encoding/hex" |
| 21 | "errors" |
| 22 | "github.com/google/gopacket" |
| 23 | "github.com/opencord/omci-lib-go" |
| 24 | me "github.com/opencord/omci-lib-go/generated" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | "strconv" |
| 27 | ) |
| 28 | |
| 29 | func ParseCreateRequest(omciPkt gopacket.Packet) (*omci.CreateRequest, error) { |
| 30 | msgLayer := omciPkt.Layer(omci.LayerTypeCreateRequest) |
| 31 | if msgLayer == nil { |
| 32 | err := "omci Msg layer could not be detected for LayerTypeCreateRequest" |
| 33 | omciLogger.Error(err) |
| 34 | return nil, errors.New(err) |
| 35 | } |
| 36 | msgObj, msgOk := msgLayer.(*omci.CreateRequest) |
| 37 | if !msgOk { |
| 38 | err := "omci Msg layer could not be assigned for LayerTypeCreateRequest" |
| 39 | omciLogger.Error(err) |
| 40 | return nil, errors.New(err) |
| 41 | } |
| 42 | return msgObj, nil |
| 43 | } |
| 44 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 45 | func CreateCreateResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, result me.Results) ([]byte, error) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 46 | |
| 47 | msgObj, err := ParseCreateRequest(omciPkt) |
| 48 | |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | omciLogger.WithFields(log.Fields{ |
| 54 | "EntityClass": msgObj.EntityClass, |
| 55 | "EntityInstance": msgObj.EntityInstance, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 56 | }).Trace("received-omci-create-request") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 57 | |
| 58 | response := &omci.CreateResponse{ |
| 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.CreateResponseType, 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-CreateResponse") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 71 | return nil, err |
| 72 | } |
| 73 | |
| 74 | log.WithFields(log.Fields{ |
| 75 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 76 | "pkt": hex.EncodeToString(pkt), |
| 77 | }).Trace("omci-create-response") |
| 78 | |
| 79 | return pkt, nil |
| 80 | } |