Search in sources :

Example 1 with ApplicationState

use of org.onosproject.app.ApplicationState in project onos by opennetworkinglab.

the class ApplicationNameCompleter method complete.

@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
    // Delegate string completer
    StringsCompleter delegate = new StringsCompleter();
    // Command name is the second argument.
    String cmd = commandLine.getArguments()[1];
    // Fetch our service and feed it's offerings to the string completer
    ApplicationService service = get(ApplicationService.class);
    Iterator<Application> it = service.getApplications().iterator();
    SortedSet<String> strings = delegate.getStrings();
    while (it.hasNext()) {
        Application app = it.next();
        ApplicationState state = service.getState(app.id());
        if ("uninstall".equals(cmd) || "download".equals(cmd) || ("activate".equals(cmd) && state == INSTALLED) || ("deactivate".equals(cmd) && state == ACTIVE)) {
            strings.add(app.id().name());
        }
    }
    // add unique suffix to candidates, if user has something in buffer
    if (!Strings.isNullOrEmpty(commandLine.getCursorArgument())) {
        List<String> suffixCandidates = strings.stream().map(full -> full.replaceFirst("org\\.onosproject\\.", "")).flatMap(appName -> {
            List<String> suffixes = new ArrayList<>();
            Deque<String> frags = new ArrayDeque<>();
            // a.b.c -> [c, b, a] -> [c, b.c, a.b.c]
            Lists.reverse(asList(appName.split("\\."))).forEach(frag -> {
                frags.addFirst(frag);
                suffixes.add(frags.stream().collect(Collectors.joining(".")));
            });
            return suffixes.stream();
        }).collect(Collectors.groupingBy(e -> e, Collectors.counting())).entrySet().stream().filter(e -> e.getValue() == 1L).map(Entry::getKey).collect(Collectors.toList());
        delegate.getStrings().addAll(suffixCandidates);
    }
    // Now let the completer do the work for figuring out what to offer.
    return delegate.complete(session, commandLine, candidates);
}
Also used : Session(org.apache.karaf.shell.api.console.Session) ApplicationState(org.onosproject.app.ApplicationState) Iterator(java.util.Iterator) SortedSet(java.util.SortedSet) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) ApplicationService(org.onosproject.app.ApplicationService) Deque(java.util.Deque) Collectors(java.util.stream.Collectors) AbstractShellCommand.get(org.onosproject.cli.AbstractShellCommand.get) CommandLine(org.apache.karaf.shell.api.console.CommandLine) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) List(java.util.List) AbstractCompleter(org.onosproject.cli.AbstractCompleter) Lists(com.google.common.collect.Lists) ACTIVE(org.onosproject.app.ApplicationState.ACTIVE) Arrays.asList(java.util.Arrays.asList) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Entry(java.util.Map.Entry) INSTALLED(org.onosproject.app.ApplicationState.INSTALLED) ArrayDeque(java.util.ArrayDeque) Application(org.onosproject.core.Application) StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) ApplicationState(org.onosproject.app.ApplicationState) ArrayList(java.util.ArrayList) Application(org.onosproject.core.Application) ArrayDeque(java.util.ArrayDeque) ApplicationService(org.onosproject.app.ApplicationService)

Example 2 with ApplicationState

use of org.onosproject.app.ApplicationState in project onos by opennetworkinglab.

the class ReviewApplicationNameCompleter method complete.

@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
    // Delegate string completer
    StringsCompleter delegate = new StringsCompleter();
    ApplicationService service = get(ApplicationService.class);
    Iterator<Application> it = service.getApplications().iterator();
    SortedSet<String> strings = delegate.getStrings();
    while (it.hasNext()) {
        Application app = it.next();
        ApplicationState state = service.getState(app.id());
        // }
        if (state == INSTALLED) {
            strings.add(app.id().name());
        }
    }
    // Now let the completer do the work for figuring out what to offer.
    return delegate.complete(session, commandLine, candidates);
}
Also used : StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) ApplicationState(org.onosproject.app.ApplicationState) Application(org.onosproject.core.Application) ApplicationService(org.onosproject.app.ApplicationService)

Aggregations

StringsCompleter (org.apache.karaf.shell.support.completers.StringsCompleter)2 ApplicationService (org.onosproject.app.ApplicationService)2 ApplicationState (org.onosproject.app.ApplicationState)2 Application (org.onosproject.core.Application)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Deque (java.util.Deque)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 SortedSet (java.util.SortedSet)1 Collectors (java.util.stream.Collectors)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 CommandLine (org.apache.karaf.shell.api.console.CommandLine)1 Session (org.apache.karaf.shell.api.console.Session)1 ACTIVE (org.onosproject.app.ApplicationState.ACTIVE)1 INSTALLED (org.onosproject.app.ApplicationState.INSTALLED)1