package org.opendaylight.openflowplugin.impl.util;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.CharMatcher;
import java.math.BigInteger;
import java.util.StringTokenizer;
-import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import java.util.regex.Pattern;
import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpVersion;
public final class HashUtil {
private static final Logger LOG = LoggerFactory.getLogger(HashUtil.class);
+ private static final CharMatcher SLASH_MATCHER = CharMatcher.is('/');
+ private static final Pattern DOUBLE_COLON = Pattern.compile("::");
private static final int BASE_16 = 16;
private static final int BASE_10 = 10;
private static final long IPV6_TOKENS_COUNT = 8;
throw new IllegalStateException("This class should not be instantiated.");
}
- public static long calculateEthernetMatchHash(EthernetMatch ethernetMatch) {
+ public static long calculateEthernetMatchHash(final EthernetMatch ethernetMatch) {
long hash = 0;
EthernetType ethernetType = ethernetMatch.getEthernetType();
return hash;
}
- public static long calculateEthenetSourceHash(EthernetSource ethernetSource) {
+ public static long calculateEthenetSourceHash(final EthernetSource ethernetSource) {
long hash = calculateMacAddressHash(ethernetSource.getAddress());
hash += calculateMacAddressHash(ethernetSource.getMask());
return hash;
}
- public static long calculateEthernetDestinationHash(EthernetDestination ethernetDestination) {
+ public static long calculateEthernetDestinationHash(final EthernetDestination ethernetDestination) {
long hash = calculateMacAddressHash(ethernetDestination.getAddress());
hash += calculateMacAddressHash(ethernetDestination.getMask());
return hash;
}
- public static long calculateMacAddressHash(MacAddress macAddress) {
+ public static long calculateMacAddressHash(final MacAddress macAddress) {
long hash = 0;
if (null != macAddress) {
return hash;
}
- public static long calculateMatchHash(final Match match, DeviceContext deviceContext) {
+ public static long calculateMatchHash(final Match match, final short version) {
long hash = 0;
long subHash = 0;
long base = 0;
base++;
if (null != match.getInPhyPort()) {
hash = 1 << base;
- subHash += calculateNodeConnectorIdHash(match.getInPhyPort(), deviceContext);
+ subHash += calculateNodeConnectorIdHash(match.getInPhyPort(), version);
}
base++;
if (null != match.getInPort()) {
hash = 1 << base;
- subHash += calculateNodeConnectorIdHash(match.getInPort(), deviceContext);
+ subHash += calculateNodeConnectorIdHash(match.getInPort(), version);
}
base++;
if (null != match.getIpMatch()) {
return hash;
}
- private static StringTokenizer getStringTokenizerWithFullAddressString(String value) {
- String ipv6Value = value.replace("::", ":0000:");
+ private static StringTokenizer getStringTokenizerWithFullAddressString(final String value) {
+ String ipv6Value = DOUBLE_COLON.matcher(value).replaceAll(":0000:");
StringTokenizer stringTokenizer = new StringTokenizer(ipv6Value, ":");
long delta = IPV6_TOKENS_COUNT - stringTokenizer.countTokens();
- StringBuffer additions = new StringBuffer();
+ StringBuilder additions = new StringBuilder();
if (delta > 0) {
while (delta > 0) {
additions.append(IPV6_TOKEN);
delta--;
}
- if (ipv6Value.contains("/")) {
- ipv6Value = ipv6Value.replace("/", additions.toString() + "/");
+ if (ipv6Value.indexOf('/') != -1) {
+ additions.append('/');
+ ipv6Value = SLASH_MATCHER.replaceFrom(ipv6Value, additions.toString());
} else {
ipv6Value += additions.toString();
}
return stringTokenizer;
}
- private static long calculateStopperBasedOnMaskValue(final Ipv6Prefix ipv6Prefix, long bitsBase) {
+ private static long calculateStopperBasedOnMaskValue(final Ipv6Prefix ipv6Prefix, final long bitsBase) {
double maskValue = extractMask(ipv6Prefix);
double bitCount = maskValue / bitsBase;
return (int) Math.ceil(bitCount);
return mask;
}
- private static long parseTokens(final StringTokenizer stringTokenizer, int base, int bitShift) {
+ private static long parseTokens(final StringTokenizer stringTokenizer, final int base, final int bitShift) {
return parseTokens(stringTokenizer, 0, base, bitShift);
}
- private static long parseTokens(final StringTokenizer stringTokenizer, long stopper, int base, int bitShift) {
+ private static long parseTokens(final StringTokenizer stringTokenizer, final long stopper, final int base, final int bitShift) {
long hash = 0;
if (stringTokenizer.countTokens() > 0) {
long step = 0;
String token = stringTokenizer.nextToken();
step++;
- if (token.equals("")) {
+ if (token.isEmpty()) {
token = "0";
}
- if (token.contains("/")) {
+ if (token.indexOf('/') != -1) {
StringTokenizer tokenizer = new StringTokenizer(token, "/");
hash += parseTokens(tokenizer, stopper, base, bitShift);
} else {
}
- private static long calculateIpAdressHash(final String address, long numberOfParts, int base) {
+ private static long calculateIpAdressHash(final String address, final long numberOfParts, final int base) {
StringTokenizer stringTokenizer = new StringTokenizer(address, ".");
long hash = parseTokens(stringTokenizer, numberOfParts, base, 8);
return hash;
}
- private static long calculateIpMatchHash(final IpMatch ipMatch) {
+ @VisibleForTesting
+ public static long calculateIpMatchHash(final IpMatch ipMatch) {
long hash = 0;
Short ipEcn = ipMatch.getIpEcn();
if (null != ipEcn) {
hash += ipProtocol;
}
- Short ipDscp = ipMatch.getIpDscp().getValue();
- if (null != ipDscp) {
- hash += ipDscp;
+ if (null != ipMatch.getIpDscp()) {
+ Short ipDscp = ipMatch.getIpDscp().getValue();
+ if (null != ipDscp) {
+ hash += ipDscp;
+ }
}
IpVersion ipVersion = ipMatch.getIpProto();
- if (null != ipVersion) {
+ if (null != ipVersion)
+
+ {
hash += ipVersion.getIntValue();
}
+
return hash;
}
- private static long calculateNodeConnectorIdHash(final NodeConnectorId inPhyPort, DeviceContext deviceContext) {
+ private static long calculateNodeConnectorIdHash(final NodeConnectorId inPhyPort, final short version) {
long hash = 0;
- short version = deviceContext.getDeviceState().getVersion();
Long portFromLogicalName = OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.get(version), inPhyPort.getValue());
hash += portFromLogicalName.intValue();
return hash;