blob: f973d9b1d975de8d7163ead770857a2eee292f24 [file] [log] [blame]
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -07001/**
2 *
3 */
4package org.onlab.onos.net.trivial.impl;
5
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");
47 private static final DeviceId DID1 = deviceId("of:foo");
48 private static final DeviceId DID2 = deviceId("of:bar");
49 private static final String MFR = "whitebox";
50 private static final String HW = "1.1.x";
51 private static final String SW1 = "3.8.1";
52 private static final String SW2 = "3.9.5";
53 private static final String SN = "43311-12345";
54
55 private static final PortNumber P1 = PortNumber.portNumber(1);
56 private static final PortNumber P2 = PortNumber.portNumber(2);
57 private static final PortNumber P3 = PortNumber.portNumber(3);
58
59 private SimpleDeviceStore simpleDeviceStore;
60 private DeviceStore deviceStore;
61
62
63
64 @BeforeClass
65 public static void setUpBeforeClass() throws Exception {
66 }
67
68 @AfterClass
69 public static void tearDownAfterClass() throws Exception {
70 }
71
72
73 @Before
74 public void setUp() throws Exception {
75 simpleDeviceStore = new SimpleDeviceStore();
76 simpleDeviceStore.activate();
77 deviceStore = simpleDeviceStore;
78 }
79
80 @After
81 public void tearDown() throws Exception {
82 simpleDeviceStore.deactivate();
83 }
84
85 private void putDevice(DeviceId deviceId, String swVersion) {
86 DeviceDescription description =
87 new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
88 HW, swVersion, SN);
89 deviceStore.createOrUpdateDevice(PID, deviceId, description);
90 }
91
92 private static void assertDevice(DeviceId id, String swVersion, Device device) {
93 assertNotNull(device);
94 assertEquals(id, device.id());
95 assertEquals(MFR, device.manufacturer());
96 assertEquals(HW, device.hwVersion());
97 assertEquals(swVersion, device.swVersion());
98 assertEquals(SN, device.serialNumber());
99 }
100
101 @Test
102 public final void testGetDeviceCount() {
103 assertEquals("initialy empty", 0, deviceStore.getDeviceCount());
104
105 putDevice(DID1, SW1);
106 putDevice(DID2, SW2);
107 putDevice(DID1, SW1);
108
109 assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount());
110 }
111
112 @Test
113 public final void testGetDevices() {
114 assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices()));
115
116 putDevice(DID1, SW1);
117 putDevice(DID2, SW2);
118 putDevice(DID1, SW1);
119
120 assertEquals("expect 2 uniq devices",
121 2, Iterables.size(deviceStore.getDevices()));
122
123 Map<DeviceId, Device> devices = new HashMap<>();
124 for (Device device : deviceStore.getDevices()) {
125 devices.put(device.id(), device);
126 }
127
128 assertDevice(DID1, SW1, devices.get(DID1));
129 assertDevice(DID2, SW2, devices.get(DID2));
130
131 // add case for new node?
132 }
133
134 @Test
135 public final void testGetDevice() {
136
137 putDevice(DID1, SW1);
138
139 assertDevice(DID1, SW1, deviceStore.getDevice(DID1));
140 assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2));
141 }
142
143 @Test
144 public final void testCreateOrUpdateDevice() {
145 DeviceDescription description =
146 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
147 HW, SW1, SN);
148 DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
149 assertEquals(DEVICE_ADDED, event.type());
150 assertDevice(DID1, SW1, event.subject());
151
152 DeviceDescription description2 =
153 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
154 HW, SW2, SN);
155 DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
156 assertEquals(DEVICE_UPDATED, event2.type());
157 assertDevice(DID1, SW2, event2.subject());
158
159 assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
160 }
161
162 @Test
163 public final void testMarkOffline() {
164
165 putDevice(DID1, SW1);
166 assertTrue(deviceStore.isAvailable(DID1));
167
168 DeviceEvent event = deviceStore.markOffline(DID1);
169 assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type());
170 assertDevice(DID1, SW1, event.subject());
171 assertFalse(deviceStore.isAvailable(DID1));
172
173 DeviceEvent event2 = deviceStore.markOffline(DID1);
174 assertNull("No change, no event", event2);
175}
176
177 @Test
178 public final void testUpdatePorts() {
179 putDevice(DID1, SW1);
180 List<PortDescription> pds = Arrays.<PortDescription>asList(
181 new DefaultPortDescription(P1, true),
182 new DefaultPortDescription(P2, true)
183 );
184
185 List<DeviceEvent> events = deviceStore.updatePorts(DID1, pds);
186
187 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
188 for (DeviceEvent event : events) {
189 assertEquals(PORT_ADDED, event.type());
190 assertDevice(DID1, SW1, event.subject());
191 assertTrue("PortNumber is one of expected",
192 expectedPorts.remove(event.port().number()));
193 assertTrue("Port is enabled", event.port().isEnabled());
194 }
195 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
196
197
198 List<PortDescription> pds2 = Arrays.<PortDescription>asList(
199 new DefaultPortDescription(P1, false),
200 new DefaultPortDescription(P2, true),
201 new DefaultPortDescription(P3, true)
202 );
203
204 events = deviceStore.updatePorts(DID1, pds2);
205 assertFalse("event should be triggered", events.isEmpty());
206 for (DeviceEvent event : events) {
207 PortNumber num = event.port().number();
208 if (P1.equals(num)) {
209 assertEquals(PORT_UPDATED, event.type());
210 assertDevice(DID1, SW1, event.subject());
211 assertFalse("Port is disabled", event.port().isEnabled());
212 } else if (P2.equals(num)) {
213 fail("P2 event not expected.");
214 } else if (P3.equals(num)) {
215 assertEquals(PORT_ADDED, event.type());
216 assertDevice(DID1, SW1, event.subject());
217 assertTrue("Port is enabled", event.port().isEnabled());
218 } else {
219 fail("Unknown port number encountered: " + num);
220 }
221 }
222
223 List<PortDescription> pds3 = Arrays.<PortDescription>asList(
224 new DefaultPortDescription(P1, false),
225 new DefaultPortDescription(P2, true)
226 );
227 events = deviceStore.updatePorts(DID1, pds3);
228 assertFalse("event should be triggered", events.isEmpty());
229 for (DeviceEvent event : events) {
230 PortNumber num = event.port().number();
231 if (P1.equals(num)) {
232 fail("P1 event not expected.");
233 } else if (P2.equals(num)) {
234 fail("P2 event not expected.");
235 } else if (P3.equals(num)) {
236 assertEquals(PORT_REMOVED, event.type());
237 assertDevice(DID1, SW1, event.subject());
238 assertTrue("Port was enabled", event.port().isEnabled());
239 } else {
240 fail("Unknown port number encountered: " + num);
241 }
242 }
243
244 }
245
246 @Test
247 public final void testUpdatePortStatus() {
248 putDevice(DID1, SW1);
249 List<PortDescription> pds = Arrays.<PortDescription>asList(
250 new DefaultPortDescription(P1, true)
251 );
252 deviceStore.updatePorts(DID1, pds);
253
254 DeviceEvent event = deviceStore.updatePortStatus(DID1,
255 new DefaultPortDescription(P1, false));
256 assertEquals(PORT_UPDATED, event.type());
257 assertDevice(DID1, SW1, event.subject());
258 assertEquals(P1, event.port().number());
259 assertFalse("Port is disabled", event.port().isEnabled());
260 }
261
262 @Test
263 public final void testGetPorts() {
264 putDevice(DID1, SW1);
265 putDevice(DID2, SW1);
266 List<PortDescription> pds = Arrays.<PortDescription>asList(
267 new DefaultPortDescription(P1, true),
268 new DefaultPortDescription(P2, true)
269 );
270 deviceStore.updatePorts(DID1, pds);
271
272 Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2);
273 List<Port> ports = deviceStore.getPorts(DID1);
274 for (Port port : ports) {
275 assertTrue("Port is enabled", port.isEnabled());
276 assertTrue("PortNumber is one of expected",
277 expectedPorts.remove(port.number()));
278 }
279 assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty());
280
281
282 assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty());
283 }
284
285 @Test
286 public final void testGetPort() {
287 putDevice(DID1, SW1);
288 putDevice(DID2, SW1);
289 List<PortDescription> pds = Arrays.<PortDescription>asList(
290 new DefaultPortDescription(P1, true),
291 new DefaultPortDescription(P2, false)
292 );
293 deviceStore.updatePorts(DID1, pds);
294
295 Port port1 = deviceStore.getPort(DID1, P1);
296 assertEquals(P1, port1.number());
297 assertTrue("Port is enabled", port1.isEnabled());
298
299 Port port2 = deviceStore.getPort(DID1, P2);
300 assertEquals(P2, port2.number());
301 assertFalse("Port is disabled", port2.isEnabled());
302
303 Port port3 = deviceStore.getPort(DID1, P3);
304 assertNull("P3 not expected", port3);
305 }
306
307 @Test
308 public final void testRemoveDevice() {
309 putDevice(DID1, SW1);
310 putDevice(DID2, SW1);
311
312 assertEquals(2, deviceStore.getDeviceCount());
313
314 DeviceEvent event = deviceStore.removeDevice(DID1);
315 assertEquals(DEVICE_REMOVED, event.type());
316 assertDevice(DID1, SW1, event.subject());
317
318 assertEquals(1, deviceStore.getDeviceCount());
319 }
320
321 // If Delegates should be called only on remote events,
322 // then Simple* should never call them, thus not test required.
323 // TODO add test for Port events when we have them
324 @Ignore("Ignore until Delegate spec. is clear.")
325 @Test
326 public final void testEvents() throws InterruptedException {
327 final CountDownLatch addLatch = new CountDownLatch(1);
328 DeviceStoreDelegate checkAdd = new DeviceStoreDelegate() {
329 @Override
330 public void notify(DeviceEvent event) {
331 assertEquals(DEVICE_ADDED, event.type());
332 assertDevice(DID1, SW1, event.subject());
333 addLatch.countDown();
334 }
335 };
336 final CountDownLatch updateLatch = new CountDownLatch(1);
337 DeviceStoreDelegate checkUpdate = new DeviceStoreDelegate() {
338 @Override
339 public void notify(DeviceEvent event) {
340 assertEquals(DEVICE_UPDATED, event.type());
341 assertDevice(DID1, SW2, event.subject());
342 updateLatch.countDown();
343 }
344 };
345 final CountDownLatch removeLatch = new CountDownLatch(1);
346 DeviceStoreDelegate checkRemove = new DeviceStoreDelegate() {
347 @Override
348 public void notify(DeviceEvent event) {
349 assertEquals(DEVICE_REMOVED, event.type());
350 assertDevice(DID1, SW2, event.subject());
351 removeLatch.countDown();
352 }
353 };
354
355 DeviceDescription description =
356 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
357 HW, SW1, SN);
358 deviceStore.setDelegate(checkAdd);
359 deviceStore.createOrUpdateDevice(PID, DID1, description);
360 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
361
362
363 DeviceDescription description2 =
364 new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR,
365 HW, SW2, SN);
366 deviceStore.unsetDelegate(checkAdd);
367 deviceStore.setDelegate(checkUpdate);
368 deviceStore.createOrUpdateDevice(PID, DID1, description2);
369 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
370
371 deviceStore.unsetDelegate(checkUpdate);
372 deviceStore.setDelegate(checkRemove);
373 deviceStore.removeDevice(DID1);
374 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
375 }
376}