blob: 431fba37008f439974fda270523159d018e455a2 [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;
25import org.onlab.onos.net.Device;
26import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.Port;
28import org.onlab.onos.net.PortNumber;
29import org.onlab.onos.net.device.DefaultDeviceDescription;
30import org.onlab.onos.net.device.DefaultPortDescription;
31import org.onlab.onos.net.device.DeviceDescription;
32import org.onlab.onos.net.device.DeviceEvent;
33import org.onlab.onos.net.device.DeviceStore;
34import org.onlab.onos.net.device.DeviceStoreDelegate;
35import org.onlab.onos.net.device.PortDescription;
36import org.onlab.onos.net.provider.ProviderId;
37
38import com.google.common.collect.Iterables;
39import com.google.common.collect.Sets;
40
41/**
42 * Test of the simple DeviceStore implementation.
43 */
44public class SimpleDeviceStoreTest {
45
46 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070047 private static final ProviderId PIDA = new ProviderId("of", "bar", true);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070048 private static final DeviceId DID1 = deviceId("of:foo");
49 private static final DeviceId DID2 = deviceId("of:bar");
50 private static final String MFR = "whitebox";
51 private static final String HW = "1.1.x";
52 private static final String SW1 = "3.8.1";
53 private static final String SW2 = "3.9.5";
54 private static final String SN = "43311-12345";
55
56 private static final PortNumber P1 = PortNumber.portNumber(1);
57 private static final PortNumber P2 = PortNumber.portNumber(2);
58 private static final PortNumber P3 = PortNumber.portNumber(3);
59
60 private SimpleDeviceStore simpleDeviceStore;
61 private DeviceStore deviceStore;
62
63
64
65 @BeforeClass
66 public static void setUpBeforeClass() throws Exception {
67 }
68
69 @AfterClass
70 public static void tearDownAfterClass() throws Exception {
71 }
72
73
74 @Before
75 public void setUp() throws Exception {
76 simpleDeviceStore = new SimpleDeviceStore();
77 simpleDeviceStore.activate();
78 deviceStore = simpleDeviceStore;
79 }
80
81 @After
82 public void tearDown() throws Exception {
83 simpleDeviceStore.deactivate();
84 }
85
86 private void putDevice(DeviceId deviceId, String swVersion) {
87 DeviceDescription description =
88 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
89 HW, swVersion, SN);
90 deviceStore.createOrUpdateDevice(PID, deviceId, description);
91 }
92
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070093 private void putDeviceAncillary(DeviceId deviceId, String swVersion) {
94 DeviceDescription description =
95 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
96 HW, swVersion, SN);
97 deviceStore.createOrUpdateDevice(PIDA, deviceId, description);
98 }
99
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700100 private static void assertDevice(DeviceId id, String swVersion, Device device) {
101 assertNotNull(device);
102 assertEquals(id, device.id());
103 assertEquals(MFR, device.manufacturer());
104 assertEquals(HW, device.hwVersion());
105 assertEquals(swVersion, device.swVersion());
106 assertEquals(SN, device.serialNumber());
107 }
108
109 @Test
110 public final void testGetDeviceCount() {
111 assertEquals("initialy empty", 0, deviceStore.getDeviceCount());
112
113 putDevice(DID1, SW1);
114 putDevice(DID2, SW2);
115 putDevice(DID1, SW1);
116
117 assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount());
118 }
119
120 @Test
121 public final void testGetDevices() {
122 assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices()));
123
124 putDevice(DID1, SW1);
125 putDevice(DID2, SW2);
126 putDevice(DID1, SW1);
127
128 assertEquals("expect 2 uniq devices",
129 2, Iterables.size(deviceStore.getDevices()));
130
131 Map<DeviceId, Device> devices = new HashMap<>();
132 for (Device device : deviceStore.getDevices()) {
133 devices.put(device.id(), device);
134 }
135
136 assertDevice(DID1, SW1, devices.get(DID1));
137 assertDevice(DID2, SW2, devices.get(DID2));
138
139 // add case for new node?
140 }
141
142 @Test
143 public final void testGetDevice() {
144
145 putDevice(DID1, SW1);
146
147 assertDevice(DID1, SW1, deviceStore.getDevice(DID1));
148 assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2));
149 }
150
151 @Test
152 public final void testCreateOrUpdateDevice() {
153 DeviceDescription description =
154 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
155 HW, SW1, SN);
156 DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
157 assertEquals(DEVICE_ADDED, event.type());
158 assertDevice(DID1, SW1, event.subject());
159
160 DeviceDescription description2 =
161 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
162 HW, SW2, SN);
163 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
164 assertEquals(DEVICE_UPDATED, event2.type());
165 assertDevice(DID1, SW2, event2.subject());
166
167 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
168 }
169
170 @Test
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700171 public final void testCreateOrUpdateDeviceAncillary() {
172 DeviceDescription description =
173 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
174 HW, SW1, SN);
175 DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description);
176 assertEquals(DEVICE_ADDED, event.type());
177 assertDevice(DID1, SW1, event.subject());
178 assertEquals(PIDA, event.subject().providerId());
179 assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1));
180
181 DeviceDescription description2 =
182 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
183 HW, SW2, SN);
184 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
185 assertEquals(DEVICE_UPDATED, event2.type());
186 assertDevice(DID1, SW2, event2.subject());
187 assertEquals(PID, event2.subject().providerId());
188 assertTrue(deviceStore.isAvailable(DID1));
189
190 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
191
192 // For now, Ancillary is ignored once primary appears
193 assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description));
194 }
195
196
197 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700198 public final void testMarkOffline() {
199
200 putDevice(DID1, SW1);
201 assertTrue(deviceStore.isAvailable(DID1));
202
203 DeviceEvent event = deviceStore.markOffline(DID1);
204 assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type());
205 assertDevice(DID1, SW1, event.subject());
206 assertFalse(deviceStore.isAvailable(DID1));
207
208 DeviceEvent event2 = deviceStore.markOffline(DID1);
209 assertNull("No change, no event", event2);
210}
211
212 @Test
213 public final void testUpdatePorts() {
214 putDevice(DID1, SW1);
215 List<PortDescription> pds = Arrays.<PortDescription>asList(
216 new DefaultPortDescription(P1, true),
217 new DefaultPortDescription(P2, true)
218 );
219
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700220 List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700221
222 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
223 for (DeviceEvent event : events) {
224 assertEquals(PORT_ADDED, event.type());
225 assertDevice(DID1, SW1, event.subject());
226 assertTrue("PortNumber is one of expected",
227 expectedPorts.remove(event.port().number()));
228 assertTrue("Port is enabled", event.port().isEnabled());
229 }
230 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
231
232
233 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
234 new DefaultPortDescription(P1, false),
235 new DefaultPortDescription(P2, true),
236 new DefaultPortDescription(P3, true)
237 );
238
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700239 events = deviceStore.updatePorts(PID, DID1, pds2);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700240 assertFalse("event should be triggered", events.isEmpty());
241 for (DeviceEvent event : events) {
242 PortNumber num = event.port().number();
243 if (P1.equals(num)) {
244 assertEquals(PORT_UPDATED, event.type());
245 assertDevice(DID1, SW1, event.subject());
246 assertFalse("Port is disabled", event.port().isEnabled());
247 } else if (P2.equals(num)) {
248 fail("P2 event not expected.");
249 } else if (P3.equals(num)) {
250 assertEquals(PORT_ADDED, event.type());
251 assertDevice(DID1, SW1, event.subject());
252 assertTrue("Port is enabled", event.port().isEnabled());
253 } else {
254 fail("Unknown port number encountered: " + num);
255 }
256 }
257
258 List<PortDescription> pds3 = Arrays.<PortDescription>asList(
259 new DefaultPortDescription(P1, false),
260 new DefaultPortDescription(P2, true)
261 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700262 events = deviceStore.updatePorts(PID, DID1, pds3);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700263 assertFalse("event should be triggered", events.isEmpty());
264 for (DeviceEvent event : events) {
265 PortNumber num = event.port().number();
266 if (P1.equals(num)) {
267 fail("P1 event not expected.");
268 } else if (P2.equals(num)) {
269 fail("P2 event not expected.");
270 } else if (P3.equals(num)) {
271 assertEquals(PORT_REMOVED, event.type());
272 assertDevice(DID1, SW1, event.subject());
273 assertTrue("Port was enabled", event.port().isEnabled());
274 } else {
275 fail("Unknown port number encountered: " + num);
276 }
277 }
278
279 }
280
281 @Test
282 public final void testUpdatePortStatus() {
283 putDevice(DID1, SW1);
284 List<PortDescription> pds = Arrays.<PortDescription>asList(
285 new DefaultPortDescription(P1, true)
286 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700287 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700288
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700289 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700290 new DefaultPortDescription(P1, false));
291 assertEquals(PORT_UPDATED, event.type());
292 assertDevice(DID1, SW1, event.subject());
293 assertEquals(P1, event.port().number());
294 assertFalse("Port is disabled", event.port().isEnabled());
Yuta HIGUCHI8e493792014-10-01 19:36:32 -0700295
296 }
297 @Test
298 public final void testUpdatePortStatusAncillary() {
299 putDeviceAncillary(DID1, SW1);
300 putDevice(DID1, SW1);
301 List<PortDescription> pds = Arrays.<PortDescription>asList(
302 new DefaultPortDescription(P1, true)
303 );
304 deviceStore.updatePorts(PID, DID1, pds);
305
306 DeviceEvent event = deviceStore.updatePortStatus(PID, DID1,
307 new DefaultPortDescription(P1, false));
308 assertEquals(PORT_UPDATED, event.type());
309 assertDevice(DID1, SW1, event.subject());
310 assertEquals(P1, event.port().number());
311 assertFalse("Port is disabled", event.port().isEnabled());
312
313 DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1,
314 new DefaultPortDescription(P1, true));
315 assertNull("Ancillary is ignored if primary exists", event2);
316
317 DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1,
318 new DefaultPortDescription(P2, true));
319 assertEquals(PORT_ADDED, event3.type());
320 assertDevice(DID1, SW1, event3.subject());
321 assertEquals(P2, event3.port().number());
322 assertFalse("Port is disabled if not given from provider", event3.port().isEnabled());
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700323 }
324
325 @Test
326 public final void testGetPorts() {
327 putDevice(DID1, SW1);
328 putDevice(DID2, SW1);
329 List<PortDescription> pds = Arrays.<PortDescription>asList(
330 new DefaultPortDescription(P1, true),
331 new DefaultPortDescription(P2, true)
332 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700333 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700334
335 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
336 List<Port> ports = deviceStore.getPorts(DID1);
337 for (Port port : ports) {
338 assertTrue("Port is enabled", port.isEnabled());
339 assertTrue("PortNumber is one of expected",
340 expectedPorts.remove(port.number()));
341 }
342 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
343
344
345 assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
346 }
347
348 @Test
349 public final void testGetPort() {
350 putDevice(DID1, SW1);
351 putDevice(DID2, SW1);
352 List<PortDescription> pds = Arrays.<PortDescription>asList(
353 new DefaultPortDescription(P1, true),
354 new DefaultPortDescription(P2, false)
355 );
Yuta HIGUCHI5f6739c2014-10-01 14:04:01 -0700356 deviceStore.updatePorts(PID, DID1, pds);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700357
358 Port port1 = deviceStore.getPort(DID1, P1);
359 assertEquals(P1, port1.number());
360 assertTrue("Port is enabled", port1.isEnabled());
361
362 Port port2 = deviceStore.getPort(DID1, P2);
363 assertEquals(P2, port2.number());
364 assertFalse("Port is disabled", port2.isEnabled());
365
366 Port port3 = deviceStore.getPort(DID1, P3);
367 assertNull("P3 not expected", port3);
368 }
369
370 @Test
371 public final void testRemoveDevice() {
372 putDevice(DID1, SW1);
373 putDevice(DID2, SW1);
374
375 assertEquals(2, deviceStore.getDeviceCount());
376
377 DeviceEvent event = deviceStore.removeDevice(DID1);
378 assertEquals(DEVICE_REMOVED, event.type());
379 assertDevice(DID1, SW1, event.subject());
380
381 assertEquals(1, deviceStore.getDeviceCount());
382 }
383
384 // If Delegates should be called only on remote events,
385 // then Simple* should never call them, thus not test required.
386 // TODO add test for Port events when we have them
387 @Ignore("Ignore until Delegate spec. is clear.")
388 @Test
389 public final void testEvents() throws InterruptedException {
390 final CountDownLatch addLatch = new CountDownLatch(1);
391 DeviceStoreDelegate checkAdd = new DeviceStoreDelegate() {
392 @Override
393 public void notify(DeviceEvent event) {
394 assertEquals(DEVICE_ADDED, event.type());
395 assertDevice(DID1, SW1, event.subject());
396 addLatch.countDown();
397 }
398 };
399 final CountDownLatch updateLatch = new CountDownLatch(1);
400 DeviceStoreDelegate checkUpdate = new DeviceStoreDelegate() {
401 @Override
402 public void notify(DeviceEvent event) {
403 assertEquals(DEVICE_UPDATED, event.type());
404 assertDevice(DID1, SW2, event.subject());
405 updateLatch.countDown();
406 }
407 };
408 final CountDownLatch removeLatch = new CountDownLatch(1);
409 DeviceStoreDelegate checkRemove = new DeviceStoreDelegate() {
410 @Override
411 public void notify(DeviceEvent event) {
412 assertEquals(DEVICE_REMOVED, event.type());
413 assertDevice(DID1, SW2, event.subject());
414 removeLatch.countDown();
415 }
416 };
417
418 DeviceDescription description =
419 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
420 HW, SW1, SN);
421 deviceStore.setDelegate(checkAdd);
422 deviceStore.createOrUpdateDevice(PID, DID1, description);
423 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
424
425
426 DeviceDescription description2 =
427 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
428 HW, SW2, SN);
429 deviceStore.unsetDelegate(checkAdd);
430 deviceStore.setDelegate(checkUpdate);
431 deviceStore.createOrUpdateDevice(PID, DID1, description2);
432 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
433
434 deviceStore.unsetDelegate(checkUpdate);
435 deviceStore.setDelegate(checkRemove);
436 deviceStore.removeDevice(DID1);
437 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
438 }
439}