Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
| 16 | package org.onlab.onos.cli; |
| 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; |
| 22 | import org.onlab.onos.core.ApplicationId; |
| 23 | import org.onlab.onos.core.CoreService; |
| 24 | |
| 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import 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") |
| 35 | public 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 | } |