From cc9faaec541abf68f500633c0f4e70f7760a78d4 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 18 Nov 2018 22:46:38 +0100 Subject: [PATCH] Do not use String.split() in VpnUtil 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 --- .../netvirt/vpnmanager/VpnUtil.java | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/VpnUtil.java b/vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/VpnUtil.java index a74ebbd4b7..35081e1eef 100644 --- a/vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/VpnUtil.java +++ b/vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/VpnUtil.java @@ -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 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 oldVpnListCopy, List 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; -- 2.36.6