blob: 250142516e3600fb8f3b5093b043709b4b731e54 [file] [log] [blame]
weibitd5037072014-06-13 10:07:01 -07001package net.onrc.onos.core.topology;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import java.util.Iterator;
10
11import net.floodlightcontroller.util.MACAddress;
12
13import org.junit.After;
14import org.junit.Before;
15import org.junit.Test;
16
17/**
18 * Unit tests for the TopologyImpl Class in the Topology module.
19 * These test cases check the sanity of getSwitch/removeSwitch,
20 * getLink/removeLink, getDevice/removeDevice, and getPort functions and
21 * verify the data objects inside the global graphDB through a injected network.
22 * The injected network has a ring topology with a large number of switches (configurable), and
23 * each switch is associated with one device and connected to two other switches.
24 */
25public class TopologyImplTest {
26 private TopologyImpl testTopology;
27 private static final Long SWITCH_HOST_PORT = 1L;
28 private static final Long SWITCH_PORT_1 = 2L;
29 private static final Long SWITCH_PORT_2 = 3L;
30
31 // Set the test network size, it should be larger than 3
32 private static final long TEST_SWITCH_NUM = 100L;
33
34 @Before
35 public void setUp() throws Exception {
36 // Create the injected network first
37 testTopology = new TopologyImpl();
38
39 // Create a number of switches and install two ports for each switch
40 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
41 SwitchImpl testSwitch = new SwitchImpl(testTopology, switchID);
42 testSwitch.addPort(SWITCH_PORT_1);
43 testSwitch.addPort(SWITCH_PORT_2);
44 testTopology.putSwitch(testSwitch);
45
46 // Create a host for each switch
47 MACAddress devMac = MACAddress.valueOf(switchID);
48 DeviceImpl testHost = new DeviceImpl(testTopology, devMac);
49 testHost.addAttachmentPoint(testSwitch.addPort(SWITCH_HOST_PORT));
50 testTopology.putDevice(testHost);
51 }
52
53 // Create one bidirectional link b/w two switches to construct a ring topology
54 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
55 LinkImpl testLinkEast = new LinkImpl(testTopology,
56 testTopology.getPort(switchID, SWITCH_PORT_2),
57 testTopology.getPort(switchID % TEST_SWITCH_NUM + 1, SWITCH_PORT_1)
58 );
59 LinkImpl testLinkWest = new LinkImpl(testTopology,
60 testTopology.getPort(switchID % TEST_SWITCH_NUM + 1, SWITCH_PORT_1),
61 testTopology.getPort(switchID, SWITCH_PORT_2)
62 );
63 testTopology.putLink(testLinkEast);
64 testTopology.putLink(testLinkWest);
65 }
66 }
67
68 @After
69 public void tearDown() throws Exception {
70
71 }
72
73 /**
74 * Test the result of getSwitch function.
75 */
76 @Test
77 public void testGetSwitch() {
78 // Verify the switch is in the graphDB
79 assertNotNull(testTopology.getSwitch(TEST_SWITCH_NUM - 1));
80
81 // Verify there is no such switch in the graphDB
82 assertNull(testTopology.getSwitch(TEST_SWITCH_NUM + 1));
83 long swID = 0;
84 long index = 0;
85 Iterator<Switch> itr = testTopology.getSwitches().iterator();
86 while (itr.hasNext()) {
87 index++;
88 swID = itr.next().getDpid();
89 assertTrue(swID >= 1 && swID <= TEST_SWITCH_NUM);
90 }
91
92 // Verify the total number of switches
93 assertEquals(TEST_SWITCH_NUM, index);
94 }
95
96 /**
97 * Test the result of getPort function.
98 */
99 @Test
100 public void testGetPort() {
101 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
102 // Verify ports are in the graphDB
103 assertNotNull(testTopology.getSwitch(switchID).getPort(SWITCH_PORT_1));
104 assertNotNull(testTopology.getSwitch(switchID).getPort(SWITCH_PORT_2));
105
106 // Verify there is no such port in the graphDB
107 assertNull(testTopology.getSwitch(switchID).getPort(SWITCH_PORT_2 + 1));
108 }
109 }
110
111 /**
112 * Test the result of getLink function.
113 */
114 @Test
115 public void testGetLink() {
116 long sw1ID = 1L;
117 long sw2ID = 3L;
118
119 // Verify there is no such link b/w these two switches
120 assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw2ID));
121 long index = 0;
122 Iterator<Link> itr = testTopology.getLinks().iterator();
123 while (itr.hasNext()) {
124 index++;
125 Link objectLink = itr.next();
126 Switch srcSw = (objectLink.getSrcSwitch());
127 Switch dstSw = (objectLink.getDstSwitch());
128 if (srcSw.getDpid() < TEST_SWITCH_NUM && dstSw.getDpid() < TEST_SWITCH_NUM) {
129 // Verify the link relationship
130 assertTrue((srcSw.getDpid() == dstSw.getDpid() - 1
131 || (srcSw.getDpid() == dstSw.getDpid() + 1)));
132 }
133 }
134
135 // Verify the total number of links
136 assertEquals(TEST_SWITCH_NUM * 2, index);
137 }
138
139 /**
140 * Test the result of getOutgoingLink function.
141 */
142 @Test
143 public void testGetOutgoingLink() {
144 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
145 assertNotNull(testTopology.getOutgoingLink(switchID, SWITCH_PORT_1));
146 assertNotNull(testTopology.getOutgoingLink(switchID, SWITCH_PORT_2));
147
148 // Verify there is no such link in the graphDB
149 assertNull(testTopology.getOutgoingLink(switchID, SWITCH_PORT_1 + 2));
150 }
151 }
152
153 /**
154 * Test the result of getIncomingLink function.
155 */
156 @Test
157 public void testGetIncomingLink() {
158 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
159 // Verify the links are in the graphDB
160 assertNotNull(testTopology.getIncomingLink(switchID, SWITCH_PORT_1));
161 assertNotNull(testTopology.getIncomingLink(switchID, SWITCH_PORT_2));
162
163 // Verify there is no such link in the graphDB
164 assertNull(testTopology.getIncomingLink(switchID, SWITCH_PORT_1 + 2));
165 }
166 }
167
168 /**
169 * Test the result of getDeviceByMac function.
170 */
171 @Test
172 public void testGetDeviceByMac() {
173 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
174 MACAddress devMac = MACAddress.valueOf(switchID);
175
176 // Verify the device is in the graphDB
177 assertNotNull(testTopology.getDeviceByMac(devMac));
178 }
179 }
180
181 /**
182 * Test the result of removeDevice function.
183 */
184 @Test
185 public void testRemoveDevice() {
186 int devCount = 0;
187 Iterator<Device> itr = testTopology.getDevices().iterator();
188 while (itr.hasNext()) {
189 Device currDev = itr.next();
190 testTopology.removeDevice(currDev);
191 testTopology.getDeviceByMac(currDev.getMacAddress());
192 devCount++;
193 }
194
195 // Verify all hosts have been removed successfully
196 assertEquals(TEST_SWITCH_NUM, devCount);
197 }
198
199 /**
200 * Test the result of removeLink function.
201 */
202 @Test
203 public void testRemoveLink() {
204 long index = 0;
205 Iterator<Link> itr = testTopology.getLinks().iterator();
206 while (itr.hasNext()) {
207 index++;
208 Link objectLink = itr.next();
209 Switch srcSw = (objectLink.getSrcSwitch());
210 Port srcPort = objectLink.getSrcPort();
211 Switch dstSw = (objectLink.getDstSwitch());
212 Port dstPort = objectLink.getDstPort();
213 testTopology.removeLink(objectLink);
214
215 // Verify the link was removed successfully
216 assertNull(testTopology.getLink(srcSw.getDpid(), srcPort.getNumber(),
217 dstSw.getDpid(), dstPort.getNumber()));
218 }
219
220 // Verify all links have been removed successfully
221 assertEquals(TEST_SWITCH_NUM * 2, index);
222 }
223
224 /**
225 * Test the result of removeSwitch function.
226 */
227 @Test
228 public void testRemoveSwitch() {
229 for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) {
230 Iterator<Device> itr = testTopology.getSwitch(switchID).getDevices().iterator();
231 while (itr.hasNext()) {
232 testTopology.removeDevice((Device) itr);
233 }
234 testTopology.removeSwitch(switchID);
235
236 // Verify the switch has been removed from the graphDB successfully
237 assertNull(testTopology.getSwitch(switchID));
238 }
239
240 // Verify all switches have been removed successfully
241 Iterator<Switch> itr = testTopology.getSwitches().iterator();
242 assertFalse(itr.hasNext());
243 }
244}