blob: 17cf89e2134f80edf9215ad1cb57ef7d24518593 [file] [log] [blame]
Thomas Vachuska90b453f2015-01-30 18:57:14 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cli.app;
17
Ray Milkey3078fc02015-05-06 16:14:14 -070018import java.util.Collections;
19import java.util.List;
20
Thomas Vachuska90b453f2015-01-30 18:57:14 -080021import org.apache.karaf.shell.commands.Command;
Thomas Vachuskafba28572015-03-25 18:48:59 -070022import org.apache.karaf.shell.commands.Option;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080023import org.onosproject.app.ApplicationService;
24import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080025import org.onosproject.cli.Comparators;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080026import org.onosproject.core.Application;
27
Ray Milkey3078fc02015-05-06 16:14:14 -070028import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080031
32import static com.google.common.collect.Lists.newArrayList;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080033import static org.onosproject.app.ApplicationState.ACTIVE;
34
35/**
36 * Lists application information.
37 */
38@Command(scope = "onos", name = "apps",
39 description = "Lists application information")
40public class ApplicationsListCommand extends AbstractShellCommand {
41
42 private static final String FMT =
43 "%s id=%d, name=%s, version=%s, origin=%s, description=%s, " +
44 "features=%s, featuresRepo=%s, permissions=%s";
45
Thomas Vachuska8ceff302015-04-03 11:45:53 -070046 private static final String SHORT_FMT =
Thomas Vachuskace9808a2015-04-21 09:33:26 -070047 "%s %3d %-32s %-8s %s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070048
49 @Option(name = "-s", aliases = "--short", description = "Show short output only",
50 required = false, multiValued = false)
51 private boolean shortOnly = false;
52
Thomas Vachuskafba28572015-03-25 18:48:59 -070053 @Option(name = "-a", aliases = "--active", description = "Show active only",
54 required = false, multiValued = false)
55 private boolean activeOnly = false;
56
57
Thomas Vachuska90b453f2015-01-30 18:57:14 -080058 @Override
59 protected void execute() {
60 ApplicationService service = get(ApplicationService.class);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080061 List<Application> apps = newArrayList(service.getApplications());
62 Collections.sort(apps, Comparators.APP_COMPARATOR);
63
64 if (outputJson()) {
65 print("%s", json(service, apps));
66 } else {
67 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070068 boolean isActive = service.getState(app.id()) == ACTIVE;
69 if (activeOnly && isActive || !activeOnly) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -070070 if (shortOnly) {
71 print(SHORT_FMT, isActive ? "*" : " ",
72 app.id().id(), app.id().name(), app.version(),
Thomas Vachuskace9808a2015-04-21 09:33:26 -070073 app.description());
Thomas Vachuska8ceff302015-04-03 11:45:53 -070074 } else {
75 print(FMT, isActive ? "*" : " ",
76 app.id().id(), app.id().name(), app.version(), app.origin(),
77 app.description(), app.features(),
78 app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "",
79 app.permissions());
80 }
Thomas Vachuskafba28572015-03-25 18:48:59 -070081 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080082 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080083 }
84 }
85
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080086 private JsonNode json(ApplicationService service, List<Application> apps) {
87 ObjectMapper mapper = new ObjectMapper();
88 ArrayNode result = mapper.createArrayNode();
89 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070090 boolean isActive = service.getState(app.id()) == ACTIVE;
91 if (activeOnly && isActive || !activeOnly) {
Ray Milkey3078fc02015-05-06 16:14:14 -070092 result.add(jsonForEntity(app, Application.class));
Thomas Vachuskafba28572015-03-25 18:48:59 -070093 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080094 }
95 return result;
96 }
97
Ray Milkey3078fc02015-05-06 16:14:14 -070098
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080099
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800100}