blob: 24b2da17b1fb92d24eb96346aebc461a29b3aa49 [file] [log] [blame]
Thomas Vachuska90b453f2015-01-30 18:57:14 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080026import org.onosproject.app.ApplicationService;
27import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080028import org.onosproject.utils.Comparators;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080029import org.onosproject.core.Application;
30
Ray Milkey3078fc02015-05-06 16:14:14 -070031import com.fasterxml.jackson.databind.JsonNode;
32import com.fasterxml.jackson.databind.ObjectMapper;
33import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080034
35import static com.google.common.collect.Lists.newArrayList;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080036import static org.onosproject.app.ApplicationState.ACTIVE;
37
38/**
39 * Lists application information.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
Thomas Vachuska90b453f2015-01-30 18:57:14 -080042@Command(scope = "onos", name = "apps",
43 description = "Lists application information")
44public class ApplicationsListCommand extends AbstractShellCommand {
45
46 private static final String FMT =
Jian Li02c109c2016-01-19 19:24:44 -080047 "%s id=%d, name=%s, version=%s, origin=%s, category=%s, description=%s, " +
48 "features=%s, featuresRepo=%s, apps=%s, permissions=%s, url=%s";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080049
Thomas Vachuska8ceff302015-04-03 11:45:53 -070050 private static final String SHORT_FMT =
Jian Li02c109c2016-01-19 19:24:44 -080051 "%s %3d %-36s %-8s %s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070052
53 @Option(name = "-s", aliases = "--short", description = "Show short output only",
54 required = false, multiValued = false)
55 private boolean shortOnly = false;
56
Thomas Vachuskafba28572015-03-25 18:48:59 -070057 @Option(name = "-a", aliases = "--active", description = "Show active only",
58 required = false, multiValued = false)
59 private boolean activeOnly = false;
60
Yuta HIGUCHI9892d1a2017-07-07 16:31:30 -070061 @Option(name = "-n", aliases = "--name", description = "Sort by application ID name")
62 private boolean sortByName = false;
63
Thomas Vachuskafba28572015-03-25 18:48:59 -070064
Thomas Vachuska90b453f2015-01-30 18:57:14 -080065 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 protected void doExecute() {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080067 ApplicationService service = get(ApplicationService.class);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080068 List<Application> apps = newArrayList(service.getApplications());
Yuta HIGUCHI9892d1a2017-07-07 16:31:30 -070069 if (sortByName) {
70 apps.sort(Comparator.comparing(app -> app.id().name()));
71 } else {
72 Collections.sort(apps, Comparators.APP_COMPARATOR);
73 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080074
75 if (outputJson()) {
76 print("%s", json(service, apps));
77 } else {
78 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070079 boolean isActive = service.getState(app.id()) == ACTIVE;
80 if (activeOnly && isActive || !activeOnly) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -070081 if (shortOnly) {
Andrea Campanella915e6c82016-03-09 17:55:41 -080082 String shortDescription = app.title().equals(app.id().name()) ?
83 app.description().replaceAll("[\\r\\n]", " ").replaceAll(" +", " ") :
84 app.title();
Thomas Vachuska8ceff302015-04-03 11:45:53 -070085 print(SHORT_FMT, isActive ? "*" : " ",
Andrea Campanella915e6c82016-03-09 17:55:41 -080086 app.id().id(), app.id().name(), app.version(), shortDescription);
Thomas Vachuska8ceff302015-04-03 11:45:53 -070087 } else {
88 print(FMT, isActive ? "*" : " ",
89 app.id().id(), app.id().name(), app.version(), app.origin(),
Jian Li02c109c2016-01-19 19:24:44 -080090 app.category(), app.description(), app.features(),
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080091 app.featuresRepo().map(URI::toString).orElse(""),
Jian Li02c109c2016-01-19 19:24:44 -080092 app.requiredApps(), app.permissions(), app.url());
Thomas Vachuska8ceff302015-04-03 11:45:53 -070093 }
Thomas Vachuskafba28572015-03-25 18:48:59 -070094 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080095 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080096 }
97 }
98
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080099 private JsonNode json(ApplicationService service, List<Application> apps) {
100 ObjectMapper mapper = new ObjectMapper();
101 ArrayNode result = mapper.createArrayNode();
102 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -0700103 boolean isActive = service.getState(app.id()) == ACTIVE;
104 if (activeOnly && isActive || !activeOnly) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700105 result.add(jsonForEntity(app, Application.class));
Thomas Vachuskafba28572015-03-25 18:48:59 -0700106 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800107 }
108 return result;
109 }
110
Ray Milkey3078fc02015-05-06 16:14:14 -0700111
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800112
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800113}