blob: 2ffcd1159dd4103bd6898ecde9e7c834d22a6649 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tome4729872014-09-23 00:37:37 -070019package org.onlab.onos.cli;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
tome4729872014-09-23 00:37:37 -070024import org.apache.karaf.shell.commands.Command;
25import org.onlab.onos.cluster.ClusterService;
26import org.onlab.onos.cluster.ControllerNode;
27
28import java.util.Collections;
tome4729872014-09-23 00:37:37 -070029import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
32
33/**
34 * Lists all controller cluster nodes.
35 */
36@Command(scope = "onos", name = "nodes",
37 description = "Lists all controller cluster nodes")
38public class NodesListCommand extends AbstractShellCommand {
39
40 private static final String FMT =
tomee49c372014-09-26 15:14:50 -070041 "id=%s, address=%s:%s, state=%s %s";
tome4729872014-09-23 00:37:37 -070042
tome4729872014-09-23 00:37:37 -070043 @Override
tom0872a172014-09-23 11:24:26 -070044 protected void execute() {
45 ClusterService service = get(ClusterService.class);
tome4729872014-09-23 00:37:37 -070046 List<ControllerNode> nodes = newArrayList(service.getNodes());
tom1380eee2014-09-24 09:22:02 -070047 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070048 if (outputJson()) {
49 print("%s", json(service, nodes));
50 } else {
51 ControllerNode self = service.getLocalNode();
52 for (ControllerNode node : nodes) {
53 print(FMT, node.id(), node.ip(), node.tcpPort(),
54 service.getState(node.id()),
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();
tome4729872014-09-23 00:37:37 -070064 ControllerNode self = service.getLocalNode();
65 for (ControllerNode node : nodes) {
tom32085cf2014-10-16 00:04:33 -070066 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)));
tome4729872014-09-23 00:37:37 -070072 }
tom32085cf2014-10-16 00:04:33 -070073 return result;
tome4729872014-09-23 00:37:37 -070074 }
75
76}