Search in sources :

Example 16 with MastershipRole

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

the class MastershipRoleCodecTest method getMastershipRole.

/**
 * Reads in a mastership role from the given resource and decodes it.
 *
 * @param resourceName resource to use to read the JSON for the rule
 * @return decoded mastership term object
 * @throws IOException if processing the resource fails
 */
private MastershipRole getMastershipRole(String resourceName) throws IOException {
    InputStream jsonStream = MastershipRoleCodecTest.class.getResourceAsStream(resourceName);
    JsonNode json = context.mapper().readTree(jsonStream);
    assertThat(json, notNullValue());
    MastershipRole mastershipRole = mastershipRoleCodec.decode((ObjectNode) json, context);
    assertThat(mastershipRole, notNullValue());
    return mastershipRole;
}
Also used : InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) MastershipRole(org.onosproject.net.MastershipRole)

Example 17 with MastershipRole

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

the class MastershipRoleCodecTest method testMastershipRoleEncode.

/**
 * Tests encoding of a mastership role object.
 */
@Test
public void testMastershipRoleEncode() {
    MastershipRole mastershipRole = MASTER;
    ObjectNode mastershipRoleJson = mastershipRoleCodec.encode(mastershipRole, context);
    assertThat(mastershipRoleJson, MastershipRoleJsonMatcher.matchesMastershipRole(mastershipRole));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MastershipRole(org.onosproject.net.MastershipRole) Test(org.junit.Test)

Example 18 with MastershipRole

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

the class MastershipRoleCodecTest method testMastershipRoleDecode.

/**
 * Tests decoding of mastership role JSON object.
 */
@Test
public void testMastershipRoleDecode() throws IOException {
    MastershipRole mastershipRole = getMastershipRole("MastershipRole.json");
    assertThat(mastershipRole, is(MASTER));
}
Also used : MastershipRole(org.onosproject.net.MastershipRole) Test(org.junit.Test)

Example 19 with MastershipRole

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

the class GeneralDeviceProvider method roleChanged.

@Override
public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
    final MastershipInfo mastershipInfo = mastershipService.getMastershipFor(deviceId);
    final NodeId localNodeId = clusterService.getLocalNode().id();
    if (!mastershipInfo.getRole(localNodeId).equals(newRole)) {
        log.warn("Inconsistent mastership info for {}! Requested {}, but " + "mastership service reports {}, will apply the latter...", deviceId, newRole, mastershipInfo.getRole(localNodeId));
        newRole = mastershipInfo.getRole(localNodeId);
    }
    final DeviceHandshaker handshaker = getBehaviour(deviceId, DeviceHandshaker.class);
    if (handshaker == null) {
        log.error("Null handshaker. Unable to notify role {} to {}", newRole, deviceId);
        return;
    }
    // Derive preference value.
    final int preference;
    switch(newRole) {
        case MASTER:
            preference = 0;
            break;
        case STANDBY:
            preference = mastershipInfo.backups().indexOf(localNodeId) + 1;
            if (preference == 0) {
                // Not found in list.
                log.error("Unable to derive mastership preference for {}, " + "requested role {} but local node ID was " + "not found among list of backup nodes " + "reported by mastership service", deviceId, newRole);
                return;
            }
            break;
        case NONE:
            // No preference for NONE, apply as is.
            Pair<MastershipRole, Integer> pairRolePref = Pair.of(newRole, -1);
            if (log.isDebugEnabled()) {
                log.debug("Notifying role {} to {}", newRole, deviceId);
            } else if (!pairRolePref.equals(lastRoleRequest.get(deviceId))) {
                log.info("Notifying role {} to {}", newRole, deviceId);
            }
            lastRoleRequest.put(deviceId, pairRolePref);
            handshaker.roleChanged(newRole);
            return;
        default:
            log.error("Unrecognized mastership role {}", newRole);
            return;
    }
    Pair<MastershipRole, Integer> pairRolePref = Pair.of(newRole, preference);
    if (log.isDebugEnabled()) {
        log.debug("Notifying role {} (preference {}) for term {} to {}", newRole, preference, mastershipInfo.term(), deviceId);
    } else if (!pairRolePref.equals(lastRoleRequest.get(deviceId))) {
        log.info("Notifying role {} (preference {}) for term {} to {}", newRole, preference, mastershipInfo.term(), deviceId);
    }
    lastRoleRequest.put(deviceId, pairRolePref);
    try {
        handshaker.roleChanged(preference, mastershipInfo.term());
    } catch (UnsupportedOperationException e) {
        // Preference-based method not supported.
        handshaker.roleChanged(newRole);
    }
}
Also used : DeviceHandshaker(org.onosproject.net.device.DeviceHandshaker) NodeId(org.onosproject.cluster.NodeId) MastershipInfo(org.onosproject.mastership.MastershipInfo) MastershipRole(org.onosproject.net.MastershipRole)

Example 20 with MastershipRole

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

the class OvsdbDeviceProvider method roleChanged.

@Override
public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
    // Mastership handshake to be compliant with the ONOS core
    if (newRole != null && newRole != NONE) {
        final MastershipRole role = mastershipService.getLocalRole(deviceId);
        providerService.receivedRoleReply(deviceId, role, role);
    }
}
Also used : 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