blob: 2b9b9c45679cc528059a37be6101c2fe2795022a [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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 */
Thomas Vachuska90b453f2015-01-30 18:57:14 -080016package org.onosproject.cli.app;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070017
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;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080022import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080023import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070026
27import java.util.Collections;
28import java.util.List;
29
30import static com.google.common.collect.Lists.newArrayList;
31
32/**
33 * Lists application ID information.
34 */
Thomas Vachuska90b453f2015-01-30 18:57:14 -080035@Command(scope = "onos", name = "app-ids",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070036 description = "Lists application ID information")
37public class ApplicationIdListCommand extends AbstractShellCommand {
38
39 @Override
40 protected void execute() {
41 CoreService service = get(CoreService.class);
42 List<ApplicationId> ids = newArrayList(service.getAppIds());
43 Collections.sort(ids, Comparators.APP_ID_COMPARATOR);
44
45 if (outputJson()) {
46 print("%s", json(ids));
47 } else {
48 for (ApplicationId id : ids) {
49 print("id=%d, name=%s", id.id(), id.name());
50 }
51 }
52 }
53
54 // ApplicationId
55 private JsonNode json(List<ApplicationId> ids) {
56 ObjectMapper mapper = new ObjectMapper();
57 ArrayNode result = mapper.createArrayNode();
58 for (ApplicationId id : ids) {
59 result.add(mapper.createObjectNode()
60 .put("id", id.id())
61 .put("name", id.name()));
62 }
63 return result;
64 }
65
66}