blob: cbc238d9f32e488dccea92844e67f2caadb47a3c [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
tome4729872014-09-23 00:37:37 -070017
Madan Jampani7d2fab22015-03-18 17:21:57 -070018import static com.google.common.collect.Lists.newArrayList;
tome4729872014-09-23 00:37:37 -070019
20import java.util.Collections;
tome4729872014-09-23 00:37:37 -070021import java.util.List;
22
Madan Jampani7d2fab22015-03-18 17:21:57 -070023import org.apache.karaf.shell.commands.Command;
24import org.onlab.util.Tools;
25import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.ControllerNode;
27
28import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ArrayNode;
tome4729872014-09-23 00:37:37 -070031
32/**
33 * Lists all controller cluster nodes.
34 */
35@Command(scope = "onos", name = "nodes",
36 description = "Lists all controller cluster nodes")
37public class NodesListCommand extends AbstractShellCommand {
38
39 private static final String FMT =
Madan Jampani7d2fab22015-03-18 17:21:57 -070040 "id=%s, address=%s:%s, state=%s, updated=%s %s";
tome4729872014-09-23 00:37:37 -070041
tome4729872014-09-23 00:37:37 -070042 @Override
tom0872a172014-09-23 11:24:26 -070043 protected void execute() {
44 ClusterService service = get(ClusterService.class);
tome4729872014-09-23 00:37:37 -070045 List<ControllerNode> nodes = newArrayList(service.getNodes());
tom1380eee2014-09-24 09:22:02 -070046 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070047 if (outputJson()) {
48 print("%s", json(service, nodes));
49 } else {
50 ControllerNode self = service.getLocalNode();
51 for (ControllerNode node : nodes) {
52 print(FMT, node.id(), node.ip(), node.tcpPort(),
53 service.getState(node.id()),
Madan Jampani7d2fab22015-03-18 17:21:57 -070054 Tools.timeAgo(service.getLastUpdated(node.id()).getMillis()),
tom32085cf2014-10-16 00:04:33 -070055 node.equals(self) ? "*" : "");
56 }
57 }
58 }
59
60 // Produces JSON structure.
61 private JsonNode json(ClusterService service, List<ControllerNode> nodes) {
62 ObjectMapper mapper = new ObjectMapper();
63 ArrayNode result = mapper.createArrayNode();
tome4729872014-09-23 00:37:37 -070064 ControllerNode self = service.getLocalNode();
65 for (ControllerNode node : nodes) {
tom32085cf2014-10-16 00:04:33 -070066 result.add(mapper.createObjectNode()
67 .put("id", node.id().toString())
68 .put("ip", node.ip().toString())
69 .put("tcpPort", node.tcpPort())
70 .put("state", service.getState(node.id()).toString())
71 .put("self", node.equals(self)));
tome4729872014-09-23 00:37:37 -070072 }
tom32085cf2014-10-16 00:04:33 -070073 return result;
tome4729872014-09-23 00:37:37 -070074 }
75
76}