Search in sources :

Example 6 with Application

use of org.onosproject.core.Application in project onos by opennetworkinglab.

the class ApplicationsWebResource method installApp.

/**
 * Install a new application.
 * Uploads application archive stream and optionally activates the
 * application.
 *
 * @param raw   json object containing location (url) of application oar
 * @return 200 OK; 404; 401
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response installApp(InputStream raw) {
    Application app;
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), raw);
        URL url = new URL(jsonTree.get(URL).asText());
        boolean activate = false;
        if (jsonTree.has(ACTIVATE)) {
            activate = jsonTree.get(ACTIVATE).asBoolean();
        }
        ApplicationAdminService service = get(ApplicationAdminService.class);
        app = service.install(url.openStream());
        if (activate) {
            service.activate(app.id());
        }
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
    return ok(codec(Application.class).encode(app, this)).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IOException(java.io.IOException) Application(org.onosproject.core.Application) URL(java.net.URL) ApplicationAdminService(org.onosproject.app.ApplicationAdminService) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 7 with Application

use of org.onosproject.core.Application in project onos by opennetworkinglab.

the class ApplicationsWebResource method getApps.

/**
 * Get all installed applications.
 * Returns array of all installed applications.
 *
 * @return 200 OK
 * @onos.rsModel Applications
 */
@GET
public Response getApps() {
    ApplicationAdminService service = get(ApplicationAdminService.class);
    Set<Application> apps = service.getApplications();
    return ok(encodeArray(Application.class, "applications", apps)).build();
}
Also used : Application(org.onosproject.core.Application) ApplicationAdminService(org.onosproject.app.ApplicationAdminService) GET(javax.ws.rs.GET)

Example 8 with Application

use of org.onosproject.core.Application in project onos by opennetworkinglab.

the class ApplicationCommand method manageApp.

// Manages the specified application.
private boolean manageApp(ApplicationAdminService service, String name) {
    ApplicationId appId = service.getId(name);
    if (appId == null) {
        List<Application> matches = service.getApplications().stream().filter(app -> app.id().name().matches(".*\\." + name + "$")).collect(Collectors.toList());
        if (matches.size() == 1) {
            // Found match
            appId = matches.iterator().next().id();
        } else if (!matches.isEmpty()) {
            print("Did you mean one of: %s", matches.stream().map(Application::id).map(ApplicationId::name).collect(Collectors.toList()));
            return false;
        }
    }
    if (appId == null) {
        print("No such application: %s", name);
        return false;
    }
    String action;
    if (command.equals(UNINSTALL)) {
        service.uninstall(appId);
        action = "Uninstalled";
    } else if (command.equals(ACTIVATE)) {
        service.activate(appId);
        action = "Activated";
    } else if (command.equals(DEACTIVATE)) {
        service.deactivate(appId);
        action = "Deactivated";
    } else {
        print("Unsupported command: %s", command);
        return false;
    }
    print("%s %s", action, appId.name());
    return true;
}
Also used : URL(java.net.URL) Argument(org.apache.karaf.shell.api.action.Argument) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Command(org.apache.karaf.shell.api.action.Command) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) ApplicationAdminService(org.onosproject.app.ApplicationAdminService) ByteStreams(com.google.common.io.ByteStreams) Service(org.apache.karaf.shell.api.action.lifecycle.Service) ApplicationId(org.onosproject.core.ApplicationId) Completion(org.apache.karaf.shell.api.action.Completion) Application(org.onosproject.core.Application) ApplicationId(org.onosproject.core.ApplicationId) Application(org.onosproject.core.Application)

Example 9 with Application

use of org.onosproject.core.Application 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 10 with Application

use of org.onosproject.core.Application in project onos by opennetworkinglab.

the class AllApplicationNamesCompleter method choices.

@Override
public List<String> choices() {
    // Fetch the service and return the list of app names
    ApplicationService service = get(ApplicationService.class);
    Iterator<Application> it = service.getApplications().iterator();
    // Add each app name to the list of choices.
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false).filter(app -> service.getState(app.id()) == INSTALLED).map(app -> app.id().name()).collect(toList());
}
Also used : AbstractChoicesCompleter(org.onosproject.cli.AbstractChoicesCompleter) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Iterator(java.util.Iterator) Spliterators(java.util.Spliterators) Service(org.apache.karaf.shell.api.action.lifecycle.Service) INSTALLED(org.onosproject.app.ApplicationState.INSTALLED) StreamSupport(java.util.stream.StreamSupport) ApplicationService(org.onosproject.app.ApplicationService) Spliterator(java.util.Spliterator) AbstractShellCommand.get(org.onosproject.cli.AbstractShellCommand.get) Application(org.onosproject.core.Application) Application(org.onosproject.core.Application) ApplicationService(org.onosproject.app.ApplicationService)

Aggregations

Application (org.onosproject.core.Application)32 DefaultApplication (org.onosproject.core.DefaultApplication)12 Test (org.junit.Test)8 List (java.util.List)6 ApplicationAdminService (org.onosproject.app.ApplicationAdminService)6 ApplicationEvent (org.onosproject.app.ApplicationEvent)6 ApplicationId (org.onosproject.core.ApplicationId)6 IOException (java.io.IOException)4 Lists (com.google.common.collect.Lists)3 InputStream (java.io.InputStream)3 Collectors (java.util.stream.Collectors)3 Service (org.apache.karaf.shell.api.action.lifecycle.Service)3 ApplicationService (org.onosproject.app.ApplicationService)3 ApplicationState (org.onosproject.app.ApplicationState)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Charsets (com.google.common.base.Charsets)2 MoreObjects (com.google.common.base.MoreObjects)2 Preconditions (com.google.common.base.Preconditions)2 Throwables (com.google.common.base.Throwables)2