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=23d785e0c76b029b4819af3804f8e2f8ca50185c;hpb=541d0a36997f292bb037a2199463431eee538358;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 23d785e0c7..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 @@ -40,10 +40,11 @@ public abstract class NetUtils { * @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]); } /** @@ -131,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; @@ -292,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 @@ -319,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 { @@ -342,6 +345,19 @@ 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 @@ -354,8 +370,8 @@ public abstract class NetUtils { * @return the int variable containing the unsigned byte value */ public static int getUnsignedByte(byte b) { - return (b > 0)? (int)b : ((int)b & 0x7F | 0x80); - } + return (b > 0)? (int)b : (b & 0x7F | 0x80); + } /** * Return the unsigned value of the passed short variable @@ -363,7 +379,7 @@ public abstract class NetUtils { * @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 : ((int)s & 0x7FFF | 0x8000); - } + public static int getUnsignedShort(short s) { + return (s > 0)? (int)s : (s & 0x7FFF | 0x8000); + } }