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; |
| 22 | import org.onosproject.app.ApplicationService; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.Comparators; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 25 | import org.onosproject.core.Application; |
| 26 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.collect.Lists.newArrayList; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 31 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 32 | |
| 33 | /** |
| 34 | * Lists application information. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "apps", |
| 37 | description = "Lists application information") |
| 38 | public class ApplicationsListCommand extends AbstractShellCommand { |
| 39 | |
| 40 | private static final String FMT = |
| 41 | "%s id=%d, name=%s, version=%s, origin=%s, description=%s, " + |
| 42 | "features=%s, featuresRepo=%s, permissions=%s"; |
| 43 | |
| 44 | @Override |
| 45 | protected void execute() { |
| 46 | ApplicationService service = get(ApplicationService.class); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 47 | List<Application> apps = newArrayList(service.getApplications()); |
| 48 | Collections.sort(apps, Comparators.APP_COMPARATOR); |
| 49 | |
| 50 | if (outputJson()) { |
| 51 | print("%s", json(service, apps)); |
| 52 | } else { |
| 53 | for (Application app : apps) { |
| 54 | print(FMT, service.getState(app.id()) == ACTIVE ? "*" : " ", |
| 55 | app.id().id(), app.id().name(), app.version(), app.origin(), |
| 56 | app.description(), app.features(), |
| 57 | app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "", |
| 58 | app.permissions()); |
| 59 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 63 | private JsonNode json(ApplicationService service, List<Application> apps) { |
| 64 | ObjectMapper mapper = new ObjectMapper(); |
| 65 | ArrayNode result = mapper.createArrayNode(); |
| 66 | for (Application app : apps) { |
| 67 | result.add(json(service, mapper, app)); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | protected JsonNode json(ApplicationService service, ObjectMapper mapper, |
| 73 | Application app) { |
| 74 | return mapper.createObjectNode() |
| 75 | .put("name", app.id().name()) |
| 76 | .put("id", app.id().id()) |
| 77 | .put("version", app.version().toString()) |
| 78 | .put("description", app.description()) |
| 79 | .put("origin", app.origin()) |
| 80 | .put("permissions", app.permissions().toString()) |
| 81 | .put("featuresRepo", app.featuresRepo().isPresent() ? |
| 82 | app.featuresRepo().get().toString() : "") |
| 83 | .put("features", app.features().toString()) |
| 84 | .put("state", service.getState(app.id()).toString()); |
| 85 | } |
| 86 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 87 | } |