Do not use String.split() in VpnUtil 23/77923/3
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 18 Nov 2018 21:46:38 +0000 (22:46 +0100)
committerRobert Varga <nite@hq.sk>
Fri, 23 Nov 2018 18:08:04 +0000 (18:08 +0000)
Rather than compiling regexes and allocating String[]s, use a simple
indexOf() and substring getter to implement almost equivalent
functionality. This breaks on malformed inputs, like 1.2.3.4//24, but
that should be fine.

Change-Id: I1cb294abb3f17c84d332f8075f84e62c1dea86a6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/VpnUtil.java

index a74ebbd4b79a20523dcc5d62704612ba8d306174..35081e1eef7462a285faf6b4a3080bfaf1b76684 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,32 +2262,38 @@ 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) {
@@ -2332,8 +2334,8 @@ public final class VpnUtil {
     }
 
     public boolean isDualRouterVpnUpdate(List<String> oldVpnListCopy, List<String> newVpnListCopy) {
-        if ((oldVpnListCopy.size() == 2 && newVpnListCopy.size() == 3)
-                || (oldVpnListCopy.size() == 3 && newVpnListCopy.size() == 2)) {
+        if (oldVpnListCopy.size() == 2 && newVpnListCopy.size() == 3
+                || oldVpnListCopy.size() == 3 && newVpnListCopy.size() == 2) {
             return true;
         }
         return false;