Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package util |
| 17 | |
| 18 | import ( |
| 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. |
| 27 | func 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 42 | // IsSliceSame - check and return true if the two slices are identical |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 43 | func IsSliceSame(ref, rcvd []uint32) bool { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 44 | 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 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 65 | // IsPbitSliceSame - check and return true if the two slices are identical |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 66 | func IsPbitSliceSame(ref, rcvd []of.PbitType) bool { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 67 | 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. |
| 89 | func IsNniPort(id uint32) bool { |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 90 | return (id >= 0x1000000) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Uint32ToByte to convert uint32 to byte |
| 94 | func 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. |
| 101 | func 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. |
| 109 | func 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 |
| 116 | func 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 |
| 136 | func 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 | } |