@Override
    public CompletableFuture<MastershipRole> requestRole(NetworkId networkId,
                                                         DeviceId deviceId) {
        //query+possible reelection
        NodeId node = clusterService.getLocalNode().id();
        MastershipRole role = getRole(networkId, node, deviceId);

        Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);

        switch (role) {
            case MASTER:
                return CompletableFuture.completedFuture(MastershipRole.MASTER);
            case STANDBY:
                if (getMaster(networkId, deviceId) == null) {
                    // no master => become master
                    masterMap.put(deviceId, node);
                    incrementTerm(networkId, deviceId);
                    // remove from backup list
                    removeFromBackups(networkId, deviceId, node);
                    notifyDelegate(networkId, new MastershipEvent(MASTER_CHANGED, deviceId,
                        getMastership(networkId, deviceId)));
                    return CompletableFuture.completedFuture(MastershipRole.MASTER);
                }
                return CompletableFuture.completedFuture(MastershipRole.STANDBY);
            case NONE:
                if (getMaster(networkId, deviceId) == null) {
                    // no master => become master
                    masterMap.put(deviceId, node);
                    incrementTerm(networkId, deviceId);
                    notifyDelegate(networkId, new MastershipEvent(MASTER_CHANGED, deviceId,
                        getMastership(networkId, deviceId)));
                    return CompletableFuture.completedFuture(MastershipRole.MASTER);
                }
                // add to backup list
                if (addToBackup(networkId, deviceId, node)) {
                    notifyDelegate(networkId, new MastershipEvent(BACKUPS_CHANGED, deviceId,
                        getMastership(networkId, deviceId)));
                }
                return CompletableFuture.completedFuture(MastershipRole.STANDBY);
            default:
                log.warn("unknown Mastership Role {}", role);
        }
        return CompletableFuture.completedFuture(role);
    }
@Test
    public void requestRole() {
        //NONE - become MASTER
        put(VNID1, VDID1, N1, false, false);
        assertEquals("wrong role", MASTER,
                     Futures.getUnchecked(sms.requestRole(VNID1, VDID1)));

        //was STANDBY - become MASTER
        put(VNID1, VDID2, N1, false, true);
        assertEquals("wrong role", MASTER,
                     Futures.getUnchecked(sms.requestRole(VNID1, VDID2)));

        //other MASTER - stay STANDBY
        put(VNID1, VDID3, N2, true, false);
        assertEquals("wrong role", STANDBY,
                     Futures.getUnchecked(sms.requestRole(VNID1, VDID3)));

        //local (N1) is MASTER - stay MASTER
        put(VNID1, VDID4, N1, true, true);
        assertEquals("wrong role", MASTER,
                     Futures.getUnchecked(sms.requestRole(VNID1, VDID4)));
    }