Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 18 | import static com.google.common.collect.Lists.newArrayList; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 19 | |
| 20 | import java.util.Collections; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
| 27 | |
| 28 | import com.fasterxml.jackson.databind.JsonNode; |
| 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Lists all controller cluster nodes. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "nodes", |
| 36 | description = "Lists all controller cluster nodes") |
| 37 | public class NodesListCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private static final String FMT = |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 40 | "id=%s, address=%s:%s, state=%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() { |
| 44 | ClusterService service = get(ClusterService.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) { |
| 52 | print(FMT, node.id(), node.ip(), node.tcpPort(), |
| 53 | service.getState(node.id()), |
Madan Jampani | 7d2fab2 | 2015-03-18 17:21:57 -0700 | [diff] [blame] | 54 | Tools.timeAgo(service.getLastUpdated(node.id()).getMillis()), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 55 | node.equals(self) ? "*" : ""); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Produces JSON structure. |
| 61 | private JsonNode json(ClusterService service, List<ControllerNode> nodes) { |
| 62 | ObjectMapper mapper = new ObjectMapper(); |
| 63 | ArrayNode result = mapper.createArrayNode(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 64 | ControllerNode self = service.getLocalNode(); |
| 65 | for (ControllerNode node : nodes) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 66 | result.add(mapper.createObjectNode() |
| 67 | .put("id", node.id().toString()) |
| 68 | .put("ip", node.ip().toString()) |
| 69 | .put("tcpPort", node.tcpPort()) |
| 70 | .put("state", service.getState(node.id()).toString()) |
| 71 | .put("self", node.equals(self))); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 72 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 73 | return result; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } |