blob: 141a4f0cb62a40409f4c142c7e75c502c224a89e [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,
22 * getLink/removeLink, getDevice/removeDevice, and getPort functions and
23 * verify 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 (configurable), and
25 * each switch is associated with one device and connected to two other switches.
26 */
27public class TopologyImplTest {
28 private TopologyImpl testTopology;
29 private static final Long SWITCH_HOST_PORT = 1L;
30 private static final Long SWITCH_PORT_1 = 2L;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070031 private static final PortNumber PORT_NUMBER_1 = new PortNumber(SWITCH_PORT_1.shortValue());
weibitd5037072014-06-13 10:07:01 -070032 private static final Long SWITCH_PORT_2 = 3L;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070033 private static final PortNumber PORT_NUMBER_2 = new PortNumber(SWITCH_PORT_2.shortValue());
weibitd5037072014-06-13 10:07:01 -070034
35 // Set the test network size, it should be larger than 3
36 private static final long TEST_SWITCH_NUM = 100L;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070037 private static final long TEST_HOST_NUM = TEST_SWITCH_NUM;
weibitd5037072014-06-13 10:07:01 -070038
39 @Before
40 public void setUp() throws Exception {
41 // Create the injected network first
42 testTopology = new TopologyImpl();
43
44 // Create a number of switches and install two ports for each switch
45 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070046 final Dpid dpid = new Dpid(switchID);
47 SwitchEvent testSwitch = new SwitchEvent(dpid);
weibitd5037072014-06-13 10:07:01 -070048 testTopology.putSwitch(testSwitch);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070049 testTopology.putPort(new PortEvent(dpid, PORT_NUMBER_1));
50 testTopology.putPort(new PortEvent(dpid, PORT_NUMBER_2));
51 PortEvent hostPort = new PortEvent(dpid,
52 new PortNumber(SWITCH_HOST_PORT.shortValue()));
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070053 testTopology.putPort(hostPort);
weibitd5037072014-06-13 10:07:01 -070054
55 // Create a host for each switch
56 MACAddress devMac = MACAddress.valueOf(switchID);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070057 HostEvent testHost = new HostEvent(devMac);
58 testHost.addAttachmentPoint(hostPort.getSwitchPort());
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070059 testTopology.putHost(testHost);
weibitd5037072014-06-13 10:07:01 -070060 }
61
62 // Create one bidirectional link b/w two switches to construct a ring topology
63 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070064 final Dpid dpidA = new Dpid(switchID);
65 final Dpid dpidB = new Dpid(switchID % TEST_SWITCH_NUM + 1);
66 LinkEvent testLinkEast = new LinkEvent(
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070067 testTopology.getPort(dpidA, PORT_NUMBER_2).getSwitchPort(),
68 testTopology.getPort(dpidB, PORT_NUMBER_1).getSwitchPort()
weibitd5037072014-06-13 10:07:01 -070069 );
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070070 LinkEvent testLinkWest = new LinkEvent(
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070071 testTopology.getPort(dpidB, PORT_NUMBER_1).getSwitchPort(),
72 testTopology.getPort(dpidA, PORT_NUMBER_2).getSwitchPort()
weibitd5037072014-06-13 10:07:01 -070073 );
74 testTopology.putLink(testLinkEast);
75 testTopology.putLink(testLinkWest);
76 }
77 }
78
79 @After
80 public void tearDown() throws Exception {
81
82 }
83
84 /**
85 * Test the result of getSwitch function.
86 */
87 @Test
88 public void testGetSwitch() {
89 // Verify the switch is in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070090 assertNotNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM - 1)));
weibitd5037072014-06-13 10:07:01 -070091
92 // Verify there is no such switch in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070093 assertNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM + 1)));
weibitd5037072014-06-13 10:07:01 -070094 long index = 0;
95 Iterator<Switch> itr = testTopology.getSwitches().iterator();
96 while (itr.hasNext()) {
97 index++;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070098 Dpid swID = itr.next().getDpid();
99 assertThat(swID.value(),
100 is(both(greaterThanOrEqualTo(1L))
101 .and(lessThanOrEqualTo(TEST_SWITCH_NUM))));
weibitd5037072014-06-13 10:07:01 -0700102 }
103
104 // Verify the total number of switches
105 assertEquals(TEST_SWITCH_NUM, index);
106 }
107
108 /**
109 * Test the result of getPort function.
110 */
111 @Test
112 public void testGetPort() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700113 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700114 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
115 // Verify ports are in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700116 final Dpid dpid = new Dpid(switchID);
117 assertNotNull(testTopology.getSwitch(dpid).getPort(PORT_NUMBER_1));
118 assertNotNull(testTopology.getSwitch(dpid).getPort(PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700119
120 // Verify there is no such port in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700121 assertNull(testTopology.getSwitch(dpid).getPort(bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700122 }
123 }
124
125 /**
126 * Test the result of getLink function.
127 */
128 @Test
129 public void testGetLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700130 Dpid sw1ID = new Dpid(1L);
131 Dpid sw3ID = new Dpid(3L);
weibitd5037072014-06-13 10:07:01 -0700132
133 // Verify there is no such link b/w these two switches
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700134 assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw3ID));
weibitd5037072014-06-13 10:07:01 -0700135 long index = 0;
136 Iterator<Link> itr = testTopology.getLinks().iterator();
137 while (itr.hasNext()) {
138 index++;
139 Link objectLink = itr.next();
140 Switch srcSw = (objectLink.getSrcSwitch());
141 Switch dstSw = (objectLink.getDstSwitch());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700142
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700143 LinkTuple linkId = objectLink.getLinkTuple();
144 // Verify the link through #getLink
145 Link linkA = testTopology.getLink(linkId.getSrc().getDpid(),
146 linkId.getSrc().getPortNumber(),
147 linkId.getDst().getDpid(),
148 linkId.getDst().getPortNumber());
149 assertEquals(linkId, linkA.getLinkTuple());
150
151 Link linkB = testTopology.getLink(linkId.getSrc().getDpid(),
152 linkId.getSrc().getPortNumber(),
153 linkId.getDst().getDpid(),
154 linkId.getDst().getPortNumber(),
155 TopologyElement.TYPE_PACKET_LAYER);
156 assertEquals(linkId, linkB.getLinkTuple());
157
158
159
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700160 // confirm link is forming a link
161 final long smallerDpid = Math.min(srcSw.getDpid().value(), dstSw.getDpid().value());
162 final long largerDpid = Math.max(srcSw.getDpid().value(), dstSw.getDpid().value());
163 assertThat(largerDpid - smallerDpid,
164 is(either(equalTo(1L)).or(equalTo(TEST_SWITCH_NUM - 1))));
weibitd5037072014-06-13 10:07:01 -0700165 }
166
167 // Verify the total number of links
168 assertEquals(TEST_SWITCH_NUM * 2, index);
169 }
170
171 /**
172 * Test the result of getOutgoingLink function.
173 */
174 @Test
175 public void testGetOutgoingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700176 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700177 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700178 final Dpid dpid = new Dpid(switchID);
179 assertNotNull(testTopology.getOutgoingLink(dpid, PORT_NUMBER_1));
180 assertNotNull(testTopology.getOutgoingLink(dpid, PORT_NUMBER_2));
181
182 Link la = testTopology.getOutgoingLink(dpid, PORT_NUMBER_2,
183 TopologyElement.TYPE_PACKET_LAYER);
184 Link lb = testTopology.getOutgoingLink(dpid, PORT_NUMBER_2);
185
186 assertTrue(la.getLinkTuple().equals(lb.getLinkTuple()));
187
188 Collection<Link> links = testTopology.getOutgoingLinks(
189 new SwitchPort(dpid, PORT_NUMBER_1));
190 assertEquals(1, links.size());
weibitd5037072014-06-13 10:07:01 -0700191
192 // Verify there is no such link in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700193 assertNull(testTopology.getOutgoingLink(dpid, bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700194 }
195 }
196
197 /**
198 * Test the result of getIncomingLink function.
199 */
200 @Test
201 public void testGetIncomingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700202 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700203 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
204 // Verify the links are in the graphDB
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700205 final Dpid dpid = new Dpid(switchID);
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700206 assertNotNull(testTopology.getIncomingLink(
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700207 dpid, PORT_NUMBER_1));
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700208 assertNotNull(testTopology.getIncomingLink(
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700209 dpid, PORT_NUMBER_2));
210
211 Link la = testTopology.getIncomingLink(dpid, PORT_NUMBER_2,
212 TopologyElement.TYPE_PACKET_LAYER);
213 Link lb = testTopology.getIncomingLink(dpid, PORT_NUMBER_2);
214
215 assertTrue(la.getLinkTuple().equals(lb.getLinkTuple()));
216
217 Collection<Link> links = testTopology.getIncomingLinks(
218 new SwitchPort(dpid, PORT_NUMBER_1));
219 assertEquals(1, links.size());
weibitd5037072014-06-13 10:07:01 -0700220
221 // Verify there is no such link in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700222 assertNull(testTopology.getIncomingLink(
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700223 dpid, bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700224 }
225 }
226
227 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700228 * Test the result of getHostByMac function.
weibitd5037072014-06-13 10:07:01 -0700229 */
230 @Test
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700231 public void testGetHostByMac() {
weibitd5037072014-06-13 10:07:01 -0700232 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
233 MACAddress devMac = MACAddress.valueOf(switchID);
234
235 // Verify the device is in the graphDB
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700236 assertNotNull(testTopology.getHostByMac(devMac));
weibitd5037072014-06-13 10:07:01 -0700237 }
238 }
239
240 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700241 * Test the result of removeHost function.
weibitd5037072014-06-13 10:07:01 -0700242 */
243 @Test
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700244 public void testRemoveHost() {
weibitd5037072014-06-13 10:07:01 -0700245 int devCount = 0;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700246 Iterator<Host> itr = testTopology.getHosts().iterator();
weibitd5037072014-06-13 10:07:01 -0700247 while (itr.hasNext()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700248 Host currDev = itr.next();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700249 final MACAddress mac = currDev.getMacAddress();
250 testTopology.removeHost(mac);
251 assertNull(testTopology.getHostByMac(mac));
weibitd5037072014-06-13 10:07:01 -0700252 devCount++;
253 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700254 for (Switch sw : testTopology.getSwitches()) {
255 for (Port port : sw.getPorts()) {
256 assertTrue(port.getHosts().isEmpty());
257 }
258 }
weibitd5037072014-06-13 10:07:01 -0700259
260 // Verify all hosts have been removed successfully
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700261 assertEquals(TEST_HOST_NUM, devCount);
weibitd5037072014-06-13 10:07:01 -0700262 }
263
264 /**
265 * Test the result of removeLink function.
266 */
267 @Test
268 public void testRemoveLink() {
269 long index = 0;
270 Iterator<Link> itr = testTopology.getLinks().iterator();
271 while (itr.hasNext()) {
272 index++;
273 Link objectLink = itr.next();
274 Switch srcSw = (objectLink.getSrcSwitch());
275 Port srcPort = objectLink.getSrcPort();
276 Switch dstSw = (objectLink.getDstSwitch());
277 Port dstPort = objectLink.getDstPort();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700278
279 testTopology.removeLink(objectLink.getLinkTuple());
weibitd5037072014-06-13 10:07:01 -0700280
281 // Verify the link was removed successfully
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700282 assertNull(testTopology.getLink(
283 srcSw.getDpid(), srcPort.getNumber(),
weibitd5037072014-06-13 10:07:01 -0700284 dstSw.getDpid(), dstPort.getNumber()));
285 }
286
287 // Verify all links have been removed successfully
288 assertEquals(TEST_SWITCH_NUM * 2, index);
289 }
290
291 /**
292 * Test the result of removeSwitch function.
293 */
294 @Test
295 public void testRemoveSwitch() {
296 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700297 final Dpid dpid = new Dpid(switchID);
298 Iterator<Host> itr = testTopology.getSwitch(dpid).getHosts().iterator();
weibitd5037072014-06-13 10:07:01 -0700299 while (itr.hasNext()) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700300 testTopology.removeHost(itr.next().getMacAddress());
weibitd5037072014-06-13 10:07:01 -0700301 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700302 for (Port port : testTopology.getSwitch(dpid).getPorts()) {
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -0700303 testTopology.removePort(port.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700304 }
305 testTopology.removeSwitch(dpid);
weibitd5037072014-06-13 10:07:01 -0700306
307 // Verify the switch has been removed from the graphDB successfully
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700308 assertNull(testTopology.getSwitch(dpid));
weibitd5037072014-06-13 10:07:01 -0700309 }
310
311 // Verify all switches have been removed successfully
312 Iterator<Switch> itr = testTopology.getSwitches().iterator();
313 assertFalse(itr.hasNext());
314 }
315}