blob: 2295370163e416e5c2fae851c9db3025c7cb6f08 [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.topology.impl;
tom4d0dd3a2014-09-14 23:12:28 -07002
3import org.junit.After;
4import org.junit.Before;
5import org.junit.Test;
6import org.onlab.onos.event.Event;
7import org.onlab.onos.event.impl.TestEventDispatcher;
8import org.onlab.onos.net.ConnectPoint;
9import org.onlab.onos.net.DefaultDevice;
10import org.onlab.onos.net.DefaultLink;
11import org.onlab.onos.net.Device;
12import org.onlab.onos.net.DeviceId;
13import org.onlab.onos.net.Link;
14import org.onlab.onos.net.Path;
15import org.onlab.onos.net.provider.AbstractProvider;
16import org.onlab.onos.net.provider.ProviderId;
tombe988312014-09-19 18:38:47 -070017import org.onlab.onos.net.topology.DefaultGraphDescription;
tom4d0dd3a2014-09-14 23:12:28 -070018import org.onlab.onos.net.topology.GraphDescription;
19import org.onlab.onos.net.topology.LinkWeight;
20import org.onlab.onos.net.topology.Topology;
21import org.onlab.onos.net.topology.TopologyCluster;
22import org.onlab.onos.net.topology.TopologyEdge;
23import org.onlab.onos.net.topology.TopologyEvent;
24import org.onlab.onos.net.topology.TopologyGraph;
25import org.onlab.onos.net.topology.TopologyListener;
26import org.onlab.onos.net.topology.TopologyProvider;
27import org.onlab.onos.net.topology.TopologyProviderRegistry;
28import org.onlab.onos.net.topology.TopologyProviderService;
29import org.onlab.onos.net.topology.TopologyService;
tom202175a2014-09-19 19:00:11 -070030import org.onlab.onos.net.trivial.impl.SimpleTopologyStore;
tom4d0dd3a2014-09-14 23:12:28 -070031
32import java.util.ArrayList;
33import java.util.List;
34import java.util.Set;
35
36import static com.google.common.collect.ImmutableSet.of;
37import static org.junit.Assert.*;
38import static org.onlab.onos.net.DeviceId.deviceId;
39import static org.onlab.onos.net.PortNumber.portNumber;
40import static org.onlab.onos.net.topology.ClusterId.clusterId;
41import static org.onlab.onos.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
42
43/**
44 * Test of the topology subsystem.
45 */
tom10262dd2014-09-19 10:51:19 -070046public class TopologyManagerTest {
tom4d0dd3a2014-09-14 23:12:28 -070047
tom7e02cda2014-09-18 12:05:46 -070048 private static final ProviderId PID = new ProviderId("of", "foo");
tom4d0dd3a2014-09-14 23:12:28 -070049
tom10262dd2014-09-19 10:51:19 -070050 private TopologyManager mgr;
tom4d0dd3a2014-09-14 23:12:28 -070051
52 protected TopologyService service;
53 protected TopologyProviderRegistry registry;
54 protected TopologyProviderService providerService;
55 protected TestProvider provider;
56 protected TestListener listener = new TestListener();
57
58 @Before
59 public void setUp() {
tom10262dd2014-09-19 10:51:19 -070060 mgr = new TopologyManager();
tom4d0dd3a2014-09-14 23:12:28 -070061 service = mgr;
62 registry = mgr;
63
tom10262dd2014-09-19 10:51:19 -070064 mgr.store = new SimpleTopologyStore();
tom4d0dd3a2014-09-14 23:12:28 -070065 mgr.eventDispatcher = new TestEventDispatcher();
66 mgr.activate();
67
68 service.addListener(listener);
69
70 provider = new TestProvider();
71 providerService = registry.register(provider);
72
73 assertTrue("provider should be registered",
74 registry.getProviders().contains(provider.id()));
75 }
76
77 @After
78 public void tearDown() {
79 mgr.deactivate();
80 service.removeListener(listener);
81 }
82
83 @Test
84 public void basics() {
85 Topology topology = service.currentTopology();
86 assertNull("no topo expected", topology);
87 submitTopologyGraph();
88 validateEvents(TOPOLOGY_CHANGED);
89 topology = service.currentTopology();
90 assertTrue("should be latest", service.isLatest(topology));
91
92 submitTopologyGraph();
93 validateEvents(TOPOLOGY_CHANGED);
94 assertFalse("should be latest", service.isLatest(topology));
95 }
96
97 private void submitTopologyGraph() {
98 Set<Device> devices = of(device("a"), device("b"),
99 device("c"), device("d"),
100 device("e"), device("f"));
101 Set<Link> links = of(link("a", 1, "b", 1), link("b", 1, "a", 1),
102 link("b", 2, "c", 1), link("c", 1, "b", 2),
103 link("c", 2, "d", 1), link("d", 1, "c", 2),
104 link("d", 2, "a", 2), link("a", 2, "d", 2),
105 link("e", 1, "f", 1), link("f", 1, "e", 1));
106 GraphDescription data = new DefaultGraphDescription(4321L, devices, links);
107 providerService.topologyChanged(data, null);
108 }
109
110 @Test
111 public void clusters() {
112 submitTopologyGraph();
113 Topology topology = service.currentTopology();
114 assertNotNull("topo expected", topology);
115 assertEquals("wrong cluster count", 2, topology.clusterCount());
116 assertEquals("wrong device count", 6, topology.deviceCount());
117 assertEquals("wrong link count", 10, topology.linkCount());
118 assertEquals("wrong path count", 18, topology.pathCount());
119
120 assertEquals("wrong cluster count", 2, service.getClusters(topology).size());
121
122 TopologyCluster cluster = service.getCluster(topology, clusterId(0));
123 assertEquals("wrong device count", 4, cluster.deviceCount());
124 assertEquals("wrong device count", 4, service.getClusterDevices(topology, cluster).size());
125 assertEquals("wrong link count", 8, cluster.linkCount());
126 assertEquals("wrong link count", 8, service.getClusterLinks(topology, cluster).size());
127 }
128
129 @Test
130 public void structure() {
131 submitTopologyGraph();
132 Topology topology = service.currentTopology();
133
134 assertTrue("should be infrastructure point",
135 service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(1))));
136 assertFalse("should not be infrastructure point",
137 service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(3))));
138
139 // One of these cannot be a broadcast point... or we have a loop...
140 assertFalse("should not be broadcast point",
141 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(1))) &&
142 service.isBroadcastPoint(topology, new ConnectPoint(did("b"), portNumber(1))) &&
143 service.isBroadcastPoint(topology, new ConnectPoint(did("c"), portNumber(1))) &&
144 service.isBroadcastPoint(topology, new ConnectPoint(did("d"), portNumber(1))));
145 assertTrue("should be broadcast point",
146 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3))));
147 }
148
149 @Test
150 public void graph() {
151 submitTopologyGraph();
152 Topology topology = service.currentTopology();
153 TopologyGraph graph = service.getGraph(topology);
154 assertEquals("wrong vertex count", 6, graph.getVertexes().size());
155 assertEquals("wrong edge count", 10, graph.getEdges().size());
156 }
157
158 @Test
159 public void precomputedPath() {
160 submitTopologyGraph();
161 Topology topology = service.currentTopology();
162 Set<Path> paths = service.getPaths(topology, did("a"), did("c"));
163 assertEquals("wrong path count", 2, paths.size());
164 Path path = paths.iterator().next();
165 assertEquals("wrong path length", 2, path.links().size());
166 assertEquals("wrong path cost", 2, path.cost(), 0.01);
167 }
168
169 @Test
170 public void onDemandPath() {
171 submitTopologyGraph();
172 Topology topology = service.currentTopology();
173 LinkWeight weight = new LinkWeight() {
174 @Override
175 public double weight(TopologyEdge edge) {
176 return 3.3;
177 }
178 };
179
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
187 // Short-hand for creating a link.
tombe988312014-09-19 18:38:47 -0700188 public static Link link(String src, int sp, String dst, int dp) {
tom4d0dd3a2014-09-14 23:12:28 -0700189 return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)),
190 new ConnectPoint(did(dst), portNumber(dp)),
191 Link.Type.DIRECT);
192 }
193
194 // Crates a new device with the specified id
tombe988312014-09-19 18:38:47 -0700195 public static Device device(String id) {
tom4d0dd3a2014-09-14 23:12:28 -0700196 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
197 "mfg", "1.0", "1.1", "1234");
198 }
199
200 // Short-hand for producing a device id from a string
tombe988312014-09-19 18:38:47 -0700201 public static DeviceId did(String id) {
tom4d0dd3a2014-09-14 23:12:28 -0700202 return deviceId("of:" + id);
203 }
204
205 protected void validateEvents(Enum... types) {
206 int i = 0;
207 assertEquals("wrong events received", types.length, listener.events.size());
208 for (Event event : listener.events) {
209 assertEquals("incorrect event type", types[i], event.type());
210 i++;
211 }
212 listener.events.clear();
213 }
214
215 private class TestProvider extends AbstractProvider implements TopologyProvider {
216 public TestProvider() {
217 super(PID);
218 }
219 }
220
221 private static class TestListener implements TopologyListener {
222 final List<TopologyEvent> events = new ArrayList<>();
223
224 @Override
225 public void event(TopologyEvent event) {
226 events.add(event);
227 }
228 }
229
Yuta HIGUCHI4e9c37c2014-09-21 14:34:33 -0700230}