tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.topology.impl; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 2 | |
| 3 | import org.junit.After; |
| 4 | import org.junit.Before; |
| 5 | import org.junit.Test; |
| 6 | import org.onlab.onos.event.Event; |
| 7 | import org.onlab.onos.event.impl.TestEventDispatcher; |
| 8 | import org.onlab.onos.net.ConnectPoint; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.Device; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.Link; |
| 11 | import org.onlab.onos.net.Path; |
| 12 | import org.onlab.onos.net.provider.AbstractProvider; |
| 13 | import org.onlab.onos.net.provider.ProviderId; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.topology.DefaultGraphDescription; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.topology.GraphDescription; |
| 16 | import org.onlab.onos.net.topology.LinkWeight; |
| 17 | import org.onlab.onos.net.topology.Topology; |
| 18 | import org.onlab.onos.net.topology.TopologyCluster; |
| 19 | import org.onlab.onos.net.topology.TopologyEdge; |
| 20 | import org.onlab.onos.net.topology.TopologyEvent; |
| 21 | import org.onlab.onos.net.topology.TopologyGraph; |
| 22 | import org.onlab.onos.net.topology.TopologyListener; |
| 23 | import org.onlab.onos.net.topology.TopologyProvider; |
| 24 | import org.onlab.onos.net.topology.TopologyProviderRegistry; |
| 25 | import org.onlab.onos.net.topology.TopologyProviderService; |
| 26 | import org.onlab.onos.net.topology.TopologyService; |
tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 27 | import org.onlab.onos.store.trivial.impl.SimpleTopologyStore; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.collect.ImmutableSet.of; |
| 34 | import static org.junit.Assert.*; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 35 | import static org.onlab.onos.net.NetTestTools.*; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 36 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 37 | import static org.onlab.onos.net.topology.ClusterId.clusterId; |
| 38 | import static org.onlab.onos.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED; |
| 39 | |
| 40 | /** |
| 41 | * Test of the topology subsystem. |
| 42 | */ |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 43 | public class TopologyManagerTest { |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 44 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 45 | private static final ProviderId PID = new ProviderId("of", "foo"); |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 46 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 47 | private TopologyManager mgr; |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 48 | |
| 49 | protected TopologyService service; |
| 50 | protected TopologyProviderRegistry registry; |
| 51 | protected TopologyProviderService providerService; |
| 52 | protected TestProvider provider; |
| 53 | protected TestListener listener = new TestListener(); |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() { |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 57 | mgr = new TopologyManager(); |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 58 | service = mgr; |
| 59 | registry = mgr; |
| 60 | |
tom | 10262dd | 2014-09-19 10:51:19 -0700 | [diff] [blame] | 61 | mgr.store = new SimpleTopologyStore(); |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 62 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 63 | mgr.activate(); |
| 64 | |
| 65 | service.addListener(listener); |
| 66 | |
| 67 | provider = new TestProvider(); |
| 68 | providerService = registry.register(provider); |
| 69 | |
| 70 | assertTrue("provider should be registered", |
| 71 | registry.getProviders().contains(provider.id())); |
| 72 | } |
| 73 | |
| 74 | @After |
| 75 | public void tearDown() { |
| 76 | mgr.deactivate(); |
| 77 | service.removeListener(listener); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void basics() { |
| 82 | Topology topology = service.currentTopology(); |
| 83 | assertNull("no topo expected", topology); |
| 84 | submitTopologyGraph(); |
| 85 | validateEvents(TOPOLOGY_CHANGED); |
| 86 | topology = service.currentTopology(); |
| 87 | assertTrue("should be latest", service.isLatest(topology)); |
| 88 | |
| 89 | submitTopologyGraph(); |
| 90 | validateEvents(TOPOLOGY_CHANGED); |
| 91 | assertFalse("should be latest", service.isLatest(topology)); |
| 92 | } |
| 93 | |
| 94 | private void submitTopologyGraph() { |
| 95 | Set<Device> devices = of(device("a"), device("b"), |
| 96 | device("c"), device("d"), |
| 97 | device("e"), device("f")); |
| 98 | Set<Link> links = of(link("a", 1, "b", 1), link("b", 1, "a", 1), |
| 99 | link("b", 2, "c", 1), link("c", 1, "b", 2), |
| 100 | link("c", 2, "d", 1), link("d", 1, "c", 2), |
| 101 | link("d", 2, "a", 2), link("a", 2, "d", 2), |
| 102 | link("e", 1, "f", 1), link("f", 1, "e", 1)); |
| 103 | GraphDescription data = new DefaultGraphDescription(4321L, devices, links); |
| 104 | providerService.topologyChanged(data, null); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void clusters() { |
| 109 | submitTopologyGraph(); |
| 110 | Topology topology = service.currentTopology(); |
| 111 | assertNotNull("topo expected", topology); |
| 112 | assertEquals("wrong cluster count", 2, topology.clusterCount()); |
| 113 | assertEquals("wrong device count", 6, topology.deviceCount()); |
| 114 | assertEquals("wrong link count", 10, topology.linkCount()); |
| 115 | assertEquals("wrong path count", 18, topology.pathCount()); |
| 116 | |
| 117 | assertEquals("wrong cluster count", 2, service.getClusters(topology).size()); |
| 118 | |
| 119 | TopologyCluster cluster = service.getCluster(topology, clusterId(0)); |
| 120 | assertEquals("wrong device count", 4, cluster.deviceCount()); |
| 121 | assertEquals("wrong device count", 4, service.getClusterDevices(topology, cluster).size()); |
| 122 | assertEquals("wrong link count", 8, cluster.linkCount()); |
| 123 | assertEquals("wrong link count", 8, service.getClusterLinks(topology, cluster).size()); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void structure() { |
| 128 | submitTopologyGraph(); |
| 129 | Topology topology = service.currentTopology(); |
| 130 | |
| 131 | assertTrue("should be infrastructure point", |
| 132 | service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(1)))); |
| 133 | assertFalse("should not be infrastructure point", |
| 134 | service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(3)))); |
| 135 | |
| 136 | // One of these cannot be a broadcast point... or we have a loop... |
tom | 88a6f11 | 2014-10-09 18:00:19 -0700 | [diff] [blame] | 137 | // assertFalse("should not be broadcast point", |
| 138 | // service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(1))) && |
| 139 | // service.isBroadcastPoint(topology, new ConnectPoint(did("b"), portNumber(1))) && |
| 140 | // service.isBroadcastPoint(topology, new ConnectPoint(did("c"), portNumber(1))) && |
| 141 | // service.isBroadcastPoint(topology, new ConnectPoint(did("d"), portNumber(1)))); |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 142 | assertTrue("should be broadcast point", |
| 143 | service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3)))); |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void graph() { |
| 148 | submitTopologyGraph(); |
| 149 | Topology topology = service.currentTopology(); |
| 150 | TopologyGraph graph = service.getGraph(topology); |
| 151 | assertEquals("wrong vertex count", 6, graph.getVertexes().size()); |
| 152 | assertEquals("wrong edge count", 10, graph.getEdges().size()); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void precomputedPath() { |
| 157 | submitTopologyGraph(); |
| 158 | Topology topology = service.currentTopology(); |
| 159 | Set<Path> paths = service.getPaths(topology, did("a"), did("c")); |
| 160 | assertEquals("wrong path count", 2, paths.size()); |
| 161 | Path path = paths.iterator().next(); |
| 162 | assertEquals("wrong path length", 2, path.links().size()); |
| 163 | assertEquals("wrong path cost", 2, path.cost(), 0.01); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void onDemandPath() { |
| 168 | submitTopologyGraph(); |
| 169 | Topology topology = service.currentTopology(); |
| 170 | LinkWeight weight = new LinkWeight() { |
| 171 | @Override |
| 172 | public double weight(TopologyEdge edge) { |
| 173 | return 3.3; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | Set<Path> paths = service.getPaths(topology, did("a"), did("c"), weight); |
| 178 | assertEquals("wrong path count", 2, paths.size()); |
| 179 | Path path = paths.iterator().next(); |
| 180 | assertEquals("wrong path length", 2, path.links().size()); |
| 181 | assertEquals("wrong path cost", 6.6, path.cost(), 0.01); |
| 182 | } |
| 183 | |
tom | 4d0dd3a | 2014-09-14 23:12:28 -0700 | [diff] [blame] | 184 | protected void validateEvents(Enum... types) { |
| 185 | int i = 0; |
| 186 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 187 | for (Event event : listener.events) { |
| 188 | assertEquals("incorrect event type", types[i], event.type()); |
| 189 | i++; |
| 190 | } |
| 191 | listener.events.clear(); |
| 192 | } |
| 193 | |
| 194 | private class TestProvider extends AbstractProvider implements TopologyProvider { |
| 195 | public TestProvider() { |
| 196 | super(PID); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private static class TestListener implements TopologyListener { |
| 201 | final List<TopologyEvent> events = new ArrayList<>(); |
| 202 | |
| 203 | @Override |
| 204 | public void event(TopologyEvent event) { |
| 205 | events.add(event); |
| 206 | } |
| 207 | } |
| 208 | |
Yuta HIGUCHI | 4e9c37c | 2014-09-21 14:34:33 -0700 | [diff] [blame] | 209 | } |