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 | */ |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.device.impl; |
| 17 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 18 | import static org.easymock.EasyMock.*; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.*; |
| 20 | import static org.onlab.onos.net.Device.Type.SWITCH; |
| 21 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 22 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 23 | import static org.onlab.onos.cluster.ControllerNode.State.*; |
| 24 | import static org.onlab.onos.net.DefaultAnnotations.union; |
| 25 | import static java.util.Arrays.asList; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 26 | |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 27 | import java.io.IOException; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 28 | import java.util.Arrays; |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 29 | import java.util.Collections; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | import java.util.concurrent.CountDownLatch; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 37 | import org.easymock.Capture; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 38 | import org.junit.After; |
| 39 | import org.junit.AfterClass; |
| 40 | import org.junit.Before; |
| 41 | import org.junit.BeforeClass; |
| 42 | import org.junit.Ignore; |
| 43 | import org.junit.Test; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 44 | import org.onlab.onos.cluster.ClusterEventListener; |
| 45 | import org.onlab.onos.cluster.ClusterService; |
| 46 | import org.onlab.onos.cluster.ControllerNode; |
| 47 | import org.onlab.onos.cluster.ControllerNode.State; |
| 48 | import org.onlab.onos.cluster.DefaultControllerNode; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 49 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 50 | import org.onlab.onos.mastership.MastershipServiceAdapter; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 51 | import org.onlab.onos.mastership.MastershipTerm; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.Annotations; |
| 53 | import org.onlab.onos.net.DefaultAnnotations; |
| 54 | import org.onlab.onos.net.Device; |
| 55 | import org.onlab.onos.net.DeviceId; |
| 56 | import org.onlab.onos.net.Port; |
| 57 | import org.onlab.onos.net.PortNumber; |
| 58 | import org.onlab.onos.net.SparseAnnotations; |
| 59 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
| 60 | import org.onlab.onos.net.device.DefaultPortDescription; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 61 | import org.onlab.onos.net.device.DeviceClockService; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 62 | import org.onlab.onos.net.device.DeviceDescription; |
| 63 | import org.onlab.onos.net.device.DeviceEvent; |
| 64 | import org.onlab.onos.net.device.DeviceStore; |
| 65 | import org.onlab.onos.net.device.DeviceStoreDelegate; |
| 66 | import org.onlab.onos.net.device.PortDescription; |
| 67 | import org.onlab.onos.net.provider.ProviderId; |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 68 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 69 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
| 70 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
| 71 | import org.onlab.onos.store.cluster.messaging.MessageSubject; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 72 | import org.onlab.packet.ChassisId; |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 73 | import org.onlab.packet.IpAddress; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 74 | |
| 75 | import com.google.common.collect.Iterables; |
| 76 | import com.google.common.collect.Sets; |
| 77 | |
| 78 | |
| 79 | // TODO add tests for remote replication |
| 80 | /** |
| 81 | * Test of the gossip based distributed DeviceStore implementation. |
| 82 | */ |
| 83 | public class GossipDeviceStoreTest { |
| 84 | |
| 85 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 86 | private static final ProviderId PIDA = new ProviderId("of", "bar", true); |
| 87 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 88 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 89 | private static final String MFR = "whitebox"; |
| 90 | private static final String HW = "1.1.x"; |
| 91 | private static final String SW1 = "3.8.1"; |
| 92 | private static final String SW2 = "3.9.5"; |
| 93 | private static final String SN = "43311-12345"; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 94 | private static final ChassisId CID = new ChassisId(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 95 | |
| 96 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 97 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 98 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 99 | |
| 100 | private static final SparseAnnotations A1 = DefaultAnnotations.builder() |
| 101 | .set("A1", "a1") |
| 102 | .set("B1", "b1") |
| 103 | .build(); |
| 104 | private static final SparseAnnotations A1_2 = DefaultAnnotations.builder() |
| 105 | .remove("A1") |
| 106 | .set("B3", "b3") |
| 107 | .build(); |
| 108 | private static final SparseAnnotations A2 = DefaultAnnotations.builder() |
| 109 | .set("A2", "a2") |
| 110 | .set("B2", "b2") |
| 111 | .build(); |
| 112 | private static final SparseAnnotations A2_2 = DefaultAnnotations.builder() |
| 113 | .remove("A2") |
| 114 | .set("B4", "b4") |
| 115 | .build(); |
| 116 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 117 | // local node |
| 118 | private static final NodeId NID1 = new NodeId("local"); |
| 119 | private static final ControllerNode ONOS1 = |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 120 | new DefaultControllerNode(NID1, IpAddress.valueOf("127.0.0.1")); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 121 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 122 | // remote node |
| 123 | private static final NodeId NID2 = new NodeId("remote"); |
| 124 | private static final ControllerNode ONOS2 = |
Pavlin Radoslavov | 444b519 | 2014-10-28 10:45:19 -0700 | [diff] [blame] | 125 | new DefaultControllerNode(NID2, IpAddress.valueOf("127.0.0.2")); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 126 | private static final List<SparseAnnotations> NO_ANNOTATION = Collections.<SparseAnnotations>emptyList(); |
| 127 | |
| 128 | |
| 129 | private TestGossipDeviceStore testGossipDeviceStore; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 130 | private GossipDeviceStore gossipDeviceStore; |
| 131 | private DeviceStore deviceStore; |
| 132 | |
| 133 | private DeviceClockManager deviceClockManager; |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 134 | private DeviceClockService deviceClockService; |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 135 | private ClusterCommunicationService clusterCommunicator; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 136 | @BeforeClass |
| 137 | public static void setUpBeforeClass() throws Exception { |
| 138 | } |
| 139 | |
| 140 | @AfterClass |
| 141 | public static void tearDownAfterClass() throws Exception { |
| 142 | } |
| 143 | |
| 144 | |
| 145 | @Before |
| 146 | public void setUp() throws Exception { |
| 147 | deviceClockManager = new DeviceClockManager(); |
| 148 | deviceClockManager.activate(); |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 149 | deviceClockService = deviceClockManager; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 150 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 151 | deviceClockManager.setMastershipTerm(DID1, MastershipTerm.of(NID1, 1)); |
| 152 | deviceClockManager.setMastershipTerm(DID2, MastershipTerm.of(NID1, 2)); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 153 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 154 | clusterCommunicator = createNiceMock(ClusterCommunicationService.class); |
| 155 | clusterCommunicator.addSubscriber(anyObject(MessageSubject.class), |
| 156 | anyObject(ClusterMessageHandler.class)); |
| 157 | expectLastCall().anyTimes(); |
| 158 | replay(clusterCommunicator); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 159 | ClusterService clusterService = new TestClusterService(); |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 160 | |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 161 | testGossipDeviceStore = new TestGossipDeviceStore(deviceClockService, clusterService, clusterCommunicator); |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 162 | testGossipDeviceStore.mastershipService = new TestMastershipService(); |
| 163 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 164 | gossipDeviceStore = testGossipDeviceStore; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 165 | gossipDeviceStore.activate(); |
| 166 | deviceStore = gossipDeviceStore; |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 167 | verify(clusterCommunicator); |
| 168 | reset(clusterCommunicator); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @After |
| 172 | public void tearDown() throws Exception { |
| 173 | gossipDeviceStore.deactivate(); |
| 174 | deviceClockManager.deactivate(); |
| 175 | } |
| 176 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 177 | private void putDevice(DeviceId deviceId, String swVersion, |
| 178 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 179 | DeviceDescription description = |
| 180 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 181 | HW, swVersion, SN, CID, annotations); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 182 | reset(clusterCommunicator); |
| 183 | try { |
| 184 | expect(clusterCommunicator.broadcast(anyObject(ClusterMessage.class))) |
| 185 | .andReturn(true).anyTimes(); |
| 186 | } catch (IOException e) { |
| 187 | fail("Should never reach here"); |
| 188 | } |
| 189 | replay(clusterCommunicator); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 190 | deviceStore.createOrUpdateDevice(PID, deviceId, description); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 191 | verify(clusterCommunicator); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 194 | private void putDeviceAncillary(DeviceId deviceId, String swVersion, |
| 195 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 196 | DeviceDescription description = |
| 197 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 198 | HW, swVersion, SN, CID, annotations); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 199 | deviceStore.createOrUpdateDevice(PIDA, deviceId, description); |
| 200 | } |
| 201 | |
| 202 | private static void assertDevice(DeviceId id, String swVersion, Device device) { |
| 203 | assertNotNull(device); |
| 204 | assertEquals(id, device.id()); |
| 205 | assertEquals(MFR, device.manufacturer()); |
| 206 | assertEquals(HW, device.hwVersion()); |
| 207 | assertEquals(swVersion, device.swVersion()); |
| 208 | assertEquals(SN, device.serialNumber()); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Verifies that Annotations created by merging {@code annotations} is |
| 213 | * equal to actual Annotations. |
| 214 | * |
| 215 | * @param actual Annotations to check |
| 216 | * @param annotations |
| 217 | */ |
| 218 | private static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 219 | SparseAnnotations expected = DefaultAnnotations.builder().build(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 220 | for (SparseAnnotations a : annotations) { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 221 | expected = DefaultAnnotations.union(expected, a); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 222 | } |
| 223 | assertEquals(expected.keys(), actual.keys()); |
| 224 | for (String key : expected.keys()) { |
| 225 | assertEquals(expected.value(key), actual.value(key)); |
| 226 | } |
| 227 | } |
| 228 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 229 | private static void assertDeviceDescriptionEquals(DeviceDescription expected, |
| 230 | DeviceDescription actual) { |
| 231 | if (expected == actual) { |
| 232 | return; |
| 233 | } |
| 234 | assertEquals(expected.deviceURI(), actual.deviceURI()); |
| 235 | assertEquals(expected.hwVersion(), actual.hwVersion()); |
| 236 | assertEquals(expected.manufacturer(), actual.manufacturer()); |
| 237 | assertEquals(expected.serialNumber(), actual.serialNumber()); |
| 238 | assertEquals(expected.swVersion(), actual.swVersion()); |
| 239 | |
| 240 | assertAnnotationsEquals(actual.annotations(), expected.annotations()); |
| 241 | } |
| 242 | |
| 243 | private static void assertDeviceDescriptionEquals(DeviceDescription expected, |
| 244 | List<SparseAnnotations> expectedAnnotations, |
| 245 | DeviceDescription actual) { |
| 246 | if (expected == actual) { |
| 247 | return; |
| 248 | } |
| 249 | assertEquals(expected.deviceURI(), actual.deviceURI()); |
| 250 | assertEquals(expected.hwVersion(), actual.hwVersion()); |
| 251 | assertEquals(expected.manufacturer(), actual.manufacturer()); |
| 252 | assertEquals(expected.serialNumber(), actual.serialNumber()); |
| 253 | assertEquals(expected.swVersion(), actual.swVersion()); |
| 254 | |
| 255 | assertAnnotationsEquals(actual.annotations(), |
| 256 | expectedAnnotations.toArray(new SparseAnnotations[0])); |
| 257 | } |
| 258 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 259 | @Test |
| 260 | public final void testGetDeviceCount() { |
| 261 | assertEquals("initialy empty", 0, deviceStore.getDeviceCount()); |
| 262 | |
| 263 | putDevice(DID1, SW1); |
| 264 | putDevice(DID2, SW2); |
| 265 | putDevice(DID1, SW1); |
| 266 | |
| 267 | assertEquals("expect 2 uniq devices", 2, deviceStore.getDeviceCount()); |
| 268 | } |
| 269 | |
| 270 | @Test |
| 271 | public final void testGetDevices() { |
| 272 | assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices())); |
| 273 | |
| 274 | putDevice(DID1, SW1); |
| 275 | putDevice(DID2, SW2); |
| 276 | putDevice(DID1, SW1); |
| 277 | |
| 278 | assertEquals("expect 2 uniq devices", |
| 279 | 2, Iterables.size(deviceStore.getDevices())); |
| 280 | |
| 281 | Map<DeviceId, Device> devices = new HashMap<>(); |
| 282 | for (Device device : deviceStore.getDevices()) { |
| 283 | devices.put(device.id(), device); |
| 284 | } |
| 285 | |
| 286 | assertDevice(DID1, SW1, devices.get(DID1)); |
| 287 | assertDevice(DID2, SW2, devices.get(DID2)); |
| 288 | |
| 289 | // add case for new node? |
| 290 | } |
| 291 | |
| 292 | @Test |
| 293 | public final void testGetDevice() { |
| 294 | |
| 295 | putDevice(DID1, SW1); |
| 296 | |
| 297 | assertDevice(DID1, SW1, deviceStore.getDevice(DID1)); |
| 298 | assertNull("DID2 shouldn't be there", deviceStore.getDevice(DID2)); |
| 299 | } |
| 300 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 301 | private void assertInternalDeviceEvent(NodeId sender, |
| 302 | DeviceId deviceId, |
| 303 | ProviderId providerId, |
| 304 | DeviceDescription expectedDesc, |
| 305 | Capture<ClusterMessage> actualMsg) { |
| 306 | assertTrue(actualMsg.hasCaptured()); |
| 307 | assertEquals(sender, actualMsg.getValue().sender()); |
| 308 | assertEquals(GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, |
| 309 | actualMsg.getValue().subject()); |
| 310 | InternalDeviceEvent addEvent |
| 311 | = testGossipDeviceStore.deserialize(actualMsg.getValue().payload()); |
| 312 | assertEquals(deviceId, addEvent.deviceId()); |
| 313 | assertEquals(providerId, addEvent.providerId()); |
| 314 | assertDeviceDescriptionEquals(expectedDesc, addEvent.deviceDescription().value()); |
| 315 | } |
| 316 | |
| 317 | private void assertInternalDeviceEvent(NodeId sender, |
| 318 | DeviceId deviceId, |
| 319 | ProviderId providerId, |
| 320 | DeviceDescription expectedDesc, |
| 321 | List<SparseAnnotations> expectedAnnotations, |
| 322 | Capture<ClusterMessage> actualMsg) { |
| 323 | assertTrue(actualMsg.hasCaptured()); |
| 324 | assertEquals(sender, actualMsg.getValue().sender()); |
| 325 | assertEquals(GossipDeviceStoreMessageSubjects.DEVICE_UPDATE, |
| 326 | actualMsg.getValue().subject()); |
| 327 | InternalDeviceEvent addEvent |
| 328 | = testGossipDeviceStore.deserialize(actualMsg.getValue().payload()); |
| 329 | assertEquals(deviceId, addEvent.deviceId()); |
| 330 | assertEquals(providerId, addEvent.providerId()); |
| 331 | assertDeviceDescriptionEquals(expectedDesc, expectedAnnotations, addEvent.deviceDescription().value()); |
| 332 | } |
| 333 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 334 | @Test |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 335 | public final void testCreateOrUpdateDevice() throws IOException { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 336 | DeviceDescription description = |
| 337 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 338 | HW, SW1, SN, CID); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 339 | Capture<ClusterMessage> bcast = new Capture<>(); |
| 340 | |
| 341 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 342 | DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description); |
| 343 | assertEquals(DEVICE_ADDED, event.type()); |
| 344 | assertDevice(DID1, SW1, event.subject()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 345 | verify(clusterCommunicator); |
| 346 | assertInternalDeviceEvent(NID1, DID1, PID, description, bcast); |
| 347 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 348 | |
| 349 | DeviceDescription description2 = |
| 350 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 351 | HW, SW2, SN, CID); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 352 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 353 | DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 354 | assertEquals(DEVICE_UPDATED, event2.type()); |
| 355 | assertDevice(DID1, SW2, event2.subject()); |
| 356 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 357 | verify(clusterCommunicator); |
| 358 | assertInternalDeviceEvent(NID1, DID1, PID, description2, bcast); |
| 359 | reset(clusterCommunicator); |
| 360 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 361 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); |
| 362 | } |
| 363 | |
| 364 | @Test |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 365 | public final void testCreateOrUpdateDeviceAncillary() throws IOException { |
| 366 | // add |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 367 | DeviceDescription description = |
| 368 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 369 | HW, SW1, SN, CID, A2); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 370 | Capture<ClusterMessage> bcast = new Capture<>(); |
| 371 | |
| 372 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 373 | DeviceEvent event = deviceStore.createOrUpdateDevice(PIDA, DID1, description); |
| 374 | assertEquals(DEVICE_ADDED, event.type()); |
| 375 | assertDevice(DID1, SW1, event.subject()); |
| 376 | assertEquals(PIDA, event.subject().providerId()); |
| 377 | assertAnnotationsEquals(event.subject().annotations(), A2); |
| 378 | assertFalse("Ancillary will not bring device up", deviceStore.isAvailable(DID1)); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 379 | verify(clusterCommunicator); |
| 380 | assertInternalDeviceEvent(NID1, DID1, PIDA, description, bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 381 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 382 | // update from primary |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 383 | DeviceDescription description2 = |
| 384 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 385 | HW, SW2, SN, CID, A1); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 386 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 387 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 388 | DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 389 | assertEquals(DEVICE_UPDATED, event2.type()); |
| 390 | assertDevice(DID1, SW2, event2.subject()); |
| 391 | assertEquals(PID, event2.subject().providerId()); |
| 392 | assertAnnotationsEquals(event2.subject().annotations(), A1, A2); |
| 393 | assertTrue(deviceStore.isAvailable(DID1)); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 394 | verify(clusterCommunicator); |
| 395 | assertInternalDeviceEvent(NID1, DID1, PID, description2, bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 396 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 397 | // no-op update from primary |
| 398 | resetCommunicatorExpectingNoBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 399 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); |
| 400 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 401 | verify(clusterCommunicator); |
| 402 | assertFalse("no broadcast expected", bcast.hasCaptured()); |
| 403 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 404 | // For now, Ancillary is ignored once primary appears |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 405 | resetCommunicatorExpectingNoBroadcast(bcast); |
| 406 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 407 | assertNull("No change expected", deviceStore.createOrUpdateDevice(PIDA, DID1, description)); |
| 408 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 409 | verify(clusterCommunicator); |
| 410 | assertFalse("no broadcast expected", bcast.hasCaptured()); |
| 411 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 412 | // But, Ancillary annotations will be in effect |
| 413 | DeviceDescription description3 = |
| 414 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 415 | HW, SW1, SN, CID, A2_2); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 416 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 417 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 418 | DeviceEvent event3 = deviceStore.createOrUpdateDevice(PIDA, DID1, description3); |
| 419 | assertEquals(DEVICE_UPDATED, event3.type()); |
| 420 | // basic information will be the one from Primary |
| 421 | assertDevice(DID1, SW2, event3.subject()); |
| 422 | assertEquals(PID, event3.subject().providerId()); |
| 423 | // but annotation from Ancillary will be merged |
| 424 | assertAnnotationsEquals(event3.subject().annotations(), A1, A2, A2_2); |
| 425 | assertTrue(deviceStore.isAvailable(DID1)); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 426 | verify(clusterCommunicator); |
| 427 | // note: only annotation from PIDA is sent over the wire |
| 428 | assertInternalDeviceEvent(NID1, DID1, PIDA, description3, |
| 429 | asList(union(A2, A2_2)), bcast); |
| 430 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
| 434 | @Test |
| 435 | public final void testMarkOffline() { |
| 436 | |
| 437 | putDevice(DID1, SW1); |
| 438 | assertTrue(deviceStore.isAvailable(DID1)); |
| 439 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 440 | Capture<ClusterMessage> bcast = new Capture<>(); |
| 441 | |
| 442 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 443 | DeviceEvent event = deviceStore.markOffline(DID1); |
| 444 | assertEquals(DEVICE_AVAILABILITY_CHANGED, event.type()); |
| 445 | assertDevice(DID1, SW1, event.subject()); |
| 446 | assertFalse(deviceStore.isAvailable(DID1)); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 447 | verify(clusterCommunicator); |
| 448 | // TODO: verify broadcast message |
| 449 | assertTrue(bcast.hasCaptured()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 450 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 451 | |
| 452 | resetCommunicatorExpectingNoBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 453 | DeviceEvent event2 = deviceStore.markOffline(DID1); |
| 454 | assertNull("No change, no event", event2); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 455 | verify(clusterCommunicator); |
| 456 | assertFalse(bcast.hasCaptured()); |
| 457 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 458 | |
| 459 | @Test |
| 460 | public final void testUpdatePorts() { |
| 461 | putDevice(DID1, SW1); |
| 462 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 463 | new DefaultPortDescription(P1, true), |
| 464 | new DefaultPortDescription(P2, true) |
| 465 | ); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 466 | Capture<ClusterMessage> bcast = new Capture<>(); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 467 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 468 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 469 | List<DeviceEvent> events = deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 470 | verify(clusterCommunicator); |
| 471 | // TODO: verify broadcast message |
| 472 | assertTrue(bcast.hasCaptured()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 473 | |
| 474 | Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2); |
| 475 | for (DeviceEvent event : events) { |
| 476 | assertEquals(PORT_ADDED, event.type()); |
| 477 | assertDevice(DID1, SW1, event.subject()); |
| 478 | assertTrue("PortNumber is one of expected", |
| 479 | expectedPorts.remove(event.port().number())); |
| 480 | assertTrue("Port is enabled", event.port().isEnabled()); |
| 481 | } |
| 482 | assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty()); |
| 483 | |
| 484 | |
| 485 | List<PortDescription> pds2 = Arrays.<PortDescription>asList( |
| 486 | new DefaultPortDescription(P1, false), |
| 487 | new DefaultPortDescription(P2, true), |
| 488 | new DefaultPortDescription(P3, true) |
| 489 | ); |
| 490 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 491 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 492 | events = deviceStore.updatePorts(PID, DID1, pds2); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 493 | verify(clusterCommunicator); |
| 494 | // TODO: verify broadcast message |
| 495 | assertTrue(bcast.hasCaptured()); |
| 496 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 497 | assertFalse("event should be triggered", events.isEmpty()); |
| 498 | for (DeviceEvent event : events) { |
| 499 | PortNumber num = event.port().number(); |
| 500 | if (P1.equals(num)) { |
| 501 | assertEquals(PORT_UPDATED, event.type()); |
| 502 | assertDevice(DID1, SW1, event.subject()); |
| 503 | assertFalse("Port is disabled", event.port().isEnabled()); |
| 504 | } else if (P2.equals(num)) { |
| 505 | fail("P2 event not expected."); |
| 506 | } else if (P3.equals(num)) { |
| 507 | assertEquals(PORT_ADDED, event.type()); |
| 508 | assertDevice(DID1, SW1, event.subject()); |
| 509 | assertTrue("Port is enabled", event.port().isEnabled()); |
| 510 | } else { |
| 511 | fail("Unknown port number encountered: " + num); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | List<PortDescription> pds3 = Arrays.<PortDescription>asList( |
| 516 | new DefaultPortDescription(P1, false), |
| 517 | new DefaultPortDescription(P2, true) |
| 518 | ); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 519 | resetCommunicatorExpectingSingleBroadcast(bcast); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 520 | events = deviceStore.updatePorts(PID, DID1, pds3); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 521 | verify(clusterCommunicator); |
| 522 | // TODO: verify broadcast message |
| 523 | assertTrue(bcast.hasCaptured()); |
| 524 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 525 | assertFalse("event should be triggered", events.isEmpty()); |
| 526 | for (DeviceEvent event : events) { |
| 527 | PortNumber num = event.port().number(); |
| 528 | if (P1.equals(num)) { |
| 529 | fail("P1 event not expected."); |
| 530 | } else if (P2.equals(num)) { |
| 531 | fail("P2 event not expected."); |
| 532 | } else if (P3.equals(num)) { |
| 533 | assertEquals(PORT_REMOVED, event.type()); |
| 534 | assertDevice(DID1, SW1, event.subject()); |
| 535 | assertTrue("Port was enabled", event.port().isEnabled()); |
| 536 | } else { |
| 537 | fail("Unknown port number encountered: " + num); |
| 538 | } |
| 539 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | @Test |
| 543 | public final void testUpdatePortStatus() { |
| 544 | putDevice(DID1, SW1); |
| 545 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 546 | new DefaultPortDescription(P1, true) |
| 547 | ); |
| 548 | deviceStore.updatePorts(PID, DID1, pds); |
| 549 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 550 | Capture<ClusterMessage> bcast = new Capture<>(); |
| 551 | |
| 552 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 553 | final DefaultPortDescription desc = new DefaultPortDescription(P1, false); |
| 554 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, desc); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 555 | assertEquals(PORT_UPDATED, event.type()); |
| 556 | assertDevice(DID1, SW1, event.subject()); |
| 557 | assertEquals(P1, event.port().number()); |
| 558 | assertFalse("Port is disabled", event.port().isEnabled()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 559 | verify(clusterCommunicator); |
| 560 | assertInternalPortStatusEvent(NID1, DID1, PID, desc, NO_ANNOTATION, bcast); |
| 561 | assertTrue(bcast.hasCaptured()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 562 | } |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 563 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 564 | @Test |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 565 | public final void testUpdatePortStatusAncillary() throws IOException { |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 566 | putDeviceAncillary(DID1, SW1); |
| 567 | putDevice(DID1, SW1); |
| 568 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 569 | new DefaultPortDescription(P1, true, A1) |
| 570 | ); |
| 571 | deviceStore.updatePorts(PID, DID1, pds); |
| 572 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 573 | Capture<ClusterMessage> bcast = new Capture<>(); |
| 574 | |
| 575 | |
| 576 | // update port from primary |
| 577 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 578 | final DefaultPortDescription desc1 = new DefaultPortDescription(P1, false, A1_2); |
| 579 | DeviceEvent event = deviceStore.updatePortStatus(PID, DID1, desc1); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 580 | assertEquals(PORT_UPDATED, event.type()); |
| 581 | assertDevice(DID1, SW1, event.subject()); |
| 582 | assertEquals(P1, event.port().number()); |
| 583 | assertAnnotationsEquals(event.port().annotations(), A1, A1_2); |
| 584 | assertFalse("Port is disabled", event.port().isEnabled()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 585 | verify(clusterCommunicator); |
| 586 | assertInternalPortStatusEvent(NID1, DID1, PID, desc1, asList(A1, A1_2), bcast); |
| 587 | assertTrue(bcast.hasCaptured()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 588 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 589 | // update port from ancillary with no attributes |
| 590 | resetCommunicatorExpectingNoBroadcast(bcast); |
| 591 | final DefaultPortDescription desc2 = new DefaultPortDescription(P1, true); |
| 592 | DeviceEvent event2 = deviceStore.updatePortStatus(PIDA, DID1, desc2); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 593 | assertNull("Ancillary is ignored if primary exists", event2); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 594 | verify(clusterCommunicator); |
| 595 | assertFalse(bcast.hasCaptured()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 596 | |
| 597 | // but, Ancillary annotation update will be notified |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 598 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 599 | final DefaultPortDescription desc3 = new DefaultPortDescription(P1, true, A2); |
| 600 | DeviceEvent event3 = deviceStore.updatePortStatus(PIDA, DID1, desc3); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 601 | assertEquals(PORT_UPDATED, event3.type()); |
| 602 | assertDevice(DID1, SW1, event3.subject()); |
| 603 | assertEquals(P1, event3.port().number()); |
| 604 | assertAnnotationsEquals(event3.port().annotations(), A1, A1_2, A2); |
| 605 | assertFalse("Port is disabled", event3.port().isEnabled()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 606 | verify(clusterCommunicator); |
| 607 | assertInternalPortStatusEvent(NID1, DID1, PIDA, desc3, asList(A2), bcast); |
| 608 | assertTrue(bcast.hasCaptured()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 609 | |
| 610 | // port only reported from Ancillary will be notified as down |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 611 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 612 | final DefaultPortDescription desc4 = new DefaultPortDescription(P2, true); |
| 613 | DeviceEvent event4 = deviceStore.updatePortStatus(PIDA, DID1, desc4); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 614 | assertEquals(PORT_ADDED, event4.type()); |
| 615 | assertDevice(DID1, SW1, event4.subject()); |
| 616 | assertEquals(P2, event4.port().number()); |
| 617 | assertAnnotationsEquals(event4.port().annotations()); |
| 618 | assertFalse("Port is disabled if not given from primary provider", |
| 619 | event4.port().isEnabled()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 620 | verify(clusterCommunicator); |
| 621 | // TODO: verify broadcast message content |
| 622 | assertInternalPortStatusEvent(NID1, DID1, PIDA, desc4, NO_ANNOTATION, bcast); |
| 623 | assertTrue(bcast.hasCaptured()); |
| 624 | } |
| 625 | |
| 626 | private void assertInternalPortStatusEvent(NodeId sender, DeviceId did, |
| 627 | ProviderId pid, DefaultPortDescription expectedDesc, |
| 628 | List<SparseAnnotations> expectedAnnotations, Capture<ClusterMessage> actualMsg) { |
| 629 | |
| 630 | assertTrue(actualMsg.hasCaptured()); |
| 631 | assertEquals(sender, actualMsg.getValue().sender()); |
| 632 | assertEquals(GossipDeviceStoreMessageSubjects.PORT_STATUS_UPDATE, |
| 633 | actualMsg.getValue().subject()); |
| 634 | InternalPortStatusEvent addEvent |
| 635 | = testGossipDeviceStore.deserialize(actualMsg.getValue().payload()); |
| 636 | assertEquals(did, addEvent.deviceId()); |
| 637 | assertEquals(pid, addEvent.providerId()); |
| 638 | assertPortDescriptionEquals(expectedDesc, expectedAnnotations, |
| 639 | addEvent.portDescription().value()); |
| 640 | |
| 641 | } |
| 642 | |
| 643 | private void assertPortDescriptionEquals( |
| 644 | PortDescription expectedDesc, |
| 645 | List<SparseAnnotations> expectedAnnotations, |
| 646 | PortDescription actual) { |
| 647 | |
| 648 | assertEquals(expectedDesc.portNumber(), actual.portNumber()); |
| 649 | assertEquals(expectedDesc.isEnabled(), actual.isEnabled()); |
| 650 | |
| 651 | assertAnnotationsEquals(actual.annotations(), |
| 652 | expectedAnnotations.toArray(new SparseAnnotations[0])); |
| 653 | } |
| 654 | |
| 655 | private void resetCommunicatorExpectingNoBroadcast( |
| 656 | Capture<ClusterMessage> bcast) { |
| 657 | bcast.reset(); |
| 658 | reset(clusterCommunicator); |
| 659 | replay(clusterCommunicator); |
| 660 | } |
| 661 | |
| 662 | private void resetCommunicatorExpectingSingleBroadcast( |
| 663 | Capture<ClusterMessage> bcast) { |
| 664 | |
| 665 | bcast.reset(); |
| 666 | reset(clusterCommunicator); |
| 667 | try { |
| 668 | expect(clusterCommunicator.broadcast(capture(bcast))).andReturn(true).once(); |
| 669 | } catch (IOException e) { |
| 670 | fail("Should never reach here"); |
| 671 | } |
| 672 | replay(clusterCommunicator); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | @Test |
| 676 | public final void testGetPorts() { |
| 677 | putDevice(DID1, SW1); |
| 678 | putDevice(DID2, SW1); |
| 679 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 680 | new DefaultPortDescription(P1, true), |
| 681 | new DefaultPortDescription(P2, true) |
| 682 | ); |
| 683 | deviceStore.updatePorts(PID, DID1, pds); |
| 684 | |
| 685 | Set<PortNumber> expectedPorts = Sets.newHashSet(P1, P2); |
| 686 | List<Port> ports = deviceStore.getPorts(DID1); |
| 687 | for (Port port : ports) { |
| 688 | assertTrue("Port is enabled", port.isEnabled()); |
| 689 | assertTrue("PortNumber is one of expected", |
| 690 | expectedPorts.remove(port.number())); |
| 691 | } |
| 692 | assertTrue("Event for all expectedport appeared", expectedPorts.isEmpty()); |
| 693 | |
| 694 | |
| 695 | assertTrue("DID2 has no ports", deviceStore.getPorts(DID2).isEmpty()); |
| 696 | } |
| 697 | |
| 698 | @Test |
| 699 | public final void testGetPort() { |
| 700 | putDevice(DID1, SW1); |
| 701 | putDevice(DID2, SW1); |
| 702 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 703 | new DefaultPortDescription(P1, true), |
| 704 | new DefaultPortDescription(P2, false) |
| 705 | ); |
| 706 | deviceStore.updatePorts(PID, DID1, pds); |
| 707 | |
| 708 | Port port1 = deviceStore.getPort(DID1, P1); |
| 709 | assertEquals(P1, port1.number()); |
| 710 | assertTrue("Port is enabled", port1.isEnabled()); |
| 711 | |
| 712 | Port port2 = deviceStore.getPort(DID1, P2); |
| 713 | assertEquals(P2, port2.number()); |
| 714 | assertFalse("Port is disabled", port2.isEnabled()); |
| 715 | |
| 716 | Port port3 = deviceStore.getPort(DID1, P3); |
| 717 | assertNull("P3 not expected", port3); |
| 718 | } |
| 719 | |
| 720 | @Test |
| 721 | public final void testRemoveDevice() { |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 722 | putDevice(DID1, SW1, A1); |
| 723 | List<PortDescription> pds = Arrays.<PortDescription>asList( |
| 724 | new DefaultPortDescription(P1, true, A2) |
| 725 | ); |
| 726 | deviceStore.updatePorts(PID, DID1, pds); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 727 | putDevice(DID2, SW1); |
| 728 | |
| 729 | assertEquals(2, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 730 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 731 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations(), A1); |
| 732 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations(), A2); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 733 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 734 | Capture<ClusterMessage> bcast = new Capture<>(); |
| 735 | |
| 736 | resetCommunicatorExpectingSingleBroadcast(bcast); |
| 737 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 738 | DeviceEvent event = deviceStore.removeDevice(DID1); |
| 739 | assertEquals(DEVICE_REMOVED, event.type()); |
| 740 | assertDevice(DID1, SW1, event.subject()); |
| 741 | |
| 742 | assertEquals(1, deviceStore.getDeviceCount()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 743 | assertEquals(0, deviceStore.getPorts(DID1).size()); |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 744 | verify(clusterCommunicator); |
| 745 | // TODO: verify broadcast message |
| 746 | assertTrue(bcast.hasCaptured()); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 747 | |
| 748 | // putBack Device, Port w/o annotation |
| 749 | putDevice(DID1, SW1); |
| 750 | List<PortDescription> pds2 = Arrays.<PortDescription>asList( |
| 751 | new DefaultPortDescription(P1, true) |
| 752 | ); |
| 753 | deviceStore.updatePorts(PID, DID1, pds2); |
| 754 | |
| 755 | // annotations should not survive |
| 756 | assertEquals(2, deviceStore.getDeviceCount()); |
| 757 | assertEquals(1, deviceStore.getPorts(DID1).size()); |
| 758 | assertAnnotationsEquals(deviceStore.getDevice(DID1).annotations()); |
| 759 | assertAnnotationsEquals(deviceStore.getPort(DID1, P1).annotations()); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | // If Delegates should be called only on remote events, |
| 763 | // then Simple* should never call them, thus not test required. |
| 764 | // TODO add test for Port events when we have them |
| 765 | @Ignore("Ignore until Delegate spec. is clear.") |
| 766 | @Test |
| 767 | public final void testEvents() throws InterruptedException { |
| 768 | final CountDownLatch addLatch = new CountDownLatch(1); |
| 769 | DeviceStoreDelegate checkAdd = new DeviceStoreDelegate() { |
| 770 | @Override |
| 771 | public void notify(DeviceEvent event) { |
| 772 | assertEquals(DEVICE_ADDED, event.type()); |
| 773 | assertDevice(DID1, SW1, event.subject()); |
| 774 | addLatch.countDown(); |
| 775 | } |
| 776 | }; |
| 777 | final CountDownLatch updateLatch = new CountDownLatch(1); |
| 778 | DeviceStoreDelegate checkUpdate = new DeviceStoreDelegate() { |
| 779 | @Override |
| 780 | public void notify(DeviceEvent event) { |
| 781 | assertEquals(DEVICE_UPDATED, event.type()); |
| 782 | assertDevice(DID1, SW2, event.subject()); |
| 783 | updateLatch.countDown(); |
| 784 | } |
| 785 | }; |
| 786 | final CountDownLatch removeLatch = new CountDownLatch(1); |
| 787 | DeviceStoreDelegate checkRemove = new DeviceStoreDelegate() { |
| 788 | @Override |
| 789 | public void notify(DeviceEvent event) { |
| 790 | assertEquals(DEVICE_REMOVED, event.type()); |
| 791 | assertDevice(DID1, SW2, event.subject()); |
| 792 | removeLatch.countDown(); |
| 793 | } |
| 794 | }; |
| 795 | |
| 796 | DeviceDescription description = |
| 797 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 798 | HW, SW1, SN, CID); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 799 | deviceStore.setDelegate(checkAdd); |
| 800 | deviceStore.createOrUpdateDevice(PID, DID1, description); |
| 801 | assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS)); |
| 802 | |
| 803 | |
| 804 | DeviceDescription description2 = |
| 805 | new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 806 | HW, SW2, SN, CID); |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 807 | deviceStore.unsetDelegate(checkAdd); |
| 808 | deviceStore.setDelegate(checkUpdate); |
| 809 | deviceStore.createOrUpdateDevice(PID, DID1, description2); |
| 810 | assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS)); |
| 811 | |
| 812 | deviceStore.unsetDelegate(checkUpdate); |
| 813 | deviceStore.setDelegate(checkRemove); |
| 814 | deviceStore.removeDevice(DID1); |
| 815 | assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS)); |
| 816 | } |
| 817 | |
Yuta HIGUCHI | e8252bb | 2014-10-22 09:41:01 -0700 | [diff] [blame] | 818 | private final class TestMastershipService extends MastershipServiceAdapter { |
| 819 | @Override |
| 820 | public NodeId getMasterFor(DeviceId deviceId) { |
| 821 | return NID1; |
| 822 | } |
| 823 | } |
| 824 | |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 825 | private static final class TestGossipDeviceStore extends GossipDeviceStore { |
| 826 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 827 | public TestGossipDeviceStore( |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 828 | DeviceClockService deviceClockService, |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 829 | ClusterService clusterService, |
| 830 | ClusterCommunicationService clusterCommunicator) { |
Yuta HIGUCHI | 093e83e | 2014-10-10 22:26:11 -0700 | [diff] [blame] | 831 | this.deviceClockService = deviceClockService; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 832 | this.clusterService = clusterService; |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 833 | this.clusterCommunicator = clusterCommunicator; |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 834 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 835 | |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 836 | public <T> T deserialize(byte[] bytes) { |
| 837 | return SERIALIZER.decode(bytes); |
| 838 | } |
Madan Jampani | 47c9373 | 2014-10-06 20:46:08 -0700 | [diff] [blame] | 839 | } |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 840 | |
| 841 | private static final class TestClusterService implements ClusterService { |
| 842 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 843 | private final Map<NodeId, ControllerNode> nodes = new HashMap<>(); |
| 844 | private final Map<NodeId, ControllerNode.State> nodeStates = new HashMap<>(); |
| 845 | |
| 846 | public TestClusterService() { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 847 | nodes.put(NID1, ONOS1); |
| 848 | nodeStates.put(NID1, ACTIVE); |
| 849 | |
| 850 | nodes.put(NID2, ONOS2); |
| 851 | nodeStates.put(NID2, ACTIVE); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | @Override |
| 855 | public ControllerNode getLocalNode() { |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 856 | return GossipDeviceStoreTest.ONOS1; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public Set<ControllerNode> getNodes() { |
| 861 | return Sets.newHashSet(nodes.values()); |
| 862 | } |
| 863 | |
| 864 | @Override |
| 865 | public ControllerNode getNode(NodeId nodeId) { |
| 866 | return nodes.get(nodeId); |
| 867 | } |
| 868 | |
| 869 | @Override |
| 870 | public State getState(NodeId nodeId) { |
| 871 | return nodeStates.get(nodeId); |
| 872 | } |
| 873 | |
| 874 | @Override |
| 875 | public void addListener(ClusterEventListener listener) { |
| 876 | } |
| 877 | |
| 878 | @Override |
| 879 | public void removeListener(ClusterEventListener listener) { |
| 880 | } |
| 881 | } |
Yuta HIGUCHI | 67a527f | 2014-10-02 22:23:54 -0700 | [diff] [blame] | 882 | } |