Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
| 5 | import static org.junit.Assert.fail; |
| 6 | |
| 7 | import java.net.InetAddress; |
| 8 | import java.net.UnknownHostException; |
| 9 | import java.util.Arrays; |
| 10 | import java.util.HashMap; |
| 11 | import java.util.Iterator; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | import org.junit.After; |
| 15 | import org.junit.Before; |
| 16 | import org.junit.Ignore; |
| 17 | import org.junit.Test; |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import com.google.common.net.InetAddresses; |
| 22 | |
| 23 | public class PtreeTest { |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 24 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | private Logger log = LoggerFactory.getLogger(PtreeTest.class); |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 26 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | private Ptree ptree; |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame^] | 28 | private PatriciaTree<RibEntry> ooPtree; |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 29 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | private Map<String, byte[]> byteAddresses; |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 31 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | @Before |
| 33 | public void setUp() throws Exception { |
| 34 | ptree = new Ptree(32); |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame^] | 35 | ooPtree = new PatriciaTree<RibEntry>(32); |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 36 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | String[] strPrefixes = { |
| 38 | "192.168.10.0/24", |
| 39 | "192.168.10.0/23", |
| 40 | "192.168.10.0/22", |
| 41 | "192.0.0.0/7", |
| 42 | "192.168.11.0/24", |
| 43 | "10.0.23.128/25", |
| 44 | "206.17.144.0/20", |
| 45 | "9.17.0.0/12", |
| 46 | "192.168.0.0/16" |
| 47 | }; |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 48 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | byteAddresses = new HashMap<String, byte[]>(strPrefixes.length + 10); |
| 50 | for (String prefix : strPrefixes) { |
| 51 | String address = prefix.split("/")[0]; |
| 52 | int prefixLength = Integer.parseInt(prefix.split("/")[1]); |
| 53 | byteAddresses.put(prefix, InetAddresses.forString(address).getAddress()); |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 54 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 55 | PtreeNode node = ptree.acquire(byteAddresses.get(prefix), prefixLength); |
Pavlin Radoslavov | b630929 | 2014-04-11 03:25:47 -0700 | [diff] [blame] | 56 | // node.rib = new RibEntry("192.168.10.101", "192.168.60.1"); |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame^] | 57 | ooPtree.put(new Prefix(byteAddresses.get(prefix), prefixLength), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | new RibEntry("192.168.10.101", "192.168.60.1")); |
| 59 | } |
| 60 | } |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | } |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 65 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | @Ignore |
| 67 | @Test |
| 68 | public void testAcquireByteArray() { |
| 69 | fail("Not yet implemented"); |
| 70 | } |
| 71 | |
| 72 | @Ignore |
| 73 | @Test |
| 74 | public void testAcquireByteArrayInt() { |
| 75 | //First let's test an empty Ptree |
| 76 | Ptree localPtree = new Ptree(32); |
| 77 | PtreeNode node = localPtree.acquire(new byte[]{0x00, 0x00, 0x00, 0x00}); |
Pavlin Radoslavov | b630929 | 2014-04-11 03:25:47 -0700 | [diff] [blame] | 78 | // assertTrue(node != null && node.rib == null); |
| 79 | assertTrue(node != null); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | |
| 81 | //Now let's look at the prepopulated tree |
| 82 | String testPrefix = "206.17.144.0/20"; |
| 83 | PtreeNode existingNode = ptree.acquire(byteAddresses.get(testPrefix), 20); |
| 84 | printByteArray(existingNode.key); |
| 85 | printByteArray(byteAddresses.get(testPrefix)); |
Pavlin Radoslavov | b630929 | 2014-04-11 03:25:47 -0700 | [diff] [blame] | 86 | // assertTrue(existingNode != null && existingNode.rib == null); |
| 87 | assertTrue(existingNode != null); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | |
| 89 | assertTrue(Arrays.equals(existingNode.key, byteAddresses.get(testPrefix))); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testLookup() { |
| 94 | String prefix1 = "192.168.10.12"; |
| 95 | int length1 = 29; |
| 96 | PtreeNode node1 = ptree.lookup(InetAddresses.forString(prefix1).getAddress(), length1); |
| 97 | |
| 98 | //There should be no direct match |
| 99 | assertTrue(node1 == null); |
| 100 | |
| 101 | log.debug("{} null: {}", "node1", node1 == null ? "true" : "false"); |
| 102 | |
| 103 | String prefix2 = "206.17.144.0"; |
| 104 | int length2 = 20; |
| 105 | PtreeNode node2 = ptree.lookup(InetAddresses.forString(prefix2).getAddress(), length2); |
| 106 | |
| 107 | assertTrue(node2 != null); |
| 108 | assertTrue(Arrays.equals(node2.key, byteAddresses.get(prefix2 + "/" + length2))); |
| 109 | |
| 110 | log.debug("{} null: {}", "node2", node2 == null ? "true" : "false"); |
| 111 | if (node2 != null) { |
| 112 | log.debug("{} key: {}, keybits: {}", new Object[]{"node2", node2.key, node2.keyBits}); |
| 113 | } |
| 114 | |
| 115 | String prefix3 = "192.0.0.0"; |
| 116 | int length3 = 7; |
| 117 | PtreeNode node3 = ptree.lookup(InetAddresses.forString(prefix3).getAddress(), length3); |
| 118 | assertTrue(node3 != null); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void testMatch() { |
| 123 | String prefix1 = "192.168.10.12"; |
| 124 | int length1 = 29; |
| 125 | PtreeNode node1 = ptree.match(InetAddresses.forString(prefix1).getAddress(), length1); |
| 126 | |
| 127 | //There should be no direct match, but we should get the covering prefix |
| 128 | assertTrue(node1 != null); |
| 129 | assertTrue(Arrays.equals(node1.key, byteAddresses.get("192.168.10.0/24"))); |
| 130 | |
| 131 | log.debug("{} null: {}", "node1", node1 == null ? "true" : "false"); |
| 132 | if (node1 != null) { |
| 133 | log.debug("{} key: {}, keybits: {}", new Object[]{"node1", node1.key, node1.keyBits}); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Ignore |
| 138 | @Test |
| 139 | public void testTraverse() { |
| 140 | |
| 141 | String expected = "[0, 0, 0, 0]/0\n"; |
| 142 | expected += "[8, 0, 0, 0]/6\n"; |
| 143 | expected += "[9, 17, 0, 0]/12\n"; |
| 144 | expected += "[10, 0, 23, -128]/25\n"; |
| 145 | expected += "[-64, 0, 0, 0]/4\n"; |
| 146 | expected += "[-64, -88, 0, 0]/16\n"; |
| 147 | expected += "[-64, -88, 8, 0]/22\n"; |
| 148 | expected += "[-64, -88, 10, 0]/23\n"; |
| 149 | expected += "[-64, -88, 10, 0]/24\n"; |
| 150 | expected += "[-64, -88, 11, 0]/24\n"; |
| 151 | expected += "[-50, 17, -112, 0]/20\n"; |
| 152 | |
| 153 | PtreeNode node; |
| 154 | String result = ""; |
| 155 | |
| 156 | for (node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 157 | result += printByteArray(node.key) + "/" + node.keyBits + "\n"; |
| 158 | } |
| 159 | |
| 160 | assertEquals(expected, result); |
| 161 | } |
| 162 | |
| 163 | @Ignore |
| 164 | @Test |
| 165 | public void testBegin() { |
| 166 | fail("Not yet implemented"); |
| 167 | } |
| 168 | |
| 169 | @Ignore |
| 170 | @Test |
| 171 | public void testNext() { |
| 172 | fail("Not yet implemented"); |
| 173 | } |
| 174 | |
| 175 | @Ignore |
| 176 | @Test |
| 177 | public void testDelReference() { |
| 178 | fail("Not yet implemented"); |
| 179 | } |
| 180 | |
| 181 | @Ignore |
| 182 | @Test |
| 183 | public void testMisc() { |
| 184 | int bitIndex = -1; |
| 185 | int index = (int) (bitIndex / Byte.SIZE); |
| 186 | int bit = (int) (bitIndex % Byte.SIZE); |
| 187 | |
| 188 | log.debug("index {} bit {}", index, bit); |
| 189 | log.debug("percent {}", 1 % 8); |
| 190 | |
| 191 | //PtreeNode node1 = new PtreeNode(new byte[] {0x0, 0x0, 0x0, 0x0}, 0, 4); |
| 192 | PtreeNode node1 = new PtreeNode(null, 0, 4); |
| 193 | log.debug("node1: key {}, keybits {}", printByteArray(node1.key), node1.keyBits); |
| 194 | |
| 195 | //PtreeNode node2 = new PtreeNode(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff}, |
| 196 | PtreeNode node2 = new PtreeNode(null, |
| 197 | 32, 4); |
| 198 | log.debug("node2: key {}, keybits {}", printByteArray(node2.key), node2.keyBits); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testIteration() { |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame^] | 203 | Iterator<IPatriciaTree.Entry<RibEntry>> it = ooPtree.iterator(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 204 | |
| 205 | while (it.hasNext()) { |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame^] | 206 | IPatriciaTree.Entry<RibEntry> entry = it.next(); |
| 207 | log.debug("PatriciaTree prefix {} \t {}", entry.getPrefix(), entry.getPrefix().printAsBits()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | try { |
| 211 | PtreeNode node; |
| 212 | for (node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 213 | log.debug("Ptree prefix {}/{}", InetAddress.getByAddress(node.key).getHostAddress(), node.keyBits); |
| 214 | } |
| 215 | } catch (UnknownHostException e) { |
| 216 | |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | private String printByteArray(byte[] array) { |
| 221 | String result = "["; |
| 222 | for (byte b : array) { |
| 223 | result += b + ", "; |
| 224 | } |
| 225 | result = result.substring(0, result.length() - 2); |
| 226 | result += "]"; |
| 227 | return result; |
| 228 | } |
Jonathan Hart | 8f5f468 | 2013-08-07 22:13:39 +1200 | [diff] [blame] | 229 | |
| 230 | } |