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