blob: b0b1eed478c9bb7bf65113d612e6e0072dfcc1fd [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 */
44public class SimpleTopologyManagerTest {
45
tom7e02cda2014-09-18 12:05:46 -070046 private static final ProviderId PID = new ProviderId("of", "foo");
tom4d0dd3a2014-09-14 23:12:28 -070047
48 private SimpleTopologyManager mgr;
49
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() {
58 mgr = new SimpleTopologyManager();
59 service = mgr;
60 registry = mgr;
61
62 mgr.eventDispatcher = new TestEventDispatcher();
63 mgr.activate();
64
65 service.addListener(listener);
66
67 provider = new TestProvider();
68 providerService = registry.register(provider);
69
70 assertTrue("provider should be registered",
71 registry.getProviders().contains(provider.id()));
72 }
73
74 @After
75 public void tearDown() {
76 mgr.deactivate();
77 service.removeListener(listener);
78 }
79
80 @Test
81 public void basics() {
82 Topology topology = service.currentTopology();
83 assertNull("no topo expected", topology);
84 submitTopologyGraph();
85 validateEvents(TOPOLOGY_CHANGED);
86 topology = service.currentTopology();
87 assertTrue("should be latest", service.isLatest(topology));
88
89 submitTopologyGraph();
90 validateEvents(TOPOLOGY_CHANGED);
91 assertFalse("should be latest", service.isLatest(topology));
92 }
93
94 private void submitTopologyGraph() {
95 Set<Device> devices = of(device("a"), device("b"),
96 device("c"), device("d"),
97 device("e"), device("f"));
98 Set<Link> links = of(link("a", 1, "b", 1), link("b", 1, "a", 1),
99 link("b", 2, "c", 1), link("c", 1, "b", 2),
100 link("c", 2, "d", 1), link("d", 1, "c", 2),
101 link("d", 2, "a", 2), link("a", 2, "d", 2),
102 link("e", 1, "f", 1), link("f", 1, "e", 1));
103 GraphDescription data = new DefaultGraphDescription(4321L, devices, links);
104 providerService.topologyChanged(data, null);
105 }
106
107 @Test
108 public void clusters() {
109 submitTopologyGraph();
110 Topology topology = service.currentTopology();
111 assertNotNull("topo expected", topology);
112 assertEquals("wrong cluster count", 2, topology.clusterCount());
113 assertEquals("wrong device count", 6, topology.deviceCount());
114 assertEquals("wrong link count", 10, topology.linkCount());
115 assertEquals("wrong path count", 18, topology.pathCount());
116
117 assertEquals("wrong cluster count", 2, service.getClusters(topology).size());
118
119 TopologyCluster cluster = service.getCluster(topology, clusterId(0));
120 assertEquals("wrong device count", 4, cluster.deviceCount());
121 assertEquals("wrong device count", 4, service.getClusterDevices(topology, cluster).size());
122 assertEquals("wrong link count", 8, cluster.linkCount());
123 assertEquals("wrong link count", 8, service.getClusterLinks(topology, cluster).size());
124 }
125
126 @Test
127 public void structure() {
128 submitTopologyGraph();
129 Topology topology = service.currentTopology();
130
131 assertTrue("should be infrastructure point",
132 service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(1))));
133 assertFalse("should not be infrastructure point",
134 service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(3))));
135
136 // One of these cannot be a broadcast point... or we have a loop...
137 assertFalse("should not be broadcast point",
138 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(1))) &&
139 service.isBroadcastPoint(topology, new ConnectPoint(did("b"), portNumber(1))) &&
140 service.isBroadcastPoint(topology, new ConnectPoint(did("c"), portNumber(1))) &&
141 service.isBroadcastPoint(topology, new ConnectPoint(did("d"), portNumber(1))));
142 assertTrue("should be broadcast point",
143 service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3))));
144 }
145
146 @Test
147 public void graph() {
148 submitTopologyGraph();
149 Topology topology = service.currentTopology();
150 TopologyGraph graph = service.getGraph(topology);
151 assertEquals("wrong vertex count", 6, graph.getVertexes().size());
152 assertEquals("wrong edge count", 10, graph.getEdges().size());
153 }
154
155 @Test
156 public void precomputedPath() {
157 submitTopologyGraph();
158 Topology topology = service.currentTopology();
159 Set<Path> paths = service.getPaths(topology, did("a"), did("c"));
160 assertEquals("wrong path count", 2, paths.size());
161 Path path = paths.iterator().next();
162 assertEquals("wrong path length", 2, path.links().size());
163 assertEquals("wrong path cost", 2, path.cost(), 0.01);
164 }
165
166 @Test
167 public void onDemandPath() {
168 submitTopologyGraph();
169 Topology topology = service.currentTopology();
170 LinkWeight weight = new LinkWeight() {
171 @Override
172 public double weight(TopologyEdge edge) {
173 return 3.3;
174 }
175 };
176
177 Set<Path> paths = service.getPaths(topology, did("a"), did("c"), weight);
178 assertEquals("wrong path count", 2, paths.size());
179 Path path = paths.iterator().next();
180 assertEquals("wrong path length", 2, path.links().size());
181 assertEquals("wrong path cost", 6.6, path.cost(), 0.01);
182 }
183
184 // Short-hand for creating a link.
tomb86066b2014-09-15 09:52:24 -0700185 static Link link(String src, int sp, String dst, int dp) {
tom4d0dd3a2014-09-14 23:12:28 -0700186 return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)),
187 new ConnectPoint(did(dst), portNumber(dp)),
188 Link.Type.DIRECT);
189 }
190
191 // Crates a new device with the specified id
tomb86066b2014-09-15 09:52:24 -0700192 static Device device(String id) {
tom4d0dd3a2014-09-14 23:12:28 -0700193 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
194 "mfg", "1.0", "1.1", "1234");
195 }
196
197 // Short-hand for producing a device id from a string
tomb86066b2014-09-15 09:52:24 -0700198 static DeviceId did(String id) {
tom4d0dd3a2014-09-14 23:12:28 -0700199 return deviceId("of:" + id);
200 }
201
202 protected void validateEvents(Enum... types) {
203 int i = 0;
204 assertEquals("wrong events received", types.length, listener.events.size());
205 for (Event event : listener.events) {
206 assertEquals("incorrect event type", types[i], event.type());
207 i++;
208 }
209 listener.events.clear();
210 }
211
212 private class TestProvider extends AbstractProvider implements TopologyProvider {
213 public TestProvider() {
214 super(PID);
215 }
216 }
217
218 private static class TestListener implements TopologyListener {
219 final List<TopologyEvent> events = new ArrayList<>();
220
221 @Override
222 public void event(TopologyEvent event) {
223 events.add(event);
224 }
225 }
226
227}