blob: 91984d44afaf6b81d72942fc561a7bf41553d268 [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo40e067f2019-10-16 16:59:41 -07003
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
17package common
18
19import (
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010020 "net"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070021 "strconv"
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010022
David K. Bainbridgec415efe2021-08-19 13:05:21 +000023 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070024)
25
26func 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 Bozakov958d81c2019-12-13 22:09:48 +010033
34// GetIPAddr returns the IPv4 address of an interface. 0.0.0.0 is returned if the IP cannot be determined.
35func 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}