blob: bffca3651c0a2098f8f18cc098f956210d913067 [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
6import java.util.Iterator;
7
8import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07009import net.onrc.onos.core.util.Dpid;
10import net.onrc.onos.core.util.PortNumber;
weibitd5037072014-06-13 10:07:01 -070011
12import org.junit.After;
13import org.junit.Before;
14import org.junit.Test;
15
16/**
17 * Unit tests for the TopologyImpl Class in the Topology module.
18 * These test cases check the sanity of getSwitch/removeSwitch,
19 * getLink/removeLink, getDevice/removeDevice, and getPort functions and
20 * verify the data objects inside the global graphDB through a injected network.
21 * The injected network has a ring topology with a large number of switches (configurable), and
22 * each switch is associated with one device and connected to two other switches.
23 */
24public class TopologyImplTest {
25 private TopologyImpl testTopology;
26 private static final Long SWITCH_HOST_PORT = 1L;
27 private static final Long SWITCH_PORT_1 = 2L;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070028 private static final PortNumber PORT_NUMBER_1 = new PortNumber(SWITCH_PORT_1.shortValue());
weibitd5037072014-06-13 10:07:01 -070029 private static final Long SWITCH_PORT_2 = 3L;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070030 private static final PortNumber PORT_NUMBER_2 = new PortNumber(SWITCH_PORT_2.shortValue());
weibitd5037072014-06-13 10:07:01 -070031
32 // Set the test network size, it should be larger than 3
33 private static final long TEST_SWITCH_NUM = 100L;
34
35 @Before
36 public void setUp() throws Exception {
37 // Create the injected network first
38 testTopology = new TopologyImpl();
39
40 // Create a number of switches and install two ports for each switch
41 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
42 SwitchImpl testSwitch = new SwitchImpl(testTopology, switchID);
weibitd5037072014-06-13 10:07:01 -070043 testTopology.putSwitch(testSwitch);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070044 testTopology.putPort(new PortImpl(testTopology,
45 new Dpid(switchID), PORT_NUMBER_1));
46 testTopology.putPort(new PortImpl(testTopology,
47 new Dpid(switchID), PORT_NUMBER_2));
48 Port hostPort = new PortImpl(testTopology,
49 new Dpid(switchID), new PortNumber(SWITCH_HOST_PORT.shortValue()));
50 testTopology.putPort(hostPort);
weibitd5037072014-06-13 10:07:01 -070051
52 // Create a host for each switch
53 MACAddress devMac = MACAddress.valueOf(switchID);
54 DeviceImpl testHost = new DeviceImpl(testTopology, devMac);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070055 testHost.addAttachmentPoint(hostPort);
weibitd5037072014-06-13 10:07:01 -070056 testTopology.putDevice(testHost);
57 }
58
59 // Create one bidirectional link b/w two switches to construct a ring topology
60 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
61 LinkImpl testLinkEast = new LinkImpl(testTopology,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070062 testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2),
63 testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1)
weibitd5037072014-06-13 10:07:01 -070064 );
65 LinkImpl testLinkWest = new LinkImpl(testTopology,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070066 testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1),
67 testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2)
weibitd5037072014-06-13 10:07:01 -070068 );
69 testTopology.putLink(testLinkEast);
70 testTopology.putLink(testLinkWest);
71 }
72 }
73
74 @After
75 public void tearDown() throws Exception {
76
77 }
78
79 /**
80 * Test the result of getSwitch function.
81 */
82 @Test
83 public void testGetSwitch() {
84 // Verify the switch is in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070085 assertNotNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM - 1)));
weibitd5037072014-06-13 10:07:01 -070086
87 // Verify there is no such switch in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070088 assertNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM + 1)));
weibitd5037072014-06-13 10:07:01 -070089 long index = 0;
90 Iterator<Switch> itr = testTopology.getSwitches().iterator();
91 while (itr.hasNext()) {
92 index++;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070093 Dpid swID = itr.next().getDpid();
94 assertThat(swID.value(),
95 is(both(greaterThanOrEqualTo(1L))
96 .and(lessThanOrEqualTo(TEST_SWITCH_NUM))));
weibitd5037072014-06-13 10:07:01 -070097 }
98
99 // Verify the total number of switches
100 assertEquals(TEST_SWITCH_NUM, index);
101 }
102
103 /**
104 * Test the result of getPort function.
105 */
106 @Test
107 public void testGetPort() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700108 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700109 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
110 // Verify ports are in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700111 assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_1));
112 assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700113
114 // Verify there is no such port in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700115 assertNull(testTopology.getSwitch(new Dpid(switchID)).getPort(bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700116 }
117 }
118
119 /**
120 * Test the result of getLink function.
121 */
122 @Test
123 public void testGetLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700124 Dpid sw1ID = new Dpid(1L);
125 Dpid sw3ID = new Dpid(3L);
weibitd5037072014-06-13 10:07:01 -0700126
127 // Verify there is no such link b/w these two switches
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700128 assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw3ID));
weibitd5037072014-06-13 10:07:01 -0700129 long index = 0;
130 Iterator<Link> itr = testTopology.getLinks().iterator();
131 while (itr.hasNext()) {
132 index++;
133 Link objectLink = itr.next();
134 Switch srcSw = (objectLink.getSrcSwitch());
135 Switch dstSw = (objectLink.getDstSwitch());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700136
137 // confirm link is forming a link
138 final long smallerDpid = Math.min(srcSw.getDpid().value(), dstSw.getDpid().value());
139 final long largerDpid = Math.max(srcSw.getDpid().value(), dstSw.getDpid().value());
140 assertThat(largerDpid - smallerDpid,
141 is(either(equalTo(1L)).or(equalTo(TEST_SWITCH_NUM - 1))));
weibitd5037072014-06-13 10:07:01 -0700142 }
143
144 // Verify the total number of links
145 assertEquals(TEST_SWITCH_NUM * 2, index);
146 }
147
148 /**
149 * Test the result of getOutgoingLink function.
150 */
151 @Test
152 public void testGetOutgoingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700153 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700154 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700155 assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_1));
156 assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700157
158 // Verify there is no such link in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700159 assertNull(testTopology.getOutgoingLink(new Dpid(switchID), bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700160 }
161 }
162
163 /**
164 * Test the result of getIncomingLink function.
165 */
166 @Test
167 public void testGetIncomingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700168 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700169 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
170 // Verify the links are in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700171 assertNotNull(testTopology.getIncomingLink(
172 new Dpid(switchID), PORT_NUMBER_1));
173 assertNotNull(testTopology.getIncomingLink(
174 new Dpid(switchID), PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700175
176 // Verify there is no such link in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700177 assertNull(testTopology.getIncomingLink(
178 new Dpid(switchID), bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700179 }
180 }
181
182 /**
183 * Test the result of getDeviceByMac function.
184 */
185 @Test
186 public void testGetDeviceByMac() {
187 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
188 MACAddress devMac = MACAddress.valueOf(switchID);
189
190 // Verify the device is in the graphDB
191 assertNotNull(testTopology.getDeviceByMac(devMac));
192 }
193 }
194
195 /**
196 * Test the result of removeDevice function.
197 */
198 @Test
199 public void testRemoveDevice() {
200 int devCount = 0;
201 Iterator<Device> itr = testTopology.getDevices().iterator();
202 while (itr.hasNext()) {
203 Device currDev = itr.next();
204 testTopology.removeDevice(currDev);
205 testTopology.getDeviceByMac(currDev.getMacAddress());
206 devCount++;
207 }
208
209 // Verify all hosts have been removed successfully
210 assertEquals(TEST_SWITCH_NUM, devCount);
211 }
212
213 /**
214 * Test the result of removeLink function.
215 */
216 @Test
217 public void testRemoveLink() {
218 long index = 0;
219 Iterator<Link> itr = testTopology.getLinks().iterator();
220 while (itr.hasNext()) {
221 index++;
222 Link objectLink = itr.next();
223 Switch srcSw = (objectLink.getSrcSwitch());
224 Port srcPort = objectLink.getSrcPort();
225 Switch dstSw = (objectLink.getDstSwitch());
226 Port dstPort = objectLink.getDstPort();
227 testTopology.removeLink(objectLink);
228
229 // Verify the link was removed successfully
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700230 assertNull(testTopology.getLink(
231 srcSw.getDpid(), srcPort.getNumber(),
weibitd5037072014-06-13 10:07:01 -0700232 dstSw.getDpid(), dstPort.getNumber()));
233 }
234
235 // Verify all links have been removed successfully
236 assertEquals(TEST_SWITCH_NUM * 2, index);
237 }
238
239 /**
240 * Test the result of removeSwitch function.
241 */
242 @Test
243 public void testRemoveSwitch() {
244 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700245 Iterator<Device> itr = testTopology.getSwitch(new Dpid(switchID)).getDevices().iterator();
weibitd5037072014-06-13 10:07:01 -0700246 while (itr.hasNext()) {
Yuta HIGUCHI435d9da2014-07-04 10:55:09 -0700247 testTopology.removeDevice(itr.next());
weibitd5037072014-06-13 10:07:01 -0700248 }
249 testTopology.removeSwitch(switchID);
250
251 // Verify the switch has been removed from the graphDB successfully
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700252 assertNull(testTopology.getSwitch(new Dpid(switchID)));
weibitd5037072014-06-13 10:07:01 -0700253 }
254
255 // Verify all switches have been removed successfully
256 Iterator<Switch> itr = testTopology.getSwitches().iterator();
257 assertFalse(itr.hasNext());
258 }
259}