Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 17 | package org.onosproject.store.trivial; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 18 | |
| 19 | import static org.junit.Assert.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import static org.onosproject.net.Device.Type.SWITCH; |
| 21 | import static org.onosproject.net.DeviceId.deviceId; |
| 22 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
| 23 | import static org.onosproject.net.NetTestTools.assertAnnotationsEquals; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.Arrays; |
| 26 | import java.util.HashMap; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | import java.util.Set; |
| 30 | import java.util.concurrent.CountDownLatch; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | |
| 33 | import org.junit.After; |
| 34 | import org.junit.AfterClass; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.BeforeClass; |
| 37 | import org.junit.Ignore; |
| 38 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.DefaultAnnotations; |
| 40 | import org.onosproject.net.Device; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.Port; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.SparseAnnotations; |
| 45 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 46 | import org.onosproject.net.device.DefaultPortDescription; |
| 47 | import org.onosproject.net.device.DeviceDescription; |
| 48 | import org.onosproject.net.device.DeviceEvent; |
| 49 | import org.onosproject.net.device.DeviceStore; |
| 50 | import org.onosproject.net.device.DeviceStoreDelegate; |
| 51 | import org.onosproject.net.device.PortDescription; |
| 52 | import org.onosproject.net.provider.ProviderId; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 53 | |
| 54 | import com.google.common.collect.Iterables; |
| 55 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | 3e5d11a | 2014-11-04 14:16:44 -0800 | [diff] [blame] | 56 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 57 | import org.onlab.packet.ChassisId; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Test of the simple DeviceStore implementation. |
| 61 | */ |
| 62 | public class SimpleDeviceStoreTest { |
| 63 | |
| 64 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 65 | private static final ProviderId PIDA = new ProviderId("of", "bar", true); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 66 | 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"; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 73 | private static final ChassisId CID = new ChassisId(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 74 | |
| 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 HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 79 | 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 HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 96 | 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 HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 122 | private void putDevice(DeviceId deviceId, String swVersion, |
| 123 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 124 | DeviceDescription description = |
| 125 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 126 | HW, swVersion, SN, CID, annotations); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 127 | deviceStore.createOrUpdateDevice(PID, deviceId, description); |
| 128 | } |
| 129 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 130 | private void putDeviceAncillary(DeviceId deviceId, String swVersion, |
| 131 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 132 | DeviceDescription description = |
| 133 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 134 | HW, swVersion, SN, CID, annotations); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 135 | deviceStore.createOrUpdateDevice(PIDA, deviceId, description); |
| 136 | } |
| 137 | |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 138 | 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 |
mskala | 0d0c683 | 2017-07-12 11:21:23 +0200 | [diff] [blame] | 159 | 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 HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 175 | 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, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 209 | HW, SW1, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 210 | 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, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 216 | HW, SW2, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 217 | 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 HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 225 | public final void testCreateOrUpdateDeviceAncillary() { |
| 226 | DeviceDescription description = |
| 227 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 228 | HW, SW1, SN, CID, A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 229 | 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 HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 233 | assertAnnotationsEquals(event.subject().annotations(), A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 234 | assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1)); |
| 235 | |
| 236 | DeviceDescription description2 = |
| 237 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 238 | HW, SW2, SN, CID, A1); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 239 | 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 HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 243 | assertAnnotationsEquals(event2.subject().annotations(), A1, A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 244 | 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 HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 250 | |
| 251 | // But, Ancillary annotations will be in effect |
| 252 | DeviceDescription description3 = |
| 253 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 254 | HW, SW1, SN, CID, A2_2); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 255 | 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 HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
| 266 | @Test |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 267 | 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 SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 284 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 285 | new DefaultPortDescription(P1, true), |
| 286 | new DefaultPortDescription(P2, true) |
| 287 | ); |
| 288 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 289 | List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 290 | |
| 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 SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 302 | List<PortDescription> pds2 = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 303 | new DefaultPortDescription(P1, false), |
| 304 | new DefaultPortDescription(P2, true), |
| 305 | new DefaultPortDescription(P3, true) |
| 306 | ); |
| 307 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 308 | events = deviceStore.updatePorts(PID, DID1, pds2); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 309 | 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 SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 327 | List<PortDescription> pds3 = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 328 | new DefaultPortDescription(P1, false), |
| 329 | new DefaultPortDescription(P2, true) |
| 330 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 331 | events = deviceStore.updatePorts(PID, DID1, pds3); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 332 | 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 SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 353 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 354 | new DefaultPortDescription(P1, true) |
| 355 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 356 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 357 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 358 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 359 | new DefaultPortDescription(P1, false)); |
| 360 | 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 HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 364 | |
| 365 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 366 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 367 | @Test |
| 368 | public final void testUpdatePortStatusAncillary() { |
| 369 | putDeviceAncillary(DID1, SW1); |
| 370 | putDevice(DID1, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 371 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 372 | new DefaultPortDescription(P1, true, A1) |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 373 | ); |
| 374 | deviceStore.updatePorts(PID, DID1, pds); |
| 375 | |
| 376 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 377 | new DefaultPortDescription(P1, false, A1_2)); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 378 | assertEquals(PORT_UPDATED, event.type()); |
| 379 | assertDevice(DID1, SW1, event.subject()); |
| 380 | assertEquals(P1, event.port().number()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 381 | assertAnnotationsEquals(event.port().annotations(), A1, A1_2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 382 | assertFalse("Port is disabled", event.port().isEnabled()); |
| 383 | |
| 384 | DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1, |
| 385 | new DefaultPortDescription(P1, true)); |
| 386 | assertNull("Ancillary is ignored if primary exists", event2); |
| 387 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 388 | // but, Ancillary annotation update will be notified |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 389 | DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1, |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 390 | new DefaultPortDescription(P1, true, A2)); |
| 391 | assertEquals(PORT_UPDATED, event3.type()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 392 | assertDevice(DID1, SW1, event3.subject()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 393 | 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, |
| 399 | new DefaultPortDescription(P2, true)); |
| 400 | 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 HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Test |
| 409 | public final void testGetPorts() { |
| 410 | putDevice(DID1, SW1); |
| 411 | putDevice(DID2, SW1); |
| 412 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 413 | new DefaultPortDescription(P1, true), |
| 414 | new DefaultPortDescription(P2, true) |
| 415 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 416 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 417 | |
| 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 SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 435 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 436 | new DefaultPortDescription(P1, true), |
| 437 | new DefaultPortDescription(P2, false) |
| 438 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 439 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 440 | |
| 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 HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 455 | putDevice(DID1, SW1, A1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 456 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 457 | new DefaultPortDescription(P1, true, A2) |
| 458 | ); |
| 459 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 460 | putDevice(DID2, SW1); |
| 461 | |
| 462 | assertEquals(2, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 463 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 464 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1); |
| 465 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 466 | |
| 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 HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 472 | assertEquals(0, deviceStore.getPorts(DID1).size()); |
| 473 | |
| 474 | // putBack Device, Port w/o annotation |
| 475 | putDevice(DID1, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 476 | List<PortDescription> pds2 = Arrays.asList( |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 477 | new DefaultPortDescription(P1, true) |
| 478 | ); |
| 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 HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 486 | } |
| 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 SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 495 | DeviceStoreDelegate checkAdd = event -> { |
| 496 | assertEquals(DEVICE_ADDED, event.type()); |
| 497 | assertDevice(DID1, SW1, event.subject()); |
| 498 | addLatch.countDown(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 499 | }; |
| 500 | final CountDownLatch updateLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 501 | DeviceStoreDelegate checkUpdate = event -> { |
| 502 | assertEquals(DEVICE_UPDATED, event.type()); |
| 503 | assertDevice(DID1, SW2, event.subject()); |
| 504 | updateLatch.countDown(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 505 | }; |
| 506 | final CountDownLatch removeLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 507 | DeviceStoreDelegate checkRemove = event -> { |
| 508 | assertEquals(DEVICE_REMOVED, event.type()); |
| 509 | assertDevice(DID1, SW2, event.subject()); |
| 510 | removeLatch.countDown(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | DeviceDescription description = |
| 514 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 515 | HW, SW1, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 516 | 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, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 523 | HW, SW2, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 524 | 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 | } |