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