f7451a475c290a4dbb846b86daa4162017e96552
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / HashUtilTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.util;
10
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.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class HashUtilTest {
20
21     private static final Logger LOG = LoggerFactory.getLogger(HashUtilTest.class);
22
23     private static final MacAddress[] MAC_ADDRESSES = {
24             new MacAddress("00:00:00:00:00:00"),
25             new MacAddress("AA:BB:CC:DD:EE:FF"),
26             new MacAddress("FF:EE:DD:CC:BB:AA"),
27             new MacAddress("0A:37:C1:00:AB:FF"),
28             new MacAddress("0A:37:C1:00:AB:FE"),
29             new MacAddress("0A:37:C1:00:FE:AB")
30     };
31
32     private static final Ipv4Prefix[] IPV_4_PREFIXES = {
33             new Ipv4Prefix("10.0.0.1/24"),
34             new Ipv4Prefix("10.0.1.0/24"),
35             new Ipv4Prefix("10.0.1.0/31"),
36             new Ipv4Prefix("0.0.0.0/32"),
37             new Ipv4Prefix("4.3.2.1/32"),
38             new Ipv4Prefix("1.2.3.4/32")
39     };
40
41     private static final Ipv6Prefix[] IPV_6_PREFIXES = {
42             new Ipv6Prefix("FFFF:0DB8:0000:0000:0000:0000:1428:57ab"),
43             new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:1428:57ab"),
44             new Ipv6Prefix("0DB8:2001:0000:0000:0000:0000:1428:57ab"),
45             new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:57ab:1428")
46     };
47
48     private static final Ipv6Prefix shortIpv6 = new Ipv6Prefix("fe80::2acf:e9ff:fe21:6431/128");
49     private static final Ipv6Prefix fullIpv6 = new Ipv6Prefix("fe80:0000:2acf:e9ff:fe21:6431:0000:0000/128");
50     private static final Ipv6Prefix[] IPV_6_PREFIXES_WITH_MASK = {
51             new Ipv6Prefix("FFFF:0DB8:0000:0000:0000:0000:1428:57ab/72"),
52             new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:1428:57ab/94"),
53             new Ipv6Prefix("0DB8:2001:0000:0000:0000:0000:1428:57ab/32"),
54             new Ipv6Prefix("2001:0DB8:0000:0000:0000:0000:57ab:1428/64")
55     };
56
57
58     @Test
59     public void trivialLongTest() {
60
61         Long longA = new Long(42);
62         Long longB = new Long(42);
63         Assert.assertNotSame(longA, longB);
64         Long longC = Long.valueOf(42);
65         Long longD = Long.valueOf(42);
66         Assert.assertSame(longC, longD);
67
68     }
69
70
71     @Test
72     public void testCalculateMatchHash() throws Exception {
73         long hashShort = HashUtil.calculateIpv6PrefixHash(shortIpv6);
74         long hashLong = HashUtil.calculateIpv6PrefixHash(fullIpv6);
75         Assert.assertEquals(hashShort, hashLong);
76     }
77
78     @Test
79     public void testCalculateMacAddressHash() {
80         for (int i = 0; i < MAC_ADDRESSES.length - 1; i++) {
81             long hash = HashUtil.calculateMacAddressHash(MAC_ADDRESSES[i]);
82             long otherHash = HashUtil.calculateMacAddressHash(MAC_ADDRESSES[i + 1]);
83             Assert.assertNotEquals(hash, otherHash);
84         }
85     }
86
87     @Test
88     public void testCalculateIpv4PrefixHash() {
89         for (int i = 0; i < IPV_4_PREFIXES.length - 1; i++) {
90             Ipv4Prefix prefixA = IPV_4_PREFIXES[i];
91             Ipv4Prefix prefixB = IPV_4_PREFIXES[i + 1];
92             long hash = HashUtil.calculateIpv4PrefixHash(prefixA);
93             long hash_n = HashUtil.calculateIpv4PrefixHash(prefixB);
94             LOG.info("Comparing {} vs. {} (hash {} vs. hash {})", prefixA, prefixB, hash, hash_n);
95             Assert.assertNotEquals(hash, hash_n);
96         }
97
98     }
99
100     @Test
101     public void testCalculateIpv6PrefixHash() {
102         for (int i = 0; i < IPV_6_PREFIXES.length - 1; i++) {
103             long hash_n = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES[i]);
104             long hash_n1 = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES[i + 1]);
105             Assert.assertNotNull(hash_n);
106             Assert.assertNotNull(hash_n1);
107             Assert.assertNotEquals(hash_n, hash_n1);
108         }
109     }
110
111     @Test
112     public void testCalculateIpv6PrefixHashWithMask() {
113         for (int i = 0; i < IPV_6_PREFIXES.length - 1; i++) {
114             long hash_n = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES_WITH_MASK[i]);
115             long hash_n1 = HashUtil.calculateIpv6PrefixHash(IPV_6_PREFIXES_WITH_MASK[i + 1]);
116             Assert.assertNotNull(hash_n);
117             Assert.assertNotNull(hash_n1);
118             Assert.assertNotEquals(hash_n, hash_n1);
119         }
120     }
121
122 }