Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cli.app; |
| 17 | |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 18 | import java.net.URI; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 24 | import org.onosproject.app.ApplicationService; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 26 | import org.onosproject.utils.Comparators; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 27 | import org.onosproject.core.Application; |
| 28 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.JsonNode; |
| 30 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 31 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 32 | |
| 33 | import static com.google.common.collect.Lists.newArrayList; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 34 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 35 | |
| 36 | /** |
| 37 | * Lists application information. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "apps", |
| 40 | description = "Lists application information") |
| 41 | public class ApplicationsListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FMT = |
Jian Li | 02c109c | 2016-01-19 19:24:44 -0800 | [diff] [blame] | 44 | "%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 Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 46 | |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 47 | private static final String SHORT_FMT = |
Jian Li | 02c109c | 2016-01-19 19:24:44 -0800 | [diff] [blame] | 48 | "%s %3d %-36s %-8s %s"; |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 49 | |
| 50 | @Option(name = "-s", aliases = "--short", description = "Show short output only", |
| 51 | required = false, multiValued = false) |
| 52 | private boolean shortOnly = false; |
| 53 | |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 54 | @Option(name = "-a", aliases = "--active", description = "Show active only", |
| 55 | required = false, multiValued = false) |
| 56 | private boolean activeOnly = false; |
| 57 | |
| 58 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 59 | @Override |
| 60 | protected void execute() { |
| 61 | ApplicationService service = get(ApplicationService.class); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 62 | 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 Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 69 | boolean isActive = service.getState(app.id()) == ACTIVE; |
| 70 | if (activeOnly && isActive || !activeOnly) { |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 71 | if (shortOnly) { |
Andrea Campanella | 915e6c8 | 2016-03-09 17:55:41 -0800 | [diff] [blame] | 72 | String shortDescription = app.title().equals(app.id().name()) ? |
| 73 | app.description().replaceAll("[\\r\\n]", " ").replaceAll(" +", " ") : |
| 74 | app.title(); |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 75 | print(SHORT_FMT, isActive ? "*" : " ", |
Andrea Campanella | 915e6c8 | 2016-03-09 17:55:41 -0800 | [diff] [blame] | 76 | app.id().id(), app.id().name(), app.version(), shortDescription); |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 77 | } else { |
| 78 | print(FMT, isActive ? "*" : " ", |
| 79 | app.id().id(), app.id().name(), app.version(), app.origin(), |
Jian Li | 02c109c | 2016-01-19 19:24:44 -0800 | [diff] [blame] | 80 | app.category(), app.description(), app.features(), |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 81 | app.featuresRepo().map(URI::toString).orElse(""), |
Jian Li | 02c109c | 2016-01-19 19:24:44 -0800 | [diff] [blame] | 82 | app.requiredApps(), app.permissions(), app.url()); |
Thomas Vachuska | 8ceff30 | 2015-04-03 11:45:53 -0700 | [diff] [blame] | 83 | } |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 84 | } |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 85 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 89 | private JsonNode json(ApplicationService service, List<Application> apps) { |
| 90 | ObjectMapper mapper = new ObjectMapper(); |
| 91 | ArrayNode result = mapper.createArrayNode(); |
| 92 | for (Application app : apps) { |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 93 | boolean isActive = service.getState(app.id()) == ACTIVE; |
| 94 | if (activeOnly && isActive || !activeOnly) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 95 | result.add(jsonForEntity(app, Application.class)); |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 96 | } |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 97 | } |
| 98 | return result; |
| 99 | } |
| 100 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 101 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 102 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 103 | } |