X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fcommons%2Fliblldp%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fliblldp%2FNetUtils.java;h=2ed77879cba48e470987be89d275bd00b5be902a;hp=0320cf6b47208762d31c480f5b8b9fa5c71ea8c4;hb=f6d4d8759fcce4d35f38f5a5a056acf1d3e0b4b2;hpb=4d73514271a9caaa59b7a042d206ba4aad6b3fe0 diff --git a/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/NetUtils.java b/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/NetUtils.java index 0320cf6b47..2ed77879cb 100644 --- a/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/NetUtils.java +++ b/opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/NetUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory; * networking data structures */ public abstract class NetUtils { - protected static final Logger logger = LoggerFactory.getLogger(NetUtils.class); + protected static final Logger LOG = LoggerFactory.getLogger(NetUtils.class); /** * Constant holding the number of bits in a byte */ @@ -52,11 +52,11 @@ public abstract class NetUtils { * the 4 bytes long byte array * @return the integer number */ - public static int byteArray4ToInt(byte[] ba) { + public static int byteArray4ToInt(final byte[] ba) { if (ba == null || ba.length != 4) { return 0; } - return (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]; } /** @@ -68,7 +68,7 @@ public abstract class NetUtils { * Zero is returned if {@code ba} is {@code null} or * the length of it is not six. */ - public static long byteArray6ToLong(byte[] ba) { + public static long byteArray6ToLong(final byte[] ba) { if (ba == null || ba.length != MACAddrLengthInBytes) { return 0L; } @@ -107,8 +107,8 @@ public abstract class NetUtils { * the integer number * @return the byte array */ - public static byte[] intToByteArray4(int i) { - return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff), + public static byte[] intToByteArray4(final int i) { + return new byte[] { (byte) (i >> 24 & 0xff), (byte) (i >> 16 & 0xff), (byte) (i >> 8 & 0xff), (byte) (i & 0xff) }; } @@ -120,12 +120,12 @@ public abstract class NetUtils { * the IP address in integer form * @return the IP address in InetAddress form */ - public static InetAddress getInetAddress(int address) { + public static InetAddress getInetAddress(final int address) { InetAddress ip = null; try { ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address)); - } catch (UnknownHostException e) { - logger.error("", e); + } catch (final UnknownHostException e) { + LOG.error("", e); } return ip; } @@ -142,13 +142,13 @@ public abstract class NetUtils { * boolean representing the IP version of the returned address * @return */ - public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) { - if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) { + public static InetAddress getInetNetworkMask(final int prefixMaskLength, final boolean isV6) { + if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) { return null; } byte v4Address[] = { 0, 0, 0, 0 }; byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - byte address[] = (isV6) ? v6Address : v4Address; + byte address[] = isV6 ? v6Address : v4Address; int numBytes = prefixMaskLength / 8; int numBits = prefixMaskLength % 8; int i = 0; @@ -158,15 +158,15 @@ public abstract class NetUtils { if (numBits > 0) { int rem = 0; for (int j = 0; j < numBits; j++) { - rem |= 1 << (7 - j); + rem |= 1 << 7 - j; } address[i] = (byte) rem; } try { return InetAddress.getByAddress(address); - } catch (UnknownHostException e) { - logger.error("", e); + } catch (final UnknownHostException e) { + LOG.error("", e); } return null; } @@ -180,7 +180,7 @@ public abstract class NetUtils { * the subnet mask as byte array * @return the prefix length as number of bits */ - public static int getSubnetMaskLength(byte[] subnetMask) { + public static int getSubnetMaskLength(final byte[] subnetMask) { int maskLength = 0; if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) { int index = 0; @@ -208,7 +208,7 @@ public abstract class NetUtils { * the subnet mask as InetAddress * @return the prefix length as number of bits */ - public static int getSubnetMaskLength(InetAddress subnetMask) { + public static int getSubnetMaskLength(final InetAddress subnetMask) { return subnetMask == null ? 0 : NetUtils.getSubnetMaskLength(subnetMask.getAddress()); } @@ -223,22 +223,22 @@ public abstract class NetUtils { * the length of the prefix network mask * @return the subnet prefix IP address in InetAddress form */ - public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) { + public static InetAddress getSubnetPrefix(final InetAddress ip, final int maskLen) { int bytes = maskLen / 8; int bits = maskLen % 8; byte modifiedByte; byte[] sn = ip.getAddress(); if (bits > 0) { - modifiedByte = (byte) (sn[bytes] >> (8 - bits)); - sn[bytes] = (byte) (modifiedByte << (8 - bits)); + modifiedByte = (byte) (sn[bytes] >> 8 - bits); + sn[bytes] = (byte) (modifiedByte << 8 - bits); bytes++; } for (; bytes < sn.length; bytes++) { - sn[bytes] = (byte) (0); + sn[bytes] = (byte) 0; } try { return InetAddress.getByAddress(sn); - } catch (UnknownHostException e) { + } catch (final UnknownHostException e) { return null; } } @@ -268,10 +268,10 @@ public abstract class NetUtils { * @param filterMask * @return */ - public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask, - InetAddress filterMask) { + public static boolean inetAddressConflict(final InetAddress testAddress, final InetAddress filterAddress, final InetAddress testMask, + final InetAddress filterMask) { // Sanity check - if ((testAddress == null) || (filterAddress == null)) { + if (testAddress == null || filterAddress == null) { return false; } @@ -280,9 +280,9 @@ public abstract class NetUtils { return false; } - int testMaskLen = (testMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils + int testMaskLen = testMask == null ? testAddress instanceof Inet4Address ? 32 : 128 : NetUtils .getSubnetMaskLength(testMask); - int filterMaskLen = (filterMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils + int filterMaskLen = filterMask == null ? testAddress instanceof Inet4Address ? 32 : 128 : NetUtils .getSubnetMaskLength(filterMask); // Mask length check. Test mask has to be more specific than filter one @@ -293,7 +293,7 @@ public abstract class NetUtils { // Subnet Prefix on filter mask length must be the same InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen); InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen); - return (!prefix1.equals(prefix2)); + return !prefix1.equals(prefix2); } /** @@ -303,7 +303,7 @@ public abstract class NetUtils { * the byte array representing the MAC address * @return true if all MAC bytes are zero */ - public static boolean isZeroMAC(byte[] mac) { + public static boolean isZeroMAC(final byte[] mac) { for (short i = 0; i < 6; i++) { if (mac[i] != 0) { return false; @@ -319,7 +319,7 @@ public abstract class NetUtils { * @param MACAddress * @return */ - public static boolean isBroadcastMACAddr(byte[] MACAddress) { + public static boolean isBroadcastMACAddr(final byte[] MACAddress) { if (MACAddress.length == MACAddrLengthInBytes) { for (int i = 0; i < 6; i++) { if (MACAddress[i] != BroadcastMACAddr[i]) { @@ -338,7 +338,7 @@ public abstract class NetUtils { * @param MACAddress * @return */ - public static boolean isUnicastMACAddr(byte[] MACAddress) { + public static boolean isUnicastMACAddr(final byte[] MACAddress) { if (MACAddress.length == MACAddrLengthInBytes) { return (MACAddress[0] & 1) == 0; } @@ -353,7 +353,7 @@ public abstract class NetUtils { * @param MACAddress * @return */ - public static boolean isMulticastMACAddr(byte[] MACAddress) { + public static boolean isMulticastMACAddr(final byte[] MACAddress) { if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) { return (MACAddress[0] & 1) != 0; } @@ -367,7 +367,7 @@ public abstract class NetUtils { * the IP address to test * @return true if the address is all zero */ - public static boolean isAny(InetAddress ip) { + public static boolean isAny(final InetAddress ip) { for (byte b : ip.getAddress()) { if (b != 0) { return false; @@ -376,19 +376,19 @@ public abstract class NetUtils { return true; } - public static boolean fieldsConflict(int field1, int field2) { - if ((field1 == 0) || (field2 == 0) || (field1 == field2)) { + public static boolean fieldsConflict(final int field1, final int field2) { + if (field1 == 0 || field2 == 0 || field1 == field2) { return false; } return true; } - public static InetAddress parseInetAddress(String addressString) { + public static InetAddress parseInetAddress(final String addressString) { InetAddress address = null; try { address = InetAddress.getByName(addressString); - } catch (UnknownHostException e) { - logger.error("", e); + } catch (final UnknownHostException e) { + LOG.error("", e); } return address; } @@ -401,7 +401,7 @@ public abstract class NetUtils { * the v4 address as A.B.C.D/MM * @return */ - public static boolean isIPv4AddressValid(String cidr) { + public static boolean isIPv4AddressValid(final String cidr) { if (cidr == null) { return false; } @@ -415,7 +415,7 @@ public abstract class NetUtils { } if (values.length >= 2) { int prefix = Integer.valueOf(values[1]); - if ((prefix < 0) || (prefix > 32)) { + if (prefix < 0 || prefix > 32) { return false; } } @@ -430,7 +430,7 @@ public abstract class NetUtils { * the v6 address as A::1/MMM * @return */ - public static boolean isIPv6AddressValid(String cidr) { + public static boolean isIPv6AddressValid(final String cidr) { if (cidr == null) { return false; } @@ -443,13 +443,13 @@ public abstract class NetUtils { if (!(addr instanceof Inet6Address)) { return false; } - } catch (UnknownHostException ex) { + } catch (final UnknownHostException ex) { return false; } if (values.length >= 2) { int prefix = Integer.valueOf(values[1]); - if ((prefix < 0) || (prefix > 128)) { + if (prefix < 0 || prefix > 128) { return false; } } @@ -464,7 +464,7 @@ public abstract class NetUtils { * the v4 or v6 address as IP/MMM * @return */ - public static boolean isIPAddressValid(String cidr) { + public static boolean isIPAddressValid(final String cidr) { return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr); } @@ -479,7 +479,7 @@ public abstract class NetUtils { * the byte value * @return the int variable containing the unsigned byte value */ - public static int getUnsignedByte(byte b) { + public static int getUnsignedByte(final byte b) { return b & 0xFF; } @@ -490,7 +490,7 @@ public abstract class NetUtils { * the short value * @return the int variable containing the unsigned short value */ - public static int getUnsignedShort(short s) { + public static int getUnsignedShort(final short s) { return s & 0xFFFF; } @@ -501,11 +501,11 @@ public abstract class NetUtils { * true for IPv6, false for Ipv4 * @return The highest IPv4 or IPv6 address */ - public static InetAddress gethighestIP(boolean v6) { + public static InetAddress gethighestIP(final boolean v6) { try { - return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress + return v6 ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress .getByName("255.255.255.255"); - } catch (UnknownHostException e) { + } catch (final UnknownHostException e) { return null; } }