blob: 51e5e810d3edf8158fe19dca59bb54312478a419 [file] [log] [blame]
Girish Gowdra041dcb32020-11-16 16:54:30 -08001/*
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 adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
19
20import (
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000021 "bytes"
ozgecanetsia4b232302020-11-11 10:58:10 +030022 "encoding/binary"
Girish Gowdra041dcb32020-11-16 16:54:30 -080023 "errors"
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000024 "fmt"
ozgecanetsia4b232302020-11-11 10:58:10 +030025 "net"
Girish Gowdra041dcb32020-11-16 16:54:30 -080026 "regexp"
27 "strconv"
28 "strings"
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000029
30 me "github.com/opencord/omci-lib-go/generated"
Girish Gowdra041dcb32020-11-16 16:54:30 -080031)
32
33// GetTpIDFromTpPath extracts TpID from the TpPath.
34// On success it returns a valid TpID and nil error.
35// On failure it returns TpID as 0 and the error.
36func GetTpIDFromTpPath(tpPath string) (uint8, error) {
37 // tpPath is of the format <technology>/<table_id>/olt-{}/pon-{}/onu-{}/uni-{}
38 // A sample tpPath is ==> XGS-PON/64/olt-{12345abcd}/pon-{0}/onu-{1}/uni-{1}
39 var tpPathFormat = regexp.MustCompile(`^[a-zA-Z\-_]+/[0-9]+/olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`)
40
41 // Ensure tpPath is of the format <technology>/<table_id>/<uni_port_name>
42 if !tpPathFormat.Match([]byte(tpPath)) {
43 return 0, errors.New("tp-path-not-confirming-to-format")
44 }
45 // Extract the TP table-id field.
46 tpID, err := strconv.Atoi(strings.Split(tpPath, "/")[1])
47 // Atoi returns uint64 and need to be type-casted to uint8 as tpID is uint8 size.
48 return uint8(tpID), err
49}
ozgecanetsia4b232302020-11-11 10:58:10 +030050
51//IPToInt32 transforms an IP of net.Ip type to int32
52func IPToInt32(ip net.IP) uint32 {
53 if len(ip) == 16 {
54 return binary.BigEndian.Uint32(ip[12:16])
55 }
56 return binary.BigEndian.Uint32(ip)
57}
ozgecanetsiab5000ef2020-11-27 14:38:20 +030058
59//AsByteSlice transforms a string of manually set bits to a byt array
60func AsByteSlice(bitString string) []byte {
61 var out []byte
62 var str string
63
64 for i := len(bitString); i > 0; i -= 8 {
65 if i-8 < 0 {
66 str = bitString[0:i]
67 } else {
68 str = bitString[i-8 : i]
69 }
70 v, err := strconv.ParseUint(str, 2, 8)
71 if err != nil {
72 panic(err)
73 }
74 out = append([]byte{byte(v)}, out...)
75 }
76 return out
77}
Girish Gowdra6afb56a2021-04-27 17:47:57 -070078
79// TwosComplementToSignedInt16 convert 2s complement to signed int16
80func TwosComplementToSignedInt16(val uint16) int16 {
81 var uint16MsbMask uint16 = 0x8000
82 if val&uint16MsbMask == uint16MsbMask {
83 return int16(^val+1) * -1
84 }
85
86 return int16(val)
87}
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000088
89// TrimStringFromMeOctet trim string out of Me octet
90func TrimStringFromMeOctet(input interface{}) string {
91 ifBytes, _ := me.InterfaceToOctets(input)
92 return fmt.Sprintf("%s", bytes.Trim(ifBytes, "\x00"))
93}