blob: e03af4549a6f4aa43164dded8397d6dcaf60980a [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.ObjectMapper;
tom13cb4852014-09-11 12:44:17 -07004import com.google.common.collect.Lists;
5import org.apache.karaf.shell.commands.Argument;
6import org.apache.karaf.shell.commands.Command;
tom91c7bd02014-09-25 22:50:44 -07007import org.onlab.onos.cli.Comparators;
tom13cb4852014-09-11 12:44:17 -07008import org.onlab.onos.net.DeviceId;
9import org.onlab.onos.net.topology.TopologyCluster;
10
11import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070012import java.util.List;
13
tom32085cf2014-10-16 00:04:33 -070014import static org.onlab.onos.cli.MastersListCommand.json;
tom13cb4852014-09-11 12:44:17 -070015import static org.onlab.onos.net.topology.ClusterId.clusterId;
16
17/**
18 * Lists devices of the specified topology cluster in the current topology.
19 */
20@Command(scope = "onos", name = "cluster-devices",
21 description = "Lists devices of the specified topology cluster in the current topology")
22public class ClusterDevicesCommand extends ClustersListCommand {
23
24 @Argument(index = 0, name = "id", description = "Cluster ID",
25 required = false, multiValued = false)
26 String id = null;
27
tom13cb4852014-09-11 12:44:17 -070028 @Override
tom0872a172014-09-23 11:24:26 -070029 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070030 int cid = Integer.parseInt(id);
31 init();
32 TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
tom9eb57fb2014-09-11 19:42:38 -070033 if (cluster == null) {
34 error("No such cluster %s", cid);
35 } else {
36 List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster));
tom1380eee2014-09-24 09:22:02 -070037 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070038 if (outputJson()) {
39 print("%s", json(new ObjectMapper(), ids));
40 } else {
41 for (DeviceId deviceId : ids) {
42 print("%s", deviceId);
43 }
tom9eb57fb2014-09-11 19:42:38 -070044 }
tom13cb4852014-09-11 12:44:17 -070045 }
tom13cb4852014-09-11 12:44:17 -070046 }
47
tom13cb4852014-09-11 12:44:17 -070048}