blob: 6e91738af1d46fff15f45a45ed03e9d0d7e57126 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Jon Halle97083c2015-04-20 15:18:56 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Madan Jampani7d2fab22015-03-18 17:21:57 -070022import org.apache.karaf.shell.commands.Command;
Jon Halle97083c2015-04-20 15:18:56 -070023import org.joda.time.DateTime;
Madan Jampani7d2fab22015-03-18 17:21:57 -070024import org.onlab.util.Tools;
25import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.ControllerNode;
Ray Milkeyc7477292016-03-11 10:53:43 -080027import org.onosproject.utils.Comparators;
Madan Jampani7d2fab22015-03-18 17:21:57 -070028
Jon Halle97083c2015-04-20 15:18:56 -070029import java.util.Collections;
30import java.util.List;
31
32import static com.google.common.collect.Lists.newArrayList;
33
tome4729872014-09-23 00:37:37 -070034
35/**
36 * Lists all controller cluster nodes.
37 */
38@Command(scope = "onos", name = "nodes",
39 description = "Lists all controller cluster nodes")
40public class NodesListCommand extends AbstractShellCommand {
41
42 private static final String FMT =
Madan Jampani7d2fab22015-03-18 17:21:57 -070043 "id=%s, address=%s:%s, state=%s, updated=%s %s";
tome4729872014-09-23 00:37:37 -070044
tome4729872014-09-23 00:37:37 -070045 @Override
tom0872a172014-09-23 11:24:26 -070046 protected void execute() {
47 ClusterService service = get(ClusterService.class);
tome4729872014-09-23 00:37:37 -070048 List<ControllerNode> nodes = newArrayList(service.getNodes());
tom1380eee2014-09-24 09:22:02 -070049 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070050 if (outputJson()) {
51 print("%s", json(service, nodes));
52 } else {
53 ControllerNode self = service.getLocalNode();
54 for (ControllerNode node : nodes) {
Jon Halle97083c2015-04-20 15:18:56 -070055 DateTime lastUpdated = service.getLastUpdated(node.id());
56 String timeAgo = "Never";
57 if (lastUpdated != null) {
58 timeAgo = Tools.timeAgo(lastUpdated.getMillis());
59 }
tom32085cf2014-10-16 00:04:33 -070060 print(FMT, node.id(), node.ip(), node.tcpPort(),
61 service.getState(node.id()),
Jon Halle97083c2015-04-20 15:18:56 -070062 timeAgo,
tom32085cf2014-10-16 00:04:33 -070063 node.equals(self) ? "*" : "");
64 }
65 }
66 }
67
68 // Produces JSON structure.
69 private JsonNode json(ClusterService service, List<ControllerNode> nodes) {
70 ObjectMapper mapper = new ObjectMapper();
71 ArrayNode result = mapper.createArrayNode();
tome4729872014-09-23 00:37:37 -070072 ControllerNode self = service.getLocalNode();
73 for (ControllerNode node : nodes) {
Jon Halle97083c2015-04-20 15:18:56 -070074 ControllerNode.State nodeState = service.getState(node.id());
75 ObjectNode newNode = mapper.createObjectNode()
tom32085cf2014-10-16 00:04:33 -070076 .put("id", node.id().toString())
77 .put("ip", node.ip().toString())
78 .put("tcpPort", node.tcpPort())
Jon Halle97083c2015-04-20 15:18:56 -070079 .put("self", node.equals(self));
80
81 if (nodeState != null) {
82 newNode.put("state", nodeState.toString());
83 }
84 result.add(newNode);
tome4729872014-09-23 00:37:37 -070085 }
tom32085cf2014-10-16 00:04:33 -070086 return result;
tome4729872014-09-23 00:37:37 -070087 }
88
89}