Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 15 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.app; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import org.apache.karaf.shell.commands.Command; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 23 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.collect.Lists.newArrayList; |
| 31 | |
| 32 | /** |
| 33 | * Lists application ID information. |
| 34 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 35 | @Command(scope = "onos", name = "app-ids", |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 36 | description = "Lists application ID information") |
| 37 | public 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 | } |