Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 2 | * Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 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 | |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 17 | // Package common provides global definitions |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 18 | package common |
| 19 | |
| 20 | import ( |
| 21 | "bytes" |
| 22 | "context" |
| 23 | "encoding/binary" |
| 24 | "errors" |
| 25 | "fmt" |
| 26 | "net" |
| 27 | "regexp" |
| 28 | "strconv" |
| 29 | "strings" |
mpagenko | 101ac94 | 2021-11-16 15:01:29 +0000 | [diff] [blame] | 30 | "time" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 31 | |
| 32 | "github.com/looplab/fsm" |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 33 | me "github.com/opencord/omci-lib-go/v2/generated" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 34 | "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. |
| 40 | func 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 Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 55 | // IPToInt32 transforms an IP of net.Ip type to int32 |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 56 | func 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 Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 63 | // AsByteSlice transforms a string of manually set bits to a byt array |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 64 | func 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 |
| 84 | func 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 |
| 94 | func TrimStringFromMeOctet(input interface{}) string { |
| 95 | ifBytes, _ := me.InterfaceToOctets(input) |
| 96 | return fmt.Sprintf("%s", bytes.Trim(ifBytes, "\x00")) |
| 97 | } |
| 98 | |
| 99 | //////////////////////////////////////////////////////////////////////// |
| 100 | |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 101 | // NewAdapterFsm - FSM details including event, device and channel. |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 102 | func 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 |
| 112 | func (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 |
| 120 | func 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 |
| 131 | func 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 |
| 142 | func 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 |
| 150 | func 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 | } |
ozgecanetsia | 0e3111f | 2021-10-19 18:04:15 +0300 | [diff] [blame] | 157 | |
| 158 | // GenerateVoipUNISideMEID return VoipUNISideMEEntityID |
| 159 | func 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 | } |
mpagenko | 101ac94 | 2021-11-16 15:01:29 +0000 | [diff] [blame] | 165 | |
Joey Armstrong | 89c812c | 2024-01-12 19:00:20 -0500 | [diff] [blame] | 166 | // WaitTimeout of waitGroupWithTimeOut is blocking |
| 167 | // |
| 168 | // returns true, if the wg request was executed successfully, false on timeout |
mpagenko | 101ac94 | 2021-11-16 15:01:29 +0000 | [diff] [blame] | 169 | func (wg *WaitGroupWithTimeOut) WaitTimeout(timeout time.Duration) bool { |
| 170 | done := make(chan struct{}) |
| 171 | |
| 172 | go func() { |
| 173 | defer close(done) |
| 174 | wg.Wait() |
| 175 | }() |
| 176 | |
| 177 | select { |
| 178 | case <-done: |
| 179 | return true |
| 180 | |
| 181 | case <-time.After(timeout): |
| 182 | return false |
| 183 | } |
| 184 | } |