blob: 7fa2ff596b1d98ad7ce2217edffa0d402d55cbeb [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.topology.impl;
tom4d0dd3a2014-09-14 23:12:28 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.onos.event.Event;
22import org.onlab.onos.event.impl.TestEventDispatcher;
23import org.onlab.onos.net.ConnectPoint;
tom4d0dd3a2014-09-14 23:12:28 -070024import org.onlab.onos.net.Device;
tom4d0dd3a2014-09-14 23:12:28 -070025import org.onlab.onos.net.Link;
26import org.onlab.onos.net.Path;
27import org.onlab.onos.net.provider.AbstractProvider;
28import org.onlab.onos.net.provider.ProviderId;
tombe988312014-09-19 18:38:47 -070029import org.onlab.onos.net.topology.DefaultGraphDescription;
tom4d0dd3a2014-09-14 23:12:28 -070030import org.onlab.onos.net.topology.GraphDescription;
31import org.onlab.onos.net.topology.LinkWeight;
32import org.onlab.onos.net.topology.Topology;
33import org.onlab.onos.net.topology.TopologyCluster;
34import org.onlab.onos.net.topology.TopologyEdge;
35import org.onlab.onos.net.topology.TopologyEvent;
36import org.onlab.onos.net.topology.TopologyGraph;
37import org.onlab.onos.net.topology.TopologyListener;
38import org.onlab.onos.net.topology.TopologyProvider;
39import org.onlab.onos.net.topology.TopologyProviderRegistry;
40import org.onlab.onos.net.topology.TopologyProviderService;
41import org.onlab.onos.net.topology.TopologyService;
tomea961ff2014-10-01 12:45:15 -070042import org.onlab.onos.store.trivial.impl.SimpleTopologyStore;
tom4d0dd3a2014-09-14 23:12:28 -070043
44import java.util.ArrayList;
45import java.util.List;
46import java.util.Set;
47
48import static com.google.common.collect.ImmutableSet.of;
49import static org.junit.Assert.*;
tomca90c462014-09-22 11:40:58 -070050import static org.onlab.onos.net.NetTestTools.*;
tom4d0dd3a2014-09-14 23:12:28 -070051import static org.onlab.onos.net.PortNumber.portNumber;
52import static org.onlab.onos.net.topology.ClusterId.clusterId;
53import static org.onlab.onos.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
54
55/**
56 * Test of the topology subsystem.
57 */
tom10262dd2014-09-19 10:51:19 -070058public class TopologyManagerTest {
tom4d0dd3a2014-09-14 23:12:28 -070059
tom7e02cda2014-09-18 12:05:46 -070060 private static final ProviderId PID = new ProviderId("of", "foo");
tom4d0dd3a2014-09-14 23:12:28 -070061
tom10262dd2014-09-19 10:51:19 -070062 private TopologyManager mgr;
tom4d0dd3a2014-09-14 23:12:28 -070063
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() {
tom10262dd2014-09-19 10:51:19 -070072 mgr = new TopologyManager();
tom4d0dd3a2014-09-14 23:12:28 -070073 service = mgr;
74 registry = mgr;
75
tom10262dd2014-09-19 10:51:19 -070076 mgr.store = new SimpleTopologyStore();
tom4d0dd3a2014-09-14 23:12:28 -070077 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...
tom88a6f112014-10-09 18:00:19 -0700152// 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))));
tom4d0dd3a2014-09-14 23:12:28 -0700157 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
tom4d0dd3a2014-09-14 23:12:28 -0700199 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 Vachuska0e752bd2014-10-22 22:33:41 -0700213
214 @Override
215 public void triggerRecompute() {
216 }
tom4d0dd3a2014-09-14 23:12:28 -0700217 }
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 HIGUCHI4e9c37c2014-09-21 14:34:33 -0700228}