blob: c518b4355e981ba24830ededc9b8cdeea6b683c8 [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
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
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
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080028import java.util.Collections;
29import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080032import static org.onosproject.app.ApplicationState.ACTIVE;
33
34/**
35 * Lists application information.
36 */
37@Command(scope = "onos", name = "apps",
38 description = "Lists application information")
39public class ApplicationsListCommand extends AbstractShellCommand {
40
41 private static final String FMT =
42 "%s id=%d, name=%s, version=%s, origin=%s, description=%s, " +
43 "features=%s, featuresRepo=%s, permissions=%s";
44
Thomas Vachuska8ceff302015-04-03 11:45:53 -070045 private static final String SHORT_FMT =
Thomas Vachuskace9808a2015-04-21 09:33:26 -070046 "%s %3d %-32s %-8s %s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070047
48 @Option(name = "-s", aliases = "--short", description = "Show short output only",
49 required = false, multiValued = false)
50 private boolean shortOnly = false;
51
Thomas Vachuskafba28572015-03-25 18:48:59 -070052 @Option(name = "-a", aliases = "--active", description = "Show active only",
53 required = false, multiValued = false)
54 private boolean activeOnly = false;
55
56
Thomas Vachuska90b453f2015-01-30 18:57:14 -080057 @Override
58 protected void execute() {
59 ApplicationService service = get(ApplicationService.class);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080060 List<Application> apps = newArrayList(service.getApplications());
61 Collections.sort(apps, Comparators.APP_COMPARATOR);
62
63 if (outputJson()) {
64 print("%s", json(service, apps));
65 } else {
66 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070067 boolean isActive = service.getState(app.id()) == ACTIVE;
68 if (activeOnly && isActive || !activeOnly) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -070069 if (shortOnly) {
70 print(SHORT_FMT, isActive ? "*" : " ",
71 app.id().id(), app.id().name(), app.version(),
Thomas Vachuskace9808a2015-04-21 09:33:26 -070072 app.description());
Thomas Vachuska8ceff302015-04-03 11:45:53 -070073 } else {
74 print(FMT, isActive ? "*" : " ",
75 app.id().id(), app.id().name(), app.version(), app.origin(),
76 app.description(), app.features(),
77 app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "",
78 app.permissions());
79 }
Thomas Vachuskafba28572015-03-25 18:48:59 -070080 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080081 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080082 }
83 }
84
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080085 private JsonNode json(ApplicationService service, List<Application> apps) {
86 ObjectMapper mapper = new ObjectMapper();
87 ArrayNode result = mapper.createArrayNode();
88 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070089 boolean isActive = service.getState(app.id()) == ACTIVE;
90 if (activeOnly && isActive || !activeOnly) {
91 result.add(json(service, mapper, app));
92 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080093 }
94 return result;
95 }
96
97 protected JsonNode json(ApplicationService service, ObjectMapper mapper,
98 Application app) {
99 return mapper.createObjectNode()
100 .put("name", app.id().name())
101 .put("id", app.id().id())
102 .put("version", app.version().toString())
103 .put("description", app.description())
104 .put("origin", app.origin())
105 .put("permissions", app.permissions().toString())
106 .put("featuresRepo", app.featuresRepo().isPresent() ?
107 app.featuresRepo().get().toString() : "")
108 .put("features", app.features().toString())
109 .put("state", service.getState(app.id()).toString());
110 }
111
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800112}