blob: d9debdb000464e6dbdb62927edd2455e2030131b [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 Vachuskafba28572015-03-25 18:48:59 -070045 @Option(name = "-a", aliases = "--active", description = "Show active only",
46 required = false, multiValued = false)
47 private boolean activeOnly = false;
48
49
Thomas Vachuska90b453f2015-01-30 18:57:14 -080050 @Override
51 protected void execute() {
52 ApplicationService service = get(ApplicationService.class);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080053 List<Application> apps = newArrayList(service.getApplications());
54 Collections.sort(apps, Comparators.APP_COMPARATOR);
55
56 if (outputJson()) {
57 print("%s", json(service, apps));
58 } else {
59 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070060 boolean isActive = service.getState(app.id()) == ACTIVE;
61 if (activeOnly && isActive || !activeOnly) {
62 print(FMT, isActive ? "*" : " ",
63 app.id().id(), app.id().name(), app.version(), app.origin(),
64 app.description(), app.features(),
65 app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "",
66 app.permissions());
67 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080068 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080069 }
70 }
71
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080072 private JsonNode json(ApplicationService service, List<Application> apps) {
73 ObjectMapper mapper = new ObjectMapper();
74 ArrayNode result = mapper.createArrayNode();
75 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070076 boolean isActive = service.getState(app.id()) == ACTIVE;
77 if (activeOnly && isActive || !activeOnly) {
78 result.add(json(service, mapper, app));
79 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080080 }
81 return result;
82 }
83
84 protected JsonNode json(ApplicationService service, ObjectMapper mapper,
85 Application app) {
86 return mapper.createObjectNode()
87 .put("name", app.id().name())
88 .put("id", app.id().id())
89 .put("version", app.version().toString())
90 .put("description", app.description())
91 .put("origin", app.origin())
92 .put("permissions", app.permissions().toString())
93 .put("featuresRepo", app.featuresRepo().isPresent() ?
94 app.featuresRepo().get().toString() : "")
95 .put("features", app.features().toString())
96 .put("state", service.getState(app.id()).toString());
97 }
98
Thomas Vachuska90b453f2015-01-30 18:57:14 -080099}