tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli; |
| 2 | |
| 3 | import org.apache.karaf.shell.commands.Command; |
| 4 | import org.onlab.onos.cluster.ClusterService; |
| 5 | import org.onlab.onos.cluster.ControllerNode; |
| 6 | |
| 7 | import java.util.Collections; |
| 8 | import java.util.Comparator; |
| 9 | import java.util.List; |
| 10 | |
| 11 | import 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") |
| 18 | public 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 | } |