blob: 17162a21f4afcc17e6715084cdbb44b25d0de864 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
tome4729872014-09-23 00:37:37 -070016package org.onlab.onos.cli;
17
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
tome4729872014-09-23 00:37:37 -070021import org.apache.karaf.shell.commands.Command;
22import org.onlab.onos.cluster.ClusterService;
23import org.onlab.onos.cluster.ControllerNode;
24
25import java.util.Collections;
tome4729872014-09-23 00:37:37 -070026import java.util.List;
27
28import static com.google.common.collect.Lists.newArrayList;
29
30/**
31 * Lists all controller cluster nodes.
32 */
33@Command(scope = "onos", name = "nodes",
34 description = "Lists all controller cluster nodes")
35public class NodesListCommand extends AbstractShellCommand {
36
37 private static final String FMT =
tomee49c372014-09-26 15:14:50 -070038 "id=%s, address=%s:%s, state=%s %s";
tome4729872014-09-23 00:37:37 -070039
tome4729872014-09-23 00:37:37 -070040 @Override
tom0872a172014-09-23 11:24:26 -070041 protected void execute() {
42 ClusterService service = get(ClusterService.class);
tome4729872014-09-23 00:37:37 -070043 List<ControllerNode> nodes = newArrayList(service.getNodes());
tom1380eee2014-09-24 09:22:02 -070044 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070045 if (outputJson()) {
46 print("%s", json(service, nodes));
47 } else {
48 ControllerNode self = service.getLocalNode();
49 for (ControllerNode node : nodes) {
50 print(FMT, node.id(), node.ip(), node.tcpPort(),
51 service.getState(node.id()),
52 node.equals(self) ? "*" : "");
53 }
54 }
55 }
56
57 // Produces JSON structure.
58 private JsonNode json(ClusterService service, List<ControllerNode> nodes) {
59 ObjectMapper mapper = new ObjectMapper();
60 ArrayNode result = mapper.createArrayNode();
tome4729872014-09-23 00:37:37 -070061 ControllerNode self = service.getLocalNode();
62 for (ControllerNode node : nodes) {
tom32085cf2014-10-16 00:04:33 -070063 result.add(mapper.createObjectNode()
64 .put("id", node.id().toString())
65 .put("ip", node.ip().toString())
66 .put("tcpPort", node.tcpPort())
67 .put("state", service.getState(node.id()).toString())
68 .put("self", node.equals(self)));
tome4729872014-09-23 00:37:37 -070069 }
tom32085cf2014-10-16 00:04:33 -070070 return result;
tome4729872014-09-23 00:37:37 -070071 }
72
73}