blob: 5a1483a509bbe22ecec14ea0e6e41967f1a39f3f [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070015 */
16package org.onlab.onos.cli;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import org.apache.karaf.shell.commands.Command;
22import org.onlab.onos.core.ApplicationId;
23import org.onlab.onos.core.CoreService;
24
25import java.util.Collections;
26import java.util.List;
27
28import static com.google.common.collect.Lists.newArrayList;
29
30/**
31 * Lists application ID information.
32 */
33@Command(scope = "onos", name = "apps",
34 description = "Lists application ID information")
35public class ApplicationIdListCommand extends AbstractShellCommand {
36
37 @Override
38 protected void execute() {
39 CoreService service = get(CoreService.class);
40 List<ApplicationId> ids = newArrayList(service.getAppIds());
41 Collections.sort(ids, Comparators.APP_ID_COMPARATOR);
42
43 if (outputJson()) {
44 print("%s", json(ids));
45 } else {
46 for (ApplicationId id : ids) {
47 print("id=%d, name=%s", id.id(), id.name());
48 }
49 }
50 }
51
52 // ApplicationId
53 private JsonNode json(List<ApplicationId> ids) {
54 ObjectMapper mapper = new ObjectMapper();
55 ArrayNode result = mapper.createArrayNode();
56 for (ApplicationId id : ids) {
57 result.add(mapper.createObjectNode()
58 .put("id", id.id())
59 .put("name", id.name()));
60 }
61 return result;
62 }
63
64}