Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.datastore.topology; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | import static org.hamcrest.Matchers.*; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Arrays; |
| 8 | import java.util.HashMap; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.TreeMap; |
| 12 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame^] | 13 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 14 | import net.onrc.onos.core.datastore.IKVClient; |
| 15 | import net.onrc.onos.core.datastore.IKVTable; |
| 16 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 17 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 18 | import net.onrc.onos.core.datastore.WrongVersionException; |
| 19 | import net.onrc.onos.core.datastore.topology.KVDevice; |
| 20 | import net.onrc.onos.core.datastore.topology.KVLink; |
| 21 | import net.onrc.onos.core.datastore.topology.KVPort; |
| 22 | import net.onrc.onos.core.datastore.topology.KVSwitch; |
| 23 | import net.onrc.onos.core.datastore.utils.ByteArrayComparator; |
| 24 | import net.onrc.onos.core.datastore.utils.KVObject; |
| 25 | import net.onrc.onos.core.datastore.utils.KVObject.WriteOp; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 26 | |
| 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | |
| 31 | public class KVTopologyTest { |
| 32 | |
| 33 | static final long VERSION_NONEXISTENT = DataStoreClient.getClient().VERSION_NONEXISTENT(); |
| 34 | |
| 35 | private static final byte[] DEVICE2_MAC_SW2P2 = new byte[] { 6, 5, 4, 3, 2, 1, 0 }; |
| 36 | |
| 37 | private static final Long SW2_PORTNO2 = 2L; |
| 38 | |
| 39 | private static final Long SW2_PORTNO1 = 1L; |
| 40 | |
| 41 | private static final Long DPID2 = 0x2L; |
| 42 | |
| 43 | private static final byte[] DEVICE1_MAC_SW1P1 = new byte[] { 0, 1, 2, 3, 4, 5, 6 }; |
| 44 | |
| 45 | private static final Long SW1_PORTNO2 = 2L; |
| 46 | |
| 47 | private static final Long SW1_PORTNO1 = 1L; |
| 48 | |
| 49 | private static final Long DPID1 = 0x1L; |
| 50 | |
| 51 | @Before |
| 52 | @After |
| 53 | public void wipeTopology() throws Exception { |
| 54 | IKVTable switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME); |
| 55 | DataStoreClient.getClient().dropTable(switchTable); |
| 56 | |
| 57 | IKVTable portTable = DataStoreClient.getClient().getTable(KVPort.GLOBAL_PORT_TABLE_NAME); |
| 58 | DataStoreClient.getClient().dropTable(portTable); |
| 59 | |
| 60 | IKVTable linkTable = DataStoreClient.getClient().getTable(KVLink.GLOBAL_LINK_TABLE_NAME); |
| 61 | DataStoreClient.getClient().dropTable(linkTable); |
| 62 | |
| 63 | IKVTable deviceTable = DataStoreClient.getClient().getTable(KVDevice.GLOBAL_DEVICE_TABLE_NAME); |
| 64 | DataStoreClient.getClient().dropTable(deviceTable); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void basic_switch_test() { |
| 69 | // create switch 0x1 |
| 70 | try { |
| 71 | KVSwitch sw = new KVSwitch(DPID1); |
| 72 | sw.setStatus(KVSwitch.STATUS.ACTIVE); |
| 73 | sw.create(); |
| 74 | assertNotEquals(VERSION_NONEXISTENT, sw.getVersion()); |
| 75 | assertEquals(DPID1, sw.getDpid()); |
| 76 | assertEquals(KVSwitch.STATUS.ACTIVE, sw.getStatus()); |
| 77 | } catch (ObjectExistsException e) { |
| 78 | e.printStackTrace(); |
| 79 | fail("Create Switch Failed " + e); |
| 80 | } |
| 81 | |
| 82 | // read switch 0x1 |
| 83 | KVSwitch swRead = new KVSwitch(DPID1); |
| 84 | try { |
| 85 | swRead.read(); |
| 86 | assertNotEquals(VERSION_NONEXISTENT, swRead.getVersion()); |
| 87 | assertEquals(DPID1, swRead.getDpid()); |
| 88 | assertEquals(KVSwitch.STATUS.ACTIVE, swRead.getStatus()); |
| 89 | } catch (ObjectDoesntExistException e) { |
| 90 | e.printStackTrace(); |
| 91 | fail("Reading Switch Failed " + e); |
| 92 | } |
| 93 | |
| 94 | // and update 0x1 |
| 95 | swRead.setStatus(KVSwitch.STATUS.INACTIVE); |
| 96 | try { |
| 97 | swRead.update(); |
| 98 | assertNotEquals(VERSION_NONEXISTENT, swRead.getVersion()); |
| 99 | assertEquals(DPID1, swRead.getDpid()); |
| 100 | assertEquals(KVSwitch.STATUS.INACTIVE, swRead.getStatus()); |
| 101 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 102 | e.printStackTrace(); |
| 103 | fail("Updating Switch Failed " + e); |
| 104 | } |
| 105 | |
| 106 | // read 0x1 again and delete |
| 107 | KVSwitch swRead2 = new KVSwitch(DPID1); |
| 108 | try { |
| 109 | swRead2.read(); |
| 110 | assertNotEquals(VERSION_NONEXISTENT, swRead2.getVersion()); |
| 111 | assertEquals(DPID1, swRead2.getDpid()); |
| 112 | assertEquals(KVSwitch.STATUS.INACTIVE, swRead2.getStatus()); |
| 113 | } catch (ObjectDoesntExistException e) { |
| 114 | e.printStackTrace(); |
| 115 | fail("Reading Switch Again Failed " + e); |
| 116 | } |
| 117 | |
| 118 | try { |
| 119 | swRead2.delete(); |
| 120 | assertNotEquals(VERSION_NONEXISTENT, swRead2.getVersion()); |
| 121 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 122 | e.printStackTrace(); |
| 123 | fail("Deleting Switch Failed " + e); |
| 124 | } |
| 125 | |
| 126 | // make sure 0x1 is deleted |
| 127 | KVObject swRead3 = new KVSwitch(DPID1); |
| 128 | try { |
| 129 | swRead3.read(); |
| 130 | fail(swRead3 + " was supposed to be deleted, but read succeed"); |
| 131 | } catch (ObjectDoesntExistException e) { |
| 132 | System.out.println("-- " + swRead3 + " not found as expected--"); |
| 133 | e.printStackTrace(System.out); |
| 134 | System.out.println("---------------------------------------"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void topology_setup_and_tear_down() { |
| 140 | topology_setup(); |
| 141 | topology_walk(); |
| 142 | topology_delete(); |
| 143 | } |
| 144 | |
| 145 | private static void topology_setup() { |
| 146 | |
| 147 | // d1 - s1p1 - s1 - s1p2 - s2p1 - s2 - s2p2 |
| 148 | |
| 149 | KVSwitch sw1 = new KVSwitch(DPID1); |
| 150 | sw1.setStatus(KVSwitch.STATUS.ACTIVE); |
| 151 | try { |
| 152 | sw1.create(); |
| 153 | assertNotEquals(VERSION_NONEXISTENT, sw1.getVersion()); |
| 154 | assertEquals(DPID1, sw1.getDpid()); |
| 155 | assertEquals(KVSwitch.STATUS.ACTIVE, sw1.getStatus()); |
| 156 | } catch (ObjectExistsException e) { |
| 157 | e.printStackTrace(); |
| 158 | fail("Switch creation failed " + e); |
| 159 | } |
| 160 | |
| 161 | KVPort sw1p1 = new KVPort(DPID1, SW1_PORTNO1); |
| 162 | sw1p1.setStatus(KVPort.STATUS.ACTIVE); |
| 163 | KVPort sw1p2 = new KVPort(DPID1, SW1_PORTNO2); |
| 164 | sw1p2.setStatus(KVPort.STATUS.ACTIVE); |
| 165 | try { |
| 166 | sw1p1.create(); |
| 167 | assertNotEquals(VERSION_NONEXISTENT, sw1p1.getVersion()); |
| 168 | assertEquals(DPID1, sw1p1.getDpid()); |
| 169 | assertEquals(SW1_PORTNO1, sw1p1.getNumber()); |
| 170 | assertEquals(KVPort.STATUS.ACTIVE, sw1p1.getStatus()); |
| 171 | |
| 172 | sw1p2.create(); |
| 173 | assertNotEquals(VERSION_NONEXISTENT, sw1p2.getVersion()); |
| 174 | assertEquals(DPID1, sw1p2.getDpid()); |
| 175 | assertEquals(SW1_PORTNO2, sw1p2.getNumber()); |
| 176 | assertEquals(KVPort.STATUS.ACTIVE, sw1p2.getStatus()); |
| 177 | } catch (ObjectExistsException e) { |
| 178 | e.printStackTrace(); |
| 179 | fail("Port creation failed " + e); |
| 180 | } |
| 181 | |
| 182 | try { |
| 183 | sw1.update(); |
| 184 | assertNotEquals(VERSION_NONEXISTENT, sw1.getVersion()); |
| 185 | assertEquals(DPID1, sw1.getDpid()); |
| 186 | assertEquals(KVSwitch.STATUS.ACTIVE, sw1.getStatus()); |
| 187 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 188 | e.printStackTrace(); |
| 189 | fail("Switch update failed " + e); |
| 190 | } |
| 191 | |
| 192 | KVDevice d1 = new KVDevice(DEVICE1_MAC_SW1P1); |
| 193 | d1.addPortId(sw1p1.getId()); |
| 194 | |
| 195 | try { |
| 196 | d1.create(); |
| 197 | assertNotEquals(VERSION_NONEXISTENT, d1.getVersion()); |
| 198 | assertEquals(1, d1.getAllPortIds().size()); |
| 199 | assertArrayEquals(sw1p1.getId(), d1.getAllPortIds().iterator().next()); |
| 200 | |
| 201 | try { |
| 202 | sw1p1.update(); |
| 203 | assertNotEquals(VERSION_NONEXISTENT, sw1p1.getVersion()); |
| 204 | assertEquals(DPID1, sw1p1.getDpid()); |
| 205 | assertEquals(SW1_PORTNO1, sw1p1.getNumber()); |
| 206 | assertEquals(KVPort.STATUS.ACTIVE, sw1p1.getStatus()); |
| 207 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 208 | e.printStackTrace(); |
| 209 | fail("Link update failed " + e); |
| 210 | } |
| 211 | } catch (ObjectExistsException e) { |
| 212 | e.printStackTrace(); |
| 213 | fail("Device creation failed " + e); |
| 214 | } |
| 215 | |
| 216 | KVSwitch sw2 = new KVSwitch(DPID2); |
| 217 | sw2.setStatus(KVSwitch.STATUS.ACTIVE); |
| 218 | KVPort sw2p1 = new KVPort(DPID2, SW2_PORTNO1); |
| 219 | sw2p1.setStatus(KVPort.STATUS.ACTIVE); |
| 220 | KVPort sw2p2 = new KVPort(DPID2, SW2_PORTNO2); |
| 221 | sw2p2.setStatus(KVPort.STATUS.ACTIVE); |
| 222 | |
| 223 | KVDevice d2 = new KVDevice(DEVICE2_MAC_SW2P2); |
| 224 | d2.addPortId(sw2p2.getId()); |
| 225 | |
| 226 | IKVClient client = DataStoreClient.getClient(); |
| 227 | |
| 228 | List<WriteOp> groupOp = Arrays.asList( |
| 229 | sw2.createOp(client), sw2p1.createOp(client), |
| 230 | sw2p2.createOp(client), d2.createOp(client) ); |
| 231 | boolean failed = KVObject.multiWrite(groupOp); |
| 232 | if (failed) { |
| 233 | for ( WriteOp op : groupOp ) { |
| 234 | System.err.println(op); |
| 235 | } |
| 236 | fail("Some of Switch/Port/Device creation failed"); |
| 237 | } else { |
| 238 | assertNotEquals(VERSION_NONEXISTENT, sw2.getVersion()); |
| 239 | assertEquals(DPID2, sw2.getDpid()); |
| 240 | assertEquals(KVSwitch.STATUS.ACTIVE, sw2.getStatus()); |
| 241 | |
| 242 | assertNotEquals(VERSION_NONEXISTENT, sw2p1.getVersion()); |
| 243 | assertEquals(DPID2, sw2p1.getDpid()); |
| 244 | assertEquals(SW2_PORTNO1, sw2p1.getNumber()); |
| 245 | assertEquals(KVPort.STATUS.ACTIVE, sw2p1.getStatus()); |
| 246 | |
| 247 | assertNotEquals(VERSION_NONEXISTENT, sw2p2.getVersion()); |
| 248 | assertEquals(DPID2, sw2p2.getDpid()); |
| 249 | assertEquals(SW2_PORTNO2, sw2p2.getNumber()); |
| 250 | assertEquals(KVPort.STATUS.ACTIVE, sw2p2.getStatus()); |
| 251 | |
| 252 | assertNotEquals(VERSION_NONEXISTENT, d2.getVersion()); |
| 253 | assertEquals(1, d2.getAllPortIds().size()); |
| 254 | assertArrayEquals(sw2p2.getId(), d2.getAllPortIds().iterator().next()); |
| 255 | } |
| 256 | |
| 257 | KVLink l1 = new KVLink(DPID1, SW1_PORTNO2, DPID2, SW2_PORTNO1); |
| 258 | l1.setStatus(KVLink.STATUS.ACTIVE); |
| 259 | |
| 260 | try { |
| 261 | l1.create(); |
| 262 | assertNotEquals(VERSION_NONEXISTENT, l1.getVersion()); |
| 263 | assertEquals(KVLink.STATUS.ACTIVE, l1.getStatus()); |
| 264 | assertArrayEquals(sw1.getId(), l1.getSrc().getSwitchID()); |
| 265 | assertArrayEquals(sw1p2.getId(), l1.getSrc().getPortID()); |
| 266 | assertArrayEquals(sw2.getId(), l1.getDst().getSwitchID()); |
| 267 | assertArrayEquals(sw2p1.getId(), l1.getDst().getPortID()); |
| 268 | |
| 269 | try { |
| 270 | sw1p2.update(); |
| 271 | assertNotEquals(VERSION_NONEXISTENT, sw1p2.getVersion()); |
| 272 | assertEquals(DPID1, sw1p2.getDpid()); |
| 273 | assertEquals(SW1_PORTNO2, sw1p2.getNumber()); |
| 274 | assertEquals(KVPort.STATUS.ACTIVE, sw1p2.getStatus()); |
| 275 | |
| 276 | sw2p1.update(); |
| 277 | assertNotEquals(VERSION_NONEXISTENT, sw2p1.getVersion()); |
| 278 | assertEquals(DPID2, sw2p1.getDpid()); |
| 279 | assertEquals(SW2_PORTNO1, sw2p1.getNumber()); |
| 280 | assertEquals(KVPort.STATUS.ACTIVE, sw2p1.getStatus()); |
| 281 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 282 | e.printStackTrace(); |
| 283 | fail("Port update failed " + e); |
| 284 | } |
| 285 | } catch (ObjectExistsException e) { |
| 286 | e.printStackTrace(); |
| 287 | fail("Link creation failed " + e); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | private static void topology_walk() { |
| 293 | Iterable<KVSwitch> swIt = KVSwitch.getAllSwitches(); |
| 294 | List<Long> switchesExpected = new ArrayList<>(Arrays.asList(DPID1, DPID2)); |
| 295 | |
| 296 | System.out.println("Enumerating Switches start"); |
| 297 | for (KVSwitch sw : swIt) { |
| 298 | System.out.println(sw + " @ " + sw.getVersion()); |
| 299 | assertNotEquals(VERSION_NONEXISTENT, sw.getVersion()); |
| 300 | assertEquals(KVSwitch.STATUS.ACTIVE, sw.getStatus()); |
| 301 | assertThat(sw.getDpid(), is(anyOf(equalTo(DPID1), equalTo(DPID2)))); |
| 302 | assertThat(switchesExpected, hasItem(sw.getDpid())); |
| 303 | switchesExpected.remove(sw.getDpid()); |
| 304 | } |
| 305 | System.out.println("Enumerating Switches end"); |
| 306 | |
| 307 | KVSwitch sw1 = new KVSwitch(DPID1); |
| 308 | try { |
| 309 | sw1.read(); |
| 310 | assertNotEquals(VERSION_NONEXISTENT, sw1.getVersion()); |
| 311 | assertEquals(DPID1, sw1.getDpid()); |
| 312 | assertEquals(KVSwitch.STATUS.ACTIVE, sw1.getStatus()); |
| 313 | } catch (ObjectDoesntExistException e) { |
| 314 | e.printStackTrace(); |
| 315 | fail("Reading switch failed " + e); |
| 316 | } |
| 317 | |
| 318 | KVSwitch sw2 = new KVSwitch(DPID2); |
| 319 | if (KVObject.multiRead( Arrays.asList(sw2) )) { |
| 320 | fail("Failed to read switch " + sw2); |
| 321 | } else { |
| 322 | assertNotEquals(VERSION_NONEXISTENT, sw2.getVersion()); |
| 323 | assertEquals(DPID2, sw2.getDpid()); |
| 324 | assertEquals(KVSwitch.STATUS.ACTIVE, sw2.getStatus()); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | // DPID -> [port_no] |
| 329 | @SuppressWarnings("serial") |
| 330 | Map<Long,List<Long>> expectedPorts = new HashMap<Long,List<Long>>() {{ |
| 331 | put(DPID1, new ArrayList<>(Arrays.asList(SW1_PORTNO1, SW1_PORTNO2))); |
| 332 | put(DPID2, new ArrayList<>(Arrays.asList(SW2_PORTNO1, SW2_PORTNO2))); |
| 333 | }}; |
| 334 | |
| 335 | for (KVPort port : KVPort.getAllPorts()) { |
| 336 | System.out.println(port + " @ " + port.getVersion()); |
| 337 | assertNotEquals(VERSION_NONEXISTENT, port.getVersion()); |
| 338 | assertEquals(KVPort.STATUS.ACTIVE, port.getStatus()); |
| 339 | assertThat(port.getDpid(), is(anyOf(equalTo(DPID1), equalTo(DPID2)))); |
| 340 | assertThat(port.getNumber(), is(anyOf(equalTo(SW1_PORTNO1), equalTo(SW1_PORTNO2)))); |
| 341 | |
| 342 | assertThat(expectedPorts, hasKey(port.getDpid())); |
| 343 | assertThat(expectedPorts.get(port.getDpid()), hasItem(port.getNumber())); |
| 344 | expectedPorts.get(port.getDpid()).remove(port.getNumber()); |
| 345 | } |
| 346 | |
| 347 | // DeviceID -> PortID |
| 348 | @SuppressWarnings("serial") |
| 349 | Map<byte[], byte[]> expectedDevice = new TreeMap<byte[], byte[]>(ByteArrayComparator.BYTEARRAY_COMPARATOR) {{ |
| 350 | put(DEVICE1_MAC_SW1P1, KVPort.getPortID(DPID1, SW1_PORTNO1)); |
| 351 | put(DEVICE2_MAC_SW2P2, KVPort.getPortID(DPID2, SW2_PORTNO2)); |
| 352 | }}; |
| 353 | |
| 354 | for (KVDevice device : KVDevice.getAllDevices()) { |
| 355 | System.out.println(device + " @ " + device.getVersion()); |
| 356 | assertNotEquals(VERSION_NONEXISTENT, device.getVersion()); |
| 357 | |
| 358 | assertThat(expectedDevice, hasKey(device.getMac())); |
| 359 | assertThat(device.getAllPortIds(), hasItem(expectedDevice.get(device.getMac()))); |
| 360 | expectedDevice.remove(device.getMac()); |
| 361 | } |
| 362 | |
| 363 | for (KVLink link : KVLink.getAllLinks()) { |
| 364 | System.out.println(link + " @ " + link.getVersion()); |
| 365 | assertNotEquals(VERSION_NONEXISTENT, link.getVersion()); |
| 366 | |
| 367 | // there is currently only 1 link SW1P2->SW2P1 |
| 368 | assertEquals(DPID1, link.getSrc().dpid); |
| 369 | assertEquals(SW1_PORTNO2, link.getSrc().number); |
| 370 | assertEquals(DPID2, link.getDst().dpid); |
| 371 | assertEquals(SW2_PORTNO1, link.getDst().number); |
| 372 | } |
| 373 | |
| 374 | } |
| 375 | |
| 376 | |
| 377 | private static void topology_delete() { |
| 378 | |
| 379 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 380 | try { |
| 381 | sw.read(); |
| 382 | sw.delete(); |
| 383 | assertNotEquals(VERSION_NONEXISTENT, sw.getVersion()); |
| 384 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 385 | e.printStackTrace(); |
| 386 | fail("Delete Switch Failed " + e); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | for (KVPort p : KVPort.getAllPorts()) { |
| 391 | try { |
| 392 | p.read(); |
| 393 | p.delete(); |
| 394 | assertNotEquals(VERSION_NONEXISTENT, p.getVersion()); |
| 395 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 396 | e.printStackTrace(); |
| 397 | fail("Delete Port Failed " + e); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | for (KVDevice d : KVDevice.getAllDevices()) { |
| 402 | d.forceDelete(); |
| 403 | assertNotEquals(VERSION_NONEXISTENT, d.getVersion()); |
| 404 | } |
| 405 | |
| 406 | for (KVLink l : KVLink.getAllLinks()) { |
| 407 | try { |
| 408 | l.read(); |
| 409 | l.delete(); |
| 410 | assertNotEquals(VERSION_NONEXISTENT, l.getVersion()); |
| 411 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 412 | e.printStackTrace(); |
| 413 | fail("Delete Link Failed " + e); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | public static void main(final String[] argv) { |
| 419 | |
| 420 | topology_setup(); |
| 421 | topology_walk(); |
| 422 | topology_delete(); |
| 423 | |
| 424 | System.exit(0); |
| 425 | } |
| 426 | |
| 427 | } |