Search in sources :

Example 1 with IntentListener

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);
    }
}
Also used : IntentListener(org.onosproject.net.intent.IntentListener) CountDownLatch(java.util.concurrent.CountDownLatch) Key(org.onosproject.net.intent.Key) IntentEvent(org.onosproject.net.intent.IntentEvent)

Example 2 with IntentListener

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();
}
Also used : IntentService(org.onosproject.net.intent.IntentService) IntentState(org.onosproject.net.intent.IntentState) CoreService(org.onosproject.core.CoreService) Intent(org.onosproject.net.intent.Intent) ApplicationId(org.onosproject.core.ApplicationId) CountDownLatch(java.util.concurrent.CountDownLatch) IntentListener(org.onosproject.net.intent.IntentListener) Key(org.onosproject.net.intent.Key) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 3 with IntentListener

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);
        }
    }
}
Also used : IntentState(org.onosproject.net.intent.IntentState) Intent(org.onosproject.net.intent.Intent) OpticalConnectivityIntent(org.onosproject.net.intent.OpticalConnectivityIntent) CountDownLatch(java.util.concurrent.CountDownLatch) IntentListener(org.onosproject.net.intent.IntentListener) Key(org.onosproject.net.intent.Key)

Example 4 with IntentListener

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);
    }
}
Also used : IntentListener(org.onosproject.net.intent.IntentListener) CountDownLatch(java.util.concurrent.CountDownLatch) Key(org.onosproject.net.intent.Key) IntentEvent(org.onosproject.net.intent.IntentEvent)

Example 5 with IntentListener

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);
}
Also used : NetworkConfigService(org.onosproject.net.config.NetworkConfigService) LinkAdminService(org.onosproject.net.link.LinkAdminService) Tools(org.onlab.util.Tools) Host(org.onosproject.net.Host) IntentEvent(org.onosproject.net.intent.IntentEvent) UiExtensionService(org.onosproject.ui.UiExtensionService) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) Link(org.onosproject.net.Link) Command(org.apache.karaf.shell.api.action.Command) PacketRequest(org.onosproject.net.packet.PacketRequest) FlowRuleService(org.onosproject.net.flow.FlowRuleService) IntentService(org.onosproject.net.intent.IntentService) MeterService(org.onosproject.net.meter.MeterService) UiTopoLayoutService(org.onosproject.ui.UiTopoLayoutService) Intent(org.onosproject.net.intent.Intent) Device(org.onosproject.net.Device) GroupService(org.onosproject.net.group.GroupService) Set(java.util.Set) Argument(org.apache.karaf.shell.api.action.Argument) HostAdminService(org.onosproject.net.host.HostAdminService) PacketService(org.onosproject.net.packet.PacketService) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) DeviceAdminService(org.onosproject.net.device.DeviceAdminService) Key(org.onosproject.net.intent.Key) WITHDRAWN(org.onosproject.net.intent.IntentState.WITHDRAWN) IntentListener(org.onosproject.net.intent.IntentListener) Service(org.apache.karaf.shell.api.action.lifecycle.Service) RegionAdminService(org.onosproject.net.region.RegionAdminService) IntentService(org.onosproject.net.intent.IntentService) Intent(org.onosproject.net.intent.Intent) CompletableFuture(java.util.concurrent.CompletableFuture) IntentListener(org.onosproject.net.intent.IntentListener) ExecutionException(java.util.concurrent.ExecutionException) Key(org.onosproject.net.intent.Key) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

IntentListener (org.onosproject.net.intent.IntentListener)5 Key (org.onosproject.net.intent.Key)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Intent (org.onosproject.net.intent.Intent)3 IntentEvent (org.onosproject.net.intent.IntentEvent)3 IntentService (org.onosproject.net.intent.IntentService)2 IntentState (org.onosproject.net.intent.IntentState)2 Sets (com.google.common.collect.Sets)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Command (org.apache.karaf.shell.api.action.Command)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 Tools (org.onlab.util.Tools)1