weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.*; |
| 4 | import static org.hamcrest.Matchers.*; |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 5 | |
| 6 | import java.util.Iterator; |
| 7 | |
| 8 | import net.floodlightcontroller.util.MACAddress; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.Dpid; |
| 10 | import net.onrc.onos.core.util.PortNumber; |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 11 | |
| 12 | import org.junit.After; |
| 13 | import org.junit.Before; |
| 14 | import 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 | */ |
| 24 | public 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 28 | private static final PortNumber PORT_NUMBER_1 = new PortNumber(SWITCH_PORT_1.shortValue()); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 29 | private static final Long SWITCH_PORT_2 = 3L; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 30 | private static final PortNumber PORT_NUMBER_2 = new PortNumber(SWITCH_PORT_2.shortValue()); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 31 | |
| 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); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 43 | testTopology.putSwitch(testSwitch); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 44 | 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); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 51 | |
| 52 | // Create a host for each switch |
| 53 | MACAddress devMac = MACAddress.valueOf(switchID); |
| 54 | DeviceImpl testHost = new DeviceImpl(testTopology, devMac); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 55 | testHost.addAttachmentPoint(hostPort); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 56 | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 62 | testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2), |
| 63 | testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1) |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 64 | ); |
| 65 | LinkImpl testLinkWest = new LinkImpl(testTopology, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 66 | testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1), |
| 67 | testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2) |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 68 | ); |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 85 | assertNotNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM - 1))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 86 | |
| 87 | // Verify there is no such switch in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 88 | assertNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM + 1))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 89 | long index = 0; |
| 90 | Iterator<Switch> itr = testTopology.getSwitches().iterator(); |
| 91 | while (itr.hasNext()) { |
| 92 | index++; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 93 | Dpid swID = itr.next().getDpid(); |
| 94 | assertThat(swID.value(), |
| 95 | is(both(greaterThanOrEqualTo(1L)) |
| 96 | .and(lessThanOrEqualTo(TEST_SWITCH_NUM)))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 97 | } |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 108 | PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 109 | for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) { |
| 110 | // Verify ports are in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 111 | assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_1)); |
| 112 | assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_2)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 113 | |
| 114 | // Verify there is no such port in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 115 | assertNull(testTopology.getSwitch(new Dpid(switchID)).getPort(bogusPortNum)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Test the result of getLink function. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testGetLink() { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 124 | Dpid sw1ID = new Dpid(1L); |
| 125 | Dpid sw3ID = new Dpid(3L); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 126 | |
| 127 | // Verify there is no such link b/w these two switches |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 128 | assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw3ID)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 129 | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 136 | |
| 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)))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 142 | } |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 153 | PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 154 | for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 155 | assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_1)); |
| 156 | assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_2)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 157 | |
| 158 | // Verify there is no such link in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 159 | assertNull(testTopology.getOutgoingLink(new Dpid(switchID), bogusPortNum)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Test the result of getIncomingLink function. |
| 165 | */ |
| 166 | @Test |
| 167 | public void testGetIncomingLink() { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 168 | PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 169 | for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) { |
| 170 | // Verify the links are in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 171 | assertNotNull(testTopology.getIncomingLink( |
| 172 | new Dpid(switchID), PORT_NUMBER_1)); |
| 173 | assertNotNull(testTopology.getIncomingLink( |
| 174 | new Dpid(switchID), PORT_NUMBER_2)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 175 | |
| 176 | // Verify there is no such link in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 177 | assertNull(testTopology.getIncomingLink( |
| 178 | new Dpid(switchID), bogusPortNum)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 179 | } |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 230 | assertNull(testTopology.getLink( |
| 231 | srcSw.getDpid(), srcPort.getNumber(), |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 232 | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 245 | Iterator<Device> itr = testTopology.getSwitch(new Dpid(switchID)).getDevices().iterator(); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 246 | while (itr.hasNext()) { |
Yuta HIGUCHI | 435d9da | 2014-07-04 10:55:09 -0700 | [diff] [blame] | 247 | testTopology.removeDevice(itr.next()); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 248 | } |
| 249 | testTopology.removeSwitch(switchID); |
| 250 | |
| 251 | // Verify the switch has been removed from the graphDB successfully |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 252 | assertNull(testTopology.getSwitch(new Dpid(switchID))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Verify all switches have been removed successfully |
| 256 | Iterator<Switch> itr = testTopology.getSwitches().iterator(); |
| 257 | assertFalse(itr.hasNext()); |
| 258 | } |
| 259 | } |