2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.util;
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class HashUtilTest {
22 private static final Logger LOG = LoggerFactory.getLogger(HashUtilTest.class);
24 private static final MacAddress[] MAC_ADDRESSES = {
25 new MacAddress("00:00:00:00:00:00"),
26 new MacAddress("AA:BB:CC:DD:EE:FF"),
27 new MacAddress("FF:EE:DD:CC:BB:AA"),
28 new MacAddress("0A:37:C1:00:AB:FF"),
29 new MacAddress("0A:37:C1:00:AB:FE"),
30 new MacAddress("0A:37:C1:00:FE:AB")
33 private static final Ipv4Prefix[] IPV_4_PREFIXES = {
34 new Ipv4Prefix("10.0.0.1/24"),
35 new Ipv4Prefix("10.0.1.0/24"),
36 new Ipv4Prefix("10.0.1.0/31"),
37 new Ipv4Prefix("0.0.0.0/32"),
38 new Ipv4Prefix("4.3.2.1/32"),
39 new Ipv4Prefix("1.2.3.4/32")
42 private static final Ipv6Prefix[] IPV_6_PREFIXES = {
43 new Ipv6Prefix("FFFF:0DB8:0000:0000:0000:0000:1428:57ab/128"),
44 new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:1428:57ab/128"),
45 new Ipv6Prefix("0DB8:2001:0000:0000:0000:0000:1428:57ab/128"),
46 new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:57ab:1428/128")
49 private static final Ipv6Prefix shortIpv6 = new Ipv6Prefix("fe80::2acf:e9ff:fe21:6431/128");
50 private static final Ipv6Prefix fullIpv6 = new Ipv6Prefix("fe80:0000:2acf:e9ff:fe21:6431:0000:0000/128");
51 private static final Ipv6Prefix[] IPV_6_PREFIXES_WITH_MASK = {
52 new Ipv6Prefix("FFFF:0DB8:0000:0000:0000:0000:1428:57ab/72"),
53 new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:1428:57ab/94"),
54 new Ipv6Prefix("0DB8:2001:0000:0000:0000:0000:1428:57ab/32"),
55 new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:57ab:1428/64")
60 public void trivialLongTest() {
62 final Long longA = new Long(42);
63 final Long longB = new Long(42);
64 Assert.assertNotSame(longA, longB);
65 final Long longC = Long.valueOf(42);
66 final Long longD = Long.valueOf(42);
67 Assert.assertSame(longC, longD);
73 public void testCalculateMatchHash() throws Exception {
74 final long hashShort = HashUtil.calculateIpv6PrefixHash(shortIpv6);
75 final long hashLong = HashUtil.calculateIpv6PrefixHash(fullIpv6);
76 Assert.assertEquals(hashShort, hashLong);
80 public void testCalculateMacAddressHash() {
81 for (int i = 0; i < MAC_ADDRESSES.length - 1; i++) {
82 final long hash = HashUtil.calculateMacAddressHash(MAC_ADDRESSES[i]);
83 final long otherHash = HashUtil.calculateMacAddressHash(MAC_ADDRESSES[i + 1]);
84 Assert.assertNotEquals(hash, otherHash);
89 public void testCalculateIpv4PrefixHash() {
90 for (int i = 0; i < IPV_4_PREFIXES.length - 1; i++) {
91 final Ipv4Prefix prefixA = IPV_4_PREFIXES[i];
92 final Ipv4Prefix prefixB = IPV_4_PREFIXES[i + 1];
93 final long hash = HashUtil.calculateIpv4PrefixHash(prefixA);
94 final long hash_n = HashUtil.calculateIpv4PrefixHash(prefixB);
95 LOG.info("Comparing {} vs. {} (hash {} vs. hash {})", prefixA, prefixB, hash, hash_n);
96 Assert.assertNotEquals(hash, hash_n);
102 public void testCalculateIpv6PrefixHash() {
103 for (int i = 0; i < IPV_6_PREFIXES.length - 1; i++) {
104 final long hash_n = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES[i]);
105 final long hash_n1 = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES[i + 1]);
106 Assert.assertNotNull(hash_n);
107 Assert.assertNotNull(hash_n1);
108 Assert.assertNotEquals(hash_n, hash_n1);
113 public void testCalculateIpv6PrefixHashWithMask() {
114 for (int i = 0; i < IPV_6_PREFIXES.length - 1; i++) {
115 final long hash_n = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES_WITH_MASK[i]);
116 final long hash_n1 = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES_WITH_MASK[i + 1]);
117 Assert.assertNotNull(hash_n);
118 Assert.assertNotNull(hash_n1);
119 Assert.assertNotEquals(hash_n, hash_n1);
124 public void calculateIpMatchHash() {
125 final IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
126 ipMatchBuilder.setIpEcn((short) 42);
127 HashUtil.calculateIpMatchHash(ipMatchBuilder.build());