blob: 9b2fb2afca83a9a63ed6ccff9e18c2a89bdb120f [file] [log] [blame]
Thomas Vachuska90b453f2015-01-30 18:57:14 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska90b453f2015-01-30 18:57:14 -08003 *
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
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080018import java.net.URI;
Ray Milkey3078fc02015-05-06 16:14:14 -070019import java.util.Collections;
Yuta HIGUCHI9892d1a2017-07-07 16:31:30 -070020import java.util.Comparator;
Ray Milkey3078fc02015-05-06 16:14:14 -070021import java.util.List;
22
Thomas Vachuska90b453f2015-01-30 18:57:14 -080023import org.apache.karaf.shell.commands.Command;
Thomas Vachuskafba28572015-03-25 18:48:59 -070024import org.apache.karaf.shell.commands.Option;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080025import org.onosproject.app.ApplicationService;
26import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080027import org.onosproject.utils.Comparators;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080028import org.onosproject.core.Application;
29
Ray Milkey3078fc02015-05-06 16:14:14 -070030import com.fasterxml.jackson.databind.JsonNode;
31import com.fasterxml.jackson.databind.ObjectMapper;
32import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080033
34import static com.google.common.collect.Lists.newArrayList;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080035import static org.onosproject.app.ApplicationState.ACTIVE;
36
37/**
38 * Lists application information.
39 */
40@Command(scope = "onos", name = "apps",
41 description = "Lists application information")
42public class ApplicationsListCommand extends AbstractShellCommand {
43
44 private static final String FMT =
Jian Li02c109c2016-01-19 19:24:44 -080045 "%s id=%d, name=%s, version=%s, origin=%s, category=%s, description=%s, " +
46 "features=%s, featuresRepo=%s, apps=%s, permissions=%s, url=%s";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080047
Thomas Vachuska8ceff302015-04-03 11:45:53 -070048 private static final String SHORT_FMT =
Jian Li02c109c2016-01-19 19:24:44 -080049 "%s %3d %-36s %-8s %s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070050
51 @Option(name = "-s", aliases = "--short", description = "Show short output only",
52 required = false, multiValued = false)
53 private boolean shortOnly = false;
54
Thomas Vachuskafba28572015-03-25 18:48:59 -070055 @Option(name = "-a", aliases = "--active", description = "Show active only",
56 required = false, multiValued = false)
57 private boolean activeOnly = false;
58
Yuta HIGUCHI9892d1a2017-07-07 16:31:30 -070059 @Option(name = "-n", aliases = "--name", description = "Sort by application ID name")
60 private boolean sortByName = false;
61
Thomas Vachuskafba28572015-03-25 18:48:59 -070062
Thomas Vachuska90b453f2015-01-30 18:57:14 -080063 @Override
64 protected void execute() {
65 ApplicationService service = get(ApplicationService.class);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080066 List<Application> apps = newArrayList(service.getApplications());
Yuta HIGUCHI9892d1a2017-07-07 16:31:30 -070067 if (sortByName) {
68 apps.sort(Comparator.comparing(app -> app.id().name()));
69 } else {
70 Collections.sort(apps, Comparators.APP_COMPARATOR);
71 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080072
73 if (outputJson()) {
74 print("%s", json(service, apps));
75 } else {
76 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070077 boolean isActive = service.getState(app.id()) == ACTIVE;
78 if (activeOnly && isActive || !activeOnly) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -070079 if (shortOnly) {
Andrea Campanella915e6c82016-03-09 17:55:41 -080080 String shortDescription = app.title().equals(app.id().name()) ?
81 app.description().replaceAll("[\\r\\n]", " ").replaceAll(" +", " ") :
82 app.title();
Thomas Vachuska8ceff302015-04-03 11:45:53 -070083 print(SHORT_FMT, isActive ? "*" : " ",
Andrea Campanella915e6c82016-03-09 17:55:41 -080084 app.id().id(), app.id().name(), app.version(), shortDescription);
Thomas Vachuska8ceff302015-04-03 11:45:53 -070085 } else {
86 print(FMT, isActive ? "*" : " ",
87 app.id().id(), app.id().name(), app.version(), app.origin(),
Jian Li02c109c2016-01-19 19:24:44 -080088 app.category(), app.description(), app.features(),
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080089 app.featuresRepo().map(URI::toString).orElse(""),
Jian Li02c109c2016-01-19 19:24:44 -080090 app.requiredApps(), app.permissions(), app.url());
Thomas Vachuska8ceff302015-04-03 11:45:53 -070091 }
Thomas Vachuskafba28572015-03-25 18:48:59 -070092 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080093 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080094 }
95 }
96
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080097 private JsonNode json(ApplicationService service, List<Application> apps) {
98 ObjectMapper mapper = new ObjectMapper();
99 ArrayNode result = mapper.createArrayNode();
100 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -0700101 boolean isActive = service.getState(app.id()) == ACTIVE;
102 if (activeOnly && isActive || !activeOnly) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700103 result.add(jsonForEntity(app, Application.class));
Thomas Vachuskafba28572015-03-25 18:48:59 -0700104 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800105 }
106 return result;
107 }
108
Ray Milkey3078fc02015-05-06 16:14:14 -0700109
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800110
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800111}