blob: 13d5cd8259a718ad248e7afe9783a1e2cbcce6c1 [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
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080021import org.apache.karaf.shell.commands.Command;
22import org.onosproject.app.ApplicationService;
23import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080024import org.onosproject.cli.Comparators;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080025import org.onosproject.core.Application;
26
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080027import java.util.Collections;
28import java.util.List;
29
30import static com.google.common.collect.Lists.newArrayList;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080031import static org.onosproject.app.ApplicationState.ACTIVE;
32
33/**
34 * Lists application information.
35 */
36@Command(scope = "onos", name = "apps",
37 description = "Lists application information")
38public 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 Vachuskaebf5e542015-02-03 19:38:13 -080047 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 Vachuska90b453f2015-01-30 18:57:14 -080060 }
61 }
62
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080063 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 Vachuska90b453f2015-01-30 18:57:14 -080087}