blob: 719307f62084aee6acfa4ce94321859aa1f74897 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
mskala0d0c6832017-07-12 11:21:23 +0200159 public final void testGetAvailableDeviceCount() {
160 assertEquals("initialy empty", 0, deviceStore.getAvailableDeviceCount());
161
162 putDevice(DID1, SW1);
163 putDevice(DID2, SW2);
164
165 deviceStore.markOffline(DID1);
166
167 assertEquals("expect 1 available device", 1, deviceStore.getAvailableDeviceCount());
168
169 putDevice(DID1, SW1);
170
171 assertEquals("expect 2 available devices", 2, deviceStore.getAvailableDeviceCount());
172 }
173
174 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700175 public final void testGetDevices() {
176 assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices()));
177
178 putDevice(DID1, SW1);
179 putDevice(DID2, SW2);
180 putDevice(DID1, SW1);
181
182 assertEquals("expect 2 uniq devices",
183 2, Iterables.size(deviceStore.getDevices()));
184
185 Map<DeviceId, Device> devices = new HashMap<>();
186 for (Device device : deviceStore.getDevices()) {
187 devices.put(device.id(), device);
188 }
189
190 assertDevice(DID1, SW1, devices.get(DID1));
191 assertDevice(DID2, SW2, devices.get(DID2));
192
193 // add case for new node?
194 }
195
196 @Test
197 public final void testGetDevice() {
198
199 putDevice(DID1, SW1);
200
201 assertDevice(DID1, SW1, deviceStore.getDevice(DID1));
202 assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2));
203 }
204
205 @Test
206 public final void testCreateOrUpdateDevice() {
207 DeviceDescription description =
208 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700209 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700210 DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
211 assertEquals(DEVICE_ADDED, event.type());
212 assertDevice(DID1, SW1, event.subject());
213
214 DeviceDescription description2 =
215 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700216 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700217 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
218 assertEquals(DEVICE_UPDATED, event2.type());
219 assertDevice(DID1, SW2, event2.subject());
220
221 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
222 }
223
224 @Test
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700225 public final void testCreateOrUpdateDeviceAncillary() {
226 DeviceDescription description =
227 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700228 HW, SW1, SN, CID, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700229 DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description);
230 assertEquals(DEVICE_ADDED, event.type());
231 assertDevice(DID1, SW1, event.subject());
232 assertEquals(PIDA, event.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700233 assertAnnotationsEquals(event.subject().annotations(), A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700234 assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1));
235
236 DeviceDescription description2 =
237 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700238 HW, SW2, SN, CID, A1);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700239 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
240 assertEquals(DEVICE_UPDATED, event2.type());
241 assertDevice(DID1, SW2, event2.subject());
242 assertEquals(PID, event2.subject().providerId());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700243 assertAnnotationsEquals(event2.subject().annotations(), A1, A2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700244 assertTrue(deviceStore.isAvailable(DID1));
245
246 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
247
248 // For now, Ancillary is ignored once primary appears
249 assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description));
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700250
251 // But, Ancillary annotations will be in effect
252 DeviceDescription description3 =
253 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700254 HW, SW1, SN, CID, A2_2);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700255 DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3);
256 assertEquals(DEVICE_UPDATED, event3.type());
257 // basic information will be the one from Primary
258 assertDevice(DID1, SW2, event3.subject());
259 assertEquals(PID, event3.subject().providerId());
260 // but annotation from Ancillary will be merged
261 assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2);
262 assertTrue(deviceStore.isAvailable(DID1));
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700263 }
264
265
266 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700267 public final void testMarkOffline() {
268
269 putDevice(DID1, SW1);
270 assertTrue(deviceStore.isAvailable(DID1));
271
272 DeviceEvent event = deviceStore.markOffline(DID1);
273 assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type());
274 assertDevice(DID1, SW1, event.subject());
275 assertFalse(deviceStore.isAvailable(DID1));
276
277 DeviceEvent event2 = deviceStore.markOffline(DID1);
278 assertNull("No change, no event", event2);
279}
280
281 @Test
282 public final void testUpdatePorts() {
283 putDevice(DID1, SW1);
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700284 List<PortDescription> pds = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800285 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(),
286 DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build()
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700287 );
288
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700289 List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700290
291 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
292 for (DeviceEvent event : events) {
293 assertEquals(PORT_ADDED, event.type());
294 assertDevice(DID1, SW1, event.subject());
295 assertTrue("PortNumber is one of expected",
296 expectedPorts.remove(event.port().number()));
297 assertTrue("Port is enabled", event.port().isEnabled());
298 }
299 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
300
301
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700302 List<PortDescription> pds2 = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800303 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).build(),
304 DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build(),
305 DefaultPortDescription.builder().withPortNumber(P3).isEnabled(true).build()
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700306 );
307
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700308 events = deviceStore.updatePorts(PID, DID1, pds2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700309 assertFalse("event should be triggered", events.isEmpty());
310 for (DeviceEvent event : events) {
311 PortNumber num = event.port().number();
312 if (P1.equals(num)) {
313 assertEquals(PORT_UPDATED, event.type());
314 assertDevice(DID1, SW1, event.subject());
315 assertFalse("Port is disabled", event.port().isEnabled());
316 } else if (P2.equals(num)) {
317 fail("P2 event not expected.");
318 } else if (P3.equals(num)) {
319 assertEquals(PORT_ADDED, event.type());
320 assertDevice(DID1, SW1, event.subject());
321 assertTrue("Port is enabled", event.port().isEnabled());
322 } else {
323 fail("Unknown port number encountered: " + num);
324 }
325 }
326
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700327 List<PortDescription> pds3 = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800328 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).build(),
329 DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build()
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700330 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700331 events = deviceStore.updatePorts(PID, DID1, pds3);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700332 assertFalse("event should be triggered", events.isEmpty());
333 for (DeviceEvent event : events) {
334 PortNumber num = event.port().number();
335 if (P1.equals(num)) {
336 fail("P1 event not expected.");
337 } else if (P2.equals(num)) {
338 fail("P2 event not expected.");
339 } else if (P3.equals(num)) {
340 assertEquals(PORT_REMOVED, event.type());
341 assertDevice(DID1, SW1, event.subject());
342 assertTrue("Port was enabled", event.port().isEnabled());
343 } else {
344 fail("Unknown port number encountered: " + num);
345 }
346 }
347
348 }
349
350 @Test
351 public final void testUpdatePortStatus() {
352 putDevice(DID1, SW1);
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700353 List<PortDescription> pds = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800354 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build()
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700355 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700356 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700357
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700358 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800359 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).build());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700360 assertEquals(PORT_UPDATED, event.type());
361 assertDevice(DID1, SW1, event.subject());
362 assertEquals(P1, event.port().number());
363 assertFalse("Port is disabled", event.port().isEnabled());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700364
365 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700366
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700367 @Test
368 public final void testUpdatePortStatusAncillary() {
369 putDeviceAncillary(DID1, SW1);
370 putDevice(DID1, SW1);
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700371 List<PortDescription> pds = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800372 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).annotations(A1).build()
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700373 );
374 deviceStore.updatePorts(PID, DID1, pds);
375
376 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800377 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(false).annotations(A1_2).build());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700378 assertEquals(PORT_UPDATED, event.type());
379 assertDevice(DID1, SW1, event.subject());
380 assertEquals(P1, event.port().number());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700381 assertAnnotationsEquals(event.port().annotations(), A1, A1_2);
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700382 assertFalse("Port is disabled", event.port().isEnabled());
383
384 DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800385 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700386 assertNull("Ancillary is ignored if primary exists", event2);
387
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700388 // but, Ancillary annotation update will be notified
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700389 DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800390 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).annotations(A2).build());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700391 assertEquals(PORT_UPDATED, event3.type());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700392 assertDevice(DID1, SW1, event3.subject());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700393 assertEquals(P1, event3.port().number());
394 assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2);
395 assertFalse("Port is disabled", event3.port().isEnabled());
396
397 // port only reported from Ancillary will be notified as down
398 DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1,
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800399 DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build());
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700400 assertEquals(PORT_ADDED, event4.type());
401 assertDevice(DID1, SW1, event4.subject());
402 assertEquals(P2, event4.port().number());
403 assertAnnotationsEquals(event4.port().annotations());
404 assertFalse("Port is disabled if not given from primary provider",
405 event4.port().isEnabled());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700406 }
407
408 @Test
409 public final void testGetPorts() {
410 putDevice(DID1, SW1);
411 putDevice(DID2, SW1);
412 List<PortDescription> pds = Arrays.<PortDescription>asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800413 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(),
414 DefaultPortDescription.builder().withPortNumber(P2).isEnabled(true).build()
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700415 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700416 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700417
418 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
419 List<Port> ports = deviceStore.getPorts(DID1);
420 for (Port port : ports) {
421 assertTrue("Port is enabled", port.isEnabled());
422 assertTrue("PortNumber is one of expected",
423 expectedPorts.remove(port.number()));
424 }
425 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
426
427
428 assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
429 }
430
431 @Test
432 public final void testGetPort() {
433 putDevice(DID1, SW1);
434 putDevice(DID2, SW1);
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700435 List<PortDescription> pds = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800436 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build(),
437 DefaultPortDescription.builder().withPortNumber(P2).isEnabled(false).build()
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700438 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700439 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700440
441 Port port1 = deviceStore.getPort(DID1, P1);
442 assertEquals(P1, port1.number());
443 assertTrue("Port is enabled", port1.isEnabled());
444
445 Port port2 = deviceStore.getPort(DID1, P2);
446 assertEquals(P2, port2.number());
447 assertFalse("Port is disabled", port2.isEnabled());
448
449 Port port3 = deviceStore.getPort(DID1, P3);
450 assertNull("P3 not expected", port3);
451 }
452
453 @Test
454 public final void testRemoveDevice() {
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700455 putDevice(DID1, SW1, A1);
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700456 List<PortDescription> pds = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800457 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).annotations(A2).build()
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700458 );
459 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700460 putDevice(DID2, SW1);
461
462 assertEquals(2, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700463 assertEquals(1, deviceStore.getPorts(DID1).size());
464 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1);
465 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700466
467 DeviceEvent event = deviceStore.removeDevice(DID1);
468 assertEquals(DEVICE_REMOVED, event.type());
469 assertDevice(DID1, SW1, event.subject());
470
471 assertEquals(1, deviceStore.getDeviceCount());
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700472 assertEquals(0, deviceStore.getPorts(DID1).size());
473
474 // putBack Device, Port w/o annotation
475 putDevice(DID1, SW1);
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700476 List<PortDescription> pds2 = Arrays.asList(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800477 DefaultPortDescription.builder().withPortNumber(P1).isEnabled(true).build()
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700478 );
479 deviceStore.updatePorts(PID, DID1, pds2);
480
481 // annotations should not survive
482 assertEquals(2, deviceStore.getDeviceCount());
483 assertEquals(1, deviceStore.getPorts(DID1).size());
484 assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations());
485 assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700486 }
487
488 // If Delegates should be called only on remote events,
489 // then Simple* should never call them, thus not test required.
490 // TODO add test for Port events when we have them
491 @Ignore("Ignore until Delegate spec. is clear.")
492 @Test
493 public final void testEvents() throws InterruptedException {
494 final CountDownLatch addLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700495 DeviceStoreDelegate checkAdd = event -> {
496 assertEquals(DEVICE_ADDED, event.type());
497 assertDevice(DID1, SW1, event.subject());
498 addLatch.countDown();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700499 };
500 final CountDownLatch updateLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700501 DeviceStoreDelegate checkUpdate = event -> {
502 assertEquals(DEVICE_UPDATED, event.type());
503 assertDevice(DID1, SW2, event.subject());
504 updateLatch.countDown();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700505 };
506 final CountDownLatch removeLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700507 DeviceStoreDelegate checkRemove = event -> {
508 assertEquals(DEVICE_REMOVED, event.type());
509 assertDevice(DID1, SW2, event.subject());
510 removeLatch.countDown();
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700511 };
512
513 DeviceDescription description =
514 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700515 HW, SW1, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700516 deviceStore.setDelegate(checkAdd);
517 deviceStore.createOrUpdateDevice(PID, DID1, description);
518 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
519
520
521 DeviceDescription description2 =
522 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
alshabib7911a052014-10-16 17:49:37 -0700523 HW, SW2, SN, CID);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700524 deviceStore.unsetDelegate(checkAdd);
525 deviceStore.setDelegate(checkUpdate);
526 deviceStore.createOrUpdateDevice(PID, DID1, description2);
527 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
528
529 deviceStore.unsetDelegate(checkUpdate);
530 deviceStore.setDelegate(checkRemove);
531 deviceStore.removeDevice(DID1);
532 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
533 }
534}