blob: ff3799599d7eee766f6082c439478ed79bf116fb [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.topology;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -07002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.hamcrest.Matchers.anyOf;
4import static org.hamcrest.Matchers.equalTo;
5import static org.hamcrest.Matchers.hasItem;
6import static org.hamcrest.Matchers.hasKey;
7import static org.hamcrest.Matchers.is;
8import static org.junit.Assert.assertArrayEquals;
9import static org.junit.Assert.assertEquals;
10import static org.junit.Assert.assertNotEquals;
11import static org.junit.Assert.assertThat;
12import static org.junit.Assert.fail;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070013
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.HashMap;
17import java.util.List;
18import java.util.Map;
19import java.util.TreeMap;
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070020import java.util.UUID;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070021
Jonathan Hart6df90172014-04-03 10:13:11 -070022import net.onrc.onos.core.datastore.DataStoreClient;
23import net.onrc.onos.core.datastore.IKVClient;
24import net.onrc.onos.core.datastore.IKVTable;
25import net.onrc.onos.core.datastore.ObjectDoesntExistException;
26import net.onrc.onos.core.datastore.ObjectExistsException;
27import net.onrc.onos.core.datastore.WrongVersionException;
Jonathan Hart6df90172014-04-03 10:13:11 -070028import net.onrc.onos.core.datastore.utils.ByteArrayComparator;
29import net.onrc.onos.core.datastore.utils.KVObject;
30import net.onrc.onos.core.datastore.utils.KVObject.WriteOp;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070031
32import org.junit.After;
33import org.junit.Before;
34import org.junit.Test;
35
36public class KVTopologyTest {
37
Yuta HIGUCHIceb21b62014-04-17 15:46:05 -070038 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 Milkey7531a342014-04-11 15:08:12 -070043 static final long VERSION_NONEXISTENT = DataStoreClient.getClient().getVersionNonexistant();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 private static final byte[] DEVICE2_MAC_SW2P2 = new byte[]{6, 5, 4, 3, 2, 1, 0};
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070046
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 Milkey269ffb92014-04-03 14:43:30 -070053 private static final byte[] DEVICE1_MAC_SW1P1 = new byte[]{0, 1, 2, 3, 4, 5, 6};
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070054
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 HIGUCHIcc561882014-05-22 10:31:32 -070061 private static final String namespace = UUID.randomUUID().toString();
62
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070063 @Before
64 @After
65 public void wipeTopology() throws Exception {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070066 IKVTable switchTable = DataStoreClient.getClient().getTable(namespace + KVSwitch.SWITCH_TABLE_SUFFIX);
Ray Milkey269ffb92014-04-03 14:43:30 -070067 DataStoreClient.getClient().dropTable(switchTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070068
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070069 IKVTable portTable = DataStoreClient.getClient().getTable(namespace + KVPort.PORT_TABLE_SUFFIX);
Ray Milkey269ffb92014-04-03 14:43:30 -070070 DataStoreClient.getClient().dropTable(portTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070071
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070072 IKVTable linkTable = DataStoreClient.getClient().getTable(namespace + KVLink.LINK_TABLE_SUFFIX);
Ray Milkey269ffb92014-04-03 14:43:30 -070073 DataStoreClient.getClient().dropTable(linkTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070074
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070075 IKVTable deviceTable = DataStoreClient.getClient().getTable(namespace + KVDevice.DEVICE_TABLE_SUFFIX);
Ray Milkey269ffb92014-04-03 14:43:30 -070076 DataStoreClient.getClient().dropTable(deviceTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070077 }
78
79 @Test
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070080 public void basicSwitchTest() {
Ray Milkey269ffb92014-04-03 14:43:30 -070081 // create switch 0x1
82 try {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070083 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -070084 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 HIGUCHI6a643132014-03-18 22:39:27 -070093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 // read switch 0x1
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070095 KVSwitch swRead = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -070096 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 HIGUCHI6a643132014-03-18 22:39:27 -0700105
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 // 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 HIGUCHI6a643132014-03-18 22:39:27 -0700117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 // read 0x1 again and delete
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700119 KVSwitch swRead2 = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 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 HIGUCHI6a643132014-03-18 22:39:27 -0700129
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 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 HIGUCHI6a643132014-03-18 22:39:27 -0700137
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 // make sure 0x1 is deleted
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700139 KVObject swRead3 = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 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 HIGUCHI6a643132014-03-18 22:39:27 -0700148 }
149
150 @Test
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700151 public void topologySetupAndTeardown() {
152 topologySetup();
153 topologyWalk();
154 topologyDelete();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700155 }
156
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700157 private static void topologySetup() {
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700158
Ray Milkey269ffb92014-04-03 14:43:30 -0700159 // d1 - s1p1 - s1 - s1p2 - s2p1 - s2 - s2p2
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700160
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700161 KVSwitch sw1 = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 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 HIGUCHI6a643132014-03-18 22:39:27 -0700172
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700173 KVPort sw1p1 = new KVPort(DPID1, SW1_PORTNO1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 sw1p1.setStatus(KVPort.STATUS.ACTIVE);
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700175 KVPort sw1p2 = new KVPort(DPID1, SW1_PORTNO2, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 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 HIGUCHI6a643132014-03-18 22:39:27 -0700183
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 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 HIGUCHI6a643132014-03-18 22:39:27 -0700193
Ray Milkey269ffb92014-04-03 14:43:30 -0700194 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 HIGUCHI6a643132014-03-18 22:39:27 -0700203
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700204 KVDevice d1 = new KVDevice(DEVICE1_MAC_SW1P1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700205 d1.addPortId(sw1p1.getId());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700206
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 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 HIGUCHI6a643132014-03-18 22:39:27 -0700212
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 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 HIGUCHI6a643132014-03-18 22:39:27 -0700227
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700228 KVSwitch sw2 = new KVSwitch(DPID2, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 sw2.setStatus(KVSwitch.STATUS.ACTIVE);
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700230 KVPort sw2p1 = new KVPort(DPID2, SW2_PORTNO1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700231 sw2p1.setStatus(KVPort.STATUS.ACTIVE);
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700232 KVPort sw2p2 = new KVPort(DPID2, SW2_PORTNO2, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700233 sw2p2.setStatus(KVPort.STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700234
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700235 KVDevice d2 = new KVDevice(DEVICE2_MAC_SW2P2, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700236 d2.addPortId(sw2p2.getId());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700237
Ray Milkey269ffb92014-04-03 14:43:30 -0700238 IKVClient client = DataStoreClient.getClient();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700239
Ray Milkey269ffb92014-04-03 14:43:30 -0700240 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 HIGUCHI6a643132014-03-18 22:39:27 -0700253
Ray Milkey269ffb92014-04-03 14:43:30 -0700254 assertNotEquals(VERSION_NONEXISTENT, sw2p1.getVersion());
255 assertEquals(DPID2, sw2p1.getDpid());
256 assertEquals(SW2_PORTNO1, sw2p1.getNumber());
257 assertEquals(KVPort.STATUS.ACTIVE, sw2p1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700258
Ray Milkey269ffb92014-04-03 14:43:30 -0700259 assertNotEquals(VERSION_NONEXISTENT, sw2p2.getVersion());
260 assertEquals(DPID2, sw2p2.getDpid());
261 assertEquals(SW2_PORTNO2, sw2p2.getNumber());
262 assertEquals(KVPort.STATUS.ACTIVE, sw2p2.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700263
Ray Milkey269ffb92014-04-03 14:43:30 -0700264 assertNotEquals(VERSION_NONEXISTENT, d2.getVersion());
265 assertEquals(1, d2.getAllPortIds().size());
266 assertArrayEquals(sw2p2.getId(), d2.getAllPortIds().iterator().next());
267 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700268
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700269 KVLink l1 = new KVLink(DPID1, SW1_PORTNO2, DPID2, SW2_PORTNO1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700270 l1.setStatus(KVLink.STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700271
Ray Milkey269ffb92014-04-03 14:43:30 -0700272 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 HIGUCHI6a643132014-03-18 22:39:27 -0700280
Ray Milkey269ffb92014-04-03 14:43:30 -0700281 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 HIGUCHI6a643132014-03-18 22:39:27 -0700287
Ray Milkey269ffb92014-04-03 14:43:30 -0700288 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 HIGUCHI6a643132014-03-18 22:39:27 -0700301 }
302
303
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700304 private static void topologyWalk() {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700305 Iterable<KVSwitch> swIt = KVSwitch.getAllSwitches(namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700306 List<Long> switchesExpected = new ArrayList<>(Arrays.asList(DPID1, DPID2));
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700307
Ray Milkey269ffb92014-04-03 14:43:30 -0700308 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 HIGUCHI6a643132014-03-18 22:39:27 -0700318
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700319 KVSwitch sw1 = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700320 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 HIGUCHI6a643132014-03-18 22:39:27 -0700329
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700330 KVSwitch sw2 = new KVSwitch(DPID2, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700331 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 HIGUCHI6a643132014-03-18 22:39:27 -0700338
339
Ray Milkey269ffb92014-04-03 14:43:30 -0700340 // DPID -> [port_no]
341 @SuppressWarnings("serial")
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700342 Map<Long, List<Long>> expectedPorts = new HashMap<Long, List<Long>>() { {
Ray Milkey269ffb92014-04-03 14:43:30 -0700343 put(DPID1, new ArrayList<>(Arrays.asList(SW1_PORTNO1, SW1_PORTNO2)));
344 put(DPID2, new ArrayList<>(Arrays.asList(SW2_PORTNO1, SW2_PORTNO2)));
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700345 } };
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700346
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700347 for (KVPort port : KVPort.getAllPorts(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700348 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 HIGUCHI6a643132014-03-18 22:39:27 -0700353
Ray Milkey269ffb92014-04-03 14:43:30 -0700354 assertThat(expectedPorts, hasKey(port.getDpid()));
355 assertThat(expectedPorts.get(port.getDpid()), hasItem(port.getNumber()));
356 expectedPorts.get(port.getDpid()).remove(port.getNumber());
357 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700358
Ray Milkey269ffb92014-04-03 14:43:30 -0700359 // DeviceID -> PortID
360 @SuppressWarnings("serial")
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700361 Map<byte[], byte[]> expectedDevice = new TreeMap<byte[], byte[]>(ByteArrayComparator.BYTEARRAY_COMPARATOR) { {
Ray Milkey269ffb92014-04-03 14:43:30 -0700362 put(DEVICE1_MAC_SW1P1, KVPort.getPortID(DPID1, SW1_PORTNO1));
363 put(DEVICE2_MAC_SW2P2, KVPort.getPortID(DPID2, SW2_PORTNO2));
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700364 } };
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700365
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700366 for (KVDevice device : KVDevice.getAllDevices(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700367 System.out.println(device + " @ " + device.getVersion());
368 assertNotEquals(VERSION_NONEXISTENT, device.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700369
Ray Milkey269ffb92014-04-03 14:43:30 -0700370 assertThat(expectedDevice, hasKey(device.getMac()));
371 assertThat(device.getAllPortIds(), hasItem(expectedDevice.get(device.getMac())));
372 expectedDevice.remove(device.getMac());
373 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700374
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700375 for (KVLink link : KVLink.getAllLinks(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700376 System.out.println(link + " @ " + link.getVersion());
377 assertNotEquals(VERSION_NONEXISTENT, link.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700378
Ray Milkey269ffb92014-04-03 14:43:30 -0700379 // 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 HIGUCHI6a643132014-03-18 22:39:27 -0700385
386 }
387
388
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700389 private static void topologyDelete() {
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700390
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700391 for (KVSwitch sw : KVSwitch.getAllSwitches(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700392 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 HIGUCHI6a643132014-03-18 22:39:27 -0700401
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700402 for (KVPort p : KVPort.getAllPorts(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700403 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 HIGUCHI6a643132014-03-18 22:39:27 -0700412
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700413 for (KVDevice d : KVDevice.getAllDevices(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700414 d.forceDelete();
415 assertNotEquals(VERSION_NONEXISTENT, d.getVersion());
416 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700417
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700418 for (KVLink l : KVLink.getAllLinks(namespace)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700419 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 HIGUCHI6a643132014-03-18 22:39:27 -0700428 }
429
430 public static void main(final String[] argv) {
431
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700432 topologySetup();
433 topologyWalk();
434 topologyDelete();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700435
Ray Milkey269ffb92014-04-03 14:43:30 -0700436 System.exit(0);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700437 }
438
439}