Merge "Cleanup"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / common / IpConversionUtil.java
index ea4815a16c991df10a8bbc786ff20d30b398ea52..f5b8c44d5a5461def93e1c974564e9904bec45bf 100644 (file)
@@ -24,7 +24,6 @@ import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Iterator;
 import java.util.List;
-
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
@@ -35,12 +34,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.opendaylight.ipv6.arbitrary
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
-/**
- * Created by Martin Bobak <mbobak@cisco.com> on 5.3.2015.
- * v6 routines added by Anton Ivanov on 14.6.2015
- * Arbitrary masks by sai.marapareddy@gmail.com
- */
 public final class IpConversionUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(IpConversionUtil.class);
@@ -131,6 +124,10 @@ public final class IpConversionUtil {
     }
 
     public static Ipv4Prefix createPrefix(final Ipv4Address ipv4Address, final byte [] bytemask){
+        if (bytemask == null) {
+            return createPrefix(ipv4Address);
+        }
+
         return IetfInetUtil.INSTANCE.ipv4PrefixFor(ipv4Address, countBits(bytemask));
     }
 
@@ -185,6 +182,10 @@ public final class IpConversionUtil {
     }
 
     public static Ipv6Prefix createPrefix(final Ipv6Address ipv6Address, final byte [] bytemask){
+        if (bytemask == null) {
+            return createPrefix(ipv6Address);
+        }
+
         return IetfInetUtil.INSTANCE.ipv6PrefixFor(ipv6Address, countBits(bytemask));
     }
 
@@ -234,8 +235,6 @@ public final class IpConversionUtil {
      * @param ipv6Address - v6 Address object
      * @return - byte array of size 16. Last byte contains netmask
      */
-
-
     public static byte[] canonicalBinaryV6Address(final Ipv6Address ipv6Address) {
         /*
          * Do not modify this routine to take direct strings input!!!
@@ -243,10 +242,15 @@ public final class IpConversionUtil {
          * the input is validated via regexps in Ipv6Prefix()
          */
 
+        return canonicalBinaryV6AddressFromString(ipv6Address.getValue());
+    }
+
+
+    private static byte[] canonicalBinaryV6AddressFromString(final String ipv6Address) {
        Iterable<String> splittedV6Address = Splitter.on("%")
                 .trimResults()
                 .omitEmptyStrings()
-                .split(ipv6Address.getValue());
+                .split(ipv6Address);
         List<String> partsV6Address = Lists.newArrayList(splittedV6Address.iterator());
 
         int colonp;
@@ -363,7 +367,7 @@ public final class IpConversionUtil {
         return dst;
     }
 
-    public static String byteArrayV6AddressToString (final byte [] _binary_form) throws UnknownHostException{
+    public static String byteArrayV6AddressToString (final byte [] _binary_form) throws UnknownHostException {
         /* DO NOT DIY!!! - InetAddresses will actually print correct canonical
          * zero compressed form.
          */
@@ -402,7 +406,7 @@ public final class IpConversionUtil {
         List<String> partsV6Prefix = Lists.newArrayList(splittedV6Prefix.iterator());
 
         boolean valid = true;
-        
+
         try {
             mask = Integer.parseInt(partsV6Prefix.get(1));
             if (mask > 128) {
@@ -578,6 +582,17 @@ public final class IpConversionUtil {
         return sb.toString();
     }
 
+    /**
+     * Check if the supplied IPv6Address has any prefix
+     *
+     * @param ipv6Prefix Ipv6 prefix
+     * @return prefix if there is one, else null
+     */
+    public static Integer hasIpv6Prefix(final Ipv6Prefix ipv6Prefix) {
+        final int prefix = IpConversionUtil.extractIpv6Prefix(ipv6Prefix);
+        return prefix < IPV6_ADDRESS_LENGTH ? prefix : null;
+    }
+
     private static int ipv6PrefixByteArrayOffset(final int mask) {
         if (mask < 0) {
             return 0;
@@ -717,7 +732,7 @@ public final class IpConversionUtil {
 
     public static final byte[] convertIpv6ArbitraryMaskToByteArray(final Ipv6ArbitraryMask mask) {
         String maskValue;
-        if (mask.getValue() != null) {
+        if (mask != null && mask.getValue() != null) {
             maskValue  = mask.getValue();
         } else {
             maskValue = DEFAULT_IPV6_ARBITRARY_BITMASK;
@@ -762,14 +777,20 @@ public final class IpConversionUtil {
         return false;
     }
 
-    public static String compressedIpv6Format(final String ipv6Address) {
-        String compressedIpv6Address;
-        compressedIpv6Address = ipv6Address.replaceAll("((?::0+\\b){2,}):?(?!\\S*\\b\\1:0\\b)(\\S*)", "::$2");
-        return compressedIpv6Address;
+    private static String compressedIpv6FormatFromString(final String ipv6Address) {
+        try {
+            return byteArrayV6AddressToString(canonicalBinaryV6AddressFromString(ipv6Address));
+        } catch (UnknownHostException e) {
+            LOG.warn("Failed to compress IPv6 address {} because it is invalid", ipv6Address);
+            return ipv6Address;
+        }
+    }
+
+    public static Ipv6Address compressedIpv6AddressFormat(final Ipv6Address ipv6Address) {
+        return new Ipv6Address(compressedIpv6FormatFromString(ipv6Address.getValue()));
     }
 
     public static Ipv6ArbitraryMask compressedIpv6MaskFormat(final Ipv6ArbitraryMask ipv6Mask) {
-        String stringIpv6Mask = ipv6Mask.getValue();
-        return new Ipv6ArbitraryMask(compressedIpv6Format(stringIpv6Mask));
+        return new Ipv6ArbitraryMask(compressedIpv6FormatFromString(ipv6Mask.getValue()));
     }
 }