pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.sdnip; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.io.IOException; |
| 6 | import java.net.InetAddress; |
| 7 | import java.util.HashMap; |
| 8 | import java.util.Iterator; |
| 9 | import java.util.Map; |
| 10 | |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 14 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 15 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
| 16 | |
| 17 | public class SdnIpTest { |
| 18 | |
| 19 | private Map<String, Interface> interfaces; |
| 20 | private InvertedRadixTree<Interface> interfaceRoutes; |
| 21 | |
| 22 | private Interface longestInterfacePrefixMatch(InetAddress address) { |
| 23 | Prefix prefixToSearchFor = new Prefix(address.getAddress(), |
| 24 | Prefix.MAX_PREFIX_LENGTH); |
| 25 | Iterator<Interface> it = |
| 26 | interfaceRoutes.getValuesForKeysPrefixing( |
| 27 | prefixToSearchFor.toBinaryString()).iterator(); |
| 28 | Interface intf = null; |
| 29 | // Find the last prefix, which will be the longest prefix |
| 30 | while (it.hasNext()) { |
| 31 | intf = it.next(); |
| 32 | } |
| 33 | |
| 34 | return intf; |
| 35 | } |
| 36 | /** |
| 37 | * This is just a test of the InvertedRadixTree, rather than an actual unit |
| 38 | * test of SdnIp. |
| 39 | * |
| 40 | * @throws IOException |
| 41 | */ |
| 42 | @Test |
| 43 | public void getOutgoingInterfaceTest() throws IOException { |
| 44 | |
| 45 | interfaces = new HashMap<>(); |
| 46 | interfaceRoutes = new ConcurrentInvertedRadixTree<>( |
| 47 | new DefaultByteArrayNodeFactory()); |
| 48 | |
| 49 | Interface interface1 = new Interface("sw3-eth1", "00:00:00:00:00:00:00:a3", |
| 50 | (short) 1, "192.168.10.101", 24); |
| 51 | interfaces.put(interface1.getName(), interface1); |
| 52 | Interface interface2 = new Interface("sw5-eth1", "00:00:00:00:00:00:00:a5", |
| 53 | (short) 1, "192.168.20.101", 16); |
| 54 | interfaces.put(interface2.getName(), interface2); |
| 55 | Interface interface3 = new Interface("sw2-eth1", "00:00:00:00:00:00:00:a2", |
| 56 | (short) 1, "192.168.60.101", 16); |
| 57 | interfaces.put(interface3.getName(), interface3); |
| 58 | Interface interface4 = new Interface("sw6-eth1", "00:00:00:00:00:00:00:a6", |
| 59 | (short) 1, "192.168.60.101", 30); |
| 60 | interfaces.put(interface4.getName(), interface4); |
| 61 | Interface interface5 = new Interface("sw4-eth4", "00:00:00:00:00:00:00:a4", |
| 62 | (short) 4, "192.168.60.101", 24); |
| 63 | interfaces.put(interface5.getName(), interface5); |
| 64 | |
| 65 | for (Interface intf : interfaces.values()) { |
| 66 | Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), |
| 67 | intf.getPrefixLength()); |
| 68 | interfaceRoutes.put(prefix.toBinaryString(), intf); |
| 69 | } |
| 70 | |
| 71 | // Check whether the prefix length takes effect |
| 72 | InetAddress nextHopAddress = InetAddress.getByName("192.0.0.1"); |
| 73 | assertNotNull(nextHopAddress); |
| 74 | assertNull(longestInterfacePrefixMatch(nextHopAddress)); |
| 75 | |
| 76 | // Check whether it returns the longest matchable address |
| 77 | nextHopAddress = InetAddress.getByName("192.168.60.101"); |
| 78 | assertEquals("sw6-eth1", longestInterfacePrefixMatch(nextHopAddress).getName()); |
| 79 | |
| 80 | } |
| 81 | } |