blob: 1057d6e45813047cbbf644674526e74319ad252b [file] [log] [blame]
tome4729872014-09-23 00:37:37 -07001package org.onlab.onos.cli;
2
3import org.apache.karaf.shell.commands.Command;
4import org.onlab.onos.cluster.ClusterService;
5import org.onlab.onos.cluster.ControllerNode;
6
7import java.util.Collections;
8import java.util.Comparator;
9import java.util.List;
10
11import static com.google.common.collect.Lists.newArrayList;
12
13/**
14 * Lists all controller cluster nodes.
15 */
16@Command(scope = "onos", name = "nodes",
17 description = "Lists all controller cluster nodes")
18public class NodesListCommand extends AbstractShellCommand {
19
20 private static final String FMT =
21 "id=%s, ip=%s, state=%s %s";
22
23 protected static final Comparator<ControllerNode> ID_COMPARATOR =
24 new Comparator<ControllerNode>() {
25 @Override
26 public int compare(ControllerNode ci1, ControllerNode ci2) {
27 return ci1.id().toString().compareTo(ci2.id().toString());
28 }
29 };
30
31 @Override
32 protected Object doExecute() throws Exception {
33 ClusterService service = getService(ClusterService.class);
34 List<ControllerNode> nodes = newArrayList(service.getNodes());
35 Collections.sort(nodes, ID_COMPARATOR);
36 ControllerNode self = service.getLocalNode();
37 for (ControllerNode node : nodes) {
38 print(FMT, node.id(), node.ip(),
39 service.getState(node.id()),
40 node.equals(self) ? "*" : "");
41 }
42 return null;
43 }
44
45}