Cleanup Ipv4/Ipv6Util
[bgpcep.git] / concepts / src / main / java / org / opendaylight / protocol / concepts / Ipv6Util.java
index 57e224ed2ebf35659973fb21a141e760a0e027f6..e5316262d17e247fdc8fbb0ddb73572beb244346 100644 (file)
@@ -21,42 +21,49 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.net.InetAddresses;
+import com.google.common.primitives.Bytes;
 import com.google.common.primitives.UnsignedBytes;
 
 /**
  * Util class for creating generated Ipv6Address.
  */
 public class Ipv6Util {
+       private 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("/");
+               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);
-               byte[] tmp = Arrays.copyOfRange(bytes, 0, 16);
-               InetAddress a = null;
-               try {
-                       a = InetAddress.getByAddress(tmp);
-               } catch (UnknownHostException e) {
-                       throw new IllegalArgumentException(e.getMessage());
-               }
+               final byte[] tmp = Arrays.copyOfRange(bytes, 0, 16);
+               final InetAddress a = getAddress(tmp);
                return new Ipv6Prefix(InetAddresses.toAddrString(a) + "/" + length);
        }