Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 1 | /* |
| 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 |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
Holger Hildebrandt | fb402a6 | 2021-05-26 14:40:49 +0000 | [diff] [blame] | 21 | "bytes" |
ozgecanetsia | 4b23230 | 2020-11-11 10:58:10 +0300 | [diff] [blame] | 22 | "encoding/binary" |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 23 | "errors" |
Holger Hildebrandt | fb402a6 | 2021-05-26 14:40:49 +0000 | [diff] [blame] | 24 | "fmt" |
ozgecanetsia | 4b23230 | 2020-11-11 10:58:10 +0300 | [diff] [blame] | 25 | "net" |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 26 | "regexp" |
| 27 | "strconv" |
| 28 | "strings" |
Holger Hildebrandt | fb402a6 | 2021-05-26 14:40:49 +0000 | [diff] [blame] | 29 | |
| 30 | me "github.com/opencord/omci-lib-go/generated" |
Girish Gowdra | 041dcb3 | 2020-11-16 16:54:30 -0800 | [diff] [blame] | 31 | ) |
| 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. |
| 36 | func 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 | } |
ozgecanetsia | 4b23230 | 2020-11-11 10:58:10 +0300 | [diff] [blame] | 50 | |
| 51 | //IPToInt32 transforms an IP of net.Ip type to int32 |
| 52 | func 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 | } |
ozgecanetsia | b5000ef | 2020-11-27 14:38:20 +0300 | [diff] [blame] | 58 | |
| 59 | //AsByteSlice transforms a string of manually set bits to a byt array |
| 60 | func 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 Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 78 | |
| 79 | // TwosComplementToSignedInt16 convert 2s complement to signed int16 |
| 80 | func 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 Hildebrandt | fb402a6 | 2021-05-26 14:40:49 +0000 | [diff] [blame] | 88 | |
| 89 | // TrimStringFromMeOctet trim string out of Me octet |
| 90 | func TrimStringFromMeOctet(input interface{}) string { |
| 91 | ifBytes, _ := me.InterfaceToOctets(input) |
| 92 | return fmt.Sprintf("%s", bytes.Trim(ifBytes, "\x00")) |
| 93 | } |