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;
}
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));
}
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));
}
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);
}
}
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);
}
}
Aggregations