Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.onlab.onos.cli; |
| 20 | |
| 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onlab.onos.core.ApplicationId; |
| 26 | import org.onlab.onos.core.CoreService; |
| 27 | |
| 28 | import java.util.Collections; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | |
| 33 | /** |
| 34 | * Lists application ID information. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "apps", |
| 37 | description = "Lists application ID information") |
| 38 | public class ApplicationIdListCommand extends AbstractShellCommand { |
| 39 | |
| 40 | @Override |
| 41 | protected void execute() { |
| 42 | CoreService service = get(CoreService.class); |
| 43 | List<ApplicationId> ids = newArrayList(service.getAppIds()); |
| 44 | Collections.sort(ids, Comparators.APP_ID_COMPARATOR); |
| 45 | |
| 46 | if (outputJson()) { |
| 47 | print("%s", json(ids)); |
| 48 | } else { |
| 49 | for (ApplicationId id : ids) { |
| 50 | print("id=%d, name=%s", id.id(), id.name()); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // ApplicationId |
| 56 | private JsonNode json(List<ApplicationId> ids) { |
| 57 | ObjectMapper mapper = new ObjectMapper(); |
| 58 | ArrayNode result = mapper.createArrayNode(); |
| 59 | for (ApplicationId id : ids) { |
| 60 | result.add(mapper.createObjectNode() |
| 61 | .put("id", id.id()) |
| 62 | .put("name", id.name())); |
| 63 | } |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | } |