blob: 0711053d0e9bd5d5df2e0246e16d4625b61db4b1 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16package util
17
18import (
19 "encoding/binary"
20 "net"
21 "strings"
22
23 "voltha-go-controller/internal/pkg/of"
24)
25
26// RemoveFromSlice to remove particular value from given slice.
27func RemoveFromSlice(s []string, value string) []string {
28 i := 0
29 for i = 0; i < len(s); i++ {
30 if s[i] == value {
31 break
32 }
33 }
34 if i != len(s) {
35 //It means value is found in the slice
36 s[len(s)-1], s[i] = s[i], s[len(s)-1]
37 return s[:len(s)-1]
38 }
39 return s
40}
41
42//IsSliceSame - check and return true if the two slices are identical
43func IsSliceSame(ref, rcvd []uint32) bool {
44
45 var found bool
46 if len(ref) != len(rcvd) {
47 return false
48 }
49
50 for _, refEntry := range ref {
51 found = false
52
53 for _, rcvdEntry := range rcvd {
54 if refEntry == rcvdEntry {
55 found = true
56 break
57 }
58 }
59 if !found {
60 return false
61 }
62 }
63 return true
64}
65
66//IsPbitSliceSame - check and return true if the two slices are identical
67func IsPbitSliceSame(ref, rcvd []of.PbitType) bool {
68
69 var found bool
70 if len(ref) != len(rcvd) {
71 return false
72 }
73
74 for _, refEntry := range ref {
75 found = false
76
77 for _, rcvdEntry := range rcvd {
78 if refEntry == rcvdEntry {
79 found = true
80 break
81 }
82 }
83 if !found {
84 return false
85 }
86 }
87 return true
88}
89
90// IsNniPort is to check if given port is Nni Port.
91func IsNniPort(id uint32) bool {
92 return (id >= 0x100000)
93}
94
95// Uint32ToByte to convert uint32 to byte
96func Uint32ToByte(value uint32) []byte {
97 byteValue := make([]byte, 4)
98 binary.BigEndian.PutUint32(byteValue[0:4], value)
99 return byteValue
100}
101
102// IP2LongConv convert ip address to integer value.
103func IP2LongConv(ip net.IP) uint32 {
104 if len(ip) == 16 {
105 return binary.BigEndian.Uint32(ip[12:16])
106 }
107 return binary.BigEndian.Uint32(ip)
108}
109
110// Long2ipConv convert integer to ip address.
111func Long2ipConv(nn uint32) net.IP {
112 ip := make(net.IP, 4)
113 binary.BigEndian.PutUint32(ip, nn)
114 return ip
115}
116
117// GetExpIPList converts list or range of IPs to expanded IP list
118func GetExpIPList(ips []string) []net.IP {
119 ipList := []net.IP{}
120
121 for _, ipOrRange := range ips {
122 if strings.Contains(ipOrRange, "-") {
123 var splits = strings.Split(ipOrRange, "-")
124 ipStart := IP2LongConv(net.ParseIP(splits[0]))
125 ipEnd := IP2LongConv(net.ParseIP(splits[1]))
126
127 for i := ipStart; i <= ipEnd; i++ {
128 ipList = append(ipList, Long2ipConv(i))
129 }
130 } else {
131 ipList = append(ipList, net.ParseIP(ipOrRange))
132 }
133 }
134 return ipList
135}
136
137// MacAddrsMatch for comparison of MAC addresses and return true if MAC addresses matches
138func MacAddrsMatch(addr1 net.HardwareAddr, addr2 net.HardwareAddr) bool {
139 if len(addr1) != len(addr2) {
140 return false
141 }
142 for i := 0; i < len(addr1); i++ {
143 if addr1[i] != addr2[i] {
144 return false
145 }
146 }
147 return true
148}