blob: f38c78ea0f5afff835091dddd4cede3429c2e492 [file] [log] [blame]
Jonathan Hart054da972015-02-18 17:30:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart054da972015-02-18 17:30:28 -08003 *
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.net;
17
Ray Milkey501e0752015-03-04 16:11:50 -080018import java.util.List;
19
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Jonathan Hart054da972015-02-18 17:30:28 -080023import org.onosproject.cli.AbstractShellCommand;
Madan Jampaniccdf9da2016-05-05 14:37:27 -070024import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.store.primitives.PartitionAdminService;
28import org.onosproject.store.service.PartitionClientInfo;
Jonathan Hart054da972015-02-18 17:30:28 -080029import org.onosproject.store.service.PartitionInfo;
30import org.onosproject.store.service.StorageAdminService;
31
Ray Milkey501e0752015-03-04 16:11:50 -080032import com.fasterxml.jackson.databind.JsonNode;
33import com.fasterxml.jackson.databind.ObjectMapper;
34import com.fasterxml.jackson.databind.node.ArrayNode;
35import com.fasterxml.jackson.databind.node.ObjectNode;
Madan Jampani630c8822016-02-24 10:38:21 -080036import com.google.common.collect.Ordering;
Jonathan Hart054da972015-02-18 17:30:28 -080037
38/**
39 * Command to list the database partitions in the system.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
Jonathan Hart054da972015-02-18 17:30:28 -080042@Command(scope = "onos", name = "partitions",
43 description = "Lists information about partitions in the system")
44public class PartitionsListCommand extends AbstractShellCommand {
45
Madan Jampaniccdf9da2016-05-05 14:37:27 -070046 @Option(name = "-c", aliases = "--clients",
Jordan Halterman2bf177c2017-06-29 01:49:08 -070047 description = "Show information about partition clients",
Madan Jampaniccdf9da2016-05-05 14:37:27 -070048 required = false, multiValued = false)
49 private boolean reportClientInfo = false;
50
51 private static final String SERVER_FMT = "%-20s %8s %25s %s";
Jordan Halterman7ff7fef2017-07-31 16:08:40 -070052 private static final String CLIENT_FMT = "%-20s %8s";
Jonathan Hart054da972015-02-18 17:30:28 -080053
Ray Milkey501e0752015-03-04 16:11:50 -080054 /**
55 * Displays partition info as text.
56 *
57 * @param partitionInfo partition descriptions
58 */
59 private void displayPartitions(List<PartitionInfo> partitionInfo) {
Madan Jampanif172d402016-03-04 00:56:38 -080060 if (partitionInfo.isEmpty()) {
61 return;
62 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070063 print("----------------------------------------------------------");
Madan Jampaniccdf9da2016-05-05 14:37:27 -070064 print(SERVER_FMT, "Name", "Term", "Members", "");
Madan Jampanif1b8e172015-03-23 11:42:02 -070065 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080066
67 for (PartitionInfo info : partitionInfo) {
68 boolean first = true;
Madan Jampani630c8822016-02-24 10:38:21 -080069 for (String member : Ordering.natural().sortedCopy(info.members())) {
Jonathan Hart054da972015-02-18 17:30:28 -080070 if (first) {
Jordan Halterman980a8c12017-09-22 18:01:19 -070071 print(SERVER_FMT, info.id(), info.term(), member,
Ray Milkey501e0752015-03-04 16:11:50 -080072 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080073 first = false;
74 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -070075 print(SERVER_FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080076 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080077 }
78 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070079 if (!first) {
80 print("----------------------------------------------------------");
81 }
Jonathan Hart054da972015-02-18 17:30:28 -080082 }
83 }
Ray Milkey501e0752015-03-04 16:11:50 -080084
85 /**
Madan Jampaniccdf9da2016-05-05 14:37:27 -070086 * Displays partition client info as text.
87 *
88 * @param partitionClientInfo partition client information
89 */
90 private void displayPartitionClients(List<PartitionClientInfo> partitionClientInfo) {
91 if (partitionClientInfo.isEmpty()) {
92 return;
93 }
94 ClusterService clusterService = get(ClusterService.class);
95 print("-------------------------------------------------------------------");
Jordan Halterman2bf177c2017-06-29 01:49:08 -070096 print(CLIENT_FMT, "Name", "Servers");
Madan Jampaniccdf9da2016-05-05 14:37:27 -070097 print("-------------------------------------------------------------------");
98
99 for (PartitionClientInfo info : partitionClientInfo) {
100 boolean first = true;
101 for (NodeId serverId : Ordering.natural().sortedCopy(info.servers())) {
102 ControllerNode server = clusterService.getNode(serverId);
103 String serverString = String.format("%s:%d", server.id(), server.tcpPort());
104 if (first) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700105 print(CLIENT_FMT, info.partitionId(), serverString);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700106 first = false;
107 } else {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700108 print(CLIENT_FMT, "", serverString);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700109 }
110 }
111 if (!first) {
112 print("-------------------------------------------------------------------");
113 }
114 }
115 }
116
117 /**
Ray Milkey501e0752015-03-04 16:11:50 -0800118 * Converts partition info into a JSON object.
119 *
120 * @param partitionInfo partition descriptions
121 */
122 private JsonNode json(List<PartitionInfo> partitionInfo) {
123 ObjectMapper mapper = new ObjectMapper();
124 ArrayNode partitions = mapper.createArrayNode();
125
126 // Create a JSON node for each partition
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700127 partitionInfo.forEach(info -> {
128 ObjectNode partition = mapper.createObjectNode();
Ray Milkey501e0752015-03-04 16:11:50 -0800129
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700130 // Add each member to the "members" array for this partition
131 ArrayNode members = partition.putArray("members");
132 info.members().forEach(members::add);
Ray Milkey501e0752015-03-04 16:11:50 -0800133
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700134 // Complete the partition attributes and add it to the array
Jordan Halterman980a8c12017-09-22 18:01:19 -0700135 partition.put("name", info.id().toString())
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700136 .put("term", info.term())
137 .put("leader", info.leader());
138 partitions.add(partition);
Ray Milkey501e0752015-03-04 16:11:50 -0800139
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700140 });
Ray Milkey501e0752015-03-04 16:11:50 -0800141
142 return partitions;
143 }
144
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700145 /**
146 * Converts partition client info into a JSON object.
147 *
148 * @param partitionClientInfo partition client descriptions
149 */
150 private JsonNode jsonForClientInfo(List<PartitionClientInfo> partitionClientInfo) {
151 ObjectMapper mapper = new ObjectMapper();
152 ArrayNode partitions = mapper.createArrayNode();
153 ClusterService clusterService = get(ClusterService.class);
154
155 // Create a JSON node for each partition client
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700156 partitionClientInfo.forEach(info -> {
157 ObjectNode partition = mapper.createObjectNode();
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700158
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700159 // Add each member to the "servers" array for this partition
160 ArrayNode servers = partition.putArray("servers");
161 info.servers()
162 .stream()
163 .map(clusterService::getNode)
164 .map(node -> String.format("%s:%d", node.ip(), node.tcpPort()))
165 .forEach(servers::add);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700166
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700167 // Complete the partition attributes and add it to the array
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700168 partition.put("partitionId", info.partitionId().toString());
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700169 partitions.add(partition);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700170
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700171 });
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700172
173 return partitions;
174 }
175
Ray Milkey501e0752015-03-04 16:11:50 -0800176 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700177 protected void doExecute() {
Ray Milkey501e0752015-03-04 16:11:50 -0800178 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700179 if (reportClientInfo) {
180 PartitionAdminService partitionAdminService = get(PartitionAdminService.class);
181 List<PartitionClientInfo> partitionClientInfo = partitionAdminService.partitionClientInfo();
182 if (outputJson()) {
183 print("%s", jsonForClientInfo(partitionClientInfo));
184 } else {
185 displayPartitionClients(partitionClientInfo);
186 }
Ray Milkey501e0752015-03-04 16:11:50 -0800187 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700188 List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
189 if (outputJson()) {
190 print("%s", json(partitionInfo));
191 } else {
192 displayPartitions(partitionInfo);
193 }
Ray Milkey501e0752015-03-04 16:11:50 -0800194 }
195 }
Jonathan Hart054da972015-02-18 17:30:28 -0800196}