blob: 35160585edf695adc06dacd0f340c06fbf0c3bb0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Ray Milkey7483e1b2018-02-07 15:43:01 -080031import org.onosproject.net.topology.LinkWeigher;
32import org.onosproject.net.topology.LinkWeigherAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.topology.Topology;
34import org.onosproject.net.topology.TopologyCluster;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.topology.TopologyEvent;
36import org.onosproject.net.topology.TopologyGraph;
37import org.onosproject.net.topology.TopologyListener;
38import org.onosproject.net.topology.TopologyProvider;
39import org.onosproject.net.topology.TopologyProviderRegistry;
40import org.onosproject.net.topology.TopologyProviderService;
41import org.onosproject.net.topology.TopologyService;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070042import org.onosproject.store.trivial.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.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import static org.onosproject.net.NetTestTools.*;
51import static org.onosproject.net.PortNumber.portNumber;
52import static org.onosproject.net.topology.ClusterId.clusterId;
53import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
tom4d0dd3a2014-09-14 23:12:28 -070054
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();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070077 injectEventDispatcher(mgr, new TestEventDispatcher());
tom4d0dd3a2014-09-14 23:12:28 -070078 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));
Sho SHIMIZU2581e182015-09-25 10:22:33 -0700118 GraphDescription data = new DefaultGraphDescription(4321L, System.currentTimeMillis(), devices, links);
tom4d0dd3a2014-09-14 23:12:28 -0700119 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());
tom4d0dd3a2014-09-14 23:12:28 -0700130
131 assertEquals("wrong cluster count", 2, service.getClusters(topology).size());
132
133 TopologyCluster cluster = service.getCluster(topology, clusterId(0));
134 assertEquals("wrong device count", 4, cluster.deviceCount());
135 assertEquals("wrong device count", 4, service.getClusterDevices(topology, cluster).size());
136 assertEquals("wrong link count", 8, cluster.linkCount());
137 assertEquals("wrong link count", 8, service.getClusterLinks(topology, cluster).size());
138 }
139
140 @Test
141 public void structure() {
142 submitTopologyGraph();
143 Topology topology = service.currentTopology();
144
145 assertTrue("should be infrastructure point",
146 service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(1))));
147 assertFalse("should not be infrastructure point",
148 service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(3))));
149
tom4d0dd3a2014-09-14 23:12:28 -0700150 assertTrue("should be broadcast point",
151 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3))));
152 }
153
154 @Test
155 public void graph() {
156 submitTopologyGraph();
157 Topology topology = service.currentTopology();
158 TopologyGraph graph = service.getGraph(topology);
159 assertEquals("wrong vertex count", 6, graph.getVertexes().size());
160 assertEquals("wrong edge count", 10, graph.getEdges().size());
161 }
162
163 @Test
164 public void precomputedPath() {
165 submitTopologyGraph();
166 Topology topology = service.currentTopology();
167 Set<Path> paths = service.getPaths(topology, did("a"), did("c"));
168 assertEquals("wrong path count", 2, paths.size());
169 Path path = paths.iterator().next();
170 assertEquals("wrong path length", 2, path.links().size());
171 assertEquals("wrong path cost", 2, path.cost(), 0.01);
172 }
173
174 @Test
175 public void onDemandPath() {
176 submitTopologyGraph();
177 Topology topology = service.currentTopology();
Ray Milkey7483e1b2018-02-07 15:43:01 -0800178 LinkWeigher weight = new LinkWeigherAdapter(3.3);
tom4d0dd3a2014-09-14 23:12:28 -0700179
180 Set<Path> paths = service.getPaths(topology, did("a"), did("c"), weight);
181 assertEquals("wrong path count", 2, paths.size());
182 Path path = paths.iterator().next();
183 assertEquals("wrong path length", 2, path.links().size());
184 assertEquals("wrong path cost", 6.6, path.cost(), 0.01);
185 }
186
tom4d0dd3a2014-09-14 23:12:28 -0700187 protected void validateEvents(Enum... types) {
188 int i = 0;
189 assertEquals("wrong events received", types.length, listener.events.size());
190 for (Event event : listener.events) {
191 assertEquals("incorrect event type", types[i], event.type());
192 i++;
193 }
194 listener.events.clear();
195 }
196
197 private class TestProvider extends AbstractProvider implements TopologyProvider {
198 public TestProvider() {
199 super(PID);
200 }
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700201
202 @Override
203 public void triggerRecompute() {
204 }
tom4d0dd3a2014-09-14 23:12:28 -0700205 }
206
207 private static class TestListener implements TopologyListener {
208 final List<TopologyEvent> events = new ArrayList<>();
209
210 @Override
211 public void event(TopologyEvent event) {
212 events.add(event);
213 }
214 }
215
Yuta HIGUCHI4e9c37c2014-09-21 14:34:33 -0700216}