blob: 97d779c234c41275f7fad8bc8489fd3fe66e34d5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.topology.impl;
tom4d0dd3a2014-09-14 23:12:28 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.event.Event;
Thomas Vachuska36002e62015-05-19 16:12:29 -070022import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Device;
25import org.onosproject.net.Link;
26import org.onosproject.net.Path;
27import org.onosproject.net.provider.AbstractProvider;
28import org.onosproject.net.provider.ProviderId;
29import org.onosproject.net.topology.DefaultGraphDescription;
30import org.onosproject.net.topology.GraphDescription;
31import org.onosproject.net.topology.LinkWeight;
32import org.onosproject.net.topology.Topology;
33import org.onosproject.net.topology.TopologyCluster;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.topology.TopologyEvent;
35import org.onosproject.net.topology.TopologyGraph;
36import org.onosproject.net.topology.TopologyListener;
37import org.onosproject.net.topology.TopologyProvider;
38import org.onosproject.net.topology.TopologyProviderRegistry;
39import org.onosproject.net.topology.TopologyProviderService;
40import org.onosproject.net.topology.TopologyService;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070041import org.onosproject.store.trivial.SimpleTopologyStore;
tom4d0dd3a2014-09-14 23:12:28 -070042
43import java.util.ArrayList;
44import java.util.List;
45import java.util.Set;
46
47import static com.google.common.collect.ImmutableSet.of;
48import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import static org.onosproject.net.NetTestTools.*;
50import static org.onosproject.net.PortNumber.portNumber;
51import static org.onosproject.net.topology.ClusterId.clusterId;
52import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
tom4d0dd3a2014-09-14 23:12:28 -070053
54/**
55 * Test of the topology subsystem.
56 */
tom10262dd2014-09-19 10:51:19 -070057public class TopologyManagerTest {
tom4d0dd3a2014-09-14 23:12:28 -070058
tom7e02cda2014-09-18 12:05:46 -070059 private static final ProviderId PID = new ProviderId("of", "foo");
tom4d0dd3a2014-09-14 23:12:28 -070060
tom10262dd2014-09-19 10:51:19 -070061 private TopologyManager mgr;
tom4d0dd3a2014-09-14 23:12:28 -070062
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() {
tom10262dd2014-09-19 10:51:19 -070071 mgr = new TopologyManager();
tom4d0dd3a2014-09-14 23:12:28 -070072 service = mgr;
73 registry = mgr;
74
tom10262dd2014-09-19 10:51:19 -070075 mgr.store = new SimpleTopologyStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070076 injectEventDispatcher(mgr, new TestEventDispatcher());
tom4d0dd3a2014-09-14 23:12:28 -070077 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 SHIMIZU2581e182015-09-25 10:22:33 -0700117 GraphDescription data = new DefaultGraphDescription(4321L, System.currentTimeMillis(), devices, links);
tom4d0dd3a2014-09-14 23:12:28 -0700118 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());
tom4d0dd3a2014-09-14 23:12:28 -0700129
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
tom4d0dd3a2014-09-14 23:12:28 -0700149 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 Vachuskac31d9f12015-01-22 12:33:27 -0800177 LinkWeight weight = edge -> 3.3;
tom4d0dd3a2014-09-14 23:12:28 -0700178
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
tom4d0dd3a2014-09-14 23:12:28 -0700186 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 Vachuska0e752bd2014-10-22 22:33:41 -0700200
201 @Override
202 public void triggerRecompute() {
203 }
tom4d0dd3a2014-09-14 23:12:28 -0700204 }
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 HIGUCHI4e9c37c2014-09-21 14:34:33 -0700215}