Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 |
| 159 | public final void testGetDevices() { |
| 160 | assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices())); |
| 161 | |
| 162 | putDevice(DID1, SW1); |
| 163 | putDevice(DID2, SW2); |
| 164 | putDevice(DID1, SW1); |
| 165 | |
| 166 | assertEquals("expect 2 uniq devices", |
| 167 | 2, Iterables.size(deviceStore.getDevices())); |
| 168 | |
| 169 | Map<DeviceId, Device> devices = new HashMap<>(); |
| 170 | for (Device device : deviceStore.getDevices()) { |
| 171 | devices.put(device.id(), device); |
| 172 | } |
| 173 | |
| 174 | assertDevice(DID1, SW1, devices.get(DID1)); |
| 175 | assertDevice(DID2, SW2, devices.get(DID2)); |
| 176 | |
| 177 | // add case for new node? |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public final void testGetDevice() { |
| 182 | |
| 183 | putDevice(DID1, SW1); |
| 184 | |
| 185 | assertDevice(DID1, SW1, deviceStore.getDevice(DID1)); |
| 186 | assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2)); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public final void testCreateOrUpdateDevice() { |
| 191 | DeviceDescription description = |
| 192 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 193 | HW, SW1, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 194 | DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description); |
| 195 | assertEquals(DEVICE_ADDED, event.type()); |
| 196 | assertDevice(DID1, SW1, event.subject()); |
| 197 | |
| 198 | DeviceDescription description2 = |
| 199 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 200 | HW, SW2, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 201 | DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 202 | assertEquals(DEVICE_UPDATED, event2.type()); |
| 203 | assertDevice(DID1, SW2, event2.subject()); |
| 204 | |
| 205 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); |
| 206 | } |
| 207 | |
| 208 | @Test |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 209 | public final void testCreateOrUpdateDeviceAncillary() { |
| 210 | DeviceDescription description = |
| 211 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 212 | HW, SW1, SN, CID, A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 213 | DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description); |
| 214 | assertEquals(DEVICE_ADDED, event.type()); |
| 215 | assertDevice(DID1, SW1, event.subject()); |
| 216 | assertEquals(PIDA, event.subject().providerId()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 217 | assertAnnotationsEquals(event.subject().annotations(), A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 218 | assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1)); |
| 219 | |
| 220 | DeviceDescription description2 = |
| 221 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 222 | HW, SW2, SN, CID, A1); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 223 | DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 224 | assertEquals(DEVICE_UPDATED, event2.type()); |
| 225 | assertDevice(DID1, SW2, event2.subject()); |
| 226 | assertEquals(PID, event2.subject().providerId()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 227 | assertAnnotationsEquals(event2.subject().annotations(), A1, A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 228 | assertTrue(deviceStore.isAvailable(DID1)); |
| 229 | |
| 230 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); |
| 231 | |
| 232 | // For now, Ancillary is ignored once primary appears |
| 233 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description)); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 234 | |
| 235 | // But, Ancillary annotations will be in effect |
| 236 | DeviceDescription description3 = |
| 237 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 238 | HW, SW1, SN, CID, A2_2); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 239 | DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3); |
| 240 | assertEquals(DEVICE_UPDATED, event3.type()); |
| 241 | // basic information will be the one from Primary |
| 242 | assertDevice(DID1, SW2, event3.subject()); |
| 243 | assertEquals(PID, event3.subject().providerId()); |
| 244 | // but annotation from Ancillary will be merged |
| 245 | assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2); |
| 246 | assertTrue(deviceStore.isAvailable(DID1)); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | @Test |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 251 | public final void testMarkOffline() { |
| 252 | |
| 253 | putDevice(DID1, SW1); |
| 254 | assertTrue(deviceStore.isAvailable(DID1)); |
| 255 | |
| 256 | DeviceEvent event = deviceStore.markOffline(DID1); |
| 257 | assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type()); |
| 258 | assertDevice(DID1, SW1, event.subject()); |
| 259 | assertFalse(deviceStore.isAvailable(DID1)); |
| 260 | |
| 261 | DeviceEvent event2 = deviceStore.markOffline(DID1); |
| 262 | assertNull("No change, no event", event2); |
| 263 | } |
| 264 | |
| 265 | @Test |
| 266 | public final void testUpdatePorts() { |
| 267 | putDevice(DID1, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 268 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 269 | new DefaultPortDescription(P1, true), |
| 270 | new DefaultPortDescription(P2, true) |
| 271 | ); |
| 272 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 273 | List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 274 | |
| 275 | Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2); |
| 276 | for (DeviceEvent event : events) { |
| 277 | assertEquals(PORT_ADDED, event.type()); |
| 278 | assertDevice(DID1, SW1, event.subject()); |
| 279 | assertTrue("PortNumber is one of expected", |
| 280 | expectedPorts.remove(event.port().number())); |
| 281 | assertTrue("Port is enabled", event.port().isEnabled()); |
| 282 | } |
| 283 | assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty()); |
| 284 | |
| 285 | |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 286 | List<PortDescription> pds2 = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 287 | new DefaultPortDescription(P1, false), |
| 288 | new DefaultPortDescription(P2, true), |
| 289 | new DefaultPortDescription(P3, true) |
| 290 | ); |
| 291 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 292 | events = deviceStore.updatePorts(PID, DID1, pds2); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 293 | assertFalse("event should be triggered", events.isEmpty()); |
| 294 | for (DeviceEvent event : events) { |
| 295 | PortNumber num = event.port().number(); |
| 296 | if (P1.equals(num)) { |
| 297 | assertEquals(PORT_UPDATED, event.type()); |
| 298 | assertDevice(DID1, SW1, event.subject()); |
| 299 | assertFalse("Port is disabled", event.port().isEnabled()); |
| 300 | } else if (P2.equals(num)) { |
| 301 | fail("P2 event not expected."); |
| 302 | } else if (P3.equals(num)) { |
| 303 | assertEquals(PORT_ADDED, event.type()); |
| 304 | assertDevice(DID1, SW1, event.subject()); |
| 305 | assertTrue("Port is enabled", event.port().isEnabled()); |
| 306 | } else { |
| 307 | fail("Unknown port number encountered: " + num); |
| 308 | } |
| 309 | } |
| 310 | |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 311 | List<PortDescription> pds3 = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 312 | new DefaultPortDescription(P1, false), |
| 313 | new DefaultPortDescription(P2, true) |
| 314 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 315 | events = deviceStore.updatePorts(PID, DID1, pds3); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 316 | assertFalse("event should be triggered", events.isEmpty()); |
| 317 | for (DeviceEvent event : events) { |
| 318 | PortNumber num = event.port().number(); |
| 319 | if (P1.equals(num)) { |
| 320 | fail("P1 event not expected."); |
| 321 | } else if (P2.equals(num)) { |
| 322 | fail("P2 event not expected."); |
| 323 | } else if (P3.equals(num)) { |
| 324 | assertEquals(PORT_REMOVED, event.type()); |
| 325 | assertDevice(DID1, SW1, event.subject()); |
| 326 | assertTrue("Port was enabled", event.port().isEnabled()); |
| 327 | } else { |
| 328 | fail("Unknown port number encountered: " + num); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | } |
| 333 | |
| 334 | @Test |
| 335 | public final void testUpdatePortStatus() { |
| 336 | putDevice(DID1, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 337 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 338 | new DefaultPortDescription(P1, true) |
| 339 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 340 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 341 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 342 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 343 | new DefaultPortDescription(P1, false)); |
| 344 | assertEquals(PORT_UPDATED, event.type()); |
| 345 | assertDevice(DID1, SW1, event.subject()); |
| 346 | assertEquals(P1, event.port().number()); |
| 347 | assertFalse("Port is disabled", event.port().isEnabled()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 348 | |
| 349 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 350 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 351 | @Test |
| 352 | public final void testUpdatePortStatusAncillary() { |
| 353 | putDeviceAncillary(DID1, SW1); |
| 354 | putDevice(DID1, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 355 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 356 | new DefaultPortDescription(P1, true, A1) |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 357 | ); |
| 358 | deviceStore.updatePorts(PID, DID1, pds); |
| 359 | |
| 360 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 361 | new DefaultPortDescription(P1, false, A1_2)); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 362 | assertEquals(PORT_UPDATED, event.type()); |
| 363 | assertDevice(DID1, SW1, event.subject()); |
| 364 | assertEquals(P1, event.port().number()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 365 | assertAnnotationsEquals(event.port().annotations(), A1, A1_2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 366 | assertFalse("Port is disabled", event.port().isEnabled()); |
| 367 | |
| 368 | DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1, |
| 369 | new DefaultPortDescription(P1, true)); |
| 370 | assertNull("Ancillary is ignored if primary exists", event2); |
| 371 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 372 | // but, Ancillary annotation update will be notified |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 373 | DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1, |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 374 | new DefaultPortDescription(P1, true, A2)); |
| 375 | assertEquals(PORT_UPDATED, event3.type()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 376 | assertDevice(DID1, SW1, event3.subject()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 377 | assertEquals(P1, event3.port().number()); |
| 378 | assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2); |
| 379 | assertFalse("Port is disabled", event3.port().isEnabled()); |
| 380 | |
| 381 | // port only reported from Ancillary will be notified as down |
| 382 | DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1, |
| 383 | new DefaultPortDescription(P2, true)); |
| 384 | assertEquals(PORT_ADDED, event4.type()); |
| 385 | assertDevice(DID1, SW1, event4.subject()); |
| 386 | assertEquals(P2, event4.port().number()); |
| 387 | assertAnnotationsEquals(event4.port().annotations()); |
| 388 | assertFalse("Port is disabled if not given from primary provider", |
| 389 | event4.port().isEnabled()); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Test |
| 393 | public final void testGetPorts() { |
| 394 | putDevice(DID1, SW1); |
| 395 | putDevice(DID2, SW1); |
| 396 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 397 | new DefaultPortDescription(P1, true), |
| 398 | new DefaultPortDescription(P2, true) |
| 399 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 400 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 401 | |
| 402 | Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2); |
| 403 | List<Port> ports = deviceStore.getPorts(DID1); |
| 404 | for (Port port : ports) { |
| 405 | assertTrue("Port is enabled", port.isEnabled()); |
| 406 | assertTrue("PortNumber is one of expected", |
| 407 | expectedPorts.remove(port.number())); |
| 408 | } |
| 409 | assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty()); |
| 410 | |
| 411 | |
| 412 | assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty()); |
| 413 | } |
| 414 | |
| 415 | @Test |
| 416 | public final void testGetPort() { |
| 417 | putDevice(DID1, SW1); |
| 418 | putDevice(DID2, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 419 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 420 | new DefaultPortDescription(P1, true), |
| 421 | new DefaultPortDescription(P2, false) |
| 422 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 423 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 424 | |
| 425 | Port port1 = deviceStore.getPort(DID1, P1); |
| 426 | assertEquals(P1, port1.number()); |
| 427 | assertTrue("Port is enabled", port1.isEnabled()); |
| 428 | |
| 429 | Port port2 = deviceStore.getPort(DID1, P2); |
| 430 | assertEquals(P2, port2.number()); |
| 431 | assertFalse("Port is disabled", port2.isEnabled()); |
| 432 | |
| 433 | Port port3 = deviceStore.getPort(DID1, P3); |
| 434 | assertNull("P3 not expected", port3); |
| 435 | } |
| 436 | |
| 437 | @Test |
| 438 | public final void testRemoveDevice() { |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 439 | putDevice(DID1, SW1, A1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 440 | List<PortDescription> pds = Arrays.asList( |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 441 | new DefaultPortDescription(P1, true, A2) |
| 442 | ); |
| 443 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 444 | putDevice(DID2, SW1); |
| 445 | |
| 446 | assertEquals(2, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 447 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 448 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1); |
| 449 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 450 | |
| 451 | DeviceEvent event = deviceStore.removeDevice(DID1); |
| 452 | assertEquals(DEVICE_REMOVED, event.type()); |
| 453 | assertDevice(DID1, SW1, event.subject()); |
| 454 | |
| 455 | assertEquals(1, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 456 | assertEquals(0, deviceStore.getPorts(DID1).size()); |
| 457 | |
| 458 | // putBack Device, Port w/o annotation |
| 459 | putDevice(DID1, SW1); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 460 | List<PortDescription> pds2 = Arrays.asList( |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 461 | new DefaultPortDescription(P1, true) |
| 462 | ); |
| 463 | deviceStore.updatePorts(PID, DID1, pds2); |
| 464 | |
| 465 | // annotations should not survive |
| 466 | assertEquals(2, deviceStore.getDeviceCount()); |
| 467 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 468 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations()); |
| 469 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations()); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // If Delegates should be called only on remote events, |
| 473 | // then Simple* should never call them, thus not test required. |
| 474 | // TODO add test for Port events when we have them |
| 475 | @Ignore("Ignore until Delegate spec. is clear.") |
| 476 | @Test |
| 477 | public final void testEvents() throws InterruptedException { |
| 478 | final CountDownLatch addLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 479 | DeviceStoreDelegate checkAdd = event -> { |
| 480 | assertEquals(DEVICE_ADDED, event.type()); |
| 481 | assertDevice(DID1, SW1, event.subject()); |
| 482 | addLatch.countDown(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 483 | }; |
| 484 | final CountDownLatch updateLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 485 | DeviceStoreDelegate checkUpdate = event -> { |
| 486 | assertEquals(DEVICE_UPDATED, event.type()); |
| 487 | assertDevice(DID1, SW2, event.subject()); |
| 488 | updateLatch.countDown(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 489 | }; |
| 490 | final CountDownLatch removeLatch = new CountDownLatch(1); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 491 | DeviceStoreDelegate checkRemove = event -> { |
| 492 | assertEquals(DEVICE_REMOVED, event.type()); |
| 493 | assertDevice(DID1, SW2, event.subject()); |
| 494 | removeLatch.countDown(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | DeviceDescription description = |
| 498 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 499 | HW, SW1, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 500 | deviceStore.setDelegate(checkAdd); |
| 501 | deviceStore.createOrUpdateDevice(PID, DID1, description); |
| 502 | assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS)); |
| 503 | |
| 504 | |
| 505 | DeviceDescription description2 = |
| 506 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 507 | HW, SW2, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 508 | deviceStore.unsetDelegate(checkAdd); |
| 509 | deviceStore.setDelegate(checkUpdate); |
| 510 | deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 511 | assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS)); |
| 512 | |
| 513 | deviceStore.unsetDelegate(checkUpdate); |
| 514 | deviceStore.setDelegate(checkRemove); |
| 515 | deviceStore.removeDevice(DID1); |
| 516 | assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS)); |
| 517 | } |
| 518 | } |