X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fapi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Futils%2FNetUtils.java;h=74da40449646059b640e96465b9b01bbf93ac2d5;hb=ff1b4a79cca00743a00c3b0b1100bd0ab2b2fb31;hp=703d49a9c330ca7f6167c80d23182c9cfeb1d33c;hpb=42210c03b0a4c54706320ba9f55794c0abd4d201;p=controller.git diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java index 703d49a9c3..74da404496 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java @@ -15,6 +15,9 @@ import java.net.UnknownHostException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Utility class containing the common utility functions needed * for operating on networking data structures @@ -23,6 +26,8 @@ import java.util.regex.Pattern; * */ public abstract class NetUtils { + protected static final Logger logger = LoggerFactory + .getLogger(NetUtils.class); /** * Constant holding the number of bits in a byte */ @@ -31,14 +36,15 @@ public abstract class NetUtils { /** * Converts a 4 bytes array into an integer number * - * @param ba the 4 bytes long byte array - * @return the integer number + * @param ba the 4 bytes long byte array + * @return the integer number */ public static int byteArray4ToInt(byte[] ba) { - if (ba == null || ba.length != 4) + if (ba == null || ba.length != 4) { return 0; - return (int) ((0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 - | (0xff & ba[2]) << 8 | (0xff & ba[3])); + } + return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 + | (0xff & ba[2]) << 8 | (0xff & ba[3]); } /** @@ -57,15 +63,15 @@ public abstract class NetUtils { * Converts an IP address passed as integer value into the * respective InetAddress object * - * @param address the IP address in integer form - * @return the IP address in InetAddress form + * @param address the IP address in integer form + * @return the IP address in InetAddress form */ public static InetAddress getInetAddress(int address) { InetAddress ip = null; try { ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address)); } catch (UnknownHostException e) { - e.printStackTrace(); + logger.error("",e); } return ip; } @@ -75,8 +81,8 @@ public abstract class NetUtils { * The prefix bit mask indicates the contiguous leading bits that are NOT masked out. * Example: A prefix bit mask length of 8 will give an InetAddress Network Mask of 255.0.0.0 * - * @param prefixMaskLength integer representing the length of the prefix network mask - * @param isV6 boolean representing the IP version of the returned address + * @param prefixMaskLength integer representing the length of the prefix network mask + * @param isV6 boolean representing the IP version of the returned address * @return */ public static InetAddress getInetNetworkMask(int prefixMaskLength, @@ -105,7 +111,7 @@ public abstract class NetUtils { try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { - e.printStackTrace(); + logger.error("",e); } return null; } @@ -116,8 +122,8 @@ public abstract class NetUtils { * while ff.00.00.00 will return a subnet mask length of 24. * If the passed prefixMask object is null, 0 is returned * - * @param prefixMask the prefix mask as byte array - * @return the length of the prefix network mask + * @param prefixMask the prefix mask as byte array + * @return the length of the prefix network mask */ public static int getSubnetMaskLength(byte[] prefixMask) { int maskLength = 0; @@ -126,7 +132,7 @@ public abstract class NetUtils { int intMask = 0; int numBytes = prefixMask.length; for (int i = 0; i < numBytes; i++) { - intMask |= ((int) prefixMask[i] & 0xff) << (8 * (numBytes - 1 - i)); + intMask |= (prefixMask[i] & 0xff) << (8 * (numBytes - 1 - i)); } int bit = 1; @@ -144,8 +150,8 @@ public abstract class NetUtils { * while ff.00.00.00 will return a subnet mask length of 24 * If the passed prefixMask object is null, 0 is returned * - * @param prefixMask the prefix mask as InetAddress - * @return the length of the prefix network mask + * @param prefixMask the prefix mask as InetAddress + * @return the length of the prefix network mask */ public static int getSubnetMaskLength(InetAddress prefixMask) { return (prefixMask == null) ? 0 : NetUtils @@ -157,9 +163,9 @@ public abstract class NetUtils { * the equivalent subnet prefix IP address * Example: for ip = "172.28.30.254" and maskLen = 25 it will return "172.28.30.128" * - * @param ip the IP address in InetAddress form - * @param maskLen the length of the prefix network mask - * @return the subnet prefix IP address in InetAddress form + * @param ip the IP address in InetAddress form + * @param maskLen the length of the prefix network mask + * @return the subnet prefix IP address in InetAddress form */ public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) { int bytes = maskLen / 8; @@ -215,30 +221,28 @@ public abstract class NetUtils { return false; } - // Derive the masks length. A null mask means a full mask - int testMaskLen = (testMask != null) ? NetUtils - .getSubnetMaskLength(testMask.getAddress()) - : (testAddress instanceof Inet6Address) ? 128 : 32; - int filterMaskLen = (filterMask != null) ? NetUtils - .getSubnetMaskLength(filterMask.getAddress()) - : (filterAddress instanceof Inet6Address) ? 128 : 32; + int testMaskLen = (testMask != null) ? NetUtils.getSubnetMaskLength(testMask.getAddress()) : 0; + int filterMaskLen = (filterMask != null) ? NetUtils.getSubnetMaskLength(filterMask.getAddress()) : 0; + + int testPrefixLen = (testAddress instanceof Inet6Address) ? (128 - testMaskLen) : (32 - testMaskLen); + int filterPrefixLen = (filterAddress instanceof Inet6Address) ? (128 - filterMaskLen) : (32 - filterMaskLen); - // Mask length check. Test mask has to be more generic than filter one - if (testMaskLen < filterMaskLen) { + // Mask length check. Test mask has to be more specific than filter one + if (testPrefixLen < filterPrefixLen) { return true; } // Subnet Prefix on filter mask length must be the same - InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen); - InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen); + InetAddress prefix1 = getSubnetPrefix(testAddress, filterPrefixLen); + InetAddress prefix2 = getSubnetPrefix(filterAddress, filterPrefixLen); return (!prefix1.equals(prefix2)); } /** * Returns true if the passed MAC address is all zero * - * @param mac the byte array representing the MAC address - * @return true if all MAC bytes are zero + * @param mac the byte array representing the MAC address + * @return true if all MAC bytes are zero */ public static boolean isZeroMAC(byte[] mac) { for (short i = 0; i < 6; i++) { @@ -252,8 +256,8 @@ public abstract class NetUtils { /** * Returns true if the passed InetAddress contains all zero * - * @param ip the IP address to test - * @return true if the address is all zero + * @param ip the IP address to test + * @return true if the address is all zero */ public static boolean isAny(InetAddress ip) { for (byte b : ip.getAddress()) { @@ -276,7 +280,7 @@ public abstract class NetUtils { try { address = InetAddress.getByName(addressString); } catch (UnknownHostException e) { - e.printStackTrace(); + logger.error("",e); } return address; } @@ -289,8 +293,9 @@ public abstract class NetUtils { * @return */ public static boolean isIPv4AddressValid(String cidr) { - if (cidr == null) + if (cidr == null) { return false; + } String values[] = cidr.split("/"); Pattern ipv4Pattern = Pattern @@ -316,8 +321,9 @@ public abstract class NetUtils { * @return */ public static boolean isIPv6AddressValid(String cidr) { - if (cidr == null) + if (cidr == null) { return false; + } String values[] = cidr.split("/"); try { @@ -338,4 +344,42 @@ public abstract class NetUtils { } return true; } + + /** + * Checks if the passed IP address in string form is a valid v4 or v6 + * address. The address may specify a mask at the end as "/MMM" + * + * @param cidr + * the v4 or v6 address as IP/MMM + * @return + */ + public static boolean isIPAddressValid(String cidr) { + return NetUtils.isIPv4AddressValid(cidr) + || NetUtils.isIPv6AddressValid(cidr); + } + + /* + * Following utilities are useful when you need to + * compare or bit shift java primitive type variable + * which are inerently signed + */ + /** + * Returns the unsigned value of the passed byte variable + * + * @param b the byte value + * @return the int variable containing the unsigned byte value + */ + public static int getUnsignedByte(byte b) { + return (b > 0)? (int)b : (b & 0x7F | 0x80); + } + + /** + * Return the unsigned value of the passed short variable + * + * @param s the short value + * @return the int variable containing the unsigned short value + */ + public static int getUnsignedShort(short s) { + return (s > 0)? (int)s : (s & 0x7FFF | 0x8000); + } }