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); |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 57 | testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2), |
| 58 | testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1) |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 59 | ); |
| 60 | LinkImpl testLinkWest = new LinkImpl(testTopology, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 61 | testTopology.getPort(new Dpid(switchID % TEST_SWITCH_NUM + 1), PORT_NUMBER_1), |
| 62 | testTopology.getPort(new Dpid(switchID), PORT_NUMBER_2) |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 63 | ); |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 80 | assertNotNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM - 1))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 81 | |
| 82 | // Verify there is no such switch in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 83 | assertNull(testTopology.getSwitch(new Dpid(TEST_SWITCH_NUM + 1))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 84 | long index = 0; |
| 85 | Iterator<Switch> itr = testTopology.getSwitches().iterator(); |
| 86 | while (itr.hasNext()) { |
| 87 | index++; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 88 | Dpid swID = itr.next().getDpid(); |
| 89 | assertThat(swID.value(), |
| 90 | is(both(greaterThanOrEqualTo(1L)) |
| 91 | .and(lessThanOrEqualTo(TEST_SWITCH_NUM)))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 92 | } |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 103 | PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 104 | for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) { |
| 105 | // Verify ports are in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 106 | assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_1)); |
| 107 | assertNotNull(testTopology.getSwitch(new Dpid(switchID)).getPort(PORT_NUMBER_2)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 108 | |
| 109 | // Verify there is no such port in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 110 | assertNull(testTopology.getSwitch(new Dpid(switchID)).getPort(bogusPortNum)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Test the result of getLink function. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testGetLink() { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 119 | Dpid sw1ID = new Dpid(1L); |
| 120 | Dpid sw3ID = new Dpid(3L); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 121 | |
| 122 | // Verify there is no such link b/w these two switches |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 123 | assertNull((testTopology.getSwitch(sw1ID)).getLinkToNeighbor(sw3ID)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 124 | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 131 | |
| 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)))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 137 | } |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 148 | PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 149 | for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 150 | assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_1)); |
| 151 | assertNotNull(testTopology.getOutgoingLink(new Dpid(switchID), PORT_NUMBER_2)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 152 | |
| 153 | // Verify there is no such link in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 154 | assertNull(testTopology.getOutgoingLink(new Dpid(switchID), bogusPortNum)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Test the result of getIncomingLink function. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testGetIncomingLink() { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 163 | PortNumber bogusPortNum = new PortNumber((short) (SWITCH_PORT_2 + 1)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 164 | for (long switchID = 1; switchID <= TEST_SWITCH_NUM; switchID++) { |
| 165 | // Verify the links are in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 166 | assertNotNull(testTopology.getIncomingLink( |
| 167 | new Dpid(switchID), PORT_NUMBER_1)); |
| 168 | assertNotNull(testTopology.getIncomingLink( |
| 169 | new Dpid(switchID), PORT_NUMBER_2)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 170 | |
| 171 | // Verify there is no such link in the graphDB |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 172 | assertNull(testTopology.getIncomingLink( |
| 173 | new Dpid(switchID), bogusPortNum)); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 174 | } |
| 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 225 | assertNull(testTopology.getLink( |
| 226 | srcSw.getDpid(), srcPort.getNumber(), |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 227 | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 240 | Iterator<Device> itr = testTopology.getSwitch(new Dpid(switchID)).getDevices().iterator(); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 241 | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 247 | assertNull(testTopology.getSwitch(new Dpid(switchID))); |
weibit | d503707 | 2014-06-13 10:07:01 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Verify all switches have been removed successfully |
| 251 | Iterator<Switch> itr = testTopology.getSwitches().iterator(); |
| 252 | assertFalse(itr.hasNext()); |
| 253 | } |
| 254 | } |