blob: a82a3a2d4b5a9273a9cbba8ff8d596a048fd11bc [file] [log] [blame]
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001/*
Joey Armstronge8c091f2023-01-17 16:56:26 -05002 * Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00003 *
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"
mpagenko101ac942021-11-16 15:01:29 +000030 "time"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000031
32 "github.com/looplab/fsm"
mpagenko836a1fd2021-11-01 16:12:42 +000033 me "github.com/opencord/omci-lib-go/v2/generated"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000034 "github.com/opencord/voltha-lib-go/v7/pkg/log"
35)
36
37// GetTpIDFromTpPath extracts TpID from the TpPath.
38// On success it returns a valid TpID and nil error.
39// On failure it returns TpID as 0 and the error.
40func GetTpIDFromTpPath(tpPath string) (uint8, error) {
41 // tpPath is of the format <technology>/<table_id>/olt-{}/pon-{}/onu-{}/uni-{}
42 // A sample tpPath is ==> XGS-PON/64/olt-{12345abcd}/pon-{0}/onu-{1}/uni-{1}
43 var tpPathFormat = regexp.MustCompile(`^[a-zA-Z\-_]+/[0-9]+/olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
44
45 // Ensure tpPath is of the format <technology>/<table_id>/<uni_port_name>
46 if !tpPathFormat.Match([]byte(tpPath)) {
47 return 0, errors.New("tp-path-not-confirming-to-format")
48 }
49 // Extract the TP table-id field.
50 tpID, err := strconv.Atoi(strings.Split(tpPath, "/")[1])
51 // Atoi returns uint64 and need to be type-casted to uint8 as tpID is uint8 size.
52 return uint8(tpID), err
53}
54
55//IPToInt32 transforms an IP of net.Ip type to int32
56func IPToInt32(ip net.IP) uint32 {
57 if len(ip) == 16 {
58 return binary.BigEndian.Uint32(ip[12:16])
59 }
60 return binary.BigEndian.Uint32(ip)
61}
62
63//AsByteSlice transforms a string of manually set bits to a byt array
64func AsByteSlice(bitString string) []byte {
65 var out []byte
66 var str string
67
68 for i := len(bitString); i > 0; i -= 8 {
69 if i-8 < 0 {
70 str = bitString[0:i]
71 } else {
72 str = bitString[i-8 : i]
73 }
74 v, err := strconv.ParseUint(str, 2, 8)
75 if err != nil {
76 panic(err)
77 }
78 out = append([]byte{byte(v)}, out...)
79 }
80 return out
81}
82
83// TwosComplementToSignedInt16 convert 2s complement to signed int16
84func TwosComplementToSignedInt16(val uint16) int16 {
85 var uint16MsbMask uint16 = 0x8000
86 if val&uint16MsbMask == uint16MsbMask {
87 return int16(^val+1) * -1
88 }
89
90 return int16(val)
91}
92
93// TrimStringFromMeOctet trim string out of Me octet
94func TrimStringFromMeOctet(input interface{}) string {
95 ifBytes, _ := me.InterfaceToOctets(input)
96 return fmt.Sprintf("%s", bytes.Trim(ifBytes, "\x00"))
97}
98
99////////////////////////////////////////////////////////////////////////
100
101//NewAdapterFsm - FSM details including event, device and channel.
102func NewAdapterFsm(aName string, aDeviceID string, aCommChannel chan Message) *AdapterFsm {
103 aFsm := &AdapterFsm{
104 fsmName: aName,
105 deviceID: aDeviceID,
106 CommChan: aCommChannel,
107 }
108 return aFsm
109}
110
111// LogFsmStateChange logs FSM state changes
112func (oo *AdapterFsm) LogFsmStateChange(ctx context.Context, e *fsm.Event) {
113 logger.Debugw(ctx, "FSM state change", log.Fields{"device-id": oo.deviceID, "FSM name": oo.fsmName,
114 "event name": string(e.Event), "src state": string(e.Src), "dst state": string(e.Dst)})
115}
116
117////////////////////////////////////////////////////////////////////////
118
119// GenerateIeeMaperServiceProfileEID returns IeeMaperServiceProfileEntityID
120func GenerateIeeMaperServiceProfileEID(uniPortMacBpNo uint16, tpID uint16) (uint16, error) {
121 if tpID < tpIDStart || tpID >= tpIDEnd {
122 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
123 }
124 if uniPortMacBpNo > maxUni {
125 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
126 }
127 return (IeeMaperServiceProfileBaseEID + uniPortMacBpNo*tpRange + tpID - tpIDStart), nil
128}
129
130// GenerateANISideMBPCDEID returns ANISideMacBridgePortConfigurationDataEntryID
131func GenerateANISideMBPCDEID(uniPortMacBpNo uint16, tpID uint16) (uint16, error) {
132 if tpID < tpIDStart || tpID >= tpIDEnd {
133 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
134 }
135 if uniPortMacBpNo > maxUni {
136 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
137 }
138 return (MacBridgePortAniBaseEID + uniPortMacBpNo*tpRange + tpID - tpIDStart), nil
139}
140
141// GenerateUNISideMBPCDEID returns UNISideMacBridgePortConfigurationDataEntityID
142func GenerateUNISideMBPCDEID(uniPortMacBpNo uint16) (uint16, error) {
143 if uniPortMacBpNo > maxUni {
144 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
145 }
146 return (MacBridgePortUniBaseEID + uniPortMacBpNo), nil
147}
148
149// GenerateMcastANISideMBPCDEID returns McastANISideMacBridgePortConfigurationDataEntityID
150func GenerateMcastANISideMBPCDEID(uniPortMacBpNo uint16) (uint16, error) {
151
152 if uniPortMacBpNo > maxUni {
153 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
154 }
155 return (MacBridgePortAniMcastBaseEID + uniPortMacBpNo), nil
156}
ozgecanetsia0e3111f2021-10-19 18:04:15 +0300157
158// GenerateVoipUNISideMEID return VoipUNISideMEEntityID
159func GenerateVoipUNISideMEID(uniPortMacBpNo uint16) (uint16, error) {
160 if uniPortMacBpNo > maxUni {
161 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
162 }
163 return (VoipUniBaseEID + uniPortMacBpNo), nil
164}
mpagenko101ac942021-11-16 15:01:29 +0000165
166//WaitTimeout of waitGroupWithTimeOut is blocking
167// returns true, if the wg request was executed successfully, false on timeout
168func (wg *WaitGroupWithTimeOut) WaitTimeout(timeout time.Duration) bool {
169 done := make(chan struct{})
170
171 go func() {
172 defer close(done)
173 wg.Wait()
174 }()
175
176 select {
177 case <-done:
178 return true
179
180 case <-time.After(timeout):
181 return false
182 }
183}