blob: fe1e73b06b9691286f9a54d9ef2bff0959bb3fcd [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;
Ray Milkeycc001242018-09-18 14:55:59 -070021import org.onlab.graph.ScalarWeight;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.event.Event;
Thomas Vachuska36002e62015-05-19 16:12:29 -070023import org.onosproject.common.event.impl.TestEventDispatcher;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Device;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.provider.AbstractProvider;
29import org.onosproject.net.provider.ProviderId;
30import org.onosproject.net.topology.DefaultGraphDescription;
31import org.onosproject.net.topology.GraphDescription;
Ray Milkey7483e1b2018-02-07 15:43:01 -080032import org.onosproject.net.topology.LinkWeigher;
33import org.onosproject.net.topology.LinkWeigherAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.topology.Topology;
35import org.onosproject.net.topology.TopologyCluster;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.topology.TopologyEvent;
37import org.onosproject.net.topology.TopologyGraph;
38import org.onosproject.net.topology.TopologyListener;
39import org.onosproject.net.topology.TopologyProvider;
40import org.onosproject.net.topology.TopologyProviderRegistry;
41import org.onosproject.net.topology.TopologyProviderService;
42import org.onosproject.net.topology.TopologyService;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070043import org.onosproject.store.trivial.SimpleTopologyStore;
tom4d0dd3a2014-09-14 23:12:28 -070044
45import java.util.ArrayList;
46import java.util.List;
47import java.util.Set;
48
49import static com.google.common.collect.ImmutableSet.of;
50import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import static org.onosproject.net.NetTestTools.*;
52import static org.onosproject.net.PortNumber.portNumber;
53import static org.onosproject.net.topology.ClusterId.clusterId;
54import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
tom4d0dd3a2014-09-14 23:12:28 -070055
56/**
57 * Test of the topology subsystem.
58 */
tom10262dd2014-09-19 10:51:19 -070059public class TopologyManagerTest {
tom4d0dd3a2014-09-14 23:12:28 -070060
tom7e02cda2014-09-18 12:05:46 -070061 private static final ProviderId PID = new ProviderId("of", "foo");
tom4d0dd3a2014-09-14 23:12:28 -070062
tom10262dd2014-09-19 10:51:19 -070063 private TopologyManager mgr;
tom4d0dd3a2014-09-14 23:12:28 -070064
65 protected TopologyService service;
66 protected TopologyProviderRegistry registry;
67 protected TopologyProviderService providerService;
68 protected TestProvider provider;
69 protected TestListener listener = new TestListener();
70
71 @Before
72 public void setUp() {
tom10262dd2014-09-19 10:51:19 -070073 mgr = new TopologyManager();
tom4d0dd3a2014-09-14 23:12:28 -070074 service = mgr;
75 registry = mgr;
76
tom10262dd2014-09-19 10:51:19 -070077 mgr.store = new SimpleTopologyStore();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070078 injectEventDispatcher(mgr, new TestEventDispatcher());
tom4d0dd3a2014-09-14 23:12:28 -070079 mgr.activate();
80
81 service.addListener(listener);
82
83 provider = new TestProvider();
84 providerService = registry.register(provider);
85
86 assertTrue("provider should be registered",
87 registry.getProviders().contains(provider.id()));
88 }
89
90 @After
91 public void tearDown() {
92 mgr.deactivate();
93 service.removeListener(listener);
94 }
95
96 @Test
97 public void basics() {
98 Topology topology = service.currentTopology();
99 assertNull("no topo expected", topology);
100 submitTopologyGraph();
101 validateEvents(TOPOLOGY_CHANGED);
102 topology = service.currentTopology();
103 assertTrue("should be latest", service.isLatest(topology));
104
105 submitTopologyGraph();
106 validateEvents(TOPOLOGY_CHANGED);
107 assertFalse("should be latest", service.isLatest(topology));
108 }
109
110 private void submitTopologyGraph() {
111 Set<Device> devices = of(device("a"), device("b"),
112 device("c"), device("d"),
113 device("e"), device("f"));
114 Set<Link> links = of(link("a", 1, "b", 1), link("b", 1, "a", 1),
115 link("b", 2, "c", 1), link("c", 1, "b", 2),
116 link("c", 2, "d", 1), link("d", 1, "c", 2),
117 link("d", 2, "a", 2), link("a", 2, "d", 2),
118 link("e", 1, "f", 1), link("f", 1, "e", 1));
Sho SHIMIZU2581e182015-09-25 10:22:33 -0700119 GraphDescription data = new DefaultGraphDescription(4321L, System.currentTimeMillis(), devices, links);
tom4d0dd3a2014-09-14 23:12:28 -0700120 providerService.topologyChanged(data, null);
121 }
122
123 @Test
124 public void clusters() {
125 submitTopologyGraph();
126 Topology topology = service.currentTopology();
127 assertNotNull("topo expected", topology);
128 assertEquals("wrong cluster count", 2, topology.clusterCount());
129 assertEquals("wrong device count", 6, topology.deviceCount());
130 assertEquals("wrong link count", 10, topology.linkCount());
tom4d0dd3a2014-09-14 23:12:28 -0700131
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
tom4d0dd3a2014-09-14 23:12:28 -0700151 assertTrue("should be broadcast point",
152 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3))));
153 }
154
155 @Test
156 public void graph() {
157 submitTopologyGraph();
158 Topology topology = service.currentTopology();
159 TopologyGraph graph = service.getGraph(topology);
160 assertEquals("wrong vertex count", 6, graph.getVertexes().size());
161 assertEquals("wrong edge count", 10, graph.getEdges().size());
162 }
163
164 @Test
165 public void precomputedPath() {
166 submitTopologyGraph();
167 Topology topology = service.currentTopology();
168 Set<Path> paths = service.getPaths(topology, did("a"), did("c"));
169 assertEquals("wrong path count", 2, paths.size());
170 Path path = paths.iterator().next();
171 assertEquals("wrong path length", 2, path.links().size());
Ray Milkeycc001242018-09-18 14:55:59 -0700172 assertEquals("wrong path cost", ScalarWeight.toWeight(2), path.weight());
tom4d0dd3a2014-09-14 23:12:28 -0700173 }
174
175 @Test
176 public void onDemandPath() {
177 submitTopologyGraph();
178 Topology topology = service.currentTopology();
Ray Milkey7483e1b2018-02-07 15:43:01 -0800179 LinkWeigher weight = new LinkWeigherAdapter(3.3);
tom4d0dd3a2014-09-14 23:12:28 -0700180
181 Set<Path> paths = service.getPaths(topology, did("a"), did("c"), weight);
182 assertEquals("wrong path count", 2, paths.size());
183 Path path = paths.iterator().next();
184 assertEquals("wrong path length", 2, path.links().size());
Ray Milkeycc001242018-09-18 14:55:59 -0700185 assertEquals("wrong path cost", ScalarWeight.toWeight(6.6), path.weight());
tom4d0dd3a2014-09-14 23:12:28 -0700186 }
187
tom4d0dd3a2014-09-14 23:12:28 -0700188 protected void validateEvents(Enum... types) {
189 int i = 0;
190 assertEquals("wrong events received", types.length, listener.events.size());
191 for (Event event : listener.events) {
192 assertEquals("incorrect event type", types[i], event.type());
193 i++;
194 }
195 listener.events.clear();
196 }
197
198 private class TestProvider extends AbstractProvider implements TopologyProvider {
199 public TestProvider() {
200 super(PID);
201 }
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700202
203 @Override
204 public void triggerRecompute() {
205 }
tom4d0dd3a2014-09-14 23:12:28 -0700206 }
207
208 private static class TestListener implements TopologyListener {
209 final List<TopologyEvent> events = new ArrayList<>();
210
211 @Override
212 public void event(TopologyEvent event) {
213 events.add(event);
214 }
215 }
216
Yuta HIGUCHI4e9c37c2014-09-21 14:34:33 -0700217}