use of org.onosproject.mastership.MastershipTerm in project onos by opennetworkinglab.
the class MastershipTermCodecTest method testMastershipTermDecode.
/**
* Tests decoding of mastership term JSON object.
*/
@Test
public void testMastershipTermDecode() throws IOException {
MastershipTerm mastershipTerm = getMastershipTerm("MastershipTerm.json");
assertThat(mastershipTerm.master().id(), is("1"));
assertThat(mastershipTerm.termNumber(), is(10L));
}
use of org.onosproject.mastership.MastershipTerm in project onos by opennetworkinglab.
the class MastershipTermCodecTest method getMastershipTerm.
/**
* Reads in a mastership term 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 MastershipTerm getMastershipTerm(String resourceName) throws IOException {
InputStream jsonStream = MastershipTermCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
assertThat(json, notNullValue());
MastershipTerm mastershipTerm = mastershipTermCodec.decode((ObjectNode) json, context);
assertThat(mastershipTerm, notNullValue());
return mastershipTerm;
}
use of org.onosproject.mastership.MastershipTerm in project onos by opennetworkinglab.
the class DeviceManager method reassertRole.
/**
* Reassert role for specified device connected to this node.
*
* @param did device identifier
* @param nextRole role to apply. If NONE is specified,
* it will ask mastership service for a role and apply it.
*/
private void reassertRole(final DeviceId did, final MastershipRole nextRole) {
MastershipRole myNextRole = nextRole;
if (myNextRole == NONE && upgradeService.isLocalActive()) {
try {
mastershipService.requestRoleFor(did).get();
MastershipTerm term = termService.getMastershipTerm(did);
if (term != null && localNodeId.equals(term.master())) {
myNextRole = MASTER;
} else {
myNextRole = STANDBY;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("Interrupted waiting for Mastership", e);
} catch (ExecutionException e) {
log.error("Encountered an error waiting for Mastership", e);
}
}
switch(myNextRole) {
case MASTER:
final Device device = getDevice(did);
if (device != null && !isAvailable(did) && canMarkOnline(device)) {
post(store.markOnline(did));
}
// TODO: should apply role only if there is mismatch
log.debug("Applying role {} to {}", myNextRole, did);
if (!applyRoleAndProbe(did, MASTER)) {
log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
// immediately failed to apply role
updateMastershipFor(did);
// FIXME disconnect?
}
break;
case STANDBY:
log.debug("Applying role {} to {}", myNextRole, did);
if (!applyRoleAndProbe(did, STANDBY)) {
log.warn("Unsuccessful applying role {} to {}", myNextRole, did);
// immediately failed to apply role
updateMastershipFor(did);
// FIXME disconnect?
}
break;
case NONE:
break;
default:
// should never reach here
log.error("You didn't see anything. I did not exist.");
break;
}
}
use of org.onosproject.mastership.MastershipTerm in project onos by opennetworkinglab.
the class DeviceManager method handleMastershipEvent.
private void handleMastershipEvent(MastershipEvent event) {
log.debug("Handling mastership event");
final DeviceId did = event.subject();
// myNextRole suggested by MastershipService event
MastershipRole myNextRole;
if (event.type() == MastershipEvent.Type.SUSPENDED) {
// FIXME STANDBY OR NONE?
myNextRole = NONE;
} else if (localNodeId.equals(event.roleInfo().master())) {
// confirm latest info
MastershipTerm term = termService.getMastershipTerm(did);
final boolean iHaveControl = term != null && localNodeId.equals(term.master());
if (iHaveControl) {
myNextRole = MASTER;
} else {
myNextRole = STANDBY;
}
} else if (event.roleInfo().backups().contains(localNodeId)) {
myNextRole = STANDBY;
} else {
myNextRole = NONE;
}
log.debug("Device {} local status is {}", did, localStatus(did));
final boolean isGracePeriodOn = inGracePeriod(did);
final boolean isReachable = isReachable(did, isGracePeriodOn);
// Passed the grace period and it is still not reachable
if (!isGracePeriodOn && !isReachable) {
// device is not connected to this node, nevertheless we should get a role
if (mastershipService.getLocalRole(did) == NONE) {
log.debug("Node was instructed to be {} role for {}, " + "but this node cannot reach the device " + "and role is already None. Asking a new role " + "and then apply the disconnection protocol.", myNextRole, did);
try {
mastershipService.requestRoleFor(did).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("Interrupted waiting for Mastership", e);
} catch (ExecutionException e) {
log.error("Encountered an error waiting for Mastership", e);
}
} else if (myNextRole != NONE) {
log.warn("Node was instructed to be {} role for {}, " + "but this node cannot reach the device. " + "Apply the disconnection protocol.", myNextRole, did);
}
// Let's put some order in the candidates list
roleToAcknowledge.remove(did);
updateMastershipFor(did);
} else if (isReachable) {
// cost us a lot as it is equivalent to a probe.
if (store.getDevice(did) != null) {
log.info("{} is reachable - reasserting the role", did);
reassertRole(did, myNextRole);
} else {
log.debug("Device is not yet/no longer in the store: {}", did);
}
} else {
// Do not proceed furthermore if the grace period is still on
log.debug("Skipping mastership event {}", event);
}
}
use of org.onosproject.mastership.MastershipTerm in project onos by opennetworkinglab.
the class MastershipTermCodecTest method testMastershipTermEncode.
/**
* Tests encoding of a mastership term object.
*/
@Test
public void testMastershipTermEncode() {
NodeId masterNodeId = NodeId.nodeId("1");
long termNumber = 10;
MastershipTerm mastershipTerm = MastershipTerm.of(masterNodeId, termNumber);
ObjectNode mastershipTermJson = mastershipTermCodec.encode(mastershipTerm, context);
assertThat(mastershipTermJson, MastershipTermJsonMatcher.matchesMastershipTerm(mastershipTerm));
}
Aggregations