Search in sources :

Example 1 with IntentState

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);
            }
        }
    }
}
Also used : StringFilter(org.onlab.util.StringFilter) StringUtils(org.apache.commons.lang.StringUtils) Tools(org.onlab.util.Tools) IntentState(org.onosproject.net.intent.IntentState) HashMap(java.util.HashMap) SinglePointToMultiPointIntent(org.onosproject.net.intent.SinglePointToMultiPointIntent) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Command(org.apache.karaf.shell.api.action.Command) ConnectPoint(org.onosproject.net.ConnectPoint) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) TrafficSelector(org.onosproject.net.flow.TrafficSelector) OpticalCircuitIntent(org.onosproject.net.intent.OpticalCircuitIntent) MultiPointToSinglePointIntent(org.onosproject.net.intent.MultiPointToSinglePointIntent) IntentService(org.onosproject.net.intent.IntentService) Map(java.util.Map) Intent(org.onosproject.net.intent.Intent) JsonNode(com.fasterxml.jackson.databind.JsonNode) WorkPartitionService(org.onosproject.net.intent.WorkPartitionService) Criterion(org.onosproject.net.flow.criteria.Criterion) NodeId(org.onosproject.cluster.NodeId) PathIntent(org.onosproject.net.intent.PathIntent) HostToHostIntent(org.onosproject.net.intent.HostToHostIntent) WordUtils.uncapitalize(org.apache.commons.lang3.text.WordUtils.uncapitalize) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) LinkCollectionIntent(org.onosproject.net.intent.LinkCollectionIntent) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) Key(org.onosproject.net.intent.Key) List(java.util.List) PointToPointIntent(org.onosproject.net.intent.PointToPointIntent) Service(org.apache.karaf.shell.api.action.lifecycle.Service) ConnectivityIntent(org.onosproject.net.intent.ConnectivityIntent) OpticalOduIntent(org.onosproject.net.intent.OpticalOduIntent) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) Option(org.apache.karaf.shell.api.action.Option) OpticalConnectivityIntent(org.onosproject.net.intent.OpticalConnectivityIntent) FilteredConnectPoint(org.onosproject.net.FilteredConnectPoint) IntentService(org.onosproject.net.intent.IntentService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) WorkPartitionService(org.onosproject.net.intent.WorkPartitionService) SinglePointToMultiPointIntent(org.onosproject.net.intent.SinglePointToMultiPointIntent) OpticalCircuitIntent(org.onosproject.net.intent.OpticalCircuitIntent) MultiPointToSinglePointIntent(org.onosproject.net.intent.MultiPointToSinglePointIntent) Intent(org.onosproject.net.intent.Intent) PathIntent(org.onosproject.net.intent.PathIntent) HostToHostIntent(org.onosproject.net.intent.HostToHostIntent) LinkCollectionIntent(org.onosproject.net.intent.LinkCollectionIntent) PointToPointIntent(org.onosproject.net.intent.PointToPointIntent) ConnectivityIntent(org.onosproject.net.intent.ConnectivityIntent) OpticalOduIntent(org.onosproject.net.intent.OpticalOduIntent) OpticalConnectivityIntent(org.onosproject.net.intent.OpticalConnectivityIntent) IntentState(org.onosproject.net.intent.IntentState) StringFilter(org.onlab.util.StringFilter)

Example 2 with IntentState

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;
    }
}
Also used : IntentState(org.onosproject.net.intent.IntentState)

Example 3 with IntentState

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);
                }
            }
        }
    }
}
Also used : IntentState(org.onosproject.net.intent.IntentState) Intent(org.onosproject.net.intent.Intent) PointToPointIntent(org.onosproject.net.intent.PointToPointIntent) Key(org.onosproject.net.intent.Key) GroupKey(org.onosproject.net.group.GroupKey)

Example 4 with IntentState

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());
}
Also used : IntentState(org.onosproject.net.intent.IntentState) FlowRuleIntent(org.onosproject.net.intent.FlowRuleIntent) Intent(org.onosproject.net.intent.Intent) PathIntent(org.onosproject.net.intent.PathIntent) MockIntent(org.onosproject.net.intent.IntentTestsMocks.MockIntent)

Example 5 with IntentState

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();
}
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)

Aggregations

IntentState (org.onosproject.net.intent.IntentState)10 Intent (org.onosproject.net.intent.Intent)8 Key (org.onosproject.net.intent.Key)6 IntentService (org.onosproject.net.intent.IntentService)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 HashMap (java.util.HashMap)3 NodeId (org.onosproject.cluster.NodeId)3 HostToHostIntent (org.onosproject.net.intent.HostToHostIntent)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)2 Strings (com.google.common.base.Strings)2 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2