blob: da7695045077f3de97d5241eef38f559b26f913c [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
pierventre55a5f392021-11-05 15:37:32 +010024import org.onlab.packet.IpAddress;
Jordan Halterman28183ee2017-10-17 17:29:10 -070025import org.onosproject.cluster.ClusterAdminService;
Madan Jampani7d2fab22015-03-18 17:21:57 -070026import org.onosproject.cluster.ControllerNode;
Jordan Haltermanf70bf462017-07-29 13:12:00 -070027import org.onosproject.core.Version;
Ray Milkeyc7477292016-03-11 10:53:43 -080028import org.onosproject.utils.Comparators;
Madan Jampani7d2fab22015-03-18 17:21:57 -070029
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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038@Service
tome4729872014-09-23 00:37:37 -070039@Command(scope = "onos", name = "nodes",
Thomas Vachuska7a8de842016-03-07 20:56:35 -080040 description = "Lists all controller cluster nodes")
tome4729872014-09-23 00:37:37 -070041public class NodesListCommand extends AbstractShellCommand {
42
Jordan Haltermanf70bf462017-07-29 13:12:00 -070043 private static final String FMT = "id=%s, address=%s:%s, state=%s, version=%s, updated=%s %s";
tome4729872014-09-23 00:37:37 -070044
tome4729872014-09-23 00:37:37 -070045 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046 protected void doExecute() {
Jordan Halterman28183ee2017-10-17 17:29:10 -070047 ClusterAdminService service = get(ClusterAdminService.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) {
Ray Milkey054e23d2018-03-22 13:37:11 -070055 String timeAgo = service.localStatus(node.id());
Jordan Haltermanf70bf462017-07-29 13:12:00 -070056 Version version = service.getVersion(node.id());
tom32085cf2014-10-16 00:04:33 -070057 print(FMT, node.id(), node.ip(), node.tcpPort(),
Jordan Haltermanf70bf462017-07-29 13:12:00 -070058 service.getState(node.id()),
59 version == null ? "unknown" : version,
60 timeAgo,
61 node.equals(self) ? "*" : "");
tom32085cf2014-10-16 00:04:33 -070062 }
63 }
64 }
65
66 // Produces JSON structure.
Jordan Halterman28183ee2017-10-17 17:29:10 -070067 private JsonNode json(ClusterAdminService service, List<ControllerNode> nodes) {
tom32085cf2014-10-16 00:04:33 -070068 ObjectMapper mapper = new ObjectMapper();
69 ArrayNode result = mapper.createArrayNode();
tome4729872014-09-23 00:37:37 -070070 ControllerNode self = service.getLocalNode();
71 for (ControllerNode node : nodes) {
Jon Halle97083c2015-04-20 15:18:56 -070072 ControllerNode.State nodeState = service.getState(node.id());
Jordan Haltermanf70bf462017-07-29 13:12:00 -070073 Version nodeVersion = service.getVersion(node.id());
pierventre55a5f392021-11-05 15:37:32 +010074 IpAddress nodeIp = node.ip();
Jon Halle97083c2015-04-20 15:18:56 -070075 ObjectNode newNode = mapper.createObjectNode()
Thomas Vachuska7a8de842016-03-07 20:56:35 -080076 .put("id", node.id().toString())
pierventre55a5f392021-11-05 15:37:32 +010077 .put("ip", nodeIp != null ? nodeIp.toString() : node.host())
Thomas Vachuska7a8de842016-03-07 20:56:35 -080078 .put("tcpPort", node.tcpPort())
79 .put("self", node.equals(self));
Jon Halle97083c2015-04-20 15:18:56 -070080 if (nodeState != null) {
81 newNode.put("state", nodeState.toString());
82 }
Jordan Haltermanf70bf462017-07-29 13:12:00 -070083 if (nodeVersion != null) {
84 newNode.put("version", nodeVersion.toString());
85 }
Jon Halle97083c2015-04-20 15:18:56 -070086 result.add(newNode);
tome4729872014-09-23 00:37:37 -070087 }
tom32085cf2014-10-16 00:04:33 -070088 return result;
tome4729872014-09-23 00:37:37 -070089 }
90
91}