Search in sources :

Example 11 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method relinquishRole.

@Override
public synchronized CompletableFuture<MastershipEvent> relinquishRole(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
    MastershipRole role = getRole(networkId, nodeId, deviceId);
    switch(role) {
        case MASTER:
            NodeId backup = reelect(networkId, deviceId, nodeId);
            masterMap.put(deviceId, backup);
            incrementTerm(networkId, deviceId);
            return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
        case STANDBY:
            if (removeFromBackups(networkId, deviceId, nodeId)) {
                return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(networkId, deviceId)));
            }
            break;
        case NONE:
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
    }
    return CompletableFuture.completedFuture(null);
}
Also used : DeviceId(org.onosproject.net.DeviceId) MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 12 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method requestRole.

@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);
}
Also used : DeviceId(org.onosproject.net.DeviceId) MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 13 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method getRole.

@Override
public MastershipRole getRole(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
    Map<DeviceId, List<NodeId>> backups = getBackups(networkId);
    // just query
    NodeId current = masterMap.get(deviceId);
    MastershipRole role;
    if (current != null && current.equals(nodeId)) {
        return MastershipRole.MASTER;
    }
    if (backups.getOrDefault(deviceId, Collections.emptyList()).contains(nodeId)) {
        role = MastershipRole.STANDBY;
    } else {
        role = MastershipRole.NONE;
    }
    return role;
}
Also used : DeviceId(org.onosproject.net.DeviceId) NodeId(org.onosproject.cluster.NodeId) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MastershipRole(org.onosproject.net.MastershipRole)

Example 14 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class SimpleVirtualMastershipStore method setStandby.

@Override
public CompletableFuture<MastershipEvent> setStandby(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
    MastershipRole role = getRole(networkId, nodeId, deviceId);
    switch(role) {
        case MASTER:
            NodeId backup = reelect(networkId, deviceId, nodeId);
            if (backup == null) {
                // no master alternative
                masterMap.remove(deviceId);
                // TODO: Should there be new event type for no MASTER?
                return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
            } else {
                NodeId prevMaster = masterMap.put(deviceId, backup);
                incrementTerm(networkId, deviceId);
                addToBackup(networkId, deviceId, prevMaster);
                return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
            }
        case STANDBY:
        case NONE:
            boolean modified = addToBackup(networkId, deviceId, nodeId);
            if (modified) {
                return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(networkId, deviceId)));
            }
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
    }
    return null;
}
Also used : DeviceId(org.onosproject.net.DeviceId) MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Example 15 with MastershipRole

use of org.onosproject.net.MastershipRole in project onos by opennetworkinglab.

the class SimpleMastershipStore method setStandby.

@Override
public synchronized CompletableFuture<MastershipEvent> setStandby(NodeId nodeId, DeviceId deviceId) {
    MastershipRole role = getRole(nodeId, deviceId);
    switch(role) {
        case MASTER:
            NodeId backup = reelect(deviceId, nodeId);
            if (backup == null) {
                // no master alternative
                masterMap.remove(deviceId);
                // TODO: Should there be new event type for no MASTER?
                return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
            } else {
                NodeId prevMaster = masterMap.put(deviceId, backup);
                incrementTerm(deviceId);
                addToBackup(deviceId, prevMaster);
                return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(deviceId)));
            }
        case STANDBY:
        case NONE:
            boolean modified = addToBackup(deviceId, nodeId);
            if (modified) {
                return CompletableFuture.completedFuture(new MastershipEvent(BACKUPS_CHANGED, deviceId, getMastership(deviceId)));
            }
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
    }
    return null;
}
Also used : MastershipEvent(org.onosproject.mastership.MastershipEvent) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole)

Aggregations

MastershipRole (org.onosproject.net.MastershipRole)32 NodeId (org.onosproject.cluster.NodeId)19 MastershipEvent (org.onosproject.mastership.MastershipEvent)10 DeviceId (org.onosproject.net.DeviceId)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 MastershipTerm (org.onosproject.mastership.MastershipTerm)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 Map (java.util.Map)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ControllerNode (org.onosproject.cluster.ControllerNode)3 MastershipAdminService (org.onosproject.mastership.MastershipAdminService)3 MastershipInfo (org.onosproject.mastership.MastershipInfo)3 MastershipService (org.onosproject.mastership.MastershipService)3 DeviceEvent (org.onosproject.net.device.DeviceEvent)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Instant (java.time.Instant)2 HashSet (java.util.HashSet)2 Objects (java.util.Objects)2