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