blob: 4687139b47232af63bed15d448dee04d7f48d1fb [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;
Ray Milkey0b18b722018-10-16 13:19:15 -070026import org.apache.karaf.shell.api.action.Command;
27import org.apache.karaf.shell.api.action.lifecycle.Service;
pierventrefa5dc3c2021-11-05 15:37:32 +010028import org.onlab.packet.IpAddress;
Jordan Haltermand07f6722018-10-11 15:25:07 -070029import org.onosproject.cluster.ClusterAdminService;
30import org.onosproject.cluster.Node;
31
32import static com.google.common.collect.Lists.newArrayList;
33
34/**
35 * Lists all storage nodes.
36 */
Ray Milkey0b18b722018-10-16 13:19:15 -070037@Service
Jordan Haltermand07f6722018-10-11 15:25:07 -070038@Command(scope = "onos", name = "storage-nodes", description = "Lists all storage nodes")
39public class StorageNodesListCommand extends AbstractShellCommand {
40
41 private static final String FMT = "id=%s, address=%s:%s";
42
43 @Override
Ray Milkey0b18b722018-10-16 13:19:15 -070044 protected void doExecute() {
Jordan Haltermand07f6722018-10-11 15:25:07 -070045 ClusterAdminService service = get(ClusterAdminService.class);
46 List<Node> nodes = newArrayList(service.getConsensusNodes());
47 Collections.sort(nodes, Comparator.comparing(Node::id));
48 if (outputJson()) {
49 print("%s", json(nodes));
50 } else {
51 for (Node node : nodes) {
52 print(FMT, node.id(), node.host(), node.tcpPort());
53 }
54 }
55 }
56
57 // Produces JSON structure.
58 private JsonNode json(List<Node> nodes) {
59 ObjectMapper mapper = new ObjectMapper();
60 ArrayNode result = mapper.createArrayNode();
61 for (Node node : nodes) {
pierventrefa5dc3c2021-11-05 15:37:32 +010062 IpAddress nodeIp = node.ip();
Jordan Haltermand07f6722018-10-11 15:25:07 -070063 ObjectNode newNode = mapper.createObjectNode()
64 .put("id", node.id().toString())
pierventrefa5dc3c2021-11-05 15:37:32 +010065 .put("ip", nodeIp != null ? nodeIp.toString() : node.host())
Jordan Haltermand07f6722018-10-11 15:25:07 -070066 .put("host", node.host())
67 .put("tcpPort", node.tcpPort());
68 result.add(newNode);
69 }
70 return result;
71 }
72
73}