blob: d3c88b3584d6947ab827d53a851496683b17bf3b [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
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
17package omci
18
19import (
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
29func 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 Scandolo4b077aa2021-02-16 17:33:37 -080045func CreateCreateResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, result me.Results) ([]byte, error) {
Matteo Scandolof9d43412021-01-12 11:11:34 -080046
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 Scandolo992a23e2021-02-04 15:35:04 -080056 }).Trace("received-omci-create-request")
Matteo Scandolof9d43412021-01-12 11:11:34 -080057
58 response := &omci.CreateResponse{
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.CreateResponseType, 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-CreateResponse")
Matteo Scandolof9d43412021-01-12 11:11:34 -080071 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}