Northbound cleanup for Switch Manager
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / NetUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.sal.utils;
10
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.Arrays;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Utility class containing the common utility functions needed for operating on
24  * networking data structures
25  */
26 public abstract class NetUtils {
27     protected static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
28     /**
29      * Constant holding the number of bits in a byte
30      */
31     public static final int NumBitsInAByte = 8;
32
33     /**
34      * Constant holding the number of bytes in MAC Address
35      */
36     public static final int MACAddrLengthInBytes = 6;
37
38     /**
39      * Constant holding the number of words in MAC Address
40      */
41     public static final int MACAddrLengthInWords = 3;
42
43     /**
44      * Constant holding the broadcast MAC address
45      */
46     private static final byte[] BroadcastMACAddr = {-1, -1, -1, -1, -1, -1};
47
48     /**
49      * Converts a 4 bytes array into an integer number
50      *
51      * @param ba
52      *            the 4 bytes long byte array
53      * @return the integer number
54      */
55     public static int byteArray4ToInt(byte[] ba) {
56         if (ba == null || ba.length != 4) {
57             return 0;
58         }
59         return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 | (0xff & ba[2]) << 8 | (0xff & ba[3]);
60     }
61
62     /**
63      * Converts a 6 bytes array into a long number MAC addresses.
64      *
65      * @param ba
66      *            The 6 bytes long byte array.
67      * @return The long number.
68      *         Zero is returned if {@code ba} is {@code null} or
69      *         the length of it is not six.
70      */
71     public static long byteArray6ToLong(byte[] ba) {
72         if (ba == null || ba.length != MACAddrLengthInBytes) {
73             return 0L;
74         }
75         long num = 0L;
76         int i = 0;
77         do {
78             num <<= NumBitsInAByte;
79             num |= 0xff & ba[i];
80             i++;
81         } while (i < MACAddrLengthInBytes);
82         return num;
83     }
84
85     /**
86      * Converts a long number to a 6 bytes array for MAC addresses.
87      *
88      * @param addr
89      *            The long number.
90      * @return The byte array.
91      */
92     public static byte[] longToByteArray6(long addr){
93         byte[] mac = new byte[MACAddrLengthInBytes];
94         int i = MACAddrLengthInBytes - 1;
95         do {
96             mac[i] = (byte) addr;
97             addr >>>= NumBitsInAByte;
98             i--;
99         } while (i >= 0);
100         return mac;
101     }
102
103     /**
104      * Converts an integer number into a 4 bytes array
105      *
106      * @param i
107      *            the integer number
108      * @return the byte array
109      */
110     public static byte[] intToByteArray4(int i) {
111         return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
112                 (byte) (i & 0xff) };
113     }
114
115     /**
116      * Converts an IP address passed as integer value into the respective
117      * InetAddress object
118      *
119      * @param address
120      *            the IP address in integer form
121      * @return the IP address in InetAddress form
122      */
123     public static InetAddress getInetAddress(int address) {
124         InetAddress ip = null;
125         try {
126             ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
127         } catch (UnknownHostException e) {
128             logger.error("", e);
129         }
130         return ip;
131     }
132
133     /**
134      * Return the InetAddress Network Mask given the length of the prefix bit
135      * mask. The prefix bit mask indicates the contiguous leading bits that are
136      * NOT masked out. Example: A prefix bit mask length of 8 will give an
137      * InetAddress Network Mask of 255.0.0.0
138      *
139      * @param prefixMaskLength
140      *            integer representing the length of the prefix network mask
141      * @param isV6
142      *            boolean representing the IP version of the returned address
143      * @return
144      */
145     public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
146         if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) {
147             return null;
148         }
149         byte v4Address[] = { 0, 0, 0, 0 };
150         byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
151         byte address[] = (isV6) ? v6Address : v4Address;
152         int numBytes = prefixMaskLength / 8;
153         int numBits = prefixMaskLength % 8;
154         int i = 0;
155         for (; i < numBytes; i++) {
156             address[i] = (byte) 0xff;
157         }
158         if (numBits > 0) {
159             int rem = 0;
160             for (int j = 0; j < numBits; j++) {
161                 rem |= 1 << (7 - j);
162             }
163             address[i] = (byte) rem;
164         }
165
166         try {
167             return InetAddress.getByAddress(address);
168         } catch (UnknownHostException e) {
169             logger.error("", e);
170         }
171         return null;
172     }
173
174     /**
175      * Returns the prefix size in bits of the specified subnet mask. Example:
176      * For the subnet mask ff.ff.ff.e0 it returns 25 while for ff.00.00.00 it
177      * returns 8. If the passed subnetMask array is null, 0 is returned.
178      *
179      * @param subnetMask
180      *            the subnet mask as byte array
181      * @return the prefix length as number of bits
182      */
183     public static int getSubnetMaskLength(byte[] subnetMask) {
184         int maskLength = 0;
185         if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) {
186             int index = 0;
187             while (index < subnetMask.length && subnetMask[index] == (byte) 0xFF) {
188                 maskLength += NetUtils.NumBitsInAByte;
189                 index++;
190             }
191             if (index != subnetMask.length) {
192                 int bits = NetUtils.NumBitsInAByte - 1;
193                 while (bits >= 0 && (subnetMask[index] & 1 << bits)  != 0) {
194                     bits--;
195                     maskLength++;
196                 }
197             }
198         }
199         return maskLength;
200     }
201
202     /**
203      * Returns the prefix size in bits of the specified subnet mask. Example:
204      * For the subnet mask 255.255.255.128 it returns 25 while for 255.0.0.0 it
205      * returns 8. If the passed subnetMask object is null, 0 is returned
206      *
207      * @param subnetMask
208      *            the subnet mask as InetAddress
209      * @return the prefix length as number of bits
210      */
211     public static int getSubnetMaskLength(InetAddress subnetMask) {
212         return subnetMask == null ? 0 : NetUtils.getSubnetMaskLength(subnetMask.getAddress());
213     }
214
215     /**
216      * Given an IP address and a prefix network mask length, it returns the
217      * equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and
218      * maskLen = 25 it will return "172.28.30.128"
219      *
220      * @param ip
221      *            the IP address in InetAddress form
222      * @param maskLen
223      *            the length of the prefix network mask
224      * @return the subnet prefix IP address in InetAddress form
225      */
226     public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
227         int bytes = maskLen / 8;
228         int bits = maskLen % 8;
229         byte modifiedByte;
230         byte[] sn = ip.getAddress();
231         if (bits > 0) {
232             modifiedByte = (byte) (sn[bytes] >> (8 - bits));
233             sn[bytes] = (byte) (modifiedByte << (8 - bits));
234             bytes++;
235         }
236         for (; bytes < sn.length; bytes++) {
237             sn[bytes] = (byte) (0);
238         }
239         try {
240             return InetAddress.getByAddress(sn);
241         } catch (UnknownHostException e) {
242             return null;
243         }
244     }
245
246     /**
247      * Checks if the test address and mask conflicts with the filter address and
248      * mask
249      *
250      * For example: testAddress: 172.28.2.23 testMask: 255.255.255.0
251      * filtAddress: 172.28.1.10 testMask: 255.255.255.0 conflict
252      *
253      * testAddress: 172.28.2.23 testMask: 255.255.255.0 filtAddress: 172.28.1.10
254      * testMask: 255.255.0.0 do not conflict
255      *
256      * Null parameters are permitted
257      *
258      * @param testAddress
259      * @param filterAddress
260      * @param testMask
261      * @param filterMask
262      * @return
263      */
264     public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
265             InetAddress filterMask) {
266         // Sanity check
267         if ((testAddress == null) || (filterAddress == null)) {
268             return false;
269         }
270
271         // Presence check
272         if (isAny(testAddress) || isAny(filterAddress)) {
273             return false;
274         }
275
276         int testMaskLen = (testMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
277                 .getSubnetMaskLength(testMask);
278         int filterMaskLen = NetUtils.getSubnetMaskLength(filterMask);
279
280         // Mask length check. Test mask has to be more specific than filter one
281         if (testMaskLen < filterMaskLen) {
282             return true;
283         }
284
285         // Subnet Prefix on filter mask length must be the same
286         InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen);
287         InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen);
288         return (!prefix1.equals(prefix2));
289     }
290
291     /**
292      * Returns true if the passed MAC address is all zero
293      *
294      * @param mac
295      *            the byte array representing the MAC address
296      * @return true if all MAC bytes are zero
297      */
298     public static boolean isZeroMAC(byte[] mac) {
299         for (short i = 0; i < 6; i++) {
300             if (mac[i] != 0) {
301                 return false;
302             }
303         }
304         return true;
305     }
306
307     /**
308      * Returns true if the MAC address is the broadcast MAC address and false
309      * otherwise.
310      *
311      * @param MACAddress
312      * @return
313      */
314     public static boolean isBroadcastMACAddr(byte[] MACAddress) {
315         if (MACAddress.length == MACAddrLengthInBytes) {
316             for (int i = 0; i < 6; i++) {
317                 if (MACAddress[i] != BroadcastMACAddr[i]) {
318                     return false;
319                 }
320             }
321             return true;
322         }
323
324         return false;
325     }
326     /**
327      * Returns true if the MAC address is a unicast MAC address and false
328      * otherwise.
329      *
330      * @param MACAddress
331      * @return
332      */
333     public static boolean isUnicastMACAddr(byte[] MACAddress) {
334         if (MACAddress.length == MACAddrLengthInBytes) {
335             return (MACAddress[0] & 1) == 0;
336         }
337         return false;
338     }
339
340     /**
341      * Returns true if the MAC address is a multicast MAC address and false
342      * otherwise. Note that this explicitly returns false for the broadcast MAC
343      * address.
344      *
345      * @param MACAddress
346      * @return
347      */
348     public static boolean isMulticastMACAddr(byte[] MACAddress) {
349         if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
350             if (MACAddress[0] % 2 == 1) {
351                 return true;
352             }
353         }
354         return false;
355     }
356
357     /**
358      * Returns true if the passed InetAddress contains all zero
359      *
360      * @param ip
361      *            the IP address to test
362      * @return true if the address is all zero
363      */
364     public static boolean isAny(InetAddress ip) {
365         for (byte b : ip.getAddress()) {
366             if (b != 0) {
367                 return false;
368             }
369         }
370         return true;
371     }
372
373     public static boolean fieldsConflict(int field1, int field2) {
374         if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
375             return false;
376         }
377         return true;
378     }
379
380     public static InetAddress parseInetAddress(String addressString) {
381         InetAddress address = null;
382         try {
383             address = InetAddress.getByName(addressString);
384         } catch (UnknownHostException e) {
385             logger.error("", e);
386         }
387         return address;
388     }
389
390     /**
391      * Checks if the passed IP v4 address in string form is valid The address
392      * may specify a mask at the end as "/MM"
393      *
394      * @param cidr
395      *            the v4 address as A.B.C.D/MM
396      * @return
397      */
398     public static boolean isIPv4AddressValid(String cidr) {
399         if (cidr == null) {
400             return false;
401         }
402
403         String values[] = cidr.split("/");
404         Pattern ipv4Pattern = Pattern
405                 .compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
406         Matcher mm = ipv4Pattern.matcher(values[0]);
407         if (!mm.matches()) {
408             return false;
409         }
410         if (values.length >= 2) {
411             int prefix = Integer.valueOf(values[1]);
412             if ((prefix < 0) || (prefix > 32)) {
413                 return false;
414             }
415         }
416         return true;
417     }
418
419     /**
420      * Checks if the passed IP v6 address in string form is valid The address
421      * may specify a mask at the end as "/MMM"
422      *
423      * @param cidr
424      *            the v6 address as A::1/MMM
425      * @return
426      */
427     public static boolean isIPv6AddressValid(String cidr) {
428         if (cidr == null) {
429             return false;
430         }
431
432         String values[] = cidr.split("/");
433         try {
434             // when given an IP address, InetAddress.getByName validates the ip
435             // address
436             InetAddress addr = InetAddress.getByName(values[0]);
437             if (!(addr instanceof Inet6Address)) {
438                 return false;
439             }
440         } catch (UnknownHostException ex) {
441             return false;
442         }
443
444         if (values.length >= 2) {
445             int prefix = Integer.valueOf(values[1]);
446             if ((prefix < 0) || (prefix > 128)) {
447                 return false;
448             }
449         }
450         return true;
451     }
452
453     /**
454      * Checks if the passed IP address in string form is a valid v4 or v6
455      * address. The address may specify a mask at the end as "/MMM"
456      *
457      * @param cidr
458      *            the v4 or v6 address as IP/MMM
459      * @return
460      */
461     public static boolean isIPAddressValid(String cidr) {
462         return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
463     }
464
465     /*
466      * Following utilities are useful when you need to compare or bit shift java
467      * primitive type variable which are inerently signed
468      */
469     /**
470      * Returns the unsigned value of the passed byte variable
471      *
472      * @param b
473      *            the byte value
474      * @return the int variable containing the unsigned byte value
475      */
476     public static int getUnsignedByte(byte b) {
477         return (b > 0) ? (int) b : (b & 0x7F | 0x80);
478     }
479
480     /**
481      * Return the unsigned value of the passed short variable
482      *
483      * @param s
484      *            the short value
485      * @return the int variable containing the unsigned short value
486      */
487     public static int getUnsignedShort(short s) {
488         return (s > 0) ? (int) s : (s & 0x7FFF | 0x8000);
489     }
490
491     /**
492      * Returns the highest v4 or v6 InetAddress
493      *
494      * @param v6
495      *            true for IPv6, false for Ipv4
496      * @return The highest IPv4 or IPv6 address
497      */
498     public static InetAddress gethighestIP(boolean v6) {
499         try {
500             return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
501                     .getByName("255.255.255.255");
502         } catch (UnknownHostException e) {
503             return null;
504         }
505     }
506
507     /**
508      * Returns Broadcast MAC Address
509      *
510      * @return the byte array containing  broadcaset mac address
511      */
512     public static byte[] getBroadcastMACAddr() {
513         return Arrays.copyOf(BroadcastMACAddr, BroadcastMACAddr.length);
514     }
515
516 }