Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 17 | /** |
| 18 | * |
| 19 | */ |
tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 20 | package org.onlab.onos.store.trivial.impl; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 21 | |
| 22 | import static org.junit.Assert.*; |
| 23 | import static org.onlab.onos.net.Device.Type.SWITCH; |
| 24 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 25 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 26 | |
| 27 | import java.util.Arrays; |
| 28 | import java.util.HashMap; |
| 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | import java.util.Set; |
| 32 | import java.util.concurrent.CountDownLatch; |
| 33 | import java.util.concurrent.TimeUnit; |
| 34 | |
| 35 | import org.junit.After; |
| 36 | import org.junit.AfterClass; |
| 37 | import org.junit.Before; |
| 38 | import org.junit.BeforeClass; |
| 39 | import org.junit.Ignore; |
| 40 | import org.junit.Test; |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.Annotations; |
| 42 | import org.onlab.onos.net.DefaultAnnotations; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.Device; |
| 44 | import org.onlab.onos.net.DeviceId; |
| 45 | import org.onlab.onos.net.Port; |
| 46 | import org.onlab.onos.net.PortNumber; |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.SparseAnnotations; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
| 49 | import org.onlab.onos.net.device.DefaultPortDescription; |
| 50 | import org.onlab.onos.net.device.DeviceDescription; |
| 51 | import org.onlab.onos.net.device.DeviceEvent; |
| 52 | import org.onlab.onos.net.device.DeviceStore; |
| 53 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
| 54 | import org.onlab.onos.net.device.PortDescription; |
| 55 | import org.onlab.onos.net.provider.ProviderId; |
| 56 | |
| 57 | import com.google.common.collect.Iterables; |
| 58 | import com.google.common.collect.Sets; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 59 | import org.onlab.packet.ChassisId; |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Test of the simple DeviceStore implementation. |
| 63 | */ |
| 64 | public class SimpleDeviceStoreTest { |
| 65 | |
| 66 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 67 | private static final ProviderId PIDA = new ProviderId("of", "bar", true); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 68 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 69 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 70 | private static final String MFR = "whitebox"; |
| 71 | private static final String HW = "1.1.x"; |
| 72 | private static final String SW1 = "3.8.1"; |
| 73 | private static final String SW2 = "3.9.5"; |
| 74 | private static final String SN = "43311-12345"; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 75 | private static final ChassisId CID = new ChassisId(); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 76 | |
| 77 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 78 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 79 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 80 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 81 | private static final SparseAnnotations A1 = DefaultAnnotations.builder() |
| 82 | .set("A1", "a1") |
| 83 | .set("B1", "b1") |
| 84 | .build(); |
| 85 | private static final SparseAnnotations A1_2 = DefaultAnnotations.builder() |
| 86 | .remove("A1") |
| 87 | .set("B3", "b3") |
| 88 | .build(); |
| 89 | private static final SparseAnnotations A2 = DefaultAnnotations.builder() |
| 90 | .set("A2", "a2") |
| 91 | .set("B2", "b2") |
| 92 | .build(); |
| 93 | private static final SparseAnnotations A2_2 = DefaultAnnotations.builder() |
| 94 | .remove("A2") |
| 95 | .set("B4", "b4") |
| 96 | .build(); |
| 97 | |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 98 | private SimpleDeviceStore simpleDeviceStore; |
| 99 | private DeviceStore deviceStore; |
| 100 | |
| 101 | |
| 102 | |
| 103 | @BeforeClass |
| 104 | public static void setUpBeforeClass() throws Exception { |
| 105 | } |
| 106 | |
| 107 | @AfterClass |
| 108 | public static void tearDownAfterClass() throws Exception { |
| 109 | } |
| 110 | |
| 111 | |
| 112 | @Before |
| 113 | public void setUp() throws Exception { |
| 114 | simpleDeviceStore = new SimpleDeviceStore(); |
| 115 | simpleDeviceStore.activate(); |
| 116 | deviceStore = simpleDeviceStore; |
| 117 | } |
| 118 | |
| 119 | @After |
| 120 | public void tearDown() throws Exception { |
| 121 | simpleDeviceStore.deactivate(); |
| 122 | } |
| 123 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 124 | private void putDevice(DeviceId deviceId, String swVersion, |
| 125 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 126 | DeviceDescription description = |
| 127 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 128 | HW, swVersion, SN, CID, annotations); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 129 | deviceStore.createOrUpdateDevice(PID, deviceId, description); |
| 130 | } |
| 131 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 132 | private void putDeviceAncillary(DeviceId deviceId, String swVersion, |
| 133 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 134 | DeviceDescription description = |
| 135 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 136 | HW, swVersion, SN, CID, annotations); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 137 | deviceStore.createOrUpdateDevice(PIDA, deviceId, description); |
| 138 | } |
| 139 | |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 140 | private static void assertDevice(DeviceId id, String swVersion, Device device) { |
| 141 | assertNotNull(device); |
| 142 | assertEquals(id, device.id()); |
| 143 | assertEquals(MFR, device.manufacturer()); |
| 144 | assertEquals(HW, device.hwVersion()); |
| 145 | assertEquals(swVersion, device.swVersion()); |
| 146 | assertEquals(SN, device.serialNumber()); |
| 147 | } |
| 148 | |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 149 | // TODO slice this out somewhere |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Verifies that Annotations created by merging {@code annotations} is |
| 152 | * equal to actual Annotations. |
| 153 | * |
| 154 | * @param actual Annotations to check |
| 155 | * @param annotations |
| 156 | */ |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 157 | public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 158 | DefaultAnnotations expected = DefaultAnnotations.builder().build(); |
| 159 | for (SparseAnnotations a : annotations) { |
| 160 | expected = DefaultAnnotations.merge(expected, a); |
| 161 | } |
| 162 | assertEquals(expected.keys(), actual.keys()); |
| 163 | for (String key : expected.keys()) { |
| 164 | assertEquals(expected.value(key), actual.value(key)); |
| 165 | } |
| 166 | } |
| 167 | |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 168 | @Test |
| 169 | public final void testGetDeviceCount() { |
| 170 | assertEquals("initialy empty", 0, deviceStore.getDeviceCount()); |
| 171 | |
| 172 | putDevice(DID1, SW1); |
| 173 | putDevice(DID2, SW2); |
| 174 | putDevice(DID1, SW1); |
| 175 | |
| 176 | assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount()); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public final void testGetDevices() { |
| 181 | assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices())); |
| 182 | |
| 183 | putDevice(DID1, SW1); |
| 184 | putDevice(DID2, SW2); |
| 185 | putDevice(DID1, SW1); |
| 186 | |
| 187 | assertEquals("expect 2 uniq devices", |
| 188 | 2, Iterables.size(deviceStore.getDevices())); |
| 189 | |
| 190 | Map<DeviceId, Device> devices = new HashMap<>(); |
| 191 | for (Device device : deviceStore.getDevices()) { |
| 192 | devices.put(device.id(), device); |
| 193 | } |
| 194 | |
| 195 | assertDevice(DID1, SW1, devices.get(DID1)); |
| 196 | assertDevice(DID2, SW2, devices.get(DID2)); |
| 197 | |
| 198 | // add case for new node? |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public final void testGetDevice() { |
| 203 | |
| 204 | putDevice(DID1, SW1); |
| 205 | |
| 206 | assertDevice(DID1, SW1, deviceStore.getDevice(DID1)); |
| 207 | assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2)); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public final void testCreateOrUpdateDevice() { |
| 212 | DeviceDescription description = |
| 213 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 214 | HW, SW1, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 215 | DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description); |
| 216 | assertEquals(DEVICE_ADDED, event.type()); |
| 217 | assertDevice(DID1, SW1, event.subject()); |
| 218 | |
| 219 | DeviceDescription description2 = |
| 220 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 221 | HW, SW2, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 222 | DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 223 | assertEquals(DEVICE_UPDATED, event2.type()); |
| 224 | assertDevice(DID1, SW2, event2.subject()); |
| 225 | |
| 226 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); |
| 227 | } |
| 228 | |
| 229 | @Test |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 230 | public final void testCreateOrUpdateDeviceAncillary() { |
| 231 | DeviceDescription description = |
| 232 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 233 | HW, SW1, SN, CID, A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 234 | DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description); |
| 235 | assertEquals(DEVICE_ADDED, event.type()); |
| 236 | assertDevice(DID1, SW1, event.subject()); |
| 237 | assertEquals(PIDA, event.subject().providerId()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 238 | assertAnnotationsEquals(event.subject().annotations(), A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 239 | assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1)); |
| 240 | |
| 241 | DeviceDescription description2 = |
| 242 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 243 | HW, SW2, SN, CID, A1); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 244 | DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 245 | assertEquals(DEVICE_UPDATED, event2.type()); |
| 246 | assertDevice(DID1, SW2, event2.subject()); |
| 247 | assertEquals(PID, event2.subject().providerId()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 248 | assertAnnotationsEquals(event2.subject().annotations(), A1, A2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 249 | assertTrue(deviceStore.isAvailable(DID1)); |
| 250 | |
| 251 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); |
| 252 | |
| 253 | // For now, Ancillary is ignored once primary appears |
| 254 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description)); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 255 | |
| 256 | // But, Ancillary annotations will be in effect |
| 257 | DeviceDescription description3 = |
| 258 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 259 | HW, SW1, SN, CID, A2_2); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 260 | DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3); |
| 261 | assertEquals(DEVICE_UPDATED, event3.type()); |
| 262 | // basic information will be the one from Primary |
| 263 | assertDevice(DID1, SW2, event3.subject()); |
| 264 | assertEquals(PID, event3.subject().providerId()); |
| 265 | // but annotation from Ancillary will be merged |
| 266 | assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2); |
| 267 | assertTrue(deviceStore.isAvailable(DID1)); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | @Test |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 272 | public final void testMarkOffline() { |
| 273 | |
| 274 | putDevice(DID1, SW1); |
| 275 | assertTrue(deviceStore.isAvailable(DID1)); |
| 276 | |
| 277 | DeviceEvent event = deviceStore.markOffline(DID1); |
| 278 | assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type()); |
| 279 | assertDevice(DID1, SW1, event.subject()); |
| 280 | assertFalse(deviceStore.isAvailable(DID1)); |
| 281 | |
| 282 | DeviceEvent event2 = deviceStore.markOffline(DID1); |
| 283 | assertNull("No change, no event", event2); |
| 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public final void testUpdatePorts() { |
| 288 | putDevice(DID1, SW1); |
| 289 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 290 | new DefaultPortDescription(P1, true), |
| 291 | new DefaultPortDescription(P2, true) |
| 292 | ); |
| 293 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 294 | List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 295 | |
| 296 | Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2); |
| 297 | for (DeviceEvent event : events) { |
| 298 | assertEquals(PORT_ADDED, event.type()); |
| 299 | assertDevice(DID1, SW1, event.subject()); |
| 300 | assertTrue("PortNumber is one of expected", |
| 301 | expectedPorts.remove(event.port().number())); |
| 302 | assertTrue("Port is enabled", event.port().isEnabled()); |
| 303 | } |
| 304 | assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty()); |
| 305 | |
| 306 | |
| 307 | List<PortDescription> pds2 = Arrays.<PortDescription>asList( |
| 308 | new DefaultPortDescription(P1, false), |
| 309 | new DefaultPortDescription(P2, true), |
| 310 | new DefaultPortDescription(P3, true) |
| 311 | ); |
| 312 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 313 | events = deviceStore.updatePorts(PID, DID1, pds2); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 314 | assertFalse("event should be triggered", events.isEmpty()); |
| 315 | for (DeviceEvent event : events) { |
| 316 | PortNumber num = event.port().number(); |
| 317 | if (P1.equals(num)) { |
| 318 | assertEquals(PORT_UPDATED, event.type()); |
| 319 | assertDevice(DID1, SW1, event.subject()); |
| 320 | assertFalse("Port is disabled", event.port().isEnabled()); |
| 321 | } else if (P2.equals(num)) { |
| 322 | fail("P2 event not expected."); |
| 323 | } else if (P3.equals(num)) { |
| 324 | assertEquals(PORT_ADDED, event.type()); |
| 325 | assertDevice(DID1, SW1, event.subject()); |
| 326 | assertTrue("Port is enabled", event.port().isEnabled()); |
| 327 | } else { |
| 328 | fail("Unknown port number encountered: " + num); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | List<PortDescription> pds3 = Arrays.<PortDescription>asList( |
| 333 | new DefaultPortDescription(P1, false), |
| 334 | new DefaultPortDescription(P2, true) |
| 335 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 336 | events = deviceStore.updatePorts(PID, DID1, pds3); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 337 | assertFalse("event should be triggered", events.isEmpty()); |
| 338 | for (DeviceEvent event : events) { |
| 339 | PortNumber num = event.port().number(); |
| 340 | if (P1.equals(num)) { |
| 341 | fail("P1 event not expected."); |
| 342 | } else if (P2.equals(num)) { |
| 343 | fail("P2 event not expected."); |
| 344 | } else if (P3.equals(num)) { |
| 345 | assertEquals(PORT_REMOVED, event.type()); |
| 346 | assertDevice(DID1, SW1, event.subject()); |
| 347 | assertTrue("Port was enabled", event.port().isEnabled()); |
| 348 | } else { |
| 349 | fail("Unknown port number encountered: " + num); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
| 355 | @Test |
| 356 | public final void testUpdatePortStatus() { |
| 357 | putDevice(DID1, SW1); |
| 358 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 359 | new DefaultPortDescription(P1, true) |
| 360 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 361 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 362 | |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 363 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 364 | new DefaultPortDescription(P1, false)); |
| 365 | assertEquals(PORT_UPDATED, event.type()); |
| 366 | assertDevice(DID1, SW1, event.subject()); |
| 367 | assertEquals(P1, event.port().number()); |
| 368 | assertFalse("Port is disabled", event.port().isEnabled()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 369 | |
| 370 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 371 | |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 372 | @Test |
| 373 | public final void testUpdatePortStatusAncillary() { |
| 374 | putDeviceAncillary(DID1, SW1); |
| 375 | putDevice(DID1, SW1); |
| 376 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 377 | new DefaultPortDescription(P1, true, A1) |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 378 | ); |
| 379 | deviceStore.updatePorts(PID, DID1, pds); |
| 380 | |
| 381 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 382 | new DefaultPortDescription(P1, false, A1_2)); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 383 | assertEquals(PORT_UPDATED, event.type()); |
| 384 | assertDevice(DID1, SW1, event.subject()); |
| 385 | assertEquals(P1, event.port().number()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 386 | assertAnnotationsEquals(event.port().annotations(), A1, A1_2); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 387 | assertFalse("Port is disabled", event.port().isEnabled()); |
| 388 | |
| 389 | DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1, |
| 390 | new DefaultPortDescription(P1, true)); |
| 391 | assertNull("Ancillary is ignored if primary exists", event2); |
| 392 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 393 | // but, Ancillary annotation update will be notified |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 394 | DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1, |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 395 | new DefaultPortDescription(P1, true, A2)); |
| 396 | assertEquals(PORT_UPDATED, event3.type()); |
Yuta HIGUCHI | 8e49379 | 2014-10-01 19:36:32 -0700 | [diff] [blame] | 397 | assertDevice(DID1, SW1, event3.subject()); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 398 | assertEquals(P1, event3.port().number()); |
| 399 | assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2); |
| 400 | assertFalse("Port is disabled", event3.port().isEnabled()); |
| 401 | |
| 402 | // port only reported from Ancillary will be notified as down |
| 403 | DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1, |
| 404 | new DefaultPortDescription(P2, true)); |
| 405 | assertEquals(PORT_ADDED, event4.type()); |
| 406 | assertDevice(DID1, SW1, event4.subject()); |
| 407 | assertEquals(P2, event4.port().number()); |
| 408 | assertAnnotationsEquals(event4.port().annotations()); |
| 409 | assertFalse("Port is disabled if not given from primary provider", |
| 410 | event4.port().isEnabled()); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | @Test |
| 414 | public final void testGetPorts() { |
| 415 | putDevice(DID1, SW1); |
| 416 | putDevice(DID2, SW1); |
| 417 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 418 | new DefaultPortDescription(P1, true), |
| 419 | new DefaultPortDescription(P2, true) |
| 420 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 421 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 422 | |
| 423 | Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2); |
| 424 | List<Port> ports = deviceStore.getPorts(DID1); |
| 425 | for (Port port : ports) { |
| 426 | assertTrue("Port is enabled", port.isEnabled()); |
| 427 | assertTrue("PortNumber is one of expected", |
| 428 | expectedPorts.remove(port.number())); |
| 429 | } |
| 430 | assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty()); |
| 431 | |
| 432 | |
| 433 | assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty()); |
| 434 | } |
| 435 | |
| 436 | @Test |
| 437 | public final void testGetPort() { |
| 438 | putDevice(DID1, SW1); |
| 439 | putDevice(DID2, SW1); |
| 440 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 441 | new DefaultPortDescription(P1, true), |
| 442 | new DefaultPortDescription(P2, false) |
| 443 | ); |
Yuta HIGUCHI | 5f6739c | 2014-10-01 14:04:01 -0700 | [diff] [blame] | 444 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 445 | |
| 446 | Port port1 = deviceStore.getPort(DID1, P1); |
| 447 | assertEquals(P1, port1.number()); |
| 448 | assertTrue("Port is enabled", port1.isEnabled()); |
| 449 | |
| 450 | Port port2 = deviceStore.getPort(DID1, P2); |
| 451 | assertEquals(P2, port2.number()); |
| 452 | assertFalse("Port is disabled", port2.isEnabled()); |
| 453 | |
| 454 | Port port3 = deviceStore.getPort(DID1, P3); |
| 455 | assertNull("P3 not expected", port3); |
| 456 | } |
| 457 | |
| 458 | @Test |
| 459 | public final void testRemoveDevice() { |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 460 | putDevice(DID1, SW1, A1); |
| 461 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 462 | new DefaultPortDescription(P1, true, A2) |
| 463 | ); |
| 464 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 465 | putDevice(DID2, SW1); |
| 466 | |
| 467 | assertEquals(2, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 468 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 469 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1); |
| 470 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 471 | |
| 472 | DeviceEvent event = deviceStore.removeDevice(DID1); |
| 473 | assertEquals(DEVICE_REMOVED, event.type()); |
| 474 | assertDevice(DID1, SW1, event.subject()); |
| 475 | |
| 476 | assertEquals(1, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 477 | assertEquals(0, deviceStore.getPorts(DID1).size()); |
| 478 | |
| 479 | // putBack Device, Port w/o annotation |
| 480 | putDevice(DID1, SW1); |
| 481 | List<PortDescription> pds2 = Arrays.<PortDescription>asList( |
| 482 | new DefaultPortDescription(P1, true) |
| 483 | ); |
| 484 | deviceStore.updatePorts(PID, DID1, pds2); |
| 485 | |
| 486 | // annotations should not survive |
| 487 | assertEquals(2, deviceStore.getDeviceCount()); |
| 488 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 489 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations()); |
| 490 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations()); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // If Delegates should be called only on remote events, |
| 494 | // then Simple* should never call them, thus not test required. |
| 495 | // TODO add test for Port events when we have them |
| 496 | @Ignore("Ignore until Delegate spec. is clear.") |
| 497 | @Test |
| 498 | public final void testEvents() throws InterruptedException { |
| 499 | final CountDownLatch addLatch = new CountDownLatch(1); |
| 500 | DeviceStoreDelegate checkAdd = new DeviceStoreDelegate() { |
| 501 | @Override |
| 502 | public void notify(DeviceEvent event) { |
| 503 | assertEquals(DEVICE_ADDED, event.type()); |
| 504 | assertDevice(DID1, SW1, event.subject()); |
| 505 | addLatch.countDown(); |
| 506 | } |
| 507 | }; |
| 508 | final CountDownLatch updateLatch = new CountDownLatch(1); |
| 509 | DeviceStoreDelegate checkUpdate = new DeviceStoreDelegate() { |
| 510 | @Override |
| 511 | public void notify(DeviceEvent event) { |
| 512 | assertEquals(DEVICE_UPDATED, event.type()); |
| 513 | assertDevice(DID1, SW2, event.subject()); |
| 514 | updateLatch.countDown(); |
| 515 | } |
| 516 | }; |
| 517 | final CountDownLatch removeLatch = new CountDownLatch(1); |
| 518 | DeviceStoreDelegate checkRemove = new DeviceStoreDelegate() { |
| 519 | @Override |
| 520 | public void notify(DeviceEvent event) { |
| 521 | assertEquals(DEVICE_REMOVED, event.type()); |
| 522 | assertDevice(DID1, SW2, event.subject()); |
| 523 | removeLatch.countDown(); |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | DeviceDescription description = |
| 528 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 529 | HW, SW1, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 530 | deviceStore.setDelegate(checkAdd); |
| 531 | deviceStore.createOrUpdateDevice(PID, DID1, description); |
| 532 | assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS)); |
| 533 | |
| 534 | |
| 535 | DeviceDescription description2 = |
| 536 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 537 | HW, SW2, SN, CID); |
Yuta HIGUCHI | f5712ff | 2014-09-27 23:52:37 -0700 | [diff] [blame] | 538 | deviceStore.unsetDelegate(checkAdd); |
| 539 | deviceStore.setDelegate(checkUpdate); |
| 540 | deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 541 | assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS)); |
| 542 | |
| 543 | deviceStore.unsetDelegate(checkUpdate); |
| 544 | deviceStore.setDelegate(checkRemove); |
| 545 | deviceStore.removeDevice(DID1); |
| 546 | assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS)); |
| 547 | } |
| 548 | } |