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