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 ( |
| 21 | "errors" |
| 22 | "regexp" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | // GetTpIDFromTpPath extracts TpID from the TpPath. |
| 28 | // On success it returns a valid TpID and nil error. |
| 29 | // On failure it returns TpID as 0 and the error. |
| 30 | func GetTpIDFromTpPath(tpPath string) (uint8, error) { |
| 31 | // tpPath is of the format <technology>/<table_id>/olt-{}/pon-{}/onu-{}/uni-{} |
| 32 | // A sample tpPath is ==> XGS-PON/64/olt-{12345abcd}/pon-{0}/onu-{1}/uni-{1} |
| 33 | var tpPathFormat = regexp.MustCompile(`^[a-zA-Z\-_]+/[0-9]+/olt-{[a-z0-9\-]+}/pon-{[0-9]+}/onu-{[0-9]+}/uni-{[0-9]+}$`) |
| 34 | |
| 35 | // Ensure tpPath is of the format <technology>/<table_id>/<uni_port_name> |
| 36 | if !tpPathFormat.Match([]byte(tpPath)) { |
| 37 | return 0, errors.New("tp-path-not-confirming-to-format") |
| 38 | } |
| 39 | // Extract the TP table-id field. |
| 40 | tpID, err := strconv.Atoi(strings.Split(tpPath, "/")[1]) |
| 41 | // Atoi returns uint64 and need to be type-casted to uint8 as tpID is uint8 size. |
| 42 | return uint8(tpID), err |
| 43 | } |