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