blob: 36aab0be62c36b9ae06824791b7c905fd8efb609 [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"
Akash Sonief452f12024-12-12 18:20:28 +053021 "strconv"
Naveen Sampath04696f72022-06-13 15:19:14 +053022 "strings"
23
24 "voltha-go-controller/internal/pkg/of"
25)
26
27// RemoveFromSlice to remove particular value from given slice.
28func RemoveFromSlice(s []string, value string) []string {
29 i := 0
30 for i = 0; i < len(s); i++ {
31 if s[i] == value {
32 break
33 }
34 }
35 if i != len(s) {
36 //It means value is found in the slice
37 s[len(s)-1], s[i] = s[i], s[len(s)-1]
38 return s[:len(s)-1]
39 }
40 return s
41}
42
vinokuma926cb3e2023-03-29 11:41:06 +053043// IsSliceSame - check and return true if the two slices are identical
Naveen Sampath04696f72022-06-13 15:19:14 +053044func IsSliceSame(ref, rcvd []uint32) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +053045 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
vinokuma926cb3e2023-03-29 11:41:06 +053066// IsPbitSliceSame - check and return true if the two slices are identical
Naveen Sampath04696f72022-06-13 15:19:14 +053067func IsPbitSliceSame(ref, rcvd []of.PbitType) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +053068 var found bool
69 if len(ref) != len(rcvd) {
70 return false
71 }
72
73 for _, refEntry := range ref {
74 found = false
75
76 for _, rcvdEntry := range rcvd {
77 if refEntry == rcvdEntry {
78 found = true
79 break
80 }
81 }
82 if !found {
83 return false
84 }
85 }
86 return true
87}
88
89// IsNniPort is to check if given port is Nni Port.
90func IsNniPort(id uint32) bool {
Akash Soni53da2852023-03-15 00:31:31 +053091 return (id >= 0x1000000)
Naveen Sampath04696f72022-06-13 15:19:14 +053092}
93
94// Uint32ToByte to convert uint32 to byte
95func Uint32ToByte(value uint32) []byte {
96 byteValue := make([]byte, 4)
97 binary.BigEndian.PutUint32(byteValue[0:4], value)
98 return byteValue
99}
100
101// IP2LongConv convert ip address to integer value.
102func IP2LongConv(ip net.IP) uint32 {
103 if len(ip) == 16 {
104 return binary.BigEndian.Uint32(ip[12:16])
105 }
106 return binary.BigEndian.Uint32(ip)
107}
108
109// Long2ipConv convert integer to ip address.
110func Long2ipConv(nn uint32) net.IP {
111 ip := make(net.IP, 4)
112 binary.BigEndian.PutUint32(ip, nn)
113 return ip
114}
115
116// GetExpIPList converts list or range of IPs to expanded IP list
117func GetExpIPList(ips []string) []net.IP {
118 ipList := []net.IP{}
119
120 for _, ipOrRange := range ips {
121 if strings.Contains(ipOrRange, "-") {
122 var splits = strings.Split(ipOrRange, "-")
123 ipStart := IP2LongConv(net.ParseIP(splits[0]))
124 ipEnd := IP2LongConv(net.ParseIP(splits[1]))
125
126 for i := ipStart; i <= ipEnd; i++ {
127 ipList = append(ipList, Long2ipConv(i))
128 }
129 } else {
130 ipList = append(ipList, net.ParseIP(ipOrRange))
131 }
132 }
133 return ipList
134}
135
Akash Sonief452f12024-12-12 18:20:28 +0530136// GetUniFromMetadata returns uni port from write metadata of DS flows.
137func GetUniFromMetadata(metadata uint64) uint32 {
138 return uint32(metadata & 0xFFFFFFFF)
139}
140
141// GetUniFromDSDhcpFlow returns uni port from the flow cookie
142func GetUniFromDSDhcpFlow(cookie uint64) uint32 {
143 uniport := uint32(cookie >> 16)
144 uniport = uniport & 0xFFFFFFFF
145 return uniport
146}
147
148// GetUniPortFromFlow returns uni port from the flow data
149func GetUniPortFromFlow(nniPort string, flow *of.VoltSubFlow) uint32 {
150 var portNo uint32
151 if nniPort == strconv.Itoa(int(flow.Match.InPort)) {
152 if of.IPProtocolUDP == flow.Match.L4Protocol {
153 // For DHCP DS flow, uniport is not part of metadata. Hence retrieve it from cookie
154 portNo = GetUniFromDSDhcpFlow(flow.Cookie)
155 } else {
156 portNo = GetUniFromMetadata(flow.Action.Metadata)
157 }
158 } else {
159 portNo = flow.Match.InPort
160 }
161 return portNo
162}
163
Naveen Sampath04696f72022-06-13 15:19:14 +0530164// MacAddrsMatch for comparison of MAC addresses and return true if MAC addresses matches
165func MacAddrsMatch(addr1 net.HardwareAddr, addr2 net.HardwareAddr) bool {
166 if len(addr1) != len(addr2) {
167 return false
168 }
169 for i := 0; i < len(addr1); i++ {
170 if addr1[i] != addr2[i] {
171 return false
172 }
173 }
174 return true
175}