blob: 615741363136a6d1a9dc22ef9c26617894f2acd8 [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.
Akash Soni53da2852023-03-15 00:31:31 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
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
vinokuma926cb3e2023-03-29 11:41:06 +053042// IsSliceSame - check and return true if the two slices are identical
Naveen Sampath04696f72022-06-13 15:19:14 +053043func IsSliceSame(ref, rcvd []uint32) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +053044 var found bool
45 if len(ref) != len(rcvd) {
46 return false
47 }
48
49 for _, refEntry := range ref {
50 found = false
51
52 for _, rcvdEntry := range rcvd {
53 if refEntry == rcvdEntry {
54 found = true
55 break
56 }
57 }
58 if !found {
59 return false
60 }
61 }
62 return true
63}
64
vinokuma926cb3e2023-03-29 11:41:06 +053065// IsPbitSliceSame - check and return true if the two slices are identical
Naveen Sampath04696f72022-06-13 15:19:14 +053066func IsPbitSliceSame(ref, rcvd []of.PbitType) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +053067 var found bool
68 if len(ref) != len(rcvd) {
69 return false
70 }
71
72 for _, refEntry := range ref {
73 found = false
74
75 for _, rcvdEntry := range rcvd {
76 if refEntry == rcvdEntry {
77 found = true
78 break
79 }
80 }
81 if !found {
82 return false
83 }
84 }
85 return true
86}
87
88// IsNniPort is to check if given port is Nni Port.
89func IsNniPort(id uint32) bool {
Akash Soni53da2852023-03-15 00:31:31 +053090 return (id >= 0x1000000)
Naveen Sampath04696f72022-06-13 15:19:14 +053091}
92
93// Uint32ToByte to convert uint32 to byte
94func Uint32ToByte(value uint32) []byte {
95 byteValue := make([]byte, 4)
96 binary.BigEndian.PutUint32(byteValue[0:4], value)
97 return byteValue
98}
99
100// IP2LongConv convert ip address to integer value.
101func IP2LongConv(ip net.IP) uint32 {
102 if len(ip) == 16 {
103 return binary.BigEndian.Uint32(ip[12:16])
104 }
105 return binary.BigEndian.Uint32(ip)
106}
107
108// Long2ipConv convert integer to ip address.
109func Long2ipConv(nn uint32) net.IP {
110 ip := make(net.IP, 4)
111 binary.BigEndian.PutUint32(ip, nn)
112 return ip
113}
114
115// GetExpIPList converts list or range of IPs to expanded IP list
116func GetExpIPList(ips []string) []net.IP {
117 ipList := []net.IP{}
118
119 for _, ipOrRange := range ips {
120 if strings.Contains(ipOrRange, "-") {
121 var splits = strings.Split(ipOrRange, "-")
122 ipStart := IP2LongConv(net.ParseIP(splits[0]))
123 ipEnd := IP2LongConv(net.ParseIP(splits[1]))
124
125 for i := ipStart; i <= ipEnd; i++ {
126 ipList = append(ipList, Long2ipConv(i))
127 }
128 } else {
129 ipList = append(ipList, net.ParseIP(ipOrRange))
130 }
131 }
132 return ipList
133}
134
135// MacAddrsMatch for comparison of MAC addresses and return true if MAC addresses matches
136func MacAddrsMatch(addr1 net.HardwareAddr, addr2 net.HardwareAddr) bool {
137 if len(addr1) != len(addr2) {
138 return false
139 }
140 for i := 0; i < len(addr1); i++ {
141 if addr1[i] != addr2[i] {
142 return false
143 }
144 }
145 return true
146}