blob: 8338a776041e73e8edcee6f2075468564181ebbd [file] [log] [blame]
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -07001/**
2 *
3 */
tomea961ff2014-10-01 12:45:15 -07004package org.onlab.onos.store.trivial.impl;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -07005
6import static org.junit.Assert.*;
7import static org.onlab.onos.net.Device.Type.SWITCH;
8import static org.onlab.onos.net.DeviceId.deviceId;
9import static org.onlab.onos.net.device.DeviceEvent.Type.*;
10
11import java.util.Arrays;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
15import java.util.Set;
16import java.util.concurrent.CountDownLatch;
17import java.util.concurrent.TimeUnit;
18
19import org.junit.After;
20import org.junit.AfterClass;
21import org.junit.Before;
22import org.junit.BeforeClass;
23import org.junit.Ignore;
24import org.junit.Test;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070025import org.onlab.onos.net.Annotations;
26import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070027import org.onlab.onos.net.Device;
28import org.onlab.onos.net.DeviceId;
29import org.onlab.onos.net.Port;
30import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070031import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070032import org.onlab.onos.net.device.DefaultDeviceDescription;
33import org.onlab.onos.net.device.DefaultPortDescription;
34import org.onlab.onos.net.device.DeviceDescription;
35import org.onlab.onos.net.device.DeviceEvent;
36import org.onlab.onos.net.device.DeviceStore;
37import org.onlab.onos.net.device.DeviceStoreDelegate;
38import org.onlab.onos.net.device.PortDescription;
39import org.onlab.onos.net.provider.ProviderId;
40
41import com.google.common.collect.Iterables;
42import com.google.common.collect.Sets;
alshabib7911a052014-10-16 17:49:37 -070043import org.onlab.packet.ChassisId;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070044
45/**
46 * Test of the simple DeviceStore implementation.
47 */
48public class SimpleDeviceStoreTest {
49
50 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070051 private static final ProviderId PIDA = new ProviderId("of", "bar", true);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070052 private static final DeviceId DID1 = deviceId("of:foo");
53 private static final DeviceId DID2 = deviceId("of:bar");
54 private static final String MFR = "whitebox";
55 private static final String HW = "1.1.x";
56 private static final String SW1 = "3.8.1";
57 private static final String SW2 = "3.9.5";
58 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070059 private static final ChassisId CID = new ChassisId();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070060
61 private static final PortNumber P1 = PortNumber.portNumber(1);
62 private static final PortNumber P2 = PortNumber.portNumber(2);
63 private static final PortNumber P3 = PortNumber.portNumber(3);
64
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070065 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
66 .set("A1", "a1")
67 .set("B1", "b1")
68 .build();
69 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
70 .remove("A1")
71 .set("B3", "b3")
72 .build();
73 private static final SparseAnnotations A2 = DefaultAnnotations.builder()
74 .set("A2", "a2")
75 .set("B2", "b2")
76 .build();
77 private static final SparseAnnotations A2_2 = DefaultAnnotations.builder()
78 .remove("A2")
79 .set("B4", "b4")
80 .build();
81
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070082 private SimpleDeviceStore simpleDeviceStore;
83 private DeviceStore deviceStore;
84
85
86
87 @BeforeClass
88 public static void setUpBeforeClass() throws Exception {
89 }
90
91 @AfterClass
92 public static void tearDownAfterClass() throws Exception {
93 }
94
95
96 @Before
97 public void setUp() throws Exception {
98 simpleDeviceStore = new SimpleDeviceStore();
99 simpleDeviceStore.activate();
100 deviceStore = simpleDeviceStore;
101 }
102
103 @After
104 public void tearDown() throws Exception {
105 simpleDeviceStore.deactivate();
106 }
107
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700108 private void putDevice(DeviceId deviceId, String swVersion,
109 SparseAnnotations... annotations) {
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700110 DeviceDescription description =
111 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700112 HW, swVersion, SN, CID, annotations);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700113 deviceStore.createOrUpdateDevice(PID, deviceId, description);
114 }
115
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700116 private void putDeviceAncillary(DeviceId deviceId, String swVersion,
117 SparseAnnotations... annotations) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700118 DeviceDescription description =
119 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700120 HW, swVersion, SN, CID, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700121 deviceStore.createOrUpdateDevice(PIDA, deviceId, description);
122 }
123
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700124 private static void assertDevice(DeviceId id, String swVersion, Device device) {
125 assertNotNull(device);
126 assertEquals(id, device.id());
127 assertEquals(MFR, device.manufacturer());
128 assertEquals(HW, device.hwVersion());
129 assertEquals(swVersion, device.swVersion());
130 assertEquals(SN, device.serialNumber());
131 }
132
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700133 // TODO slice this out somewhere
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700134 /**
135 * Verifies that Annotations created by merging {@code annotations} is
136 * equal to actual Annotations.
137 *
138 * @param actual Annotations to check
139 * @param annotations
140 */
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700141 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700142 DefaultAnnotations expected = DefaultAnnotations.builder().build();
143 for (SparseAnnotations a : annotations) {
144 expected = DefaultAnnotations.merge(expected, a);
145 }
146 assertEquals(expected.keys(), actual.keys());
147 for (String key : expected.keys()) {
148 assertEquals(expected.value(key), actual.value(key));
149 }
150 }
151
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700152 @Test
153 public final void testGetDeviceCount() {
154 assertEquals("initialy empty", 0, deviceStore.getDeviceCount());
155
156 putDevice(DID1, SW1);
157 putDevice(DID2, SW2);
158 putDevice(DID1, SW1);
159
160 assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount());
161 }
162
163 @Test
164 public final void testGetDevices() {
165 assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices()));
166
167 putDevice(DID1, SW1);
168 putDevice(DID2, SW2);
169 putDevice(DID1, SW1);
170
171 assertEquals("expect 2 uniq devices",
172 2, Iterables.size(deviceStore.getDevices()));
173
174 Map<DeviceId, Device> devices = new HashMap<>();
175 for (Device device : deviceStore.getDevices()) {
176 devices.put(device.id(), device);
177 }
178
179 assertDevice(DID1, SW1, devices.get(DID1));
180 assertDevice(DID2, SW2, devices.get(DID2));
181
182 // add case for new node?
183 }
184
185 @Test
186 public final void testGetDevice() {
187
188 putDevice(DID1, SW1);
189
190 assertDevice(DID1, SW1, deviceStore.getDevice(DID1));
191 assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2));
192 }
193
194 @Test
195 public final void testCreateOrUpdateDevice() {
196 DeviceDescription description =
197 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700198 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700199 DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
200 assertEquals(DEVICE_ADDED, event.type());
201 assertDevice(DID1, SW1, event.subject());
202
203 DeviceDescription description2 =
204 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700205 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700206 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
207 assertEquals(DEVICE_UPDATED, event2.type());
208 assertDevice(DID1, SW2, event2.subject());
209
210 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
211 }
212
213 @Test
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700214 public final void testCreateOrUpdateDeviceAncillary() {
215 DeviceDescription description =
216 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700217 HW, SW1, SN, CID, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700218 DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description);
219 assertEquals(DEVICE_ADDED, event.type());
220 assertDevice(DID1, SW1, event.subject());
221 assertEquals(PIDA, event.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700222 assertAnnotationsEquals(event.subject().annotations(), A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700223 assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1));
224
225 DeviceDescription description2 =
226 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700227 HW, SW2, SN, CID, A1);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700228 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
229 assertEquals(DEVICE_UPDATED, event2.type());
230 assertDevice(DID1, SW2, event2.subject());
231 assertEquals(PID, event2.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700232 assertAnnotationsEquals(event2.subject().annotations(), A1, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700233 assertTrue(deviceStore.isAvailable(DID1));
234
235 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
236
237 // For now, Ancillary is ignored once primary appears
238 assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description));
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700239
240 // But, Ancillary annotations will be in effect
241 DeviceDescription description3 =
242 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700243 HW, SW1, SN, CID, A2_2);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700244 DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3);
245 assertEquals(DEVICE_UPDATED, event3.type());
246 // basic information will be the one from Primary
247 assertDevice(DID1, SW2, event3.subject());
248 assertEquals(PID, event3.subject().providerId());
249 // but annotation from Ancillary will be merged
250 assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2);
251 assertTrue(deviceStore.isAvailable(DID1));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700252 }
253
254
255 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700256 public final void testMarkOffline() {
257
258 putDevice(DID1, SW1);
259 assertTrue(deviceStore.isAvailable(DID1));
260
261 DeviceEvent event = deviceStore.markOffline(DID1);
262 assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type());
263 assertDevice(DID1, SW1, event.subject());
264 assertFalse(deviceStore.isAvailable(DID1));
265
266 DeviceEvent event2 = deviceStore.markOffline(DID1);
267 assertNull("No change, no event", event2);
268}
269
270 @Test
271 public final void testUpdatePorts() {
272 putDevice(DID1, SW1);
273 List<PortDescription> pds = Arrays.<PortDescription>asList(
274 new DefaultPortDescription(P1, true),
275 new DefaultPortDescription(P2, true)
276 );
277
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700278 List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700279
280 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
281 for (DeviceEvent event : events) {
282 assertEquals(PORT_ADDED, event.type());
283 assertDevice(DID1, SW1, event.subject());
284 assertTrue("PortNumber is one of expected",
285 expectedPorts.remove(event.port().number()));
286 assertTrue("Port is enabled", event.port().isEnabled());
287 }
288 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
289
290
291 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
292 new DefaultPortDescription(P1, false),
293 new DefaultPortDescription(P2, true),
294 new DefaultPortDescription(P3, true)
295 );
296
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700297 events = deviceStore.updatePorts(PID, DID1, pds2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700298 assertFalse("event should be triggered", events.isEmpty());
299 for (DeviceEvent event : events) {
300 PortNumber num = event.port().number();
301 if (P1.equals(num)) {
302 assertEquals(PORT_UPDATED, event.type());
303 assertDevice(DID1, SW1, event.subject());
304 assertFalse("Port is disabled", event.port().isEnabled());
305 } else if (P2.equals(num)) {
306 fail("P2 event not expected.");
307 } else if (P3.equals(num)) {
308 assertEquals(PORT_ADDED, event.type());
309 assertDevice(DID1, SW1, event.subject());
310 assertTrue("Port is enabled", event.port().isEnabled());
311 } else {
312 fail("Unknown port number encountered: " + num);
313 }
314 }
315
316 List<PortDescription> pds3 = Arrays.<PortDescription>asList(
317 new DefaultPortDescription(P1, false),
318 new DefaultPortDescription(P2, true)
319 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700320 events = deviceStore.updatePorts(PID, DID1, pds3);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700321 assertFalse("event should be triggered", events.isEmpty());
322 for (DeviceEvent event : events) {
323 PortNumber num = event.port().number();
324 if (P1.equals(num)) {
325 fail("P1 event not expected.");
326 } else if (P2.equals(num)) {
327 fail("P2 event not expected.");
328 } else if (P3.equals(num)) {
329 assertEquals(PORT_REMOVED, event.type());
330 assertDevice(DID1, SW1, event.subject());
331 assertTrue("Port was enabled", event.port().isEnabled());
332 } else {
333 fail("Unknown port number encountered: " + num);
334 }
335 }
336
337 }
338
339 @Test
340 public final void testUpdatePortStatus() {
341 putDevice(DID1, SW1);
342 List<PortDescription> pds = Arrays.<PortDescription>asList(
343 new DefaultPortDescription(P1, true)
344 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700345 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700346
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700347 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700348 new DefaultPortDescription(P1, false));
349 assertEquals(PORT_UPDATED, event.type());
350 assertDevice(DID1, SW1, event.subject());
351 assertEquals(P1, event.port().number());
352 assertFalse("Port is disabled", event.port().isEnabled());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700353
354 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700355
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700356 @Test
357 public final void testUpdatePortStatusAncillary() {
358 putDeviceAncillary(DID1, SW1);
359 putDevice(DID1, SW1);
360 List<PortDescription> pds = Arrays.<PortDescription>asList(
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700361 new DefaultPortDescription(P1, true, A1)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700362 );
363 deviceStore.updatePorts(PID, DID1, pds);
364
365 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700366 new DefaultPortDescription(P1, false, A1_2));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700367 assertEquals(PORT_UPDATED, event.type());
368 assertDevice(DID1, SW1, event.subject());
369 assertEquals(P1, event.port().number());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700370 assertAnnotationsEquals(event.port().annotations(), A1, A1_2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700371 assertFalse("Port is disabled", event.port().isEnabled());
372
373 DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1,
374 new DefaultPortDescription(P1, true));
375 assertNull("Ancillary is ignored if primary exists", event2);
376
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700377 // but, Ancillary annotation update will be notified
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700378 DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1,
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700379 new DefaultPortDescription(P1, true, A2));
380 assertEquals(PORT_UPDATED, event3.type());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700381 assertDevice(DID1, SW1, event3.subject());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700382 assertEquals(P1, event3.port().number());
383 assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2);
384 assertFalse("Port is disabled", event3.port().isEnabled());
385
386 // port only reported from Ancillary will be notified as down
387 DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1,
388 new DefaultPortDescription(P2, true));
389 assertEquals(PORT_ADDED, event4.type());
390 assertDevice(DID1, SW1, event4.subject());
391 assertEquals(P2, event4.port().number());
392 assertAnnotationsEquals(event4.port().annotations());
393 assertFalse("Port is disabled if not given from primary provider",
394 event4.port().isEnabled());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700395 }
396
397 @Test
398 public final void testGetPorts() {
399 putDevice(DID1, SW1);
400 putDevice(DID2, SW1);
401 List<PortDescription> pds = Arrays.<PortDescription>asList(
402 new DefaultPortDescription(P1, true),
403 new DefaultPortDescription(P2, true)
404 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700405 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700406
407 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
408 List<Port> ports = deviceStore.getPorts(DID1);
409 for (Port port : ports) {
410 assertTrue("Port is enabled", port.isEnabled());
411 assertTrue("PortNumber is one of expected",
412 expectedPorts.remove(port.number()));
413 }
414 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
415
416
417 assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
418 }
419
420 @Test
421 public final void testGetPort() {
422 putDevice(DID1, SW1);
423 putDevice(DID2, SW1);
424 List<PortDescription> pds = Arrays.<PortDescription>asList(
425 new DefaultPortDescription(P1, true),
426 new DefaultPortDescription(P2, false)
427 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700428 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700429
430 Port port1 = deviceStore.getPort(DID1, P1);
431 assertEquals(P1, port1.number());
432 assertTrue("Port is enabled", port1.isEnabled());
433
434 Port port2 = deviceStore.getPort(DID1, P2);
435 assertEquals(P2, port2.number());
436 assertFalse("Port is disabled", port2.isEnabled());
437
438 Port port3 = deviceStore.getPort(DID1, P3);
439 assertNull("P3 not expected", port3);
440 }
441
442 @Test
443 public final void testRemoveDevice() {
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700444 putDevice(DID1, SW1, A1);
445 List<PortDescription> pds = Arrays.<PortDescription>asList(
446 new DefaultPortDescription(P1, true, A2)
447 );
448 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700449 putDevice(DID2, SW1);
450
451 assertEquals(2, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700452 assertEquals(1, deviceStore.getPorts(DID1).size());
453 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1);
454 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700455
456 DeviceEvent event = deviceStore.removeDevice(DID1);
457 assertEquals(DEVICE_REMOVED, event.type());
458 assertDevice(DID1, SW1, event.subject());
459
460 assertEquals(1, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700461 assertEquals(0, deviceStore.getPorts(DID1).size());
462
463 // putBack Device, Port w/o annotation
464 putDevice(DID1, SW1);
465 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
466 new DefaultPortDescription(P1, true)
467 );
468 deviceStore.updatePorts(PID, DID1, pds2);
469
470 // annotations should not survive
471 assertEquals(2, deviceStore.getDeviceCount());
472 assertEquals(1, deviceStore.getPorts(DID1).size());
473 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations());
474 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700475 }
476
477 // If Delegates should be called only on remote events,
478 // then Simple* should never call them, thus not test required.
479 // TODO add test for Port events when we have them
480 @Ignore("Ignore until Delegate spec. is clear.")
481 @Test
482 public final void testEvents() throws InterruptedException {
483 final CountDownLatch addLatch = new CountDownLatch(1);
484 DeviceStoreDelegate checkAdd = new DeviceStoreDelegate() {
485 @Override
486 public void notify(DeviceEvent event) {
487 assertEquals(DEVICE_ADDED, event.type());
488 assertDevice(DID1, SW1, event.subject());
489 addLatch.countDown();
490 }
491 };
492 final CountDownLatch updateLatch = new CountDownLatch(1);
493 DeviceStoreDelegate checkUpdate = new DeviceStoreDelegate() {
494 @Override
495 public void notify(DeviceEvent event) {
496 assertEquals(DEVICE_UPDATED, event.type());
497 assertDevice(DID1, SW2, event.subject());
498 updateLatch.countDown();
499 }
500 };
501 final CountDownLatch removeLatch = new CountDownLatch(1);
502 DeviceStoreDelegate checkRemove = new DeviceStoreDelegate() {
503 @Override
504 public void notify(DeviceEvent event) {
505 assertEquals(DEVICE_REMOVED, event.type());
506 assertDevice(DID1, SW2, event.subject());
507 removeLatch.countDown();
508 }
509 };
510
511 DeviceDescription description =
512 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700513 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700514 deviceStore.setDelegate(checkAdd);
515 deviceStore.createOrUpdateDevice(PID, DID1, description);
516 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
517
518
519 DeviceDescription description2 =
520 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700521 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700522 deviceStore.unsetDelegate(checkAdd);
523 deviceStore.setDelegate(checkUpdate);
524 deviceStore.createOrUpdateDevice(PID, DID1, description2);
525 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
526
527 deviceStore.unsetDelegate(checkUpdate);
528 deviceStore.setDelegate(checkRemove);
529 deviceStore.removeDevice(DID1);
530 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
531 }
532}