tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 6 | import com.google.common.collect.Lists; |
| 7 | import org.apache.karaf.shell.commands.Command; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 8 | import org.onlab.onos.cli.Comparators; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.topology.TopologyCluster; |
| 10 | |
| 11 | import java.util.Collections; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 12 | import java.util.List; |
| 13 | |
| 14 | /** |
| 15 | * Lists all clusters in the current topology. |
| 16 | */ |
| 17 | @Command(scope = "onos", name = "clusters", |
| 18 | description = "Lists all clusters in the current topology") |
| 19 | public class ClustersListCommand extends TopologyCommand { |
| 20 | |
| 21 | private static final String FMT = |
tom | d0344a8 | 2014-09-12 00:20:30 -0700 | [diff] [blame] | 22 | "id=%d, devices=%d, links=%d"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 23 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 24 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 25 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 26 | init(); |
| 27 | List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology)); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 28 | Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 29 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 30 | if (outputJson()) { |
| 31 | print("%s", json(clusters)); |
| 32 | } else { |
| 33 | for (TopologyCluster cluster : clusters) { |
| 34 | print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount()); |
| 35 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 36 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 37 | } |
| 38 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 39 | // Produces a JSON result. |
| 40 | private JsonNode json(Iterable<TopologyCluster> clusters) { |
| 41 | ObjectMapper mapper = new ObjectMapper(); |
| 42 | ArrayNode result = mapper.createArrayNode(); |
| 43 | for (TopologyCluster cluster : clusters) { |
| 44 | result.add(mapper.createObjectNode() |
| 45 | .put("id", cluster.id().index()) |
| 46 | .put("deviceCount", cluster.deviceCount()) |
| 47 | .put("linkCount", cluster.linkCount())); |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 52 | } |