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