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 | |
| 45 | func CreateCreateResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 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, |
| 56 | }).Trace("recevied-omci-create-request") |
| 57 | |
| 58 | response := &omci.CreateResponse{ |
| 59 | MeBasePacket: omci.MeBasePacket{ |
| 60 | EntityClass: msgObj.EntityClass, |
| 61 | EntityInstance: msgObj.EntityInstance, |
| 62 | }, |
| 63 | Result: me.Success, |
| 64 | } |
| 65 | |
| 66 | pkt, err := serialize(omci.CreateResponseType, response, omciMsg.TransactionID) |
| 67 | if err != nil { |
| 68 | omciLogger.WithFields(log.Fields{ |
| 69 | "Err": err, |
| 70 | }).Error("cannot-serialize-CreateResponse") |
| 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 | } |
| 81 | |
| 82 | // methods used by BBR to drive the OMCI state machine |
| 83 | |
| 84 | func CreateGalEnetRequest(tid uint16) ([]byte, error) { |
| 85 | params := me.ParamData{ |
| 86 | EntityID: galEthernetEID, |
| 87 | Attributes: me.AttributeValueMap{"MaximumGemPayloadSize": maxGemPayloadSize}, |
| 88 | } |
| 89 | meDef, _ := me.NewGalEthernetProfile(params) |
| 90 | pkt, err := omci.GenFrame(meDef, omci.CreateRequestType, omci.TransactionID(tid)) |
| 91 | if err != nil { |
| 92 | omciLogger.WithField("err", err).Fatalf("Can't generate GalEnetRequest") |
| 93 | } |
| 94 | return hexEncode(pkt) |
| 95 | } |
| 96 | |
| 97 | func CreateEnableUniRequest(tid uint16, uniId uint16, enabled bool, isPtp bool) ([]byte, error) { |
| 98 | |
| 99 | var _enabled uint8 |
| 100 | if enabled { |
| 101 | _enabled = uint8(1) |
| 102 | } else { |
| 103 | _enabled = uint8(0) |
| 104 | } |
| 105 | |
| 106 | data := me.ParamData{ |
| 107 | EntityID: uniId, |
| 108 | Attributes: me.AttributeValueMap{ |
| 109 | "AdministrativeState": _enabled, |
| 110 | }, |
| 111 | } |
| 112 | var medef *me.ManagedEntity |
| 113 | var omciErr me.OmciErrors |
| 114 | |
| 115 | if isPtp { |
| 116 | medef, omciErr = me.NewPhysicalPathTerminationPointEthernetUni(data) |
| 117 | } else { |
| 118 | medef, omciErr = me.NewVirtualEthernetInterfacePoint(data) |
| 119 | } |
| 120 | if omciErr != nil { |
| 121 | return nil, omciErr.GetError() |
| 122 | } |
| 123 | pkt, err := omci.GenFrame(medef, omci.SetRequestType, omci.TransactionID(tid)) |
| 124 | if err != nil { |
| 125 | omciLogger.WithField("err", err).Fatalf("Can't generate EnableUniRequest") |
| 126 | } |
| 127 | return hexEncode(pkt) |
| 128 | } |
| 129 | |
| 130 | func CreateGemPortRequest(tid uint16) ([]byte, error) { |
| 131 | params := me.ParamData{ |
| 132 | EntityID: gemEID, |
| 133 | Attributes: me.AttributeValueMap{ |
| 134 | "PortId": 1, |
| 135 | "TContPointer": 1, |
| 136 | "Direction": 0, |
| 137 | "TrafficManagementPointerForUpstream": 0, |
| 138 | "TrafficDescriptorProfilePointerForUpstream": 0, |
| 139 | "UniCounter": 0, |
| 140 | "PriorityQueuePointerForDownStream": 0, |
| 141 | "EncryptionState": 0, |
| 142 | "TrafficDescriptorProfilePointerForDownstream": 0, |
| 143 | "EncryptionKeyRing": 0, |
| 144 | }, |
| 145 | } |
| 146 | meDef, _ := me.NewGemPortNetworkCtp(params) |
| 147 | pkt, err := omci.GenFrame(meDef, omci.CreateRequestType, omci.TransactionID(tid)) |
| 148 | if err != nil { |
| 149 | omciLogger.WithField("err", err).Fatalf("Can't generate GemPortRequest") |
| 150 | } |
| 151 | return hexEncode(pkt) |
| 152 | } |