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