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