2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.topology.impl;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.onosproject.event.Event;
22 import org.onosproject.common.event.impl.TestEventDispatcher;
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;
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;
41 import org.onosproject.store.trivial.SimpleTopologyStore;
43 import java.util.ArrayList;
44 import java.util.List;
47 import static com.google.common.collect.ImmutableSet.of;
48 import static org.junit.Assert.*;
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;
55 * Test of the topology subsystem.
57 public class TopologyManagerTest {
59 private static final ProviderId PID = new ProviderId("of", "foo");
61 private TopologyManager mgr;
63 protected TopologyService service;
64 protected TopologyProviderRegistry registry;
65 protected TopologyProviderService providerService;
66 protected TestProvider provider;
67 protected TestListener listener = new TestListener();
71 mgr = new TopologyManager();
75 mgr.store = new SimpleTopologyStore();
76 injectEventDispatcher(mgr, new TestEventDispatcher());
79 service.addListener(listener);
81 provider = new TestProvider();
82 providerService = registry.register(provider);
84 assertTrue("provider should be registered",
85 registry.getProviders().contains(provider.id()));
89 public void tearDown() {
91 service.removeListener(listener);
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));
103 submitTopologyGraph();
104 validateEvents(TOPOLOGY_CHANGED);
105 assertFalse("should be latest", service.isLatest(topology));
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));
117 GraphDescription data = new DefaultGraphDescription(4321L, devices, links);
118 providerService.topologyChanged(data, null);
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());
130 assertEquals("wrong cluster count", 2, service.getClusters(topology).size());
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());
140 public void structure() {
141 submitTopologyGraph();
142 Topology topology = service.currentTopology();
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))));
149 assertTrue("should be broadcast point",
150 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3))));
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());
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);
174 public void onDemandPath() {
175 submitTopologyGraph();
176 Topology topology = service.currentTopology();
177 LinkWeight weight = edge -> 3.3;
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);
186 protected void validateEvents(Enum... types) {
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());
193 listener.events.clear();
196 private class TestProvider extends AbstractProvider implements TopologyProvider {
197 public TestProvider() {
202 public void triggerRecompute() {
206 private static class TestListener implements TopologyListener {
207 final List<TopologyEvent> events = new ArrayList<>();
210 public void event(TopologyEvent event) {