blob: 8e8a4824fac99522244f1091c03522d0450a2ee6 [file] [log] [blame]
Thomas Vachuskae0f804a2014-10-27 23:40:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080023import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080024import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070027
28import java.util.Collections;
29import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
32
33/**
34 * Lists application ID information.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
Thomas Vachuska90b453f2015-01-30 18:57:14 -080037@Command(scope = "onos", name = "app-ids",
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070038 description = "Lists application ID information")
39public class ApplicationIdListCommand extends AbstractShellCommand {
40
41 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042 protected void doExecute() {
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070043 CoreService service = get(CoreService.class);
44 List<ApplicationId> ids = newArrayList(service.getAppIds());
45 Collections.sort(ids, Comparators.APP_ID_COMPARATOR);
46
47 if (outputJson()) {
48 print("%s", json(ids));
49 } else {
50 for (ApplicationId id : ids) {
51 print("id=%d, name=%s", id.id(), id.name());
52 }
53 }
54 }
55
56 // ApplicationId
57 private JsonNode json(List<ApplicationId> ids) {
58 ObjectMapper mapper = new ObjectMapper();
59 ArrayNode result = mapper.createArrayNode();
60 for (ApplicationId id : ids) {
61 result.add(mapper.createObjectNode()
62 .put("id", id.id())
63 .put("name", id.name()));
64 }
65 return result;
66 }
67
68}