Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 17 | |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterAdminService; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ControllerNode; |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 25 | import org.onosproject.core.Version; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 26 | import org.onosproject.utils.Comparators; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 27 | |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Lists all controller cluster nodes. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "nodes", |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 37 | description = "Lists all controller cluster nodes") |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 38 | public class NodesListCommand extends AbstractShellCommand { |
| 39 | |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 40 | private static final String FMT = "id=%s, address=%s:%s, state=%s, version=%s, updated=%s %s"; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 41 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 42 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 43 | protected void execute() { |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 44 | ClusterAdminService service = get(ClusterAdminService.class); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 45 | List<ControllerNode> nodes = newArrayList(service.getNodes()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 46 | Collections.sort(nodes, Comparators.NODE_COMPARATOR); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 47 | if (outputJson()) { |
| 48 | print("%s", json(service, nodes)); |
| 49 | } else { |
| 50 | ControllerNode self = service.getLocalNode(); |
| 51 | for (ControllerNode node : nodes) { |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 52 | String timeAgo = service.localStatus(node.id()); |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 53 | Version version = service.getVersion(node.id()); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 54 | print(FMT, node.id(), node.ip(), node.tcpPort(), |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 55 | service.getState(node.id()), |
| 56 | version == null ? "unknown" : version, |
| 57 | timeAgo, |
| 58 | node.equals(self) ? "*" : ""); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Produces JSON structure. |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 64 | private JsonNode json(ClusterAdminService service, List<ControllerNode> nodes) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 65 | ObjectMapper mapper = new ObjectMapper(); |
| 66 | ArrayNode result = mapper.createArrayNode(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 67 | ControllerNode self = service.getLocalNode(); |
| 68 | for (ControllerNode node : nodes) { |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 69 | ControllerNode.State nodeState = service.getState(node.id()); |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 70 | Version nodeVersion = service.getVersion(node.id()); |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 71 | ObjectNode newNode = mapper.createObjectNode() |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 72 | .put("id", node.id().toString()) |
| 73 | .put("ip", node.ip().toString()) |
| 74 | .put("tcpPort", node.tcpPort()) |
| 75 | .put("self", node.equals(self)); |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 76 | if (nodeState != null) { |
| 77 | newNode.put("state", nodeState.toString()); |
| 78 | } |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 79 | if (nodeVersion != null) { |
| 80 | newNode.put("version", nodeVersion.toString()); |
| 81 | } |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 82 | result.add(newNode); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 83 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 84 | return result; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } |