blob: f43ae1013f9023007ef54c945fa99dd04ea6884c [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070017/**
18 *
19 */
tomea961ff2014-10-01 12:45:15 -070020package org.onlab.onos.store.trivial.impl;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070021
22import static org.junit.Assert.*;
23import static org.onlab.onos.net.Device.Type.SWITCH;
24import static org.onlab.onos.net.DeviceId.deviceId;
25import static org.onlab.onos.net.device.DeviceEvent.Type.*;
26
27import java.util.Arrays;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32import java.util.concurrent.CountDownLatch;
33import java.util.concurrent.TimeUnit;
34
35import org.junit.After;
36import org.junit.AfterClass;
37import org.junit.Before;
38import org.junit.BeforeClass;
39import org.junit.Ignore;
40import org.junit.Test;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070041import org.onlab.onos.net.Annotations;
42import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070043import org.onlab.onos.net.Device;
44import org.onlab.onos.net.DeviceId;
45import org.onlab.onos.net.Port;
46import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070047import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070048import org.onlab.onos.net.device.DefaultDeviceDescription;
49import org.onlab.onos.net.device.DefaultPortDescription;
50import org.onlab.onos.net.device.DeviceDescription;
51import org.onlab.onos.net.device.DeviceEvent;
52import org.onlab.onos.net.device.DeviceStore;
53import org.onlab.onos.net.device.DeviceStoreDelegate;
54import org.onlab.onos.net.device.PortDescription;
55import org.onlab.onos.net.provider.ProviderId;
56
57import com.google.common.collect.Iterables;
58import com.google.common.collect.Sets;
alshabib7911a052014-10-16 17:49:37 -070059import org.onlab.packet.ChassisId;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070060
61/**
62 * Test of the simple DeviceStore implementation.
63 */
64public class SimpleDeviceStoreTest {
65
66 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070067 private static final ProviderId PIDA = new ProviderId("of", "bar", true);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070068 private static final DeviceId DID1 = deviceId("of:foo");
69 private static final DeviceId DID2 = deviceId("of:bar");
70 private static final String MFR = "whitebox";
71 private static final String HW = "1.1.x";
72 private static final String SW1 = "3.8.1";
73 private static final String SW2 = "3.9.5";
74 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070075 private static final ChassisId CID = new ChassisId();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070076
77 private static final PortNumber P1 = PortNumber.portNumber(1);
78 private static final PortNumber P2 = PortNumber.portNumber(2);
79 private static final PortNumber P3 = PortNumber.portNumber(3);
80
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070081 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
82 .set("A1", "a1")
83 .set("B1", "b1")
84 .build();
85 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
86 .remove("A1")
87 .set("B3", "b3")
88 .build();
89 private static final SparseAnnotations A2 = DefaultAnnotations.builder()
90 .set("A2", "a2")
91 .set("B2", "b2")
92 .build();
93 private static final SparseAnnotations A2_2 = DefaultAnnotations.builder()
94 .remove("A2")
95 .set("B4", "b4")
96 .build();
97
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070098 private SimpleDeviceStore simpleDeviceStore;
99 private DeviceStore deviceStore;
100
101
102
103 @BeforeClass
104 public static void setUpBeforeClass() throws Exception {
105 }
106
107 @AfterClass
108 public static void tearDownAfterClass() throws Exception {
109 }
110
111
112 @Before
113 public void setUp() throws Exception {
114 simpleDeviceStore = new SimpleDeviceStore();
115 simpleDeviceStore.activate();
116 deviceStore = simpleDeviceStore;
117 }
118
119 @After
120 public void tearDown() throws Exception {
121 simpleDeviceStore.deactivate();
122 }
123
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700124 private void putDevice(DeviceId deviceId, String swVersion,
125 SparseAnnotations... annotations) {
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700126 DeviceDescription description =
127 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700128 HW, swVersion, SN, CID, annotations);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700129 deviceStore.createOrUpdateDevice(PID, deviceId, description);
130 }
131
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700132 private void putDeviceAncillary(DeviceId deviceId, String swVersion,
133 SparseAnnotations... annotations) {
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700134 DeviceDescription description =
135 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700136 HW, swVersion, SN, CID, annotations);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700137 deviceStore.createOrUpdateDevice(PIDA, deviceId, description);
138 }
139
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700140 private static void assertDevice(DeviceId id, String swVersion, Device device) {
141 assertNotNull(device);
142 assertEquals(id, device.id());
143 assertEquals(MFR, device.manufacturer());
144 assertEquals(HW, device.hwVersion());
145 assertEquals(swVersion, device.swVersion());
146 assertEquals(SN, device.serialNumber());
147 }
148
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700149 // TODO slice this out somewhere
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700150 /**
151 * Verifies that Annotations created by merging {@code annotations} is
152 * equal to actual Annotations.
153 *
154 * @param actual Annotations to check
155 * @param annotations
156 */
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700157 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700158 DefaultAnnotations expected = DefaultAnnotations.builder().build();
159 for (SparseAnnotations a : annotations) {
160 expected = DefaultAnnotations.merge(expected, a);
161 }
162 assertEquals(expected.keys(), actual.keys());
163 for (String key : expected.keys()) {
164 assertEquals(expected.value(key), actual.value(key));
165 }
166 }
167
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700168 @Test
169 public final void testGetDeviceCount() {
170 assertEquals("initialy empty", 0, deviceStore.getDeviceCount());
171
172 putDevice(DID1, SW1);
173 putDevice(DID2, SW2);
174 putDevice(DID1, SW1);
175
176 assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount());
177 }
178
179 @Test
180 public final void testGetDevices() {
181 assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices()));
182
183 putDevice(DID1, SW1);
184 putDevice(DID2, SW2);
185 putDevice(DID1, SW1);
186
187 assertEquals("expect 2 uniq devices",
188 2, Iterables.size(deviceStore.getDevices()));
189
190 Map<DeviceId, Device> devices = new HashMap<>();
191 for (Device device : deviceStore.getDevices()) {
192 devices.put(device.id(), device);
193 }
194
195 assertDevice(DID1, SW1, devices.get(DID1));
196 assertDevice(DID2, SW2, devices.get(DID2));
197
198 // add case for new node?
199 }
200
201 @Test
202 public final void testGetDevice() {
203
204 putDevice(DID1, SW1);
205
206 assertDevice(DID1, SW1, deviceStore.getDevice(DID1));
207 assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2));
208 }
209
210 @Test
211 public final void testCreateOrUpdateDevice() {
212 DeviceDescription description =
213 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700214 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700215 DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
216 assertEquals(DEVICE_ADDED, event.type());
217 assertDevice(DID1, SW1, event.subject());
218
219 DeviceDescription description2 =
220 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700221 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700222 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
223 assertEquals(DEVICE_UPDATED, event2.type());
224 assertDevice(DID1, SW2, event2.subject());
225
226 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
227 }
228
229 @Test
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700230 public final void testCreateOrUpdateDeviceAncillary() {
231 DeviceDescription description =
232 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700233 HW, SW1, SN, CID, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700234 DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description);
235 assertEquals(DEVICE_ADDED, event.type());
236 assertDevice(DID1, SW1, event.subject());
237 assertEquals(PIDA, event.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700238 assertAnnotationsEquals(event.subject().annotations(), A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700239 assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1));
240
241 DeviceDescription description2 =
242 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700243 HW, SW2, SN, CID, A1);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700244 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
245 assertEquals(DEVICE_UPDATED, event2.type());
246 assertDevice(DID1, SW2, event2.subject());
247 assertEquals(PID, event2.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700248 assertAnnotationsEquals(event2.subject().annotations(), A1, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700249 assertTrue(deviceStore.isAvailable(DID1));
250
251 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
252
253 // For now, Ancillary is ignored once primary appears
254 assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description));
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700255
256 // But, Ancillary annotations will be in effect
257 DeviceDescription description3 =
258 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700259 HW, SW1, SN, CID, A2_2);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700260 DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3);
261 assertEquals(DEVICE_UPDATED, event3.type());
262 // basic information will be the one from Primary
263 assertDevice(DID1, SW2, event3.subject());
264 assertEquals(PID, event3.subject().providerId());
265 // but annotation from Ancillary will be merged
266 assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2);
267 assertTrue(deviceStore.isAvailable(DID1));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700268 }
269
270
271 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700272 public final void testMarkOffline() {
273
274 putDevice(DID1, SW1);
275 assertTrue(deviceStore.isAvailable(DID1));
276
277 DeviceEvent event = deviceStore.markOffline(DID1);
278 assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type());
279 assertDevice(DID1, SW1, event.subject());
280 assertFalse(deviceStore.isAvailable(DID1));
281
282 DeviceEvent event2 = deviceStore.markOffline(DID1);
283 assertNull("No change, no event", event2);
284}
285
286 @Test
287 public final void testUpdatePorts() {
288 putDevice(DID1, SW1);
289 List<PortDescription> pds = Arrays.<PortDescription>asList(
290 new DefaultPortDescription(P1, true),
291 new DefaultPortDescription(P2, true)
292 );
293
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700294 List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700295
296 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
297 for (DeviceEvent event : events) {
298 assertEquals(PORT_ADDED, event.type());
299 assertDevice(DID1, SW1, event.subject());
300 assertTrue("PortNumber is one of expected",
301 expectedPorts.remove(event.port().number()));
302 assertTrue("Port is enabled", event.port().isEnabled());
303 }
304 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
305
306
307 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
308 new DefaultPortDescription(P1, false),
309 new DefaultPortDescription(P2, true),
310 new DefaultPortDescription(P3, true)
311 );
312
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700313 events = deviceStore.updatePorts(PID, DID1, pds2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700314 assertFalse("event should be triggered", events.isEmpty());
315 for (DeviceEvent event : events) {
316 PortNumber num = event.port().number();
317 if (P1.equals(num)) {
318 assertEquals(PORT_UPDATED, event.type());
319 assertDevice(DID1, SW1, event.subject());
320 assertFalse("Port is disabled", event.port().isEnabled());
321 } else if (P2.equals(num)) {
322 fail("P2 event not expected.");
323 } else if (P3.equals(num)) {
324 assertEquals(PORT_ADDED, event.type());
325 assertDevice(DID1, SW1, event.subject());
326 assertTrue("Port is enabled", event.port().isEnabled());
327 } else {
328 fail("Unknown port number encountered: " + num);
329 }
330 }
331
332 List<PortDescription> pds3 = Arrays.<PortDescription>asList(
333 new DefaultPortDescription(P1, false),
334 new DefaultPortDescription(P2, true)
335 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700336 events = deviceStore.updatePorts(PID, DID1, pds3);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700337 assertFalse("event should be triggered", events.isEmpty());
338 for (DeviceEvent event : events) {
339 PortNumber num = event.port().number();
340 if (P1.equals(num)) {
341 fail("P1 event not expected.");
342 } else if (P2.equals(num)) {
343 fail("P2 event not expected.");
344 } else if (P3.equals(num)) {
345 assertEquals(PORT_REMOVED, event.type());
346 assertDevice(DID1, SW1, event.subject());
347 assertTrue("Port was enabled", event.port().isEnabled());
348 } else {
349 fail("Unknown port number encountered: " + num);
350 }
351 }
352
353 }
354
355 @Test
356 public final void testUpdatePortStatus() {
357 putDevice(DID1, SW1);
358 List<PortDescription> pds = Arrays.<PortDescription>asList(
359 new DefaultPortDescription(P1, true)
360 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700361 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700362
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700363 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700364 new DefaultPortDescription(P1, false));
365 assertEquals(PORT_UPDATED, event.type());
366 assertDevice(DID1, SW1, event.subject());
367 assertEquals(P1, event.port().number());
368 assertFalse("Port is disabled", event.port().isEnabled());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700369
370 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700371
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700372 @Test
373 public final void testUpdatePortStatusAncillary() {
374 putDeviceAncillary(DID1, SW1);
375 putDevice(DID1, SW1);
376 List<PortDescription> pds = Arrays.<PortDescription>asList(
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700377 new DefaultPortDescription(P1, true, A1)
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700378 );
379 deviceStore.updatePorts(PID, DID1, pds);
380
381 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700382 new DefaultPortDescription(P1, false, A1_2));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700383 assertEquals(PORT_UPDATED, event.type());
384 assertDevice(DID1, SW1, event.subject());
385 assertEquals(P1, event.port().number());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700386 assertAnnotationsEquals(event.port().annotations(), A1, A1_2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700387 assertFalse("Port is disabled", event.port().isEnabled());
388
389 DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1,
390 new DefaultPortDescription(P1, true));
391 assertNull("Ancillary is ignored if primary exists", event2);
392
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700393 // but, Ancillary annotation update will be notified
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700394 DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1,
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700395 new DefaultPortDescription(P1, true, A2));
396 assertEquals(PORT_UPDATED, event3.type());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700397 assertDevice(DID1, SW1, event3.subject());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700398 assertEquals(P1, event3.port().number());
399 assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2);
400 assertFalse("Port is disabled", event3.port().isEnabled());
401
402 // port only reported from Ancillary will be notified as down
403 DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1,
404 new DefaultPortDescription(P2, true));
405 assertEquals(PORT_ADDED, event4.type());
406 assertDevice(DID1, SW1, event4.subject());
407 assertEquals(P2, event4.port().number());
408 assertAnnotationsEquals(event4.port().annotations());
409 assertFalse("Port is disabled if not given from primary provider",
410 event4.port().isEnabled());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700411 }
412
413 @Test
414 public final void testGetPorts() {
415 putDevice(DID1, SW1);
416 putDevice(DID2, SW1);
417 List<PortDescription> pds = Arrays.<PortDescription>asList(
418 new DefaultPortDescription(P1, true),
419 new DefaultPortDescription(P2, true)
420 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700421 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700422
423 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
424 List<Port> ports = deviceStore.getPorts(DID1);
425 for (Port port : ports) {
426 assertTrue("Port is enabled", port.isEnabled());
427 assertTrue("PortNumber is one of expected",
428 expectedPorts.remove(port.number()));
429 }
430 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
431
432
433 assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
434 }
435
436 @Test
437 public final void testGetPort() {
438 putDevice(DID1, SW1);
439 putDevice(DID2, SW1);
440 List<PortDescription> pds = Arrays.<PortDescription>asList(
441 new DefaultPortDescription(P1, true),
442 new DefaultPortDescription(P2, false)
443 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700444 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700445
446 Port port1 = deviceStore.getPort(DID1, P1);
447 assertEquals(P1, port1.number());
448 assertTrue("Port is enabled", port1.isEnabled());
449
450 Port port2 = deviceStore.getPort(DID1, P2);
451 assertEquals(P2, port2.number());
452 assertFalse("Port is disabled", port2.isEnabled());
453
454 Port port3 = deviceStore.getPort(DID1, P3);
455 assertNull("P3 not expected", port3);
456 }
457
458 @Test
459 public final void testRemoveDevice() {
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700460 putDevice(DID1, SW1, A1);
461 List<PortDescription> pds = Arrays.<PortDescription>asList(
462 new DefaultPortDescription(P1, true, A2)
463 );
464 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700465 putDevice(DID2, SW1);
466
467 assertEquals(2, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700468 assertEquals(1, deviceStore.getPorts(DID1).size());
469 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1);
470 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700471
472 DeviceEvent event = deviceStore.removeDevice(DID1);
473 assertEquals(DEVICE_REMOVED, event.type());
474 assertDevice(DID1, SW1, event.subject());
475
476 assertEquals(1, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700477 assertEquals(0, deviceStore.getPorts(DID1).size());
478
479 // putBack Device, Port w/o annotation
480 putDevice(DID1, SW1);
481 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
482 new DefaultPortDescription(P1, true)
483 );
484 deviceStore.updatePorts(PID, DID1, pds2);
485
486 // annotations should not survive
487 assertEquals(2, deviceStore.getDeviceCount());
488 assertEquals(1, deviceStore.getPorts(DID1).size());
489 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations());
490 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700491 }
492
493 // If Delegates should be called only on remote events,
494 // then Simple* should never call them, thus not test required.
495 // TODO add test for Port events when we have them
496 @Ignore("Ignore until Delegate spec. is clear.")
497 @Test
498 public final void testEvents() throws InterruptedException {
499 final CountDownLatch addLatch = new CountDownLatch(1);
500 DeviceStoreDelegate checkAdd = new DeviceStoreDelegate() {
501 @Override
502 public void notify(DeviceEvent event) {
503 assertEquals(DEVICE_ADDED, event.type());
504 assertDevice(DID1, SW1, event.subject());
505 addLatch.countDown();
506 }
507 };
508 final CountDownLatch updateLatch = new CountDownLatch(1);
509 DeviceStoreDelegate checkUpdate = new DeviceStoreDelegate() {
510 @Override
511 public void notify(DeviceEvent event) {
512 assertEquals(DEVICE_UPDATED, event.type());
513 assertDevice(DID1, SW2, event.subject());
514 updateLatch.countDown();
515 }
516 };
517 final CountDownLatch removeLatch = new CountDownLatch(1);
518 DeviceStoreDelegate checkRemove = new DeviceStoreDelegate() {
519 @Override
520 public void notify(DeviceEvent event) {
521 assertEquals(DEVICE_REMOVED, event.type());
522 assertDevice(DID1, SW2, event.subject());
523 removeLatch.countDown();
524 }
525 };
526
527 DeviceDescription description =
528 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700529 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700530 deviceStore.setDelegate(checkAdd);
531 deviceStore.createOrUpdateDevice(PID, DID1, description);
532 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
533
534
535 DeviceDescription description2 =
536 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700537 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700538 deviceStore.unsetDelegate(checkAdd);
539 deviceStore.setDelegate(checkUpdate);
540 deviceStore.createOrUpdateDevice(PID, DID1, description2);
541 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
542
543 deviceStore.unsetDelegate(checkUpdate);
544 deviceStore.setDelegate(checkRemove);
545 deviceStore.removeDevice(DID1);
546 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
547 }
548}