blob: cc7ac9cc40ddc0e3cf6eb22f5e0cb2385b7fade8 [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);
43 testSwitch.addPort(SWITCH_PORT_1);
44 testSwitch.addPort(SWITCH_PORT_2);
45 testTopology.putSwitch(testSwitch);
46
47 // Create a host for each switch
48 MACAddress devMac = MACAddress.valueOf(switchID);
49 DeviceImpl testHost = new DeviceImpl(testTopology, devMac);
50 testHost.addAttachmentPoint(testSwitch.addPort(SWITCH_HOST_PORT));
51 testTopology.putDevice(testHost);
52 }
53
54 // Create one bidirectional link b/w two switches to construct a ring topology
55 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
56 LinkImpl testLinkEast = new LinkImpl(testTopology,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070057 testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2),
58 testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1)
weibitd5037072014-06-13 10:07:01 -070059 );
60 LinkImpl testLinkWest = new LinkImpl(testTopology,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070061 testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1),
62 testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2)
weibitd5037072014-06-13 10:07:01 -070063 );
64 testTopology.putLink(testLinkEast);
65 testTopology.putLink(testLinkWest);
66 }
67 }
68
69 @After
70 public void tearDown() throws Exception {
71
72 }
73
74 /**
75 * Test the result of getSwitch function.
76 */
77 @Test
78 public void testGetSwitch() {
79 // Verify the switch is in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070080 assertNotNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM - 1)));
weibitd5037072014-06-13 10:07:01 -070081
82 // Verify there is no such switch in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070083 assertNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM + 1)));
weibitd5037072014-06-13 10:07:01 -070084 long index = 0;
85 Iterator<Switch> itr = testTopology.getSwitches().iterator();
86 while (itr.hasNext()) {
87 index++;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070088 Dpid swID = itr.next().getDpid();
89 assertThat(swID.value(),
90 is(both(greaterThanOrEqualTo(1L))
91 .and(lessThanOrEqualTo(TEST_SWITCH_NUM))));
weibitd5037072014-06-13 10:07:01 -070092 }
93
94 // Verify the total number of switches
95 assertEquals(TEST_SWITCH_NUM, index);
96 }
97
98 /**
99 * Test the result of getPort function.
100 */
101 @Test
102 public void testGetPort() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700103 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700104 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
105 // Verify ports are in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700106 assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_1));
107 assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700108
109 // Verify there is no such port in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700110 assertNull(testTopology.getSwitch(new Dpid(switchID)).getPort(bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700111 }
112 }
113
114 /**
115 * Test the result of getLink function.
116 */
117 @Test
118 public void testGetLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700119 Dpid sw1ID = new Dpid(1L);
120 Dpid sw3ID = new Dpid(3L);
weibitd5037072014-06-13 10:07:01 -0700121
122 // Verify there is no such link b/w these two switches
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700123 assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw3ID));
weibitd5037072014-06-13 10:07:01 -0700124 long index = 0;
125 Iterator<Link> itr = testTopology.getLinks().iterator();
126 while (itr.hasNext()) {
127 index++;
128 Link objectLink = itr.next();
129 Switch srcSw = (objectLink.getSrcSwitch());
130 Switch dstSw = (objectLink.getDstSwitch());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700131
132 // confirm link is forming a link
133 final long smallerDpid = Math.min(srcSw.getDpid().value(), dstSw.getDpid().value());
134 final long largerDpid = Math.max(srcSw.getDpid().value(), dstSw.getDpid().value());
135 assertThat(largerDpid - smallerDpid,
136 is(either(equalTo(1L)).or(equalTo(TEST_SWITCH_NUM - 1))));
weibitd5037072014-06-13 10:07:01 -0700137 }
138
139 // Verify the total number of links
140 assertEquals(TEST_SWITCH_NUM * 2, index);
141 }
142
143 /**
144 * Test the result of getOutgoingLink function.
145 */
146 @Test
147 public void testGetOutgoingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700148 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700149 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700150 assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_1));
151 assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700152
153 // Verify there is no such link in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700154 assertNull(testTopology.getOutgoingLink(new Dpid(switchID), bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700155 }
156 }
157
158 /**
159 * Test the result of getIncomingLink function.
160 */
161 @Test
162 public void testGetIncomingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700163 PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1));
weibitd5037072014-06-13 10:07:01 -0700164 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
165 // Verify the links are in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700166 assertNotNull(testTopology.getIncomingLink(
167 new Dpid(switchID), PORT_NUMBER_1));
168 assertNotNull(testTopology.getIncomingLink(
169 new Dpid(switchID), PORT_NUMBER_2));
weibitd5037072014-06-13 10:07:01 -0700170
171 // Verify there is no such link in the graphDB
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700172 assertNull(testTopology.getIncomingLink(
173 new Dpid(switchID), bogusPortNum));
weibitd5037072014-06-13 10:07:01 -0700174 }
175 }
176
177 /**
178 * Test the result of getDeviceByMac function.
179 */
180 @Test
181 public void testGetDeviceByMac() {
182 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
183 MACAddress devMac = MACAddress.valueOf(switchID);
184
185 // Verify the device is in the graphDB
186 assertNotNull(testTopology.getDeviceByMac(devMac));
187 }
188 }
189
190 /**
191 * Test the result of removeDevice function.
192 */
193 @Test
194 public void testRemoveDevice() {
195 int devCount = 0;
196 Iterator<Device> itr = testTopology.getDevices().iterator();
197 while (itr.hasNext()) {
198 Device currDev = itr.next();
199 testTopology.removeDevice(currDev);
200 testTopology.getDeviceByMac(currDev.getMacAddress());
201 devCount++;
202 }
203
204 // Verify all hosts have been removed successfully
205 assertEquals(TEST_SWITCH_NUM, devCount);
206 }
207
208 /**
209 * Test the result of removeLink function.
210 */
211 @Test
212 public void testRemoveLink() {
213 long index = 0;
214 Iterator<Link> itr = testTopology.getLinks().iterator();
215 while (itr.hasNext()) {
216 index++;
217 Link objectLink = itr.next();
218 Switch srcSw = (objectLink.getSrcSwitch());
219 Port srcPort = objectLink.getSrcPort();
220 Switch dstSw = (objectLink.getDstSwitch());
221 Port dstPort = objectLink.getDstPort();
222 testTopology.removeLink(objectLink);
223
224 // Verify the link was removed successfully
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700225 assertNull(testTopology.getLink(
226 srcSw.getDpid(), srcPort.getNumber(),
weibitd5037072014-06-13 10:07:01 -0700227 dstSw.getDpid(), dstPort.getNumber()));
228 }
229
230 // Verify all links have been removed successfully
231 assertEquals(TEST_SWITCH_NUM * 2, index);
232 }
233
234 /**
235 * Test the result of removeSwitch function.
236 */
237 @Test
238 public void testRemoveSwitch() {
239 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700240 Iterator<Device> itr = testTopology.getSwitch(new Dpid(switchID)).getDevices().iterator();
weibitd5037072014-06-13 10:07:01 -0700241 while (itr.hasNext()) {
242 testTopology.removeDevice((Device) itr);
243 }
244 testTopology.removeSwitch(switchID);
245
246 // Verify the switch has been removed from the graphDB successfully
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700247 assertNull(testTopology.getSwitch(new Dpid(switchID)));
weibitd5037072014-06-13 10:07:01 -0700248 }
249
250 // Verify all switches have been removed successfully
251 Iterator<Switch> itr = testTopology.getSwitches().iterator();
252 assertFalse(itr.hasNext());
253 }
254}