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 | |
tom | 32085cf | 2014-10-16 00:04:33 -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; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.ControllerNode; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.Collections; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
| 28 | import static com.google.common.collect.Lists.newArrayList; |
| 29 | |
| 30 | /** |
| 31 | * Lists all controller cluster nodes. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "nodes", |
| 34 | description = "Lists all controller cluster nodes") |
| 35 | public class NodesListCommand extends AbstractShellCommand { |
| 36 | |
| 37 | private static final String FMT = |
tom | ee49c37 | 2014-09-26 15:14:50 -0700 | [diff] [blame] | 38 | "id=%s, address=%s:%s, state=%s %s"; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 39 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 40 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 41 | protected void execute() { |
| 42 | ClusterService service = get(ClusterService.class); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 43 | List<ControllerNode> nodes = newArrayList(service.getNodes()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 44 | Collections.sort(nodes, Comparators.NODE_COMPARATOR); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 45 | if (outputJson()) { |
| 46 | print("%s", json(service, nodes)); |
| 47 | } else { |
| 48 | ControllerNode self = service.getLocalNode(); |
| 49 | for (ControllerNode node : nodes) { |
| 50 | print(FMT, node.id(), node.ip(), node.tcpPort(), |
| 51 | service.getState(node.id()), |
| 52 | node.equals(self) ? "*" : ""); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Produces JSON structure. |
| 58 | private JsonNode json(ClusterService service, List<ControllerNode> nodes) { |
| 59 | ObjectMapper mapper = new ObjectMapper(); |
| 60 | ArrayNode result = mapper.createArrayNode(); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 61 | ControllerNode self = service.getLocalNode(); |
| 62 | for (ControllerNode node : nodes) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 63 | result.add(mapper.createObjectNode() |
| 64 | .put("id", node.id().toString()) |
| 65 | .put("ip", node.ip().toString()) |
| 66 | .put("tcpPort", node.tcpPort()) |
| 67 | .put("state", service.getState(node.id()).toString()) |
| 68 | .put("self", node.equals(self))); |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 69 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 70 | return result; |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } |