Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
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; |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 23 | import org.joda.time.DateTime; |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
| 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 | /** |
| 35 | * Lists all controller cluster nodes. |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "nodes", |
| 38 | description = "Lists all controller cluster nodes") |
| 39 | public class NodesListCommand extends AbstractShellCommand { |
| 40 | |
| 41 | private static final String FMT = |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 42 | "id=%s, address=%s:%s, state=%s, updated=%s %s"; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 43 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 44 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 45 | protected void execute() { |
| 46 | ClusterService service = get(ClusterService.class); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 47 | List<ControllerNode> nodes = newArrayList(service.getNodes()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 48 | Collections.sort(nodes, Comparators.NODE_COMPARATOR); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 49 | if (outputJson()) { |
| 50 | print("%s", json(service, nodes)); |
| 51 | } else { |
| 52 | ControllerNode self = service.getLocalNode(); |
| 53 | for (ControllerNode node : nodes) { |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 54 | DateTime lastUpdated = service.getLastUpdated(node.id()); |
| 55 | String timeAgo = "Never"; |
| 56 | if (lastUpdated != null) { |
| 57 | timeAgo = Tools.timeAgo(lastUpdated.getMillis()); |
| 58 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | print(FMT, node.id(), node.ip(), node.tcpPort(), |
| 60 | service.getState(node.id()), |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 61 | timeAgo, |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 62 | node.equals(self) ? "*" : ""); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Produces JSON structure. |
| 68 | private JsonNode json(ClusterService service, List<ControllerNode> nodes) { |
| 69 | ObjectMapper mapper = new ObjectMapper(); |
| 70 | ArrayNode result = mapper.createArrayNode(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 71 | ControllerNode self = service.getLocalNode(); |
| 72 | for (ControllerNode node : nodes) { |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 73 | ControllerNode.State nodeState = service.getState(node.id()); |
| 74 | ObjectNode newNode = mapper.createObjectNode() |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 75 | .put("id", node.id().toString()) |
| 76 | .put("ip", node.ip().toString()) |
| 77 | .put("tcpPort", node.tcpPort()) |
Jon Hall | e97083c | 2015-04-20 15:18:56 -0700 | [diff] [blame] | 78 | .put("self", node.equals(self)); |
| 79 | |
| 80 | if (nodeState != null) { |
| 81 | newNode.put("state", nodeState.toString()); |
| 82 | } |
| 83 | result.add(newNode); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 84 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 85 | return result; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } |