Make VpnUtil.getVpnListForVpnInterface() static
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnUtil.java
index c14d526c6dbd674fdc2953f38c87586e81ddfef4..f5655253986a504ee1827b9b4e3458b728454136 100644 (file)
@@ -799,11 +799,7 @@ public final class VpnUtil {
     }
 
     public static String getIpPrefix(String prefix) {
-        String[] prefixValues = prefix.split("/");
-        if (prefixValues.length == 1) {
-            prefix = NWUtil.toIpPrefix(prefix);
-        }
-        return prefix;
+        return prefix.indexOf('/') != -1 ? prefix : NWUtil.toIpPrefix(prefix);
     }
 
     static final FutureCallback<Void> DEFAULT_CALLBACK =
@@ -1318,7 +1314,7 @@ public final class VpnUtil {
     }
 
     private boolean doesInterfaceAndHiddenIpAddressTypeMatch(InetAddress hiddenIp, FixedIps portIp) {
-        return (hiddenIp instanceof Inet4Address && portIp.getIpAddress().getIpv4Address() != null)
+        return hiddenIp instanceof Inet4Address && portIp.getIpAddress().getIpv4Address() != null
                 || hiddenIp instanceof Inet6Address && portIp.getIpAddress().getIpv6Address() != null;
     }
 
@@ -2266,39 +2262,45 @@ public final class VpnUtil {
            Also the Subnet overlap in a VPN detection logic to be addressed for router-based-l3vpns.
     */
     static boolean areSubnetsOverlapping(String cidr1, String cidr2) {
-        String[] ipaddressValues1 = cidr1.split("/");
-        int address1 = InetAddresses.coerceToInteger(InetAddresses.forString(ipaddressValues1[0]));
-        int cidrPart1 = Integer.parseInt(ipaddressValues1[1]);
-        String[] ipaddressValues2 = cidr2.split("/");
-        int address2 = InetAddresses.coerceToInteger(InetAddresses.forString(ipaddressValues2[0]));
-        int cidrPart2 = Integer.parseInt(ipaddressValues2[1]);
-        int comparedValue = 0;
+        final int slash1 = cidr1.indexOf('/');
+        final int address1 = addressForCidr(cidr1, slash1);
+        final int cidrPart1 = maskForCidr(cidr1, slash1);
+
+        final int slash2 = cidr2.indexOf('/');
+        final int address2 = addressForCidr(cidr2, slash2);
+        final int cidrPart2 = maskForCidr(cidr2, slash2);
+
+        final int comparedValue = cidrPart1 <= cidrPart2 ? compare(address1, cidrPart1, address2)
+                : compare(address2, cidrPart2, address1);
+        return comparedValue == 0;
+    }
+
+    private static int addressForCidr(String cidr, int slash) {
+        return InetAddresses.coerceToInteger(InetAddresses.forString(cidr.substring(0, slash)));
+    }
+
+    private static int maskForCidr(String cidr, int slash) {
+        return Integer.parseInt(cidr.substring(slash + 1));
+    }
+
+    private static int compare(int address, int cidrPart, int address2) {
+        int prefix = address2 & computeNetmask(cidrPart);
+        return address ^ prefix;
+    }
+
+    private static int computeNetmask(int cidrPart) {
         int netmask = 0;
-        if (cidrPart1 <= cidrPart2) {
-            for (int j = 0; j < cidrPart1; ++j) {
-                netmask |= (1 << 31 - j);
-            }
-            int prefix = address2 & netmask;
-            comparedValue = address1 ^ prefix;
-        } else {
-            for (int j = 0; j < cidrPart2; ++j) {
-                netmask |= (1 << 31 - j);
-            }
-            int prefix = address1 & netmask;
-            comparedValue = address2 ^ prefix;
-        }
-        if (comparedValue == 0) {
-            return  true;
-        } else {
-            return false;
+        for (int j = 0; j < cidrPart; ++j) {
+            netmask |= 1 << 31 - j;
         }
+        return netmask;
     }
 
     public static String buildIpMonitorJobKey(String ip, String vpnName) {
         return VpnConstants.IP_MONITOR_JOB_PREFIX_KEY + "-" + vpnName + "-" + ip;
     }
 
-    public List<String> getVpnListForVpnInterface(VpnInterface vpnInter) {
+    public static List<String> getVpnListForVpnInterface(VpnInterface vpnInter) {
         return requireNonNullElse(vpnInter.getVpnInstanceNames(), Collections.<VpnInstanceNames>emptyList()).stream()
                 .map(VpnInstanceNames::getVpnName).collect(Collectors.toList());
     }
@@ -2330,4 +2332,9 @@ public final class VpnUtil {
     public static <T> T requireNonNullElse(@Nullable T obj, @Nonnull T defaultObj) {
         return obj != null ? obj : requireNonNull(defaultObj);
     }
+
+    public static boolean isDualRouterVpnUpdate(List<String> oldVpnListCopy, List<String> newVpnListCopy) {
+        return oldVpnListCopy.size() == 2 && newVpnListCopy.size() == 3
+                || oldVpnListCopy.size() == 3 && newVpnListCopy.size() == 2;
+    }
 }