blob: 8ea2624e2ce98e2c29da069731804ba363750458 [file] [log] [blame]
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001/*
2 * Copyright 2020-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 common provides global definitions
18package common
19
20import (
21 "bytes"
22 "context"
23 "encoding/binary"
24 "errors"
25 "fmt"
26 "net"
27 "regexp"
28 "strconv"
29 "strings"
30
31 "github.com/looplab/fsm"
32 me "github.com/opencord/omci-lib-go/generated"
33 "github.com/opencord/voltha-lib-go/v7/pkg/log"
34)
35
36// GetTpIDFromTpPath extracts TpID from the TpPath.
37// On success it returns a valid TpID and nil error.
38// On failure it returns TpID as 0 and the error.
39func GetTpIDFromTpPath(tpPath string) (uint8, error) {
40 // tpPath is of the format <technology>/<table_id>/olt-{}/pon-{}/onu-{}/uni-{}
41 // A sample tpPath is ==> XGS-PON/64/olt-{12345abcd}/pon-{0}/onu-{1}/uni-{1}
42 var tpPathFormat = regexp.MustCompile(`^[a-zA-Z\-_]+/[0-9]+/olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
43
44 // Ensure tpPath is of the format <technology>/<table_id>/<uni_port_name>
45 if !tpPathFormat.Match([]byte(tpPath)) {
46 return 0, errors.New("tp-path-not-confirming-to-format")
47 }
48 // Extract the TP table-id field.
49 tpID, err := strconv.Atoi(strings.Split(tpPath, "/")[1])
50 // Atoi returns uint64 and need to be type-casted to uint8 as tpID is uint8 size.
51 return uint8(tpID), err
52}
53
54//IPToInt32 transforms an IP of net.Ip type to int32
55func IPToInt32(ip net.IP) uint32 {
56 if len(ip) == 16 {
57 return binary.BigEndian.Uint32(ip[12:16])
58 }
59 return binary.BigEndian.Uint32(ip)
60}
61
62//AsByteSlice transforms a string of manually set bits to a byt array
63func AsByteSlice(bitString string) []byte {
64 var out []byte
65 var str string
66
67 for i := len(bitString); i > 0; i -= 8 {
68 if i-8 < 0 {
69 str = bitString[0:i]
70 } else {
71 str = bitString[i-8 : i]
72 }
73 v, err := strconv.ParseUint(str, 2, 8)
74 if err != nil {
75 panic(err)
76 }
77 out = append([]byte{byte(v)}, out...)
78 }
79 return out
80}
81
82// TwosComplementToSignedInt16 convert 2s complement to signed int16
83func TwosComplementToSignedInt16(val uint16) int16 {
84 var uint16MsbMask uint16 = 0x8000
85 if val&uint16MsbMask == uint16MsbMask {
86 return int16(^val+1) * -1
87 }
88
89 return int16(val)
90}
91
92// TrimStringFromMeOctet trim string out of Me octet
93func TrimStringFromMeOctet(input interface{}) string {
94 ifBytes, _ := me.InterfaceToOctets(input)
95 return fmt.Sprintf("%s", bytes.Trim(ifBytes, "\x00"))
96}
97
98////////////////////////////////////////////////////////////////////////
99
100//NewAdapterFsm - FSM details including event, device and channel.
101func NewAdapterFsm(aName string, aDeviceID string, aCommChannel chan Message) *AdapterFsm {
102 aFsm := &AdapterFsm{
103 fsmName: aName,
104 deviceID: aDeviceID,
105 CommChan: aCommChannel,
106 }
107 return aFsm
108}
109
110// LogFsmStateChange logs FSM state changes
111func (oo *AdapterFsm) LogFsmStateChange(ctx context.Context, e *fsm.Event) {
112 logger.Debugw(ctx, "FSM state change", log.Fields{"device-id": oo.deviceID, "FSM name": oo.fsmName,
113 "event name": string(e.Event), "src state": string(e.Src), "dst state": string(e.Dst)})
114}
115
116////////////////////////////////////////////////////////////////////////
117
118// GenerateIeeMaperServiceProfileEID returns IeeMaperServiceProfileEntityID
119func GenerateIeeMaperServiceProfileEID(uniPortMacBpNo uint16, tpID uint16) (uint16, error) {
120 if tpID < tpIDStart || tpID >= tpIDEnd {
121 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
122 }
123 if uniPortMacBpNo > maxUni {
124 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
125 }
126 return (IeeMaperServiceProfileBaseEID + uniPortMacBpNo*tpRange + tpID - tpIDStart), nil
127}
128
129// GenerateANISideMBPCDEID returns ANISideMacBridgePortConfigurationDataEntryID
130func GenerateANISideMBPCDEID(uniPortMacBpNo uint16, tpID uint16) (uint16, error) {
131 if tpID < tpIDStart || tpID >= tpIDEnd {
132 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
133 }
134 if uniPortMacBpNo > maxUni {
135 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
136 }
137 return (MacBridgePortAniBaseEID + uniPortMacBpNo*tpRange + tpID - tpIDStart), nil
138}
139
140// GenerateUNISideMBPCDEID returns UNISideMacBridgePortConfigurationDataEntityID
141func GenerateUNISideMBPCDEID(uniPortMacBpNo uint16) (uint16, error) {
142 if uniPortMacBpNo > maxUni {
143 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
144 }
145 return (MacBridgePortUniBaseEID + uniPortMacBpNo), nil
146}
147
148// GenerateMcastANISideMBPCDEID returns McastANISideMacBridgePortConfigurationDataEntityID
149func GenerateMcastANISideMBPCDEID(uniPortMacBpNo uint16) (uint16, error) {
150
151 if uniPortMacBpNo > maxUni {
152 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
153 }
154 return (MacBridgePortAniMcastBaseEID + uniPortMacBpNo), nil
155}