use of org.onosproject.net.intent.IntentState in project onos by opennetworkinglab.
the class IntentsListCommand method doExecute.
@Override
protected void doExecute() {
service = get(IntentService.class);
workPartitionService = get(WorkPartitionService.class);
if (workPartitionService == null) {
return;
}
contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
Iterable<Intent> intents;
if (pending) {
intents = service.getPending();
} else {
intents = service.getIntents();
}
// Remove intents
if (remove != null && !remove.isEmpty()) {
filter.add(remove);
contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
IntentRemoveCommand intentRemoveCmd = new IntentRemoveCommand();
if (!remove.isEmpty()) {
intentRemoveCmd.purgeIntentsInteractive(filterIntents(service));
}
return;
}
// Show detailed intents
if (!intentIds.isEmpty()) {
IntentDetailsCommand intentDetailsCmd = new IntentDetailsCommand();
intentDetailsCmd.detailIntents(intentIds);
return;
}
// Show brief intents
if (intentsSummary || miniSummary) {
Map<String, IntentSummary> summarized = summarize(intents);
if (outputJson()) {
ObjectNode summaries = mapper().createObjectNode();
summarized.forEach((n, s) -> summaries.set(uncapitalize(n), s.json(mapper())));
print("%s", summaries);
} else if (miniSummary) {
StringBuilder builder = new StringBuilder();
builder.append(summarized.remove("All").miniSummary());
summarized.values().forEach(s -> builder.append(s.miniSummary()));
print("%s", builder.toString());
} else {
StringBuilder builder = new StringBuilder();
builder.append(SUMMARY_TITLES);
builder.append('\n').append(SEPARATOR);
builder.append(summarized.remove("All").summary());
summarized.values().forEach(s -> builder.append(s.summary()));
print("%s", builder.toString());
}
return;
}
// JSON or default output
if (outputJson()) {
print("%s", json(intents));
} else {
for (Intent intent : intents) {
IntentState state = service.getIntentState(intent.key());
StringBuilder intentFormat = fullFormat(intent, state);
StringBuilder detailsIntentFormat = detailsFormat(intent, state);
String formatted = intentFormat.append(detailsIntentFormat).toString();
if (contentFilter.filter(formatted)) {
print("%s\n", formatted);
}
}
}
}
use of org.onosproject.net.intent.IntentState in project onos by opennetworkinglab.
the class IntentData method isUpdateAcceptable.
/**
* Determines whether an intent data update is allowed. The update must
* either have a higher version than the current data, or the state
* transition between two updates of the same version must be sane.
*
* @param currentData existing intent data in the store
* @param newData new intent data update proposal
* @return true if we can apply the update, otherwise false
*/
public static boolean isUpdateAcceptable(IntentData currentData, IntentData newData) {
if (currentData == null) {
return true;
} else if (currentData.version().isOlderThan(newData.version())) {
return true;
} else if (currentData.version().isNewerThan(newData.version())) {
log.trace("{} update not acceptable: current is newer", newData.key());
return false;
}
assert (currentData.version().equals(newData.version()));
if (currentData.internalStateVersion >= newData.internalStateVersion) {
log.trace("{} update not acceptable: current is newer internally", newData.key());
return false;
}
// current and new data versions are the same
IntentState currentState = currentData.state();
IntentState newState = newData.state();
switch(newState) {
case INSTALLING:
if (currentState == INSTALLING) {
log.trace("{} update not acceptable: no-op INSTALLING", newData.key());
return false;
}
// FALLTHROUGH
case REALLOCATING:
if (currentState == REALLOCATING) {
log.trace("{} update not acceptable: no-op REALLOCATING", newData.key());
return false;
} else if (currentState == INSTALLED) {
return true;
}
// FALLTHROUGH
case INSTALLED:
if (currentState == INSTALLED) {
return false;
} else if (currentState == WITHDRAWING || currentState == WITHDRAWN || currentState == PURGE_REQ) {
log.warn("Invalid state transition from {} to {} for intent {}", currentState, newState, newData.key());
return false;
}
return true;
case WITHDRAWING:
if (currentState == WITHDRAWING) {
log.trace("{} update not acceptable: no-op WITHDRAWING", newData.key());
return false;
}
// FALLTHROUGH
case WITHDRAWN:
if (currentState == WITHDRAWN) {
log.trace("{} update not acceptable: no-op WITHDRAWN", newData.key());
return false;
} else if (currentState == INSTALLING || currentState == INSTALLED || currentState == PURGE_REQ) {
log.warn("Invalid state transition from {} to {} for intent {}", currentState, newState, newData.key());
return false;
}
return true;
case FAILED:
if (currentState == FAILED) {
log.trace("{} update not acceptable: no-op FAILED", newData.key());
return false;
}
return true;
case CORRUPT:
if (currentState == CORRUPT) {
log.trace("{} update not acceptable: no-op CORRUPT", newData.key());
return false;
}
return true;
case PURGE_REQ:
// TODO we should enforce that only WITHDRAWN intents can be purged
return true;
case COMPILING:
case RECOMPILING:
case INSTALL_REQ:
case WITHDRAW_REQ:
default:
log.warn("Invalid state {} for intent {}", newState, newData.key());
return false;
}
}
use of org.onosproject.net.intent.IntentState in project onos by opennetworkinglab.
the class IntentManager method buildAndSubmitBatches.
private void buildAndSubmitBatches(Iterable<Key> intentKeys, boolean compileAllFailed) {
// Attempt recompilation of the specified intents first.
for (Key key : intentKeys) {
if (!store.isMaster(key)) {
continue;
}
Intent intent = store.getIntent(key);
if (intent == null) {
continue;
}
if (store.getPendingData(key) != null) {
continue;
}
submit(intent);
}
if (compileAllFailed) {
// If required, compile all currently failed intents.
for (Intent intent : getIntents()) {
if (!store.isMaster(intent.key())) {
continue;
}
if (store.getPendingData(intent.key()) != null) {
continue;
}
IntentState state = getIntentState(intent.key());
if (RECOMPILE.contains(state) || intentAllowsPartialFailure(intent)) {
if (WITHDRAW.contains(state)) {
withdraw(intent);
} else {
submit(intent);
}
}
}
}
}
use of org.onosproject.net.intent.IntentState in project onos by opennetworkinglab.
the class IntentManagerTest method verifyState.
public void verifyState() {
// verify that all intents are parked and the batch operation is unblocked
Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED, CORRUPT);
for (Intent i : service.getIntents()) {
IntentState state = service.getIntentState(i.key());
assertTrue("Intent " + i.id() + " is in invalid state " + state, parked.contains(state));
}
// the batch has not yet been removed when we receive the last event
// FIXME: this doesn't guarantee to avoid the race
// FIXME
// for (int tries = 0; tries < 10; tries++) {
// if (manager.batchService.getPendingOperations().isEmpty()) {
// break;
// }
// delay(10);
// }
// assertTrue("There are still pending batch operations.",
// manager.batchService.getPendingOperations().isEmpty());
}
use of org.onosproject.net.intent.IntentState in project onos by opennetworkinglab.
the class IntentsWebResource method deleteIntentById.
/**
* Withdraws intent.
* Withdraws the specified intent from the system.
*
* @param appId application identifier
* @param key intent key
* @return 204 NO CONTENT
*/
@DELETE
@Path("{appId}/{key}")
public Response deleteIntentById(@PathParam("appId") String appId, @PathParam("key") String key) {
final ApplicationId app = get(CoreService.class).getAppId(appId);
nullIsNotFound(app, APP_ID_NOT_FOUND);
Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
IntentService service = get(IntentService.class);
if (intent == null) {
intent = service.getIntent(Key.of(Long.decode(key), app));
}
if (intent == null) {
// in this case.
return Response.noContent().build();
}
Key k = intent.key();
// set up latch and listener to track uninstall progress
CountDownLatch latch = new CountDownLatch(1);
IntentListener listener = new DeleteListener(k, latch);
service.addListener(listener);
try {
// request the withdraw
service.withdraw(intent);
try {
latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.info("REST Delete operation timed out waiting for intent {}", k);
Thread.currentThread().interrupt();
}
// double check the state
IntentState state = service.getIntentState(k);
if (state == WITHDRAWN || state == FAILED) {
service.purge(intent);
}
} finally {
// clean up the listener
service.removeListener(listener);
}
return Response.noContent().build();
}
Aggregations