blob: ab2e8edefbaf95eded8b1f8717313f837a13f646 [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
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080018import java.net.URI;
Ray Milkey3078fc02015-05-06 16:14:14 -070019import java.util.Collections;
20import java.util.List;
21
Thomas Vachuska90b453f2015-01-30 18:57:14 -080022import org.apache.karaf.shell.commands.Command;
Thomas Vachuskafba28572015-03-25 18:48:59 -070023import org.apache.karaf.shell.commands.Option;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080024import org.onosproject.app.ApplicationService;
25import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080026import org.onosproject.cli.Comparators;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080027import org.onosproject.core.Application;
28
Ray Milkey3078fc02015-05-06 16:14:14 -070029import com.fasterxml.jackson.databind.JsonNode;
30import com.fasterxml.jackson.databind.ObjectMapper;
31import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080032
33import static com.google.common.collect.Lists.newArrayList;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080034import static org.onosproject.app.ApplicationState.ACTIVE;
35
36/**
37 * Lists application information.
38 */
39@Command(scope = "onos", name = "apps",
40 description = "Lists application information")
41public class ApplicationsListCommand extends AbstractShellCommand {
42
43 private static final String FMT =
Jian Li02c109c2016-01-19 19:24:44 -080044 "%s id=%d, name=%s, version=%s, origin=%s, category=%s, description=%s, " +
45 "features=%s, featuresRepo=%s, apps=%s, permissions=%s, url=%s";
Thomas Vachuska90b453f2015-01-30 18:57:14 -080046
Thomas Vachuska8ceff302015-04-03 11:45:53 -070047 private static final String SHORT_FMT =
Jian Li02c109c2016-01-19 19:24:44 -080048 "%s %3d %-36s %-8s %s";
Thomas Vachuska8ceff302015-04-03 11:45:53 -070049
50 @Option(name = "-s", aliases = "--short", description = "Show short output only",
51 required = false, multiValued = false)
52 private boolean shortOnly = false;
53
Thomas Vachuskafba28572015-03-25 18:48:59 -070054 @Option(name = "-a", aliases = "--active", description = "Show active only",
55 required = false, multiValued = false)
56 private boolean activeOnly = false;
57
58
Thomas Vachuska90b453f2015-01-30 18:57:14 -080059 @Override
60 protected void execute() {
61 ApplicationService service = get(ApplicationService.class);
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080062 List<Application> apps = newArrayList(service.getApplications());
63 Collections.sort(apps, Comparators.APP_COMPARATOR);
64
65 if (outputJson()) {
66 print("%s", json(service, apps));
67 } else {
68 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070069 boolean isActive = service.getState(app.id()) == ACTIVE;
70 if (activeOnly && isActive || !activeOnly) {
Thomas Vachuska8ceff302015-04-03 11:45:53 -070071 if (shortOnly) {
Andrea Campanella915e6c82016-03-09 17:55:41 -080072 String shortDescription = app.title().equals(app.id().name()) ?
73 app.description().replaceAll("[\\r\\n]", " ").replaceAll(" +", " ") :
74 app.title();
Thomas Vachuska8ceff302015-04-03 11:45:53 -070075 print(SHORT_FMT, isActive ? "*" : " ",
Andrea Campanella915e6c82016-03-09 17:55:41 -080076 app.id().id(), app.id().name(), app.version(), shortDescription);
Thomas Vachuska8ceff302015-04-03 11:45:53 -070077 } else {
78 print(FMT, isActive ? "*" : " ",
79 app.id().id(), app.id().name(), app.version(), app.origin(),
Jian Li02c109c2016-01-19 19:24:44 -080080 app.category(), app.description(), app.features(),
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080081 app.featuresRepo().map(URI::toString).orElse(""),
Jian Li02c109c2016-01-19 19:24:44 -080082 app.requiredApps(), app.permissions(), app.url());
Thomas Vachuska8ceff302015-04-03 11:45:53 -070083 }
Thomas Vachuskafba28572015-03-25 18:48:59 -070084 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080085 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -080086 }
87 }
88
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080089 private JsonNode json(ApplicationService service, List<Application> apps) {
90 ObjectMapper mapper = new ObjectMapper();
91 ArrayNode result = mapper.createArrayNode();
92 for (Application app : apps) {
Thomas Vachuskafba28572015-03-25 18:48:59 -070093 boolean isActive = service.getState(app.id()) == ACTIVE;
94 if (activeOnly && isActive || !activeOnly) {
Ray Milkey3078fc02015-05-06 16:14:14 -070095 result.add(jsonForEntity(app, Application.class));
Thomas Vachuskafba28572015-03-25 18:48:59 -070096 }
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080097 }
98 return result;
99 }
100
Ray Milkey3078fc02015-05-06 16:14:14 -0700101
Thomas Vachuskaebf5e542015-02-03 19:38:13 -0800102
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800103}