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