Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [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 | |
| 17 | package common |
| 18 | |
| 19 | import ( |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 20 | "net" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 21 | "strconv" |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 22 | |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func OnuSnToString(sn *openolt.SerialNumber) string { |
| 27 | s := string(sn.VendorId) |
| 28 | for _, i := range sn.VendorSpecific { |
| 29 | s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16) |
| 30 | } |
| 31 | return s |
| 32 | } |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 33 | |
| 34 | // GetIPAddr returns the IPv4 address of an interface. 0.0.0.0 is returned if the IP cannot be determined. |
| 35 | func GetIPAddr(ifname string) (string, error) { |
| 36 | ip := "0.0.0.0" |
| 37 | |
| 38 | intf, err := net.InterfaceByName(ifname) |
| 39 | if err != nil { |
| 40 | return ip, err |
| 41 | } |
| 42 | |
| 43 | addrs, err := intf.Addrs() |
| 44 | if err != nil { |
| 45 | return ip, err |
| 46 | } |
| 47 | |
| 48 | for _, addr := range addrs { |
| 49 | // get first IPv4 address |
| 50 | switch v := addr.(type) { |
| 51 | case *net.IPNet: |
| 52 | if v.IP.To4() != nil { |
| 53 | ip = v.IP.String() |
| 54 | break |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return ip, nil |
| 60 | } |