blob: fbd677886e424e96c4ff4d453191a9476e7579e9 [file] [log] [blame]
Ray Milkeyd84f89b2018-08-17 14:54:17 -07001/*
2 * Copyright 2014-present Open Networking Foundation
3 *
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
7 *
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.
15 */
16package org.onosproject.cli2;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.onosproject.cluster.ClusterAdminService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.core.Version;
27import org.onosproject.utils.Comparators;
28
29import java.util.Collections;
30import java.util.List;
31
32import static com.google.common.collect.Lists.newArrayList;
33
34/**
35 * Lists all controller cluster nodes.
36 */
37@Service
38@Command(scope = "onos", name = "nodes",
39 description = "Lists all controller cluster nodes")
40public class NodesListCommand extends AbstractShellCommand {
41
42 private static final String FMT = "id=%s, address=%s:%s, state=%s, version=%s, updated=%s %s";
43
44 @Override
45 protected void doExecute() {
46 ClusterAdminService service = get(ClusterAdminService.class);
47 List<ControllerNode> nodes = newArrayList(service.getNodes());
48 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
49 if (outputJson()) {
50 print("%s", json(service, nodes));
51 } else {
52 ControllerNode self = service.getLocalNode();
53 for (ControllerNode node : nodes) {
54 String timeAgo = service.localStatus(node.id());
55 Version version = service.getVersion(node.id());
56 print(FMT, node.id(), node.ip(), node.tcpPort(),
57 service.getState(node.id()),
58 version == null ? "unknown" : version,
59 timeAgo,
60 node.equals(self) ? "*" : "");
61 }
62 }
63 }
64
65 // Produces JSON structure.
66 private JsonNode json(ClusterAdminService service, List<ControllerNode> nodes) {
67 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
69 ControllerNode self = service.getLocalNode();
70 for (ControllerNode node : nodes) {
71 ControllerNode.State nodeState = service.getState(node.id());
72 Version nodeVersion = service.getVersion(node.id());
73 ObjectNode newNode = mapper.createObjectNode()
74 .put("id", node.id().toString())
75 .put("ip", node.ip().toString())
76 .put("tcpPort", node.tcpPort())
77 .put("self", node.equals(self));
78 if (nodeState != null) {
79 newNode.put("state", nodeState.toString());
80 }
81 if (nodeVersion != null) {
82 newNode.put("version", nodeVersion.toString());
83 }
84 result.add(newNode);
85 }
86 return result;
87 }
88
89}