Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.device.impl; |
| 2 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 3 | import com.google.common.collect.Iterables; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 4 | import com.google.common.collect.Sets; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 5 | import com.hazelcast.config.Config; |
| 6 | import com.hazelcast.core.Hazelcast; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 7 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 8 | import org.junit.After; |
| 9 | import org.junit.Before; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 10 | import org.junit.Test; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 11 | import org.onlab.onos.cluster.DefaultControllerNode; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 12 | import org.onlab.onos.cluster.MastershipServiceAdapter; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 13 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 14 | import org.onlab.onos.event.Event; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 15 | import org.onlab.onos.event.EventDeliveryService; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 16 | import org.onlab.onos.event.impl.TestEventDispatcher; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.Device; |
| 18 | import org.onlab.onos.net.DeviceId; |
| 19 | import org.onlab.onos.net.MastershipRole; |
| 20 | import org.onlab.onos.net.Port; |
| 21 | import org.onlab.onos.net.PortNumber; |
| 22 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
| 23 | import org.onlab.onos.net.device.DefaultPortDescription; |
| 24 | import org.onlab.onos.net.device.DeviceAdminService; |
| 25 | import org.onlab.onos.net.device.DeviceDescription; |
| 26 | import org.onlab.onos.net.device.DeviceEvent; |
| 27 | import org.onlab.onos.net.device.DeviceListener; |
| 28 | import org.onlab.onos.net.device.DeviceProvider; |
| 29 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
| 30 | import org.onlab.onos.net.device.DeviceProviderService; |
| 31 | import org.onlab.onos.net.device.DeviceService; |
| 32 | import org.onlab.onos.net.device.PortDescription; |
| 33 | import org.onlab.onos.net.provider.AbstractProvider; |
| 34 | import org.onlab.onos.net.provider.ProviderId; |
Yuta HIGUCHI | b5df76d | 2014-09-27 20:54:00 -0700 | [diff] [blame] | 35 | import org.onlab.onos.store.common.StoreManager; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 36 | import org.onlab.onos.store.common.StoreService; |
Yuta HIGUCHI | b5df76d | 2014-09-27 20:54:00 -0700 | [diff] [blame] | 37 | import org.onlab.onos.store.common.TestStoreManager; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 38 | import org.onlab.onos.store.device.impl.DistributedDeviceStore; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 39 | import org.onlab.onos.store.serializers.KryoSerializationManager; |
| 40 | import org.onlab.onos.store.serializers.KryoSerializationService; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 41 | import org.onlab.packet.IpPrefix; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 42 | |
| 43 | import java.util.ArrayList; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 44 | import java.util.HashSet; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 45 | import java.util.Iterator; |
| 46 | import java.util.List; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 47 | import java.util.Map.Entry; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 48 | import java.util.Set; |
Yuta HIGUCHI | 3f6d29b | 2014-09-25 01:41:15 -0700 | [diff] [blame] | 49 | import java.util.concurrent.BlockingQueue; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 50 | import java.util.concurrent.ConcurrentHashMap; |
| 51 | import java.util.concurrent.ConcurrentMap; |
Yuta HIGUCHI | 3f6d29b | 2014-09-25 01:41:15 -0700 | [diff] [blame] | 52 | import java.util.concurrent.LinkedBlockingQueue; |
| 53 | import java.util.concurrent.TimeUnit; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 54 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 55 | import static org.junit.Assert.*; |
| 56 | import static org.onlab.onos.net.Device.Type.SWITCH; |
| 57 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 58 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 59 | |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 60 | // FIXME This test is slow starting up Hazelcast on each test cases. |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 61 | // FIXME DistributedDeviceStore should have it's own test cases. |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 62 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Test codifying the device service & device provider service contracts. |
| 65 | */ |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 66 | public class DistributedDeviceManagerTest { |
| 67 | |
| 68 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 69 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 70 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 71 | private static final String MFR = "whitebox"; |
| 72 | private static final String HW = "1.1.x"; |
| 73 | private static final String SW1 = "3.8.1"; |
| 74 | private static final String SW2 = "3.9.5"; |
| 75 | private static final String SN = "43311-12345"; |
| 76 | |
| 77 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 78 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 79 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 80 | |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 81 | private static final DefaultControllerNode SELF |
| 82 | = new DefaultControllerNode(new NodeId("foobar"), |
| 83 | IpPrefix.valueOf("127.0.0.1")); |
| 84 | |
| 85 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 86 | private DeviceManager mgr; |
| 87 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 88 | protected StoreManager storeManager; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 89 | protected DeviceService service; |
| 90 | protected DeviceAdminService admin; |
| 91 | protected DeviceProviderRegistry registry; |
| 92 | protected DeviceProviderService providerService; |
| 93 | protected TestProvider provider; |
| 94 | protected TestListener listener = new TestListener(); |
| 95 | private DistributedDeviceStore dstore; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 96 | private TestMastershipManager masterManager; |
| 97 | private EventDeliveryService eventService; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 98 | private KryoSerializationManager serializationMgr; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 99 | |
| 100 | @Before |
| 101 | public void setUp() { |
| 102 | mgr = new DeviceManager(); |
| 103 | service = mgr; |
| 104 | admin = mgr; |
| 105 | registry = mgr; |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 106 | // TODO should find a way to clean Hazelcast instance without shutdown. |
| 107 | Config config = TestStoreManager.getTestConfig(); |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 108 | |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 109 | masterManager = new TestMastershipManager(); |
| 110 | |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 111 | storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config)); |
| 112 | storeManager.activate(); |
| 113 | |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 114 | serializationMgr = new KryoSerializationManager(); |
| 115 | serializationMgr.activate(); |
| 116 | |
| 117 | dstore = new TestDistributedDeviceStore(storeManager, serializationMgr); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 118 | dstore.activate(); |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 119 | |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 120 | mgr.store = dstore; |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 121 | eventService = new TestEventDispatcher(); |
| 122 | mgr.eventDispatcher = eventService; |
| 123 | mgr.mastershipService = masterManager; |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 124 | mgr.activate(); |
| 125 | |
| 126 | service.addListener(listener); |
| 127 | |
| 128 | provider = new TestProvider(); |
| 129 | providerService = registry.register(provider); |
| 130 | assertTrue("provider should be registered", |
| 131 | registry.getProviders().contains(provider.id())); |
| 132 | } |
| 133 | |
| 134 | @After |
| 135 | public void tearDown() { |
| 136 | registry.unregister(provider); |
| 137 | assertFalse("provider should not be registered", |
| 138 | registry.getProviders().contains(provider.id())); |
| 139 | service.removeListener(listener); |
| 140 | mgr.deactivate(); |
| 141 | |
| 142 | dstore.deactivate(); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 143 | serializationMgr.deactivate(); |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 144 | storeManager.deactivate(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private void connectDevice(DeviceId deviceId, String swVersion) { |
| 148 | DeviceDescription description = |
| 149 | new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR, |
| 150 | HW, swVersion, SN); |
| 151 | providerService.deviceConnected(deviceId, description); |
| 152 | assertNotNull("device should be found", service.getDevice(DID1)); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void deviceConnected() { |
| 157 | assertNull("device should not be found", service.getDevice(DID1)); |
| 158 | connectDevice(DID1, SW1); |
| 159 | validateEvents(DEVICE_ADDED); |
| 160 | |
| 161 | assertEquals("only one device expected", 1, Iterables.size(service.getDevices())); |
| 162 | Iterator<Device> it = service.getDevices().iterator(); |
| 163 | assertNotNull("one device expected", it.next()); |
| 164 | assertFalse("only one device expected", it.hasNext()); |
| 165 | |
| 166 | assertEquals("incorrect device count", 1, service.getDeviceCount()); |
| 167 | assertTrue("device should be available", service.isAvailable(DID1)); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void deviceDisconnected() { |
| 172 | connectDevice(DID1, SW1); |
| 173 | connectDevice(DID2, SW1); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 174 | validateEvents(DEVICE_ADDED, DEVICE_ADDED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 175 | assertTrue("device should be available", service.isAvailable(DID1)); |
| 176 | |
| 177 | // Disconnect |
| 178 | providerService.deviceDisconnected(DID1); |
| 179 | assertNotNull("device should not be found", service.getDevice(DID1)); |
| 180 | assertFalse("device should not be available", service.isAvailable(DID1)); |
| 181 | validateEvents(DEVICE_AVAILABILITY_CHANGED); |
| 182 | |
| 183 | // Reconnect |
| 184 | connectDevice(DID1, SW1); |
| 185 | validateEvents(DEVICE_AVAILABILITY_CHANGED); |
| 186 | |
| 187 | assertEquals("incorrect device count", 2, service.getDeviceCount()); |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void deviceUpdated() { |
| 192 | connectDevice(DID1, SW1); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 193 | validateEvents(DEVICE_ADDED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 194 | |
| 195 | connectDevice(DID1, SW2); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 196 | validateEvents(DEVICE_UPDATED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void getRole() { |
| 201 | connectDevice(DID1, SW1); |
| 202 | assertEquals("incorrect role", MastershipRole.MASTER, service.getRole(DID1)); |
| 203 | } |
| 204 | |
| 205 | @Test |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 206 | public void updatePorts() { |
| 207 | connectDevice(DID1, SW1); |
| 208 | List<PortDescription> pds = new ArrayList<>(); |
| 209 | pds.add(new DefaultPortDescription(P1, true)); |
| 210 | pds.add(new DefaultPortDescription(P2, true)); |
| 211 | pds.add(new DefaultPortDescription(P3, true)); |
| 212 | providerService.updatePorts(DID1, pds); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 213 | validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED, PORT_ADDED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 214 | pds.clear(); |
| 215 | |
| 216 | pds.add(new DefaultPortDescription(P1, false)); |
| 217 | pds.add(new DefaultPortDescription(P3, true)); |
| 218 | providerService.updatePorts(DID1, pds); |
| 219 | validateEvents(PORT_UPDATED, PORT_REMOVED); |
| 220 | } |
| 221 | |
| 222 | @Test |
| 223 | public void updatePortStatus() { |
| 224 | connectDevice(DID1, SW1); |
| 225 | List<PortDescription> pds = new ArrayList<>(); |
| 226 | pds.add(new DefaultPortDescription(P1, true)); |
| 227 | pds.add(new DefaultPortDescription(P2, true)); |
| 228 | providerService.updatePorts(DID1, pds); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 229 | validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 230 | |
| 231 | providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false)); |
| 232 | validateEvents(PORT_UPDATED); |
| 233 | providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false)); |
| 234 | assertTrue("no events expected", listener.events.isEmpty()); |
| 235 | } |
| 236 | |
| 237 | @Test |
| 238 | public void getPorts() { |
| 239 | connectDevice(DID1, SW1); |
| 240 | List<PortDescription> pds = new ArrayList<>(); |
| 241 | pds.add(new DefaultPortDescription(P1, true)); |
| 242 | pds.add(new DefaultPortDescription(P2, true)); |
| 243 | providerService.updatePorts(DID1, pds); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 244 | validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 245 | assertEquals("wrong port count", 2, service.getPorts(DID1).size()); |
| 246 | |
| 247 | Port port = service.getPort(DID1, P1); |
| 248 | assertEquals("incorrect port", P1, service.getPort(DID1, P1).number()); |
| 249 | assertEquals("incorrect state", true, service.getPort(DID1, P1).isEnabled()); |
| 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void removeDevice() { |
| 254 | connectDevice(DID1, SW1); |
| 255 | connectDevice(DID2, SW2); |
| 256 | assertEquals("incorrect device count", 2, service.getDeviceCount()); |
| 257 | admin.removeDevice(DID1); |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 258 | validateEvents(DEVICE_ADDED, DEVICE_ADDED, DEVICE_REMOVED); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 259 | assertNull("device should not be found", service.getDevice(DID1)); |
| 260 | assertNotNull("device should be found", service.getDevice(DID2)); |
| 261 | assertEquals("incorrect device count", 1, service.getDeviceCount()); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | protected void validateEvents(Enum... types) { |
Yuta HIGUCHI | 3f6d29b | 2014-09-25 01:41:15 -0700 | [diff] [blame] | 265 | for (Enum type : types) { |
| 266 | try { |
| 267 | Event event = listener.events.poll(1, TimeUnit.SECONDS); |
| 268 | assertNotNull("Timed out waiting for " + event, event); |
| 269 | assertEquals("incorrect event type", type, event.type()); |
| 270 | } catch (InterruptedException e) { |
| 271 | fail("Unexpected interrupt"); |
| 272 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 273 | } |
Yuta HIGUCHI | 3f6d29b | 2014-09-25 01:41:15 -0700 | [diff] [blame] | 274 | assertTrue("Unexpected events left", listener.events.isEmpty()); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 275 | listener.events.clear(); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | private class TestProvider extends AbstractProvider implements DeviceProvider { |
| 280 | private Device deviceReceived; |
| 281 | private MastershipRole roleReceived; |
| 282 | |
| 283 | public TestProvider() { |
| 284 | super(PID); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void triggerProbe(Device device) { |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void roleChanged(Device device, MastershipRole newRole) { |
| 293 | deviceReceived = device; |
| 294 | roleReceived = newRole; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | private static class TestListener implements DeviceListener { |
Yuta HIGUCHI | 3f6d29b | 2014-09-25 01:41:15 -0700 | [diff] [blame] | 299 | final BlockingQueue<DeviceEvent> events = new LinkedBlockingQueue<>(); |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 300 | |
| 301 | @Override |
| 302 | public void event(DeviceEvent event) { |
| 303 | events.add(event); |
| 304 | } |
| 305 | } |
| 306 | |
tom | 85ff08b | 2014-09-22 17:14:18 -0700 | [diff] [blame] | 307 | private class TestDistributedDeviceStore extends DistributedDeviceStore { |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 308 | |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 309 | public TestDistributedDeviceStore(StoreService storeService, |
| 310 | KryoSerializationService kryoSerializationService) { |
| 311 | this.storeService = storeService; |
| 312 | this.kryoSerializationService = kryoSerializationService; |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 316 | private static class TestMastershipManager extends MastershipServiceAdapter { |
Yuta HIGUCHI | b4139d8 | 2014-09-23 18:41:33 -0700 | [diff] [blame] | 317 | |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 318 | private ConcurrentMap<DeviceId, NodeId> masters = new ConcurrentHashMap<>(); |
| 319 | |
| 320 | public TestMastershipManager() { |
| 321 | // SELF master of all initially |
| 322 | masters.put(DID1, SELF.id()); |
| 323 | masters.put(DID1, SELF.id()); |
| 324 | } |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 325 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 326 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 327 | return MastershipRole.MASTER; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 332 | HashSet<DeviceId> set = Sets.newHashSet(); |
| 333 | for (Entry<DeviceId, NodeId> e : masters.entrySet()) { |
| 334 | if (e.getValue().equals(nodeId)) { |
| 335 | set.add(e.getKey()); |
| 336 | } |
| 337 | } |
| 338 | return set; |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public MastershipRole requestRoleFor(DeviceId deviceId) { |
Yuta HIGUCHI | b113c45 | 2014-09-24 00:12:03 -0700 | [diff] [blame] | 343 | if (SELF.id().equals(masters.get(deviceId))) { |
| 344 | return MastershipRole.MASTER; |
| 345 | } else { |
| 346 | return MastershipRole.STANDBY; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void relinquishMastership(DeviceId deviceId) { |
| 352 | masters.remove(deviceId, SELF.id()); |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 353 | } |
Ayaka Koshibe | a7f044e | 2014-09-23 16:56:20 -0700 | [diff] [blame] | 354 | } |
Yuta HIGUCHI | b3b2ac4 | 2014-09-21 23:37:11 -0700 | [diff] [blame] | 355 | } |