blob: 6153283712bb0a1aef9d46e45adc8163fb1d17a2 [file] [log] [blame]
Matteo Scandolo40e067f2019-10-16 16:59:41 -07001/*
2 * Copyright 2018-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
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
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070023 "github.com/opencord/voltha-protos/v4/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}