Java Code Examples for org.onosproject.net.intent.IntentService#getIntent()

The following examples show how to use org.onosproject.net.intent.IntentService#getIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntentsWebResource.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Gets intent installables by application ID and key.
 * @param appId application identifier
 * @param key   intent key
 *
 * @return 200 OK with array of the intent installables
 * @onos.rsModel Intents
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("installables/{appId}/{key}")
public Response getIntentWithInstallable(@PathParam("appId") String appId,
                                         @PathParam("key") String key) {
    final IntentService intentService = get(IntentService.class);
    final ApplicationId app = get(CoreService.class).getAppId(appId);
    nullIsNotFound(app, APP_ID_NOT_FOUND);

    Intent intent = intentService.getIntent(Key.of(key, app));
    if (intent == null) {
        long numericalKey = Long.decode(key);
        intent = intentService.getIntent(Key.of(numericalKey, app));
    }
    nullIsNotFound(intent, INTENT_NOT_FOUND);

    final Iterable<Intent> installables = intentService.getInstallableIntents(intent.key());
    final ObjectNode root = encodeArray(Intent.class, "installables", installables);
    return ok(root).build();
}
 
Example 2
Source File: OpticalIntentsWebResource.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Delete the specified optical intent.
 *
 * @param appId application identifier
 * @param keyString   intent key
 * @return 204 NO CONTENT
 */
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Path("{appId}/{key}")
public Response deleteIntent(@PathParam("appId") String appId,
                             @PathParam("key") String keyString) {

    final ApplicationId app = get(CoreService.class).getAppId(appId);
    nullIsNotFound(app, "Application Id not found");

    IntentService intentService = get(IntentService.class);
    Intent intent = intentService.getIntent(Key.of(keyString, app));
    if (intent == null) {
        intent = intentService.getIntent(Key.of(Long.decode(keyString), app));
    }
    nullIsNotFound(intent, "Intent Id is not found");

    if ((intent instanceof OpticalConnectivityIntent) || (intent instanceof OpticalCircuitIntent)) {
        intentService.withdraw(intent);
    } else {
        throw new IllegalArgumentException("Specified intent is not of type OpticalConnectivityIntent");
    }

    return Response.noContent().build();
}
 
Example 3
Source File: IntentsWebResource.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Gets all related flow entries created by a particular intent.
 * Returns all flow entries of the specified intent.
 *
 * @param appId application identifier
 * @param key   intent key
 * @return 200 OK with intent data
 * @onos.rsModel Relatedflows
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("relatedflows/{appId}/{key}")
public Response getIntentFlowsById(@PathParam("appId") String appId,
                                   @PathParam("key") String key) {
    ApplicationId applicationId = get(CoreService.class).getAppId(appId);
    nullIsNotFound(applicationId, APP_ID_NOT_FOUND);
    IntentService intentService = get(IntentService.class);
    FlowRuleService flowService = get(FlowRuleService.class);

    Intent intent = intentService.getIntent(Key.of(key, applicationId));
    if (intent == null) {
        long numericalKey = Long.decode(key);
        intent = intentService.getIntent(
                Key.of(numericalKey, applicationId));
    }
    nullIsNotFound(intent, INTENT_NOT_FOUND);

    ObjectNode root = mapper().createObjectNode();
    root.put(APP_ID, appId);
    root.put(ID, key);

    IntentFilter intentFilter = new IntentFilter(intentService, flowService);

    List<Intent> installables =
            intentService.getInstallableIntents(intent.key());

    if (intent instanceof HostToHostIntent) {
        root.put(INTENT_TYPE, HOST_TO_HOST_INTENT);
    } else if (intent instanceof PointToPointIntent) {
        root.put(INTENT_TYPE, POINT_TO_POINT_INTENT);
    } else if (intent instanceof SinglePointToMultiPointIntent) {
        root.put(INTENT_TYPE, SINGLE_TO_MULTI_POINT_INTENT);
    } else if (intent instanceof MultiPointToSinglePointIntent) {
        root.put(INTENT_TYPE, MULTI_TO_SINGLE_POINT_INTENT);
    } else {
        root.put(INTENT_TYPE, INTENT);
    }

    ArrayNode pathsNode = root.putArray(INTENT_PATHS);

    for (List<FlowEntry> flowEntries :
            intentFilter.readIntentFlows(installables)) {
        ArrayNode flowNode = pathsNode.addArray();

        for (FlowEntry entry : flowEntries) {
            flowNode.add(codec(FlowEntry.class).encode(entry, this));
        }
    }
    return ok(root).build();
}
 
Example 4
Source File: IntentsWebResource.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * 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) {
        // No such intent.  REST standards recommend a positive status code
        // 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();
}
 
Example 5
Source File: IntentRemoveCommand.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Removes the intents passed as argument, assuming these
 * belong to the application's ID provided (if any) and
 * contain a key string.
 *
 * If an application ID is provided, it will be used to further
 * filter the intents to be removed.
 *
 * @param intents list of intents to remove
 * @param applicationIdString application ID to filter intents
 * @param keyString string to filter intents
 * @param purgeAfterRemove states whether the intents should be also purged
 * @param sync states whether the cli should wait for the operation to finish
 *             before returning
 */
private void removeIntent(Iterable<Intent> intents,
                         String applicationIdString, String keyString,
                         boolean purgeAfterRemove, boolean sync) {
    IntentService intentService = get(IntentService.class);
    CoreService coreService = get(CoreService.class);
    this.applicationIdString = applicationIdString;
    this.keyString = keyString;
    this.purgeAfterRemove = purgeAfterRemove;
    this.sync = sync;
    if (purgeAfterRemove || sync) {
        print("Using \"sync\" to remove/purge intents - this may take a while...");
        print("Check \"summary\" to see remove/purge progress.");
    }

    ApplicationId appId = appId();
    if (!isNullOrEmpty(applicationIdString)) {
        appId = coreService.getAppId(applicationIdString);
        if (appId == null) {
            print("Cannot find application Id %s", applicationIdString);
            return;
        }
    }

    if (isNullOrEmpty(keyString)) {
        removeIntentsByAppId(intentService, intents, appId);

    } else {
        final Key key;
        if (keyString.startsWith("0x")) {
            // The intent uses a LongKey
            keyString = keyString.replaceFirst("0x", "");
            key = Key.of(new BigInteger(keyString, 16).longValue(), appId);
        } else {
            // The intent uses a StringKey
            key = Key.of(keyString, appId);
        }

        Intent intent = intentService.getIntent(key);
        if (intent != null) {
            removeIntent(intentService, intent);
        }
    }
}