use of org.onosproject.net.intent.IntentListener in project onos by opennetworkinglab.
the class IntentRemoveCommand method removeIntent.
/**
* Removes the intent passed as argument.
*
* @param intentService IntentService object
* @param intent intent to remove
*/
private void removeIntent(IntentService intentService, Intent intent) {
IntentListener listener = null;
Key key = intent.key();
final CountDownLatch withdrawLatch, purgeLatch;
if (purgeAfterRemove || sync) {
// set up latch and listener to track uninstall progress
withdrawLatch = new CountDownLatch(1);
purgeLatch = purgeAfterRemove ? new CountDownLatch(1) : null;
listener = (IntentEvent event) -> {
if (Objects.equals(event.subject().key(), key)) {
if (event.type() == IntentEvent.Type.WITHDRAWN || event.type() == IntentEvent.Type.FAILED) {
withdrawLatch.countDown();
} else if (purgeLatch != null && purgeAfterRemove && event.type() == IntentEvent.Type.PURGED) {
purgeLatch.countDown();
}
}
};
intentService.addListener(listener);
} else {
purgeLatch = null;
withdrawLatch = null;
}
// request the withdraw
intentService.withdraw(intent);
if (withdrawLatch != null && (purgeAfterRemove || sync)) {
try {
// wait for withdraw event
withdrawLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
print("Timed out waiting for intent {} withdraw", key);
}
if (purgeLatch != null && purgeAfterRemove && CAN_PURGE.contains(intentService.getIntentState(key))) {
intentService.purge(intent);
if (sync) {
/* TODO
Technically, the event comes before map.remove() is called.
If we depend on sync and purge working together, we will
need to address this.
*/
try {
purgeLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
print("Timed out waiting for intent {} purge", key);
}
}
}
}
if (listener != null) {
// clean up the listener
intentService.removeListener(listener);
}
}
use of org.onosproject.net.intent.IntentListener 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();
}
use of org.onosproject.net.intent.IntentListener in project onos by opennetworkinglab.
the class ServiceApplicationComponent method processTapiEvent.
/**
* Process TAPI Event from NBI.
*
* @param config TAPI Connectivity config for the event
*/
public void processTapiEvent(TapiConnectivityConfig config) {
checkNotNull(config, "Config can't be null");
Key key = Key.of(config.uuid(), appId);
// Setup the Intent
if (config.isSetup()) {
log.debug("TAPI config: {} to setup intent", config);
Intent intent = createOpticalIntent(config.leftCp(), config.rightCp(), key, appId);
intentService.submit(intent);
} else {
// Release the intent
Intent intent = intentService.getIntent(key);
if (intent == null) {
log.error("Intent for uuid {} does not exist", config.uuid());
return;
}
log.debug("TAPI config: {} to purge intent {}", config, intent);
CountDownLatch latch = new CountDownLatch(1);
IntentListener listener = new DeleteListener(key, latch);
intentService.addListener(listener);
try {
/*
* RCAS: Note, withdraw is asynchronous. We cannot call purge
* directly, because at this point it remains in the "INSTALLED"
* state.
*/
intentService.withdraw(intent);
/*
* org.onosproject.onos-core-net - 2.1.0.SNAPSHOT |
* Purge for intent 0x0 is rejected because intent state is INSTALLED
* intentService.purge(intent);
*/
try {
latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// double check the state
IntentState state = intentService.getIntentState(key);
if (state == WITHDRAWN || state == FAILED) {
intentService.purge(intent);
}
} finally {
intentService.removeListener(listener);
}
}
}
use of org.onosproject.net.intent.IntentListener in project onos by opennetworkinglab.
the class VirtualNetworkIntentRemoveCommand method removeIntent.
/**
* Removes the intent using the specified intentService.
*
* @param intentService intent service
* @param intent intent
*/
private void removeIntent(IntentService intentService, Intent intent) {
IntentListener listener = null;
Key key = intent.key();
final CountDownLatch withdrawLatch, purgeLatch;
if (purgeAfterRemove || sync) {
// set up latch and listener to track uninstall progress
withdrawLatch = new CountDownLatch(1);
purgeLatch = purgeAfterRemove ? new CountDownLatch(1) : null;
listener = (IntentEvent event) -> {
if (Objects.equals(event.subject().key(), key)) {
if (event.type() == IntentEvent.Type.WITHDRAWN || event.type() == IntentEvent.Type.FAILED) {
withdrawLatch.countDown();
} else if (purgeLatch != null && purgeAfterRemove && event.type() == IntentEvent.Type.PURGED) {
purgeLatch.countDown();
}
}
};
intentService.addListener(listener);
} else {
purgeLatch = null;
withdrawLatch = null;
}
// request the withdraw
intentService.withdraw(intent);
if ((purgeAfterRemove || sync) && purgeLatch != null) {
try {
// wait for withdraw event
withdrawLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
print("Timed out waiting for intent {} withdraw", key);
}
if (purgeAfterRemove && CAN_PURGE.contains(intentService.getIntentState(key))) {
intentService.purge(intent);
if (sync) {
/* TODO
Technically, the event comes before map.remove() is called.
If we depend on sync and purge working together, we will
need to address this.
*/
try {
purgeLatch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
print("Timed out waiting for intent {} purge", key);
}
}
}
}
if (listener != null) {
// clean up the listener
intentService.removeListener(listener);
}
}
use of org.onosproject.net.intent.IntentListener in project onos by opennetworkinglab.
the class WipeOutCommand method wipeOutIntents.
private void wipeOutIntents() {
print("Wiping intents");
IntentService intentService = get(IntentService.class);
Set<Key> keysToWithdrawn = Sets.newConcurrentHashSet();
Set<Intent> intentsToWithdrawn = Tools.stream(intentService.getIntents()).filter(intent -> intentService.getIntentState(intent.key()) != WITHDRAWN).collect(Collectors.toSet());
intentsToWithdrawn.stream().map(Intent::key).forEach(keysToWithdrawn::add);
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
IntentListener listener = e -> {
if (e.type() == IntentEvent.Type.WITHDRAWN) {
keysToWithdrawn.remove(e.subject().key());
}
if (keysToWithdrawn.isEmpty()) {
completableFuture.complete(null);
}
};
intentService.addListener(listener);
intentsToWithdrawn.forEach(intentService::withdraw);
try {
if (!intentsToWithdrawn.isEmpty()) {
// Wait 1.5 seconds for each Intent
completableFuture.get(intentsToWithdrawn.size() * 1500L, TimeUnit.MILLISECONDS);
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
print("Encountered exception while withdrawing intents: " + e.toString());
} finally {
intentService.removeListener(listener);
}
intentsToWithdrawn.forEach(intentService::purge);
}
Aggregations