Bump upstreams
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / util / MaskUtil.java
index 207cb96b8d1da1f00c6d86e617f0807898c420d9..f2ecd098956319697b25be94fe24520be834393f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014, 2017 Contextream, 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,
@@ -9,13 +9,11 @@ package org.opendaylight.lispflowmapping.lisp.util;
 
 import com.google.common.base.Preconditions;
 import com.google.common.net.InetAddresses;
-
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
-
 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.IpPrefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
@@ -33,13 +31,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6Binary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class MaskUtil {
     private static final Logger LOG = LoggerFactory.getLogger(MaskUtil.class);
-    private static final short IPV4_MAX_MASK = 32;
-    private static final short IPV6_MAX_MASK = 128;
+    public static final short IPV4_MAX_MASK = 32;
+    public static final Uint8 IPV4_MAX_MASK_UINT = Uint8.valueOf(IPV4_MAX_MASK);
+    public static final short IPV6_MAX_MASK = 128;
+    public static final Uint8 IPV6_MAX_MASK_UINT = Uint8.valueOf(IPV6_MAX_MASK);
 
     // Utility class, should not be instantiated
     private MaskUtil() {
@@ -63,17 +64,17 @@ public final class MaskUtil {
         return false;
     }
 
-    private static final int slashPosition(final String prefix) {
+    private static int slashPosition(final String prefix) {
         final int slash = prefix.lastIndexOf('/');
         Preconditions.checkArgument(slash >= 0, "Argument %s does not contain a slash", prefix);
         return slash;
     }
 
-    protected static String getPrefixAddress(final String prefix) {
+    public static String getPrefixAddress(final String prefix) {
         return prefix.substring(0, slashPosition(prefix));
     }
 
-    protected static String getPrefixMask(final String prefix) {
+    public static String getPrefixMask(final String prefix) {
         return prefix.substring(slashPosition(prefix) + 1);
     }
 
@@ -114,11 +115,11 @@ public final class MaskUtil {
         try {
             if (address instanceof Ipv4PrefixBinary) {
                 byte[] addr = ((Ipv4PrefixBinary) address).getIpv4AddressBinary().getValue();
-                short mask = ((Ipv4PrefixBinary) address).getIpv4MaskLength();
+                short mask = ((Ipv4PrefixBinary) address).getIpv4MaskLength().toJava();
                 return LispAddressUtil.asIpv4PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
             } else if (address instanceof Ipv6PrefixBinary) {
                 byte[] addr = ((Ipv6PrefixBinary) address).getIpv6AddressBinary().getValue();
-                short mask = ((Ipv6PrefixBinary) address).getIpv6MaskLength();
+                short mask = ((Ipv6PrefixBinary) address).getIpv6MaskLength().toJava();
                 return LispAddressUtil.asIpv6PrefixBinaryEid(eid, normalizeByteArray(addr, mask), mask);
             } else if (address instanceof Ipv4Prefix) {
                 String[] v4prefix = splitPrefix(((Ipv4Prefix)address).getIpv4Prefix().getValue());
@@ -141,8 +142,8 @@ public final class MaskUtil {
                 return normalizeSrcDst(eid);
             } else if (address instanceof ServicePath) {
                 // Build new Service Path eid with service index set to 0
-                long spi = ((ServicePath) address).getServicePath().getServicePathId().getValue();
-                long vni = eid.getVirtualNetworkId() != null ? eid.getVirtualNetworkId().getValue() : -1;
+                long spi = ((ServicePath) address).getServicePath().getServicePathId().getValue().toJava();
+                long vni = eid.getVirtualNetworkId() != null ? eid.getVirtualNetworkId().getValue().toJava() : -1;
                 return LispAddressUtil.asServicePathEid(vni, spi, (short)0);
             }
         } catch (UnknownHostException e) {
@@ -168,26 +169,26 @@ public final class MaskUtil {
     }
 
     private static IpPrefix normalizeIpPrefix(IpPrefix address) throws UnknownHostException {
-        String[] prefix = splitPrefix(String.valueOf(address.getValue()));
+        String[] prefix = splitPrefix(address.stringValue());
         short mask = Short.parseShort(prefix[1]);
 
         InetAddress normalizedAddress = normalizeIP(InetAddresses.forString(prefix[0]), mask);
-        return IetfInetUtil.INSTANCE.ipPrefixFor(normalizedAddress.getAddress(), mask);
+        return IetfInetUtil.ipPrefixFor(normalizedAddress.getAddress(), mask);
     }
 
     private static InetAddress normalizeIP(InetAddress address, int maskLength) throws UnknownHostException {
         return InetAddress.getByAddress(normalizeByteArray(address.getAddress(), (short) maskLength));
     }
 
-    private static byte[] normalizeByteArray(byte[] address, short maskLength) {
+    public static byte[] normalizeByteArray(byte[] address, short maskLength) {
         ByteBuffer byteRepresentation = ByteBuffer.wrap(address);
-        byte b = (byte) 0xff;
+        byte byteMask = (byte) 0xff;
         int mask = maskLength;
         for (int i = 0; i < byteRepresentation.array().length; i++) {
             if (mask >= 8) {
-                byteRepresentation.put(i, (byte) (b & byteRepresentation.get(i)));
+                byteRepresentation.put(i, (byte) (byteMask & byteRepresentation.get(i)));
             } else if (mask > 0) {
-                byteRepresentation.put(i, (byte) ((byte) (b << (8 - mask)) & byteRepresentation.get(i)));
+                byteRepresentation.put(i, (byte) ((byte) (byteMask << 8 - mask) & byteRepresentation.get(i)));
             } else {
                 byteRepresentation.put(i, (byte) (0 & byteRepresentation.get(i)));
             }
@@ -209,13 +210,6 @@ public final class MaskUtil {
         }
     }
 
-    public static short getMaskForAddress(SimpleAddress address) {
-        if (address.getIpPrefix() == null) {
-            return -1;
-        }
-        return getMaskForIpPrefix(address.getIpPrefix());
-    }
-
     private static String getIpPrefixString(IpPrefix prefix) {
         if (prefix.getIpv4Prefix() != null) {
             return prefix.getIpv4Prefix().getValue();
@@ -242,6 +236,13 @@ public final class MaskUtil {
         return getPrefixAddress(prefix.getIpv6Prefix().getValue());
     }
 
+    public static short getMaskForAddress(SimpleAddress address) {
+        if (address.getIpPrefix() == null) {
+            return -1;
+        }
+        return getMaskForIpPrefix(address.getIpPrefix());
+    }
+
     public static short getMaskForAddress(Address address) {
         if (address instanceof Ipv4) {
             return IPV4_MAX_MASK;
@@ -258,9 +259,9 @@ public final class MaskUtil {
         } else if (address instanceof InstanceId) {
             return getMaskForAddress(((InstanceId)address).getInstanceId().getAddress());
         } else if (address instanceof Ipv4PrefixBinary) {
-            return ((Ipv4PrefixBinary) address).getIpv4MaskLength();
+            return ((Ipv4PrefixBinary) address).getIpv4MaskLength().toJava();
         } else if (address instanceof Ipv6PrefixBinary) {
-            return ((Ipv6PrefixBinary) address).getIpv6MaskLength();
+            return ((Ipv6PrefixBinary) address).getIpv6MaskLength().toJava();
         }
         return -1;
     }