blob: b11785cc872a68104ef9dbe358ae180c293b366d [file] [log] [blame]
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2020-2024 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
Joey Armstrong89c812c2024-01-12 19:00:20 -050017// Package common provides global definitions
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000018package 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
Joey Armstrong89c812c2024-01-12 19:00:20 -050055// IPToInt32 transforms an IP of net.Ip type to int32
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000056func 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
Joey Armstrong89c812c2024-01-12 19:00:20 -050063// AsByteSlice transforms a string of manually set bits to a byt array
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000064func 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)
bseenivaf11b2642025-09-30 13:48:25 +053096 ifBytes, _, _ = bytes.Cut(ifBytes, []byte{0})
97 return string(ifBytes)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000098}
99
100////////////////////////////////////////////////////////////////////////
101
Joey Armstrong89c812c2024-01-12 19:00:20 -0500102// NewAdapterFsm - FSM details including event, device and channel.
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000103func NewAdapterFsm(aName string, aDeviceID string, aCommChannel chan Message) *AdapterFsm {
104 aFsm := &AdapterFsm{
105 fsmName: aName,
106 deviceID: aDeviceID,
107 CommChan: aCommChannel,
108 }
109 return aFsm
110}
111
112// LogFsmStateChange logs FSM state changes
113func (oo *AdapterFsm) LogFsmStateChange(ctx context.Context, e *fsm.Event) {
114 logger.Debugw(ctx, "FSM state change", log.Fields{"device-id": oo.deviceID, "FSM name": oo.fsmName,
115 "event name": string(e.Event), "src state": string(e.Src), "dst state": string(e.Dst)})
116}
117
118////////////////////////////////////////////////////////////////////////
119
120// GenerateIeeMaperServiceProfileEID returns IeeMaperServiceProfileEntityID
121func GenerateIeeMaperServiceProfileEID(uniPortMacBpNo uint16, tpID uint16) (uint16, error) {
122 if tpID < tpIDStart || tpID >= tpIDEnd {
123 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
124 }
125 if uniPortMacBpNo > maxUni {
126 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
127 }
128 return (IeeMaperServiceProfileBaseEID + uniPortMacBpNo*tpRange + tpID - tpIDStart), nil
129}
130
131// GenerateANISideMBPCDEID returns ANISideMacBridgePortConfigurationDataEntryID
132func GenerateANISideMBPCDEID(uniPortMacBpNo uint16, tpID uint16) (uint16, error) {
133 if tpID < tpIDStart || tpID >= tpIDEnd {
134 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
135 }
136 if uniPortMacBpNo > maxUni {
137 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
138 }
139 return (MacBridgePortAniBaseEID + uniPortMacBpNo*tpRange + tpID - tpIDStart), nil
140}
141
142// GenerateUNISideMBPCDEID returns UNISideMacBridgePortConfigurationDataEntityID
143func GenerateUNISideMBPCDEID(uniPortMacBpNo uint16) (uint16, error) {
144 if uniPortMacBpNo > maxUni {
145 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
146 }
147 return (MacBridgePortUniBaseEID + uniPortMacBpNo), nil
148}
149
150// GenerateMcastANISideMBPCDEID returns McastANISideMacBridgePortConfigurationDataEntityID
151func GenerateMcastANISideMBPCDEID(uniPortMacBpNo uint16) (uint16, error) {
152
153 if uniPortMacBpNo > maxUni {
154 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
155 }
156 return (MacBridgePortAniMcastBaseEID + uniPortMacBpNo), nil
157}
ozgecanetsia0e3111f2021-10-19 18:04:15 +0300158
159// GenerateVoipUNISideMEID return VoipUNISideMEEntityID
160func GenerateVoipUNISideMEID(uniPortMacBpNo uint16) (uint16, error) {
161 if uniPortMacBpNo > maxUni {
162 return 0, fmt.Errorf("uni macbpno out of range - %d", uniPortMacBpNo)
163 }
164 return (VoipUniBaseEID + uniPortMacBpNo), nil
165}
mpagenko101ac942021-11-16 15:01:29 +0000166
Joey Armstrong89c812c2024-01-12 19:00:20 -0500167// WaitTimeout of waitGroupWithTimeOut is blocking
168//
169// returns true, if the wg request was executed successfully, false on timeout
mpagenko101ac942021-11-16 15:01:29 +0000170func (wg *WaitGroupWithTimeOut) WaitTimeout(timeout time.Duration) bool {
171 done := make(chan struct{})
172
173 go func() {
174 defer close(done)
175 wg.Wait()
176 }()
177
178 select {
179 case <-done:
180 return true
181
182 case <-time.After(timeout):
183 return false
184 }
185}
bseeniva5f32c452025-07-18 17:30:41 +0530186
187// GenerateANISideMBPCDPortNo returns ANISideMacBridgePortConfigurationDataPortNo
188func GenerateANISideMBPCDPortNo(tpID uint16) (uint16, error) {
189 if tpID < tpIDStart || tpID >= tpIDEnd {
190 return 0, fmt.Errorf("tech profile id out of range - %d", tpID)
191 }
192 return tpID - tpIDStart, nil
193}