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