blob: 99a799341d6ff09b2e70094a8c3a58fbb5ce4abb [file] [log] [blame]
weibitd5037072014-06-13 10:07:01 -07001package net.onrc.onos.core.topology;
2
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07003import static org.junit.Assert.*;
4import static org.hamcrest.Matchers.*;
weibitd5037072014-06-13 10:07:01 -07005
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07006import java.util.Collection;
weibitd5037072014-06-13 10:07:01 -07007import java.util.Iterator;
8
9import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070010import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070011import net.onrc.onos.core.util.LinkTuple;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070012import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070013import net.onrc.onos.core.util.SwitchPort;
weibitd5037072014-06-13 10:07:01 -070014
15import org.junit.After;
16import org.junit.Before;
17import org.junit.Test;
18
19/**
20 * Unit tests for the TopologyImpl Class in the Topology module.
21 * These test cases check the sanity of getSwitch/removeSwitch,
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070022 * getLink/removeLink, getHost/removeHost, and getPort functions and verify
23 * the data objects inside the global graphDB through a injected network.
24 * The injected network has a ring topology with a large number of switches
25 * (configurable), andeach switch is associated with one host and connected to
26 * two other switches.
weibitd5037072014-06-13 10:07:01 -070027 */
28public class TopologyImplTest {
29 private TopologyImpl testTopology;
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070030 private static final int SWITCH_HOST_PORT = 1;
31 private static final PortNumber PORT_NUMBER_HOST =
32 PortNumber.uint32(SWITCH_HOST_PORT);
33 private static final int SWITCH_PORT_1 = 2;
34 private static final PortNumber PORT_NUMBER_1 =
35 PortNumber.uint32(SWITCH_PORT_1);
36 private static final int SWITCH_PORT_2 = 3;
37 private static final PortNumber PORT_NUMBER_2 =
38 PortNumber.uint32(SWITCH_PORT_2);
weibitd5037072014-06-13 10:07:01 -070039
40 // Set the test network size, it should be larger than 3
41 private static final long TEST_SWITCH_NUM = 100L;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070042 private static final long TEST_HOST_NUM = TEST_SWITCH_NUM;
weibitd5037072014-06-13 10:07:01 -070043
44 @Before
45 public void setUp() throws Exception {
46 // Create the injected network first
47 testTopology = new TopologyImpl();
48
49 // Create a number of switches and install two ports for each switch
50 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070051 final Dpid dpid = new Dpid(switchID);
52 SwitchEvent testSwitch = new SwitchEvent(dpid);
weibitd5037072014-06-13 10:07:01 -070053 testTopology.putSwitch(testSwitch);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070054 testTopology.putPort(new PortEvent(dpid, PORT_NUMBER_1));
55 testTopology.putPort(new PortEvent(dpid, PORT_NUMBER_2));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070056 PortEvent hostPort = new PortEvent(dpid, PORT_NUMBER_HOST);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070057 testTopology.putPort(hostPort);
weibitd5037072014-06-13 10:07:01 -070058
59 // Create a host for each switch
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070060 MACAddress hostMac = MACAddress.valueOf(switchID);
61 HostEvent testHost = new HostEvent(hostMac);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070062 testHost.addAttachmentPoint(hostPort.getSwitchPort());
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070063 testTopology.putHost(testHost);
weibitd5037072014-06-13 10:07:01 -070064 }
65
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070066 //
67 // Create one bidirectional link b/w two switches to construct a ring
68 // topology.
69 //
weibitd5037072014-06-13 10:07:01 -070070 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070071 final Dpid dpidA = new Dpid(switchID);
72 final Dpid dpidB = new Dpid(switchID % TEST_SWITCH_NUM + 1);
73 LinkEvent testLinkEast = new LinkEvent(
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070074 testTopology.getPort(dpidA, PORT_NUMBER_2).getSwitchPort(),
75 testTopology.getPort(dpidB, PORT_NUMBER_1).getSwitchPort()
weibitd5037072014-06-13 10:07:01 -070076 );
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070077 LinkEvent testLinkWest = new LinkEvent(
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070078 testTopology.getPort(dpidB, PORT_NUMBER_1).getSwitchPort(),
79 testTopology.getPort(dpidA, PORT_NUMBER_2).getSwitchPort()
weibitd5037072014-06-13 10:07:01 -070080 );
81 testTopology.putLink(testLinkEast);
82 testTopology.putLink(testLinkWest);
83 }
84 }
85
86 @After
87 public void tearDown() throws Exception {
88
89 }
90
91 /**
92 * Test the result of getSwitch function.
93 */
94 @Test
95 public void testGetSwitch() {
96 // Verify the switch is in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070097 assertNotNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM - 1)));
weibitd5037072014-06-13 10:07:01 -070098
99 // Verify there is no such switch in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700100 assertNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM + 1)));
weibitd5037072014-06-13 10:07:01 -0700101 long index = 0;
102 Iterator<Switch> itr = testTopology.getSwitches().iterator();
103 while (itr.hasNext()) {
104 index++;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700105 Dpid swID = itr.next().getDpid();
106 assertThat(swID.value(),
107 is(both(greaterThanOrEqualTo(1L))
108 .and(lessThanOrEqualTo(TEST_SWITCH_NUM))));
weibitd5037072014-06-13 10:07:01 -0700109 }
110
111 // Verify the total number of switches
112 assertEquals(TEST_SWITCH_NUM, index);
113 }
114
115 /**
116 * Test the result of getPort function.
117 */
118 @Test
119 public void testGetPort() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700120 PortNumber bogusPortNum = PortNumber.uint32(SWITCH_PORT_2 + 1);
weibitd5037072014-06-13 10:07:01 -0700121 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
122 // Verify ports are in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700123 final Dpid dpid = new Dpid(switchID);
124 assertNotNull(testTopology.getSwitch(dpid).getPort(PORT_NUMBER_1));
125 assertNotNull(testTopology.getSwitch(dpid).getPort(PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700126
127 // Verify there is no such port in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700128 assertNull(testTopology.getSwitch(dpid).getPort(bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700129 }
130 }
131
132 /**
133 * Test the result of getLink function.
134 */
135 @Test
136 public void testGetLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700137 Dpid sw1ID = new Dpid(1L);
138 Dpid sw3ID = new Dpid(3L);
weibitd5037072014-06-13 10:07:01 -0700139
140 // Verify there is no such link b/w these two switches
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700141 assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw3ID));
weibitd5037072014-06-13 10:07:01 -0700142 long index = 0;
143 Iterator<Link> itr = testTopology.getLinks().iterator();
144 while (itr.hasNext()) {
145 index++;
146 Link objectLink = itr.next();
147 Switch srcSw = (objectLink.getSrcSwitch());
148 Switch dstSw = (objectLink.getDstSwitch());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700149
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700150 LinkTuple linkId = objectLink.getLinkTuple();
151 // Verify the link through #getLink
152 Link linkA = testTopology.getLink(linkId.getSrc().getDpid(),
153 linkId.getSrc().getPortNumber(),
154 linkId.getDst().getDpid(),
155 linkId.getDst().getPortNumber());
156 assertEquals(linkId, linkA.getLinkTuple());
157
158 Link linkB = testTopology.getLink(linkId.getSrc().getDpid(),
159 linkId.getSrc().getPortNumber(),
160 linkId.getDst().getDpid(),
161 linkId.getDst().getPortNumber(),
162 TopologyElement.TYPE_PACKET_LAYER);
163 assertEquals(linkId, linkB.getLinkTuple());
164
165
166
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700167 // Confirm the link is formed properly
168 final long smallerDpid = Math.min(srcSw.getDpid().value(),
169 dstSw.getDpid().value());
170 final long largerDpid = Math.max(srcSw.getDpid().value(),
171 dstSw.getDpid().value());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700172 assertThat(largerDpid - smallerDpid,
173 is(either(equalTo(1L)).or(equalTo(TEST_SWITCH_NUM - 1))));
weibitd5037072014-06-13 10:07:01 -0700174 }
175
176 // Verify the total number of links
177 assertEquals(TEST_SWITCH_NUM * 2, index);
178 }
179
180 /**
181 * Test the result of getOutgoingLink function.
182 */
183 @Test
184 public void testGetOutgoingLink() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700185 PortNumber bogusPortNum = PortNumber.uint32(SWITCH_PORT_2 + 1);
weibitd5037072014-06-13 10:07:01 -0700186 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700187 final Dpid dpid = new Dpid(switchID);
188 assertNotNull(testTopology.getOutgoingLink(dpid, PORT_NUMBER_1));
189 assertNotNull(testTopology.getOutgoingLink(dpid, PORT_NUMBER_2));
190
191 Link la = testTopology.getOutgoingLink(dpid, PORT_NUMBER_2,
192 TopologyElement.TYPE_PACKET_LAYER);
193 Link lb = testTopology.getOutgoingLink(dpid, PORT_NUMBER_2);
194
195 assertTrue(la.getLinkTuple().equals(lb.getLinkTuple()));
196
197 Collection<Link> links = testTopology.getOutgoingLinks(
198 new SwitchPort(dpid, PORT_NUMBER_1));
199 assertEquals(1, links.size());
weibitd5037072014-06-13 10:07:01 -0700200
201 // Verify there is no such link in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700202 assertNull(testTopology.getOutgoingLink(dpid, bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700203 }
204 }
205
206 /**
207 * Test the result of getIncomingLink function.
208 */
209 @Test
210 public void testGetIncomingLink() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700211 PortNumber bogusPortNum = PortNumber.uint32(SWITCH_PORT_2 + 1);
weibitd5037072014-06-13 10:07:01 -0700212 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
213 // Verify the links are in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700214 final Dpid dpid = new Dpid(switchID);
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700215 assertNotNull(testTopology.getIncomingLink(
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700216 dpid, PORT_NUMBER_1));
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700217 assertNotNull(testTopology.getIncomingLink(
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700218 dpid, PORT_NUMBER_2));
219
220 Link la = testTopology.getIncomingLink(dpid, PORT_NUMBER_2,
221 TopologyElement.TYPE_PACKET_LAYER);
222 Link lb = testTopology.getIncomingLink(dpid, PORT_NUMBER_2);
223
224 assertTrue(la.getLinkTuple().equals(lb.getLinkTuple()));
225
226 Collection<Link> links = testTopology.getIncomingLinks(
227 new SwitchPort(dpid, PORT_NUMBER_1));
228 assertEquals(1, links.size());
weibitd5037072014-06-13 10:07:01 -0700229
230 // Verify there is no such link in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700231 assertNull(testTopology.getIncomingLink(
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700232 dpid, bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700233 }
234 }
235
236 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700237 * Test the result of getHostByMac function.
weibitd5037072014-06-13 10:07:01 -0700238 */
239 @Test
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700240 public void testGetHostByMac() {
weibitd5037072014-06-13 10:07:01 -0700241 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700242 MACAddress hostMac = MACAddress.valueOf(switchID);
weibitd5037072014-06-13 10:07:01 -0700243
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700244 // Verify the host is in the graphDB
245 assertNotNull(testTopology.getHostByMac(hostMac));
weibitd5037072014-06-13 10:07:01 -0700246 }
247 }
248
249 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700250 * Test the result of removeHost function.
weibitd5037072014-06-13 10:07:01 -0700251 */
252 @Test
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700253 public void testRemoveHost() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700254 int hostCount = 0;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700255 Iterator<Host> itr = testTopology.getHosts().iterator();
weibitd5037072014-06-13 10:07:01 -0700256 while (itr.hasNext()) {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700257 Host currHost = itr.next();
258 final MACAddress mac = currHost.getMacAddress();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700259 testTopology.removeHost(mac);
260 assertNull(testTopology.getHostByMac(mac));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700261 hostCount++;
weibitd5037072014-06-13 10:07:01 -0700262 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700263 for (Switch sw : testTopology.getSwitches()) {
264 for (Port port : sw.getPorts()) {
265 assertTrue(port.getHosts().isEmpty());
266 }
267 }
weibitd5037072014-06-13 10:07:01 -0700268
269 // Verify all hosts have been removed successfully
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700270 assertEquals(TEST_HOST_NUM, hostCount);
weibitd5037072014-06-13 10:07:01 -0700271 }
272
273 /**
274 * Test the result of removeLink function.
275 */
276 @Test
277 public void testRemoveLink() {
278 long index = 0;
279 Iterator<Link> itr = testTopology.getLinks().iterator();
280 while (itr.hasNext()) {
281 index++;
282 Link objectLink = itr.next();
283 Switch srcSw = (objectLink.getSrcSwitch());
284 Port srcPort = objectLink.getSrcPort();
285 Switch dstSw = (objectLink.getDstSwitch());
286 Port dstPort = objectLink.getDstPort();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700287
288 testTopology.removeLink(objectLink.getLinkTuple());
weibitd5037072014-06-13 10:07:01 -0700289
290 // Verify the link was removed successfully
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700291 assertNull(testTopology.getLink(
292 srcSw.getDpid(), srcPort.getNumber(),
weibitd5037072014-06-13 10:07:01 -0700293 dstSw.getDpid(), dstPort.getNumber()));
294 }
295
296 // Verify all links have been removed successfully
297 assertEquals(TEST_SWITCH_NUM * 2, index);
298 }
299
300 /**
301 * Test the result of removeSwitch function.
302 */
303 @Test
304 public void testRemoveSwitch() {
305 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700306 final Dpid dpid = new Dpid(switchID);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700307 Iterator<Host> itr =
308 testTopology.getSwitch(dpid).getHosts().iterator();
weibitd5037072014-06-13 10:07:01 -0700309 while (itr.hasNext()) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700310 testTopology.removeHost(itr.next().getMacAddress());
weibitd5037072014-06-13 10:07:01 -0700311 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700312 for (Port port : testTopology.getSwitch(dpid).getPorts()) {
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -0700313 testTopology.removePort(port.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700314 }
315 testTopology.removeSwitch(dpid);
weibitd5037072014-06-13 10:07:01 -0700316
317 // Verify the switch has been removed from the graphDB successfully
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700318 assertNull(testTopology.getSwitch(dpid));
weibitd5037072014-06-13 10:07:01 -0700319 }
320
321 // Verify all switches have been removed successfully
322 Iterator<Switch> itr = testTopology.getSwitches().iterator();
323 assertFalse(itr.hasNext());
324 }
Praseed Balakrishnan6f29f3e2014-07-10 10:16:31 -0700325
326 /**
327 * Tests the packet optical topology.
328 * Verify packet port having multiple links. Packet Port at packet layer
329 * has a link to another
330 * packet port and is also physically connected to a T-Port in optical
331 * later.
332 */
333 @Test
334 public void testPacketOpticalTopology() {
335 MockPacketOpticalTopology mockPacketOpticalTopology = new
336 MockPacketOpticalTopology();
337 mockPacketOpticalTopology.createSamplePacketOpticalTopology1();
338 for (Switch sw : mockPacketOpticalTopology.getSwitches()) {
339 if (sw.getType().equals(TopologyElement.TYPE_PACKET_LAYER)) {
340 Collection<Port> ports = sw.getPorts();
341 for (Port port : ports) {
342 if (port.getType().equals(TopologyElement
343 .TYPE_PACKET_LAYER)) {
344 if (!port.getOutgoingLinks().isEmpty()) {
345 assertEquals(2, port.getOutgoingLinks().size());
346 } else if (!port.getIncomingLinks().isEmpty()) {
347 assertEquals(2, port.getIncomingLinks().size());
348 }
349 }
350 }
351 }
352 }
353 }
weibitd5037072014-06-13 10:07:01 -0700354}