blob: 7b26db8cc9b7cca240e1c0122af1043c4779c5df [file] [log] [blame]
Jordan Haltermand07f6722018-10-11 15:25:07 -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.cli;
17
18import java.util.Collections;
19import java.util.Comparator;
20import java.util.List;
21
22import com.fasterxml.jackson.databind.JsonNode;
23import com.fasterxml.jackson.databind.ObjectMapper;
24import com.fasterxml.jackson.databind.node.ArrayNode;
25import com.fasterxml.jackson.databind.node.ObjectNode;
26import org.apache.karaf.shell.commands.Command;
27import org.onosproject.cluster.ClusterAdminService;
28import org.onosproject.cluster.Node;
29
30import static com.google.common.collect.Lists.newArrayList;
31
32/**
33 * Lists all storage nodes.
34 */
35@Command(scope = "onos", name = "storage-nodes", description = "Lists all storage nodes")
36public class StorageNodesListCommand extends AbstractShellCommand {
37
38 private static final String FMT = "id=%s, address=%s:%s";
39
40 @Override
41 protected void execute() {
42 ClusterAdminService service = get(ClusterAdminService.class);
43 List<Node> nodes = newArrayList(service.getConsensusNodes());
44 Collections.sort(nodes, Comparator.comparing(Node::id));
45 if (outputJson()) {
46 print("%s", json(nodes));
47 } else {
48 for (Node node : nodes) {
49 print(FMT, node.id(), node.host(), node.tcpPort());
50 }
51 }
52 }
53
54 // Produces JSON structure.
55 private JsonNode json(List<Node> nodes) {
56 ObjectMapper mapper = new ObjectMapper();
57 ArrayNode result = mapper.createArrayNode();
58 for (Node node : nodes) {
59 ObjectNode newNode = mapper.createObjectNode()
60 .put("id", node.id().toString())
61 .put("ip", node.ip().toString())
62 .put("host", node.host())
63 .put("tcpPort", node.tcpPort());
64 result.add(newNode);
65 }
66 return result;
67 }
68
69}