use com.google.common.net.InetAddresses.forString() 16/56216/2
authorIsaku Yamahata <isaku.yamahata@intel.com>
Thu, 27 Apr 2017 22:57:23 +0000 (15:57 -0700)
committerIsaku Yamahata <isaku.yamahata@intel.com>
Thu, 4 May 2017 00:48:03 +0000 (20:48 -0400)
instead of InteAddress.getByName(), use
com.google.common.net.InetAddresses.forString which  is better.

Change-Id: Ic7405524882f94ecdd614f0b533deb093db0c8f3
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronSubnet.java
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronSubnetIpAllocationPool.java

index 99a0e0e3990c87b67898879ed0d01e4453cb46e8..a3f38c85b3576de990e5d11a6139e69552061291 100644 (file)
@@ -8,11 +8,10 @@
 
 package org.opendaylight.neutron.spi;
 
+import com.google.common.net.InetAddresses;
 import java.io.Serializable;
 import java.math.BigInteger;
 import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
 import javax.xml.bind.annotation.XmlAccessType;
@@ -239,22 +238,17 @@ public final class NeutronSubnet extends NeutronBaseAttributes<NeutronSubnet> im
             if (parts.length != 2) {
                 return false;
             }
-            try {
-                int length = Integer.parseInt(parts[1]);
-                //TODO?: limit check on length
-                // convert to byte array
-                byte[] addrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
-                for (int index = length; index < IPV6_LENGTH; index++) {
-                    if (((((int) addrBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK)
-                            & (1 << (IPV6_BYTE_OFFSET - (index % IPV6_LENGTH_BYTES)))) != 0) {
-                        return (false);
-                    }
+            int length = Integer.parseInt(parts[1]);
+            //TODO?: limit check on length
+            // convert to byte array
+            byte[] addrBytes = ((Inet6Address) InetAddresses.forString(parts[0])).getAddress();
+            for (int index = length; index < IPV6_LENGTH; index++) {
+                if (((((int) addrBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK)
+                        & (1 << (IPV6_BYTE_OFFSET - (index % IPV6_LENGTH_BYTES)))) != 0) {
+                    return (false);
                 }
-                return (true);
-            } catch (UnknownHostException e) {
-                LOGGER.warn("Failure in isValidCIDR()", e);
-                return (false);
             }
+            return (true);
         }
         return false;
     }
@@ -349,23 +343,18 @@ public final class NeutronSubnet extends NeutronBaseAttributes<NeutronSubnet> im
 
         if (ipVersion == IPV6_VERSION) {
             String[] parts = cidr.split("/");
-            try {
-                int length = Integer.parseInt(parts[1]);
-                byte[] cidrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
-                byte[] ipBytes = ((Inet6Address) InetAddress.getByName(ipAddress)).getAddress();
-                for (int index = 0; index < length; index++) {
-                    if (((((int) cidrBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK) & (1 << (IPV6_BYTE_OFFSET
-                            - (index % IPV6_LENGTH_BYTES)))) != (
-                                (((int) ipBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK)
-                                & (1 << (IPV6_BYTE_OFFSET - (index % IPV6_LENGTH_BYTES))))) {
-                        return (false);
-                    }
+            int length = Integer.parseInt(parts[1]);
+            byte[] cidrBytes = ((Inet6Address) InetAddresses.forString(parts[0])).getAddress();
+            byte[] ipBytes = ((Inet6Address) InetAddresses.forString(ipAddress)).getAddress();
+            for (int index = 0; index < length; index++) {
+                if (((((int) cidrBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK) & (1 << (IPV6_BYTE_OFFSET
+                        - (index % IPV6_LENGTH_BYTES)))) != (
+                            (((int) ipBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK)
+                            & (1 << (IPV6_BYTE_OFFSET - (index % IPV6_LENGTH_BYTES))))) {
+                    return (false);
                 }
-                return (true);
-            } catch (UnknownHostException e) {
-                LOGGER.warn("Failure in isValidIp()", e);
-                return (false);
             }
+            return (true);
         }
         return false;
     }
index af16c9753f87d500616bd8cbca23cfc2c7da107d..55736f8e68de82ae30e91f8fe46cb304ce3767da 100644 (file)
@@ -8,10 +8,10 @@
 
 package org.opendaylight.neutron.spi;
 
+import com.google.common.net.InetAddresses;
 import java.io.Serializable;
 import java.math.BigInteger;
 import java.net.Inet6Address;
-import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
@@ -135,12 +135,7 @@ public final class NeutronSubnetIpAllocationPool implements Serializable {
         if (inputString == null) {
             return BigInteger.ZERO;
         }
-        try {
-            return new BigInteger(((Inet6Address) InetAddress.getByName(inputString)).getAddress());
-        } catch (UnknownHostException e) {
-            LOGGER.error("convertV6 error", e);
-            return BigInteger.ZERO;
-        }
+        return new BigInteger(((Inet6Address) InetAddresses.forString(inputString)).getAddress());
     }
 
     /**