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