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