blob: b71faeffe628e3cb8307077c22db80878d5a64cc [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Thomas Vachuskac97aa612015-06-23 16:00:18 -070017package org.onosproject.store.trivial;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070018
19import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import static org.onosproject.net.Device.Type.SWITCH;
21import static org.onosproject.net.DeviceId.deviceId;
22import static org.onosproject.net.device.DeviceEvent.Type.*;
23import static org.onosproject.net.NetTestTools.assertAnnotationsEquals;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070024
25import java.util.Arrays;
26import java.util.HashMap;
27import java.util.List;
28import java.util.Map;
29import java.util.Set;
30import java.util.concurrent.CountDownLatch;
31import java.util.concurrent.TimeUnit;
32
33import org.junit.After;
34import org.junit.AfterClass;
35import org.junit.Before;
36import org.junit.BeforeClass;
37import org.junit.Ignore;
38import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.DefaultAnnotations;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.Port;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.SparseAnnotations;
45import org.onosproject.net.device.DefaultDeviceDescription;
46import org.onosproject.net.device.DefaultPortDescription;
47import org.onosproject.net.device.DeviceDescription;
48import org.onosproject.net.device.DeviceEvent;
49import org.onosproject.net.device.DeviceStore;
50import org.onosproject.net.device.DeviceStoreDelegate;
51import org.onosproject.net.device.PortDescription;
52import org.onosproject.net.provider.ProviderId;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070053
54import com.google.common.collect.Iterables;
55import com.google.common.collect.Sets;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080056
alshabib7911a052014-10-16 17:49:37 -070057import org.onlab.packet.ChassisId;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070058
59/**
60 * Test of the simple DeviceStore implementation.
61 */
62public class SimpleDeviceStoreTest {
63
64 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070065 private static final ProviderId PIDA = new ProviderId("of", "bar", true);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070066 private static final DeviceId DID1 = deviceId("of:foo");
67 private static final DeviceId DID2 = deviceId("of:bar");
68 private static final String MFR = "whitebox";
69 private static final String HW = "1.1.x";
70 private static final String SW1 = "3.8.1";
71 private static final String SW2 = "3.9.5";
72 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070073 private static final ChassisId CID = new ChassisId();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070074
75 private static final PortNumber P1 = PortNumber.portNumber(1);
76 private static final PortNumber P2 = PortNumber.portNumber(2);
77 private static final PortNumber P3 = PortNumber.portNumber(3);
78
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070079 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
80 .set("A1", "a1")
81 .set("B1", "b1")
82 .build();
83 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
84 .remove("A1")
85 .set("B3", "b3")
86 .build();
87 private static final SparseAnnotations A2 = DefaultAnnotations.builder()
88 .set("A2", "a2")
89 .set("B2", "b2")
90 .build();
91 private static final SparseAnnotations A2_2 = DefaultAnnotations.builder()
92 .remove("A2")
93 .set("B4", "b4")
94 .build();
95
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070096 private SimpleDeviceStore simpleDeviceStore;
97 private DeviceStore deviceStore;
98
99
100
101 @BeforeClass
102 public static void setUpBeforeClass() throws Exception {
103 }
104
105 @AfterClass
106 public static void tearDownAfterClass() throws Exception {
107 }
108
109
110 @Before
111 public void setUp() throws Exception {
112 simpleDeviceStore = new SimpleDeviceStore();
113 simpleDeviceStore.activate();
114 deviceStore = simpleDeviceStore;
115 }
116
117 @After
118 public void tearDown() throws Exception {
119 simpleDeviceStore.deactivate();
120 }
121
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700122 private void putDevice(DeviceId deviceId, String swVersion,
123 SparseAnnotations... annotations) {
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700124 DeviceDescription description =
125 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700126 HW, swVersion, SN, CID, annotations);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700127 deviceStore.createOrUpdateDevice(PID, deviceId, description);
128 }
129
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700130 private void putDeviceAncillary(DeviceId deviceId, String swVersion,
131 SparseAnnotations... annotations) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700132 DeviceDescription description =
133 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700134 HW, swVersion, SN, CID, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700135 deviceStore.createOrUpdateDevice(PIDA, deviceId, description);
136 }
137
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700138 private static void assertDevice(DeviceId id, String swVersion, Device device) {
139 assertNotNull(device);
140 assertEquals(id, device.id());
141 assertEquals(MFR, device.manufacturer());
142 assertEquals(HW, device.hwVersion());
143 assertEquals(swVersion, device.swVersion());
144 assertEquals(SN, device.serialNumber());
145 }
146
147 @Test
148 public final void testGetDeviceCount() {
149 assertEquals("initialy empty", 0, deviceStore.getDeviceCount());
150
151 putDevice(DID1, SW1);
152 putDevice(DID2, SW2);
153 putDevice(DID1, SW1);
154
155 assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount());
156 }
157
158 @Test
159 public final void testGetDevices() {
160 assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices()));
161
162 putDevice(DID1, SW1);
163 putDevice(DID2, SW2);
164 putDevice(DID1, SW1);
165
166 assertEquals("expect 2 uniq devices",
167 2, Iterables.size(deviceStore.getDevices()));
168
169 Map<DeviceId, Device> devices = new HashMap<>();
170 for (Device device : deviceStore.getDevices()) {
171 devices.put(device.id(), device);
172 }
173
174 assertDevice(DID1, SW1, devices.get(DID1));
175 assertDevice(DID2, SW2, devices.get(DID2));
176
177 // add case for new node?
178 }
179
180 @Test
181 public final void testGetDevice() {
182
183 putDevice(DID1, SW1);
184
185 assertDevice(DID1, SW1, deviceStore.getDevice(DID1));
186 assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2));
187 }
188
189 @Test
190 public final void testCreateOrUpdateDevice() {
191 DeviceDescription description =
192 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700193 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700194 DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
195 assertEquals(DEVICE_ADDED, event.type());
196 assertDevice(DID1, SW1, event.subject());
197
198 DeviceDescription description2 =
199 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700200 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700201 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
202 assertEquals(DEVICE_UPDATED, event2.type());
203 assertDevice(DID1, SW2, event2.subject());
204
205 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
206 }
207
208 @Test
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700209 public final void testCreateOrUpdateDeviceAncillary() {
210 DeviceDescription description =
211 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700212 HW, SW1, SN, CID, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700213 DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description);
214 assertEquals(DEVICE_ADDED, event.type());
215 assertDevice(DID1, SW1, event.subject());
216 assertEquals(PIDA, event.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700217 assertAnnotationsEquals(event.subject().annotations(), A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700218 assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1));
219
220 DeviceDescription description2 =
221 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700222 HW, SW2, SN, CID, A1);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700223 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
224 assertEquals(DEVICE_UPDATED, event2.type());
225 assertDevice(DID1, SW2, event2.subject());
226 assertEquals(PID, event2.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700227 assertAnnotationsEquals(event2.subject().annotations(), A1, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700228 assertTrue(deviceStore.isAvailable(DID1));
229
230 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
231
232 // For now, Ancillary is ignored once primary appears
233 assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description));
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700234
235 // But, Ancillary annotations will be in effect
236 DeviceDescription description3 =
237 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700238 HW, SW1, SN, CID, A2_2);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700239 DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3);
240 assertEquals(DEVICE_UPDATED, event3.type());
241 // basic information will be the one from Primary
242 assertDevice(DID1, SW2, event3.subject());
243 assertEquals(PID, event3.subject().providerId());
244 // but annotation from Ancillary will be merged
245 assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2);
246 assertTrue(deviceStore.isAvailable(DID1));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700247 }
248
249
250 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700251 public final void testMarkOffline() {
252
253 putDevice(DID1, SW1);
254 assertTrue(deviceStore.isAvailable(DID1));
255
256 DeviceEvent event = deviceStore.markOffline(DID1);
257 assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type());
258 assertDevice(DID1, SW1, event.subject());
259 assertFalse(deviceStore.isAvailable(DID1));
260
261 DeviceEvent event2 = deviceStore.markOffline(DID1);
262 assertNull("No change, no event", event2);
263}
264
265 @Test
266 public final void testUpdatePorts() {
267 putDevice(DID1, SW1);
268 List<PortDescription> pds = Arrays.<PortDescription>asList(
269 new DefaultPortDescription(P1, true),
270 new DefaultPortDescription(P2, true)
271 );
272
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700273 List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700274
275 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
276 for (DeviceEvent event : events) {
277 assertEquals(PORT_ADDED, event.type());
278 assertDevice(DID1, SW1, event.subject());
279 assertTrue("PortNumber is one of expected",
280 expectedPorts.remove(event.port().number()));
281 assertTrue("Port is enabled", event.port().isEnabled());
282 }
283 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
284
285
286 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
287 new DefaultPortDescription(P1, false),
288 new DefaultPortDescription(P2, true),
289 new DefaultPortDescription(P3, true)
290 );
291
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700292 events = deviceStore.updatePorts(PID, DID1, pds2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700293 assertFalse("event should be triggered", events.isEmpty());
294 for (DeviceEvent event : events) {
295 PortNumber num = event.port().number();
296 if (P1.equals(num)) {
297 assertEquals(PORT_UPDATED, event.type());
298 assertDevice(DID1, SW1, event.subject());
299 assertFalse("Port is disabled", event.port().isEnabled());
300 } else if (P2.equals(num)) {
301 fail("P2 event not expected.");
302 } else if (P3.equals(num)) {
303 assertEquals(PORT_ADDED, event.type());
304 assertDevice(DID1, SW1, event.subject());
305 assertTrue("Port is enabled", event.port().isEnabled());
306 } else {
307 fail("Unknown port number encountered: " + num);
308 }
309 }
310
311 List<PortDescription> pds3 = Arrays.<PortDescription>asList(
312 new DefaultPortDescription(P1, false),
313 new DefaultPortDescription(P2, true)
314 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700315 events = deviceStore.updatePorts(PID, DID1, pds3);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700316 assertFalse("event should be triggered", events.isEmpty());
317 for (DeviceEvent event : events) {
318 PortNumber num = event.port().number();
319 if (P1.equals(num)) {
320 fail("P1 event not expected.");
321 } else if (P2.equals(num)) {
322 fail("P2 event not expected.");
323 } else if (P3.equals(num)) {
324 assertEquals(PORT_REMOVED, event.type());
325 assertDevice(DID1, SW1, event.subject());
326 assertTrue("Port was enabled", event.port().isEnabled());
327 } else {
328 fail("Unknown port number encountered: " + num);
329 }
330 }
331
332 }
333
334 @Test
335 public final void testUpdatePortStatus() {
336 putDevice(DID1, SW1);
337 List<PortDescription> pds = Arrays.<PortDescription>asList(
338 new DefaultPortDescription(P1, true)
339 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700340 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700341
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700342 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700343 new DefaultPortDescription(P1, false));
344 assertEquals(PORT_UPDATED, event.type());
345 assertDevice(DID1, SW1, event.subject());
346 assertEquals(P1, event.port().number());
347 assertFalse("Port is disabled", event.port().isEnabled());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700348
349 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700350
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700351 @Test
352 public final void testUpdatePortStatusAncillary() {
353 putDeviceAncillary(DID1, SW1);
354 putDevice(DID1, SW1);
355 List<PortDescription> pds = Arrays.<PortDescription>asList(
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700356 new DefaultPortDescription(P1, true, A1)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700357 );
358 deviceStore.updatePorts(PID, DID1, pds);
359
360 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700361 new DefaultPortDescription(P1, false, A1_2));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700362 assertEquals(PORT_UPDATED, event.type());
363 assertDevice(DID1, SW1, event.subject());
364 assertEquals(P1, event.port().number());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700365 assertAnnotationsEquals(event.port().annotations(), A1, A1_2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700366 assertFalse("Port is disabled", event.port().isEnabled());
367
368 DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1,
369 new DefaultPortDescription(P1, true));
370 assertNull("Ancillary is ignored if primary exists", event2);
371
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700372 // but, Ancillary annotation update will be notified
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700373 DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1,
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700374 new DefaultPortDescription(P1, true, A2));
375 assertEquals(PORT_UPDATED, event3.type());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700376 assertDevice(DID1, SW1, event3.subject());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700377 assertEquals(P1, event3.port().number());
378 assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2);
379 assertFalse("Port is disabled", event3.port().isEnabled());
380
381 // port only reported from Ancillary will be notified as down
382 DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1,
383 new DefaultPortDescription(P2, true));
384 assertEquals(PORT_ADDED, event4.type());
385 assertDevice(DID1, SW1, event4.subject());
386 assertEquals(P2, event4.port().number());
387 assertAnnotationsEquals(event4.port().annotations());
388 assertFalse("Port is disabled if not given from primary provider",
389 event4.port().isEnabled());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700390 }
391
392 @Test
393 public final void testGetPorts() {
394 putDevice(DID1, SW1);
395 putDevice(DID2, SW1);
396 List<PortDescription> pds = Arrays.<PortDescription>asList(
397 new DefaultPortDescription(P1, true),
398 new DefaultPortDescription(P2, true)
399 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700400 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700401
402 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
403 List<Port> ports = deviceStore.getPorts(DID1);
404 for (Port port : ports) {
405 assertTrue("Port is enabled", port.isEnabled());
406 assertTrue("PortNumber is one of expected",
407 expectedPorts.remove(port.number()));
408 }
409 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
410
411
412 assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
413 }
414
415 @Test
416 public final void testGetPort() {
417 putDevice(DID1, SW1);
418 putDevice(DID2, SW1);
419 List<PortDescription> pds = Arrays.<PortDescription>asList(
420 new DefaultPortDescription(P1, true),
421 new DefaultPortDescription(P2, false)
422 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700423 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700424
425 Port port1 = deviceStore.getPort(DID1, P1);
426 assertEquals(P1, port1.number());
427 assertTrue("Port is enabled", port1.isEnabled());
428
429 Port port2 = deviceStore.getPort(DID1, P2);
430 assertEquals(P2, port2.number());
431 assertFalse("Port is disabled", port2.isEnabled());
432
433 Port port3 = deviceStore.getPort(DID1, P3);
434 assertNull("P3 not expected", port3);
435 }
436
437 @Test
438 public final void testRemoveDevice() {
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700439 putDevice(DID1, SW1, A1);
440 List<PortDescription> pds = Arrays.<PortDescription>asList(
441 new DefaultPortDescription(P1, true, A2)
442 );
443 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700444 putDevice(DID2, SW1);
445
446 assertEquals(2, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700447 assertEquals(1, deviceStore.getPorts(DID1).size());
448 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1);
449 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700450
451 DeviceEvent event = deviceStore.removeDevice(DID1);
452 assertEquals(DEVICE_REMOVED, event.type());
453 assertDevice(DID1, SW1, event.subject());
454
455 assertEquals(1, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700456 assertEquals(0, deviceStore.getPorts(DID1).size());
457
458 // putBack Device, Port w/o annotation
459 putDevice(DID1, SW1);
460 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
461 new DefaultPortDescription(P1, true)
462 );
463 deviceStore.updatePorts(PID, DID1, pds2);
464
465 // annotations should not survive
466 assertEquals(2, deviceStore.getDeviceCount());
467 assertEquals(1, deviceStore.getPorts(DID1).size());
468 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations());
469 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700470 }
471
472 // If Delegates should be called only on remote events,
473 // then Simple* should never call them, thus not test required.
474 // TODO add test for Port events when we have them
475 @Ignore("Ignore until Delegate spec. is clear.")
476 @Test
477 public final void testEvents() throws InterruptedException {
478 final CountDownLatch addLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700479 DeviceStoreDelegate checkAdd = event -> {
480 assertEquals(DEVICE_ADDED, event.type());
481 assertDevice(DID1, SW1, event.subject());
482 addLatch.countDown();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700483 };
484 final CountDownLatch updateLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700485 DeviceStoreDelegate checkUpdate = event -> {
486 assertEquals(DEVICE_UPDATED, event.type());
487 assertDevice(DID1, SW2, event.subject());
488 updateLatch.countDown();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700489 };
490 final CountDownLatch removeLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700491 DeviceStoreDelegate checkRemove = event -> {
492 assertEquals(DEVICE_REMOVED, event.type());
493 assertDevice(DID1, SW2, event.subject());
494 removeLatch.countDown();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700495 };
496
497 DeviceDescription description =
498 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700499 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700500 deviceStore.setDelegate(checkAdd);
501 deviceStore.createOrUpdateDevice(PID, DID1, description);
502 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
503
504
505 DeviceDescription description2 =
506 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700507 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700508 deviceStore.unsetDelegate(checkAdd);
509 deviceStore.setDelegate(checkUpdate);
510 deviceStore.createOrUpdateDevice(PID, DID1, description2);
511 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
512
513 deviceStore.unsetDelegate(checkUpdate);
514 deviceStore.setDelegate(checkRemove);
515 deviceStore.removeDevice(DID1);
516 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
517 }
518}