blob: 3c35cfc92286ae460b88d046f2b05a0f3cb25a07 [file] [log] [blame]
Jonathan Hart054da972015-02-18 17:30:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Jonathan Hart054da972015-02-18 17:30:28 -080020import org.apache.karaf.shell.commands.Command;
Madan Jampaniccdf9da2016-05-05 14:37:27 -070021import org.apache.karaf.shell.commands.Option;
Jonathan Hart054da972015-02-18 17:30:28 -080022import org.onosproject.cli.AbstractShellCommand;
Madan Jampaniccdf9da2016-05-05 14:37:27 -070023import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.ControllerNode;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.store.primitives.PartitionAdminService;
27import org.onosproject.store.service.PartitionClientInfo;
Jonathan Hart054da972015-02-18 17:30:28 -080028import org.onosproject.store.service.PartitionInfo;
29import org.onosproject.store.service.StorageAdminService;
30
Ray Milkey501e0752015-03-04 16:11:50 -080031import com.fasterxml.jackson.databind.JsonNode;
32import com.fasterxml.jackson.databind.ObjectMapper;
33import com.fasterxml.jackson.databind.node.ArrayNode;
34import com.fasterxml.jackson.databind.node.ObjectNode;
Madan Jampani630c8822016-02-24 10:38:21 -080035import com.google.common.collect.Ordering;
Jonathan Hart054da972015-02-18 17:30:28 -080036
37/**
38 * Command to list the database partitions in the system.
39 */
40@Command(scope = "onos", name = "partitions",
41 description = "Lists information about partitions in the system")
42public class PartitionsListCommand extends AbstractShellCommand {
43
Madan Jampaniccdf9da2016-05-05 14:37:27 -070044 @Option(name = "-c", aliases = "--clients",
Jordan Halterman2bf177c2017-06-29 01:49:08 -070045 description = "Show information about partition clients",
Madan Jampaniccdf9da2016-05-05 14:37:27 -070046 required = false, multiValued = false)
47 private boolean reportClientInfo = false;
48
49 private static final String SERVER_FMT = "%-20s %8s %25s %s";
Jordan Halterman7ff7fef2017-07-31 16:08:40 -070050 private static final String CLIENT_FMT = "%-20s %8s";
Jonathan Hart054da972015-02-18 17:30:28 -080051
Ray Milkey501e0752015-03-04 16:11:50 -080052 /**
53 * Displays partition info as text.
54 *
55 * @param partitionInfo partition descriptions
56 */
57 private void displayPartitions(List<PartitionInfo> partitionInfo) {
Madan Jampanif172d402016-03-04 00:56:38 -080058 if (partitionInfo.isEmpty()) {
59 return;
60 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070061 print("----------------------------------------------------------");
Madan Jampaniccdf9da2016-05-05 14:37:27 -070062 print(SERVER_FMT, "Name", "Term", "Members", "");
Madan Jampanif1b8e172015-03-23 11:42:02 -070063 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080064
65 for (PartitionInfo info : partitionInfo) {
66 boolean first = true;
Madan Jampani630c8822016-02-24 10:38:21 -080067 for (String member : Ordering.natural().sortedCopy(info.members())) {
Jonathan Hart054da972015-02-18 17:30:28 -080068 if (first) {
Madan Jampaniccdf9da2016-05-05 14:37:27 -070069 print(SERVER_FMT, info.name(), info.term(), member,
Ray Milkey501e0752015-03-04 16:11:50 -080070 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080071 first = false;
72 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -070073 print(SERVER_FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080074 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080075 }
76 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070077 if (!first) {
78 print("----------------------------------------------------------");
79 }
Jonathan Hart054da972015-02-18 17:30:28 -080080 }
81 }
Ray Milkey501e0752015-03-04 16:11:50 -080082
83 /**
Madan Jampaniccdf9da2016-05-05 14:37:27 -070084 * Displays partition client info as text.
85 *
86 * @param partitionClientInfo partition client information
87 */
88 private void displayPartitionClients(List<PartitionClientInfo> partitionClientInfo) {
89 if (partitionClientInfo.isEmpty()) {
90 return;
91 }
92 ClusterService clusterService = get(ClusterService.class);
93 print("-------------------------------------------------------------------");
Jordan Halterman2bf177c2017-06-29 01:49:08 -070094 print(CLIENT_FMT, "Name", "Servers");
Madan Jampaniccdf9da2016-05-05 14:37:27 -070095 print("-------------------------------------------------------------------");
96
97 for (PartitionClientInfo info : partitionClientInfo) {
98 boolean first = true;
99 for (NodeId serverId : Ordering.natural().sortedCopy(info.servers())) {
100 ControllerNode server = clusterService.getNode(serverId);
101 String serverString = String.format("%s:%d", server.id(), server.tcpPort());
102 if (first) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700103 print(CLIENT_FMT, info.partitionId(), serverString);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700104 first = false;
105 } else {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700106 print(CLIENT_FMT, "", serverString);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700107 }
108 }
109 if (!first) {
110 print("-------------------------------------------------------------------");
111 }
112 }
113 }
114
115 /**
Ray Milkey501e0752015-03-04 16:11:50 -0800116 * Converts partition info into a JSON object.
117 *
118 * @param partitionInfo partition descriptions
119 */
120 private JsonNode json(List<PartitionInfo> partitionInfo) {
121 ObjectMapper mapper = new ObjectMapper();
122 ArrayNode partitions = mapper.createArrayNode();
123
124 // Create a JSON node for each partition
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700125 partitionInfo.forEach(info -> {
126 ObjectNode partition = mapper.createObjectNode();
Ray Milkey501e0752015-03-04 16:11:50 -0800127
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700128 // Add each member to the "members" array for this partition
129 ArrayNode members = partition.putArray("members");
130 info.members().forEach(members::add);
Ray Milkey501e0752015-03-04 16:11:50 -0800131
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700132 // Complete the partition attributes and add it to the array
133 partition.put("name", info.name())
134 .put("term", info.term())
135 .put("leader", info.leader());
136 partitions.add(partition);
Ray Milkey501e0752015-03-04 16:11:50 -0800137
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700138 });
Ray Milkey501e0752015-03-04 16:11:50 -0800139
140 return partitions;
141 }
142
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700143 /**
144 * Converts partition client info into a JSON object.
145 *
146 * @param partitionClientInfo partition client descriptions
147 */
148 private JsonNode jsonForClientInfo(List<PartitionClientInfo> partitionClientInfo) {
149 ObjectMapper mapper = new ObjectMapper();
150 ArrayNode partitions = mapper.createArrayNode();
151 ClusterService clusterService = get(ClusterService.class);
152
153 // Create a JSON node for each partition client
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700154 partitionClientInfo.forEach(info -> {
155 ObjectNode partition = mapper.createObjectNode();
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700156
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700157 // Add each member to the "servers" array for this partition
158 ArrayNode servers = partition.putArray("servers");
159 info.servers()
160 .stream()
161 .map(clusterService::getNode)
162 .map(node -> String.format("%s:%d", node.ip(), node.tcpPort()))
163 .forEach(servers::add);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700164
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700165 // Complete the partition attributes and add it to the array
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700166 partition.put("partitionId", info.partitionId().toString());
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700167 partitions.add(partition);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700168
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700169 });
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700170
171 return partitions;
172 }
173
Ray Milkey501e0752015-03-04 16:11:50 -0800174 @Override
175 protected void execute() {
176 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700177 if (reportClientInfo) {
178 PartitionAdminService partitionAdminService = get(PartitionAdminService.class);
179 List<PartitionClientInfo> partitionClientInfo = partitionAdminService.partitionClientInfo();
180 if (outputJson()) {
181 print("%s", jsonForClientInfo(partitionClientInfo));
182 } else {
183 displayPartitionClients(partitionClientInfo);
184 }
Ray Milkey501e0752015-03-04 16:11:50 -0800185 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700186 List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
187 if (outputJson()) {
188 print("%s", json(partitionInfo));
189 } else {
190 displayPartitions(partitionInfo);
191 }
Ray Milkey501e0752015-03-04 16:11:50 -0800192 }
193 }
Jonathan Hart054da972015-02-18 17:30:28 -0800194}