Cleanup Ipv4/Ipv6Util 85/3185/2
authorRobert Varga <rovarga@cisco.com>
Thu, 28 Nov 2013 10:33:13 +0000 (11:33 +0100)
committerRobert Varga <rovarga@cisco.com>
Thu, 28 Nov 2013 11:18:32 +0000 (12:18 +0100)
Change-Id: I2b66aaa2291384bee4f989a72abe62b94f103f2a
Signed-off-by: Robert Varga <rovarga@cisco.com>
concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv4Util.java
concepts/src/main/java/org/opendaylight/protocol/concepts/Ipv6Util.java
concepts/src/test/java/org/opendaylight/protocol/concepts/IPAddressesAndPrefixesTest.java

index 8a51a892f07824e18ec85a0345de2bdb4fa7c1bd..d5d71781186687d43a74b15ac55dd74a4dfb22c0 100644 (file)
@@ -35,44 +35,37 @@ public final class Ipv4Util {
 
        public static final int IP4_LENGTH = 4;
 
-       public static Ipv4Address addressForBytes(final byte[] bytes) {
+       private static InetAddress getAddress(final byte[] bytes) {
                try {
-                       return new Ipv4Address(InetAddresses.toAddrString(Inet4Address.getByAddress(bytes)));
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
+                       return Inet4Address.getByAddress(bytes);
+               } catch (UnknownHostException e) {
+                       throw new IllegalArgumentException("Failed to construct IPv4 address", e);
                }
        }
 
+       public static Ipv4Address addressForBytes(final byte[] bytes) {
+               return new Ipv4Address(InetAddresses.toAddrString(getAddress(bytes)));
+       }
+
        public static byte[] bytesForAddress(final Ipv4Address address) {
-               Inet4Address a;
-               try {
-                       a = (Inet4Address) InetAddress.getByName(address.getValue());
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final InetAddress a = InetAddresses.forString(address.getValue());
+               Preconditions.checkArgument(a instanceof Inet4Address);
                return a.getAddress();
        }
 
        public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
                final String p = prefix.getValue();
                final int sep = p.indexOf("/");
-               try {
-                       final byte[] bytes = Inet4Address.getByName(p.substring(0, sep)).getAddress();
-                       return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final InetAddress a = InetAddresses.forString(p.substring(0, sep));
+               Preconditions.checkArgument(a instanceof Inet4Address);
+               final byte[] bytes = a.getAddress();
+               return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
        }
 
        public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
                Preconditions.checkArgument(length <= bytes.length * 8);
                final byte[] tmp = Arrays.copyOfRange(bytes, 0, 4);
-               InetAddress a = null;
-               try {
-                       a = InetAddress.getByAddress(tmp);
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final InetAddress a = getAddress(tmp);
                return new Ipv4Prefix(InetAddresses.toAddrString(a) + "/" + length);
        }
 
index 8b35755e97a96b3fbf67b38dffc16169062406cd..e5316262d17e247fdc8fbb0ddb73572beb244346 100644 (file)
@@ -33,44 +33,37 @@ public class Ipv6Util {
 
        public static final int IPV6_LENGTH = 16;
 
-       public static Ipv6Address addressForBytes(final byte[] bytes) {
+       private static InetAddress getAddress(final byte[] bytes) {
                try {
-                       return new Ipv6Address(InetAddresses.toAddrString(Inet6Address.getByAddress(bytes)));
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
+                       return Inet6Address.getByAddress(bytes);
+               } catch (UnknownHostException e) {
+                       throw new IllegalArgumentException("Failed to construct IPv6 address", e);
                }
        }
 
+       public static Ipv6Address addressForBytes(final byte[] bytes) {
+               return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes)));
+       }
+
        public static byte[] bytesForAddress(final Ipv6Address address) {
-               Inet6Address a;
-               try {
-                       a = (Inet6Address) InetAddress.getByName(address.getValue());
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final InetAddress a = InetAddresses.forString(address.getValue());
+               Preconditions.checkArgument(a instanceof Inet6Address);
                return a.getAddress();
        }
 
        public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
                final String p = prefix.getValue();
                final int sep = p.indexOf("/");
-               try {
-                       final byte[] bytes = Inet6Address.getByName(p.substring(0, sep)).getAddress();
-                       return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final InetAddress a = InetAddresses.forString(p.substring(0, sep));
+               Preconditions.checkArgument(a instanceof Inet6Address);
+               final byte[] bytes = a.getAddress();
+               return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
        }
 
        public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
                Preconditions.checkArgument(length <= bytes.length * 8);
                final byte[] tmp = Arrays.copyOfRange(bytes, 0, 16);
-               InetAddress a = null;
-               try {
-                       a = InetAddress.getByAddress(tmp);
-               } catch (final UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final InetAddress a = getAddress(tmp);
                return new Ipv6Prefix(InetAddresses.toAddrString(a) + "/" + length);
        }
 
index db16a6408f2281773eac060150ce481a0eeb8921..a0a941b0692ca9ebf15676ebc7a90092e53d3bea 100644 (file)
@@ -64,7 +64,7 @@ public class IPAddressesAndPrefixesTest {
                        Ipv4Util.addressForBytes(new byte[] { 22, 44, 66, 18, 88, 33 });
                        fail();
                } catch (final IllegalArgumentException e) {
-                       assertEquals("addr is of illegal length", e.getMessage());
+                       assertEquals("Failed to construct IPv4 address", e.getMessage());
                }
        }