blob: 4e49fe9c1ec447665859328749668564d54af54c [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
23import org.onlab.util.Tools;
Jordan Halterman28183ee2017-10-17 17:29:10 -070024import org.onosproject.cluster.ClusterAdminService;
Madan Jampani7d2fab22015-03-18 17:21:57 -070025import org.onosproject.cluster.ControllerNode;
Jordan Haltermanf70bf462017-07-29 13:12:00 -070026import org.onosproject.core.Version;
Ray Milkeyc7477292016-03-11 10:53:43 -080027import org.onosproject.utils.Comparators;
Madan Jampani7d2fab22015-03-18 17:21:57 -070028
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070029import java.time.Instant;
Jon Halle97083c2015-04-20 15:18:56 -070030import java.util.Collections;
31import java.util.List;
32
33import static com.google.common.collect.Lists.newArrayList;
34
tome4729872014-09-23 00:37:37 -070035/**
36 * Lists all controller cluster nodes.
37 */
38@Command(scope = "onos", name = "nodes",
Thomas Vachuska7a8de842016-03-07 20:56:35 -080039 description = "Lists all controller cluster nodes")
tome4729872014-09-23 00:37:37 -070040public class NodesListCommand extends AbstractShellCommand {
41
Jordan Haltermanf70bf462017-07-29 13:12:00 -070042 private static final String FMT = "id=%s, address=%s:%s, state=%s, version=%s, updated=%s %s";
tome4729872014-09-23 00:37:37 -070043
tome4729872014-09-23 00:37:37 -070044 @Override
tom0872a172014-09-23 11:24:26 -070045 protected void execute() {
Jordan Halterman28183ee2017-10-17 17:29:10 -070046 ClusterAdminService service = get(ClusterAdminService.class);
tome4729872014-09-23 00:37:37 -070047 List<ControllerNode> nodes = newArrayList(service.getNodes());
tom1380eee2014-09-24 09:22:02 -070048 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070049 if (outputJson()) {
50 print("%s", json(service, nodes));
51 } else {
52 ControllerNode self = service.getLocalNode();
53 for (ControllerNode node : nodes) {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070054 Instant lastUpdated = service.getLastUpdatedInstant(node.id());
Jon Halle97083c2015-04-20 15:18:56 -070055 String timeAgo = "Never";
56 if (lastUpdated != null) {
Jon Halldeb4ff52017-11-06 13:53:26 -080057 timeAgo = Tools.timeAgo(lastUpdated.toEpochMilli());
Jon Halle97083c2015-04-20 15:18:56 -070058 }
Jordan Haltermanf70bf462017-07-29 13:12:00 -070059 Version version = service.getVersion(node.id());
tom32085cf2014-10-16 00:04:33 -070060 print(FMT, node.id(), node.ip(), node.tcpPort(),
Jordan Haltermanf70bf462017-07-29 13:12:00 -070061 service.getState(node.id()),
62 version == null ? "unknown" : version,
63 timeAgo,
64 node.equals(self) ? "*" : "");
tom32085cf2014-10-16 00:04:33 -070065 }
66 }
67 }
68
69 // Produces JSON structure.
Jordan Halterman28183ee2017-10-17 17:29:10 -070070 private JsonNode json(ClusterAdminService service, List<ControllerNode> nodes) {
tom32085cf2014-10-16 00:04:33 -070071 ObjectMapper mapper = new ObjectMapper();
72 ArrayNode result = mapper.createArrayNode();
tome4729872014-09-23 00:37:37 -070073 ControllerNode self = service.getLocalNode();
74 for (ControllerNode node : nodes) {
Jon Halle97083c2015-04-20 15:18:56 -070075 ControllerNode.State nodeState = service.getState(node.id());
Jordan Haltermanf70bf462017-07-29 13:12:00 -070076 Version nodeVersion = service.getVersion(node.id());
Jon Halle97083c2015-04-20 15:18:56 -070077 ObjectNode newNode = mapper.createObjectNode()
Thomas Vachuska7a8de842016-03-07 20:56:35 -080078 .put("id", node.id().toString())
79 .put("ip", node.ip().toString())
80 .put("tcpPort", node.tcpPort())
81 .put("self", node.equals(self));
Jon Halle97083c2015-04-20 15:18:56 -070082 if (nodeState != null) {
83 newNode.put("state", nodeState.toString());
84 }
Jordan Haltermanf70bf462017-07-29 13:12:00 -070085 if (nodeVersion != null) {
86 newNode.put("version", nodeVersion.toString());
87 }
Jon Halle97083c2015-04-20 15:18:56 -070088 result.add(newNode);
tome4729872014-09-23 00:37:37 -070089 }
tom32085cf2014-10-16 00:04:33 -070090 return result;
tome4729872014-09-23 00:37:37 -070091 }
92
93}