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