Search in sources :

Example 1 with MastershipRole

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

the class SimpleIntManager method configDeviceTask.

private void configDeviceTask(DeviceId deviceId, long creationTime) {
    if (isConfigTaskValid(deviceId, creationTime)) {
        // Task outdated.
        return;
    }
    if (!deviceService.isAvailable(deviceId)) {
        return;
    }
    final MastershipRole role = mastershipService.requestRoleForSync(deviceId);
    if (!role.equals(MastershipRole.MASTER)) {
        return;
    }
    deviceLocks.get(deviceId).lock();
    try {
        // Clean up first.
        cleanupDevice(deviceId);
        if (!configDevice(deviceId)) {
            // Clean up if fails.
            cleanupDevice(deviceId);
            return;
        }
        devicesToConfigure.remove(deviceId);
    } finally {
        deviceLocks.get(deviceId).unlock();
    }
}
Also used : MastershipRole(org.onosproject.net.MastershipRole)

Example 2 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 3 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 4 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 5 with MastershipRole

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

the class SimpleVirtualMastershipStore method setMaster.

@Override
public synchronized CompletableFuture<MastershipEvent> setMaster(NetworkId networkId, NodeId nodeId, DeviceId deviceId) {
    Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);
    MastershipRole role = getRole(networkId, nodeId, deviceId);
    switch(role) {
        case MASTER:
            // no-op
            return CompletableFuture.completedFuture(null);
        case STANDBY:
        case NONE:
            NodeId prevMaster = masterMap.put(deviceId, nodeId);
            incrementTerm(networkId, deviceId);
            removeFromBackups(networkId, deviceId, nodeId);
            addToBackup(networkId, deviceId, prevMaster);
            break;
        default:
            log.warn("unknown Mastership Role {}", role);
            return null;
    }
    return CompletableFuture.completedFuture(new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
}
Also used : DeviceId(org.onosproject.net.DeviceId) 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)18 DeviceId (org.onosproject.net.DeviceId)10 MastershipEvent (org.onosproject.mastership.MastershipEvent)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 MastershipTerm (org.onosproject.mastership.MastershipTerm)4 ExecutionException (java.util.concurrent.ExecutionException)3 MastershipInfo (org.onosproject.mastership.MastershipInfo)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Test (org.junit.Test)2