blob: f41f85ef5f716f7bbabd95d2b8f8c823ede298dd [file] [log] [blame]
tom13cb4852014-09-11 12:44:17 -07001package org.onlab.onos.cli.net;
2
tom32085cf2014-10-16 00:04:33 -07003import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
tom13cb4852014-09-11 12:44:17 -07006import com.google.common.collect.Lists;
7import org.apache.karaf.shell.commands.Command;
tom91c7bd02014-09-25 22:50:44 -07008import org.onlab.onos.cli.Comparators;
tom13cb4852014-09-11 12:44:17 -07009import org.onlab.onos.net.topology.TopologyCluster;
10
11import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070012import 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")
19public class ClustersListCommand extends TopologyCommand {
20
21 private static final String FMT =
tomd0344a82014-09-12 00:20:30 -070022 "id=%d, devices=%d, links=%d";
tom13cb4852014-09-11 12:44:17 -070023
tom13cb4852014-09-11 12:44:17 -070024 @Override
tom0872a172014-09-23 11:24:26 -070025 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070026 init();
27 List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology));
tom1380eee2014-09-24 09:22:02 -070028 Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR);
tom13cb4852014-09-11 12:44:17 -070029
tom32085cf2014-10-16 00:04:33 -070030 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 }
tom13cb4852014-09-11 12:44:17 -070036 }
tom13cb4852014-09-11 12:44:17 -070037 }
38
tom32085cf2014-10-16 00:04:33 -070039 // 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
tom13cb4852014-09-11 12:44:17 -070052}