blob: c540ea80cf13a93b97a5b0f317c183065d0b7de9 [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;
Jonathan Hart054da972015-02-18 17:30:28 -080030
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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
Jonathan Hart054da972015-02-18 17:30:28 -080041@Command(scope = "onos", name = "partitions",
42 description = "Lists information about partitions in the system")
43public class PartitionsListCommand extends AbstractShellCommand {
44
Madan Jampaniccdf9da2016-05-05 14:37:27 -070045 @Option(name = "-c", aliases = "--clients",
Jordan Halterman2bf177c2017-06-29 01:49:08 -070046 description = "Show information about partition clients",
Madan Jampaniccdf9da2016-05-05 14:37:27 -070047 required = false, multiValued = false)
48 private boolean reportClientInfo = false;
49
50 private static final String SERVER_FMT = "%-20s %8s %25s %s";
Jordan Halterman7ff7fef2017-07-31 16:08:40 -070051 private static final String CLIENT_FMT = "%-20s %8s";
Jonathan Hart054da972015-02-18 17:30:28 -080052
Ray Milkey501e0752015-03-04 16:11:50 -080053 /**
54 * Displays partition info as text.
55 *
56 * @param partitionInfo partition descriptions
57 */
58 private void displayPartitions(List<PartitionInfo> partitionInfo) {
Madan Jampanif172d402016-03-04 00:56:38 -080059 if (partitionInfo.isEmpty()) {
60 return;
61 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070062 print("----------------------------------------------------------");
Madan Jampaniccdf9da2016-05-05 14:37:27 -070063 print(SERVER_FMT, "Name", "Term", "Members", "");
Madan Jampanif1b8e172015-03-23 11:42:02 -070064 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080065
66 for (PartitionInfo info : partitionInfo) {
67 boolean first = true;
Madan Jampani630c8822016-02-24 10:38:21 -080068 for (String member : Ordering.natural().sortedCopy(info.members())) {
Jonathan Hart054da972015-02-18 17:30:28 -080069 if (first) {
Jordan Halterman980a8c12017-09-22 18:01:19 -070070 print(SERVER_FMT, info.id(), info.term(), member,
Ray Milkey501e0752015-03-04 16:11:50 -080071 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080072 first = false;
73 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -070074 print(SERVER_FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080075 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080076 }
77 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070078 if (!first) {
79 print("----------------------------------------------------------");
80 }
Jonathan Hart054da972015-02-18 17:30:28 -080081 }
82 }
Ray Milkey501e0752015-03-04 16:11:50 -080083
84 /**
Madan Jampaniccdf9da2016-05-05 14:37:27 -070085 * Displays partition client info as text.
86 *
87 * @param partitionClientInfo partition client information
88 */
89 private void displayPartitionClients(List<PartitionClientInfo> partitionClientInfo) {
90 if (partitionClientInfo.isEmpty()) {
91 return;
92 }
93 ClusterService clusterService = get(ClusterService.class);
94 print("-------------------------------------------------------------------");
Jordan Halterman2bf177c2017-06-29 01:49:08 -070095 print(CLIENT_FMT, "Name", "Servers");
Madan Jampaniccdf9da2016-05-05 14:37:27 -070096 print("-------------------------------------------------------------------");
97
98 for (PartitionClientInfo info : partitionClientInfo) {
99 boolean first = true;
100 for (NodeId serverId : Ordering.natural().sortedCopy(info.servers())) {
101 ControllerNode server = clusterService.getNode(serverId);
102 String serverString = String.format("%s:%d", server.id(), server.tcpPort());
103 if (first) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700104 print(CLIENT_FMT, info.partitionId(), serverString);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700105 first = false;
106 } else {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700107 print(CLIENT_FMT, "", serverString);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700108 }
109 }
110 if (!first) {
111 print("-------------------------------------------------------------------");
112 }
113 }
114 }
115
116 /**
Ray Milkey501e0752015-03-04 16:11:50 -0800117 * Converts partition info into a JSON object.
118 *
119 * @param partitionInfo partition descriptions
120 */
121 private JsonNode json(List<PartitionInfo> partitionInfo) {
122 ObjectMapper mapper = new ObjectMapper();
123 ArrayNode partitions = mapper.createArrayNode();
124
125 // Create a JSON node for each partition
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700126 partitionInfo.forEach(info -> {
127 ObjectNode partition = mapper.createObjectNode();
Ray Milkey501e0752015-03-04 16:11:50 -0800128
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700129 // Add each member to the "members" array for this partition
130 ArrayNode members = partition.putArray("members");
131 info.members().forEach(members::add);
Ray Milkey501e0752015-03-04 16:11:50 -0800132
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700133 // Complete the partition attributes and add it to the array
Jordan Halterman980a8c12017-09-22 18:01:19 -0700134 partition.put("name", info.id().toString())
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700135 .put("term", info.term())
136 .put("leader", info.leader());
137 partitions.add(partition);
Ray Milkey501e0752015-03-04 16:11:50 -0800138
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700139 });
Ray Milkey501e0752015-03-04 16:11:50 -0800140
141 return partitions;
142 }
143
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700144 /**
145 * Converts partition client info into a JSON object.
146 *
147 * @param partitionClientInfo partition client descriptions
148 */
149 private JsonNode jsonForClientInfo(List<PartitionClientInfo> partitionClientInfo) {
150 ObjectMapper mapper = new ObjectMapper();
151 ArrayNode partitions = mapper.createArrayNode();
152 ClusterService clusterService = get(ClusterService.class);
153
154 // Create a JSON node for each partition client
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700155 partitionClientInfo.forEach(info -> {
156 ObjectNode partition = mapper.createObjectNode();
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700157
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700158 // Add each member to the "servers" array for this partition
159 ArrayNode servers = partition.putArray("servers");
160 info.servers()
161 .stream()
162 .map(clusterService::getNode)
163 .map(node -> String.format("%s:%d", node.ip(), node.tcpPort()))
164 .forEach(servers::add);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700165
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700166 // Complete the partition attributes and add it to the array
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700167 partition.put("partitionId", info.partitionId().toString());
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700168 partitions.add(partition);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700169
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700170 });
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700171
172 return partitions;
173 }
174
Ray Milkey501e0752015-03-04 16:11:50 -0800175 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700176 protected void doExecute() {
Ray Milkey8efe2782019-02-08 12:29:39 -0800177 PartitionAdminService partitionAdminService = get(PartitionAdminService.class);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700178 if (reportClientInfo) {
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700179 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 {
Ray Milkey8efe2782019-02-08 12:29:39 -0800186 List<PartitionInfo> partitionInfo = partitionAdminService.partitionInfo();
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700187 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}