blob: 28ce78127329248dddb6a958021808c7392fd152 [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",
45 description = "Show inforamtion about partition clients",
46 required = false, multiValued = false)
47 private boolean reportClientInfo = false;
48
49 private static final String SERVER_FMT = "%-20s %8s %25s %s";
50 private static final String CLIENT_FMT = "%-20s %8s %10s %25s";
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("-------------------------------------------------------------------");
94 print(CLIENT_FMT, "Name", "SessionId", "Status", "Servers");
95 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) {
103 print(CLIENT_FMT, info.partitionId(), info.sessionId(),
104 info.status(), serverString);
105 first = false;
106 } else {
107 print(CLIENT_FMT, "", "", "", serverString);
108 }
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
134 partition.put("name", info.name())
135 .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
167 partition.put("partitionId", info.partitionId().toString())
168 .put("sessionId", info.sessionId())
169 .put("status", info.status().toString());
170 partitions.add(partition);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700171
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700172 });
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700173
174 return partitions;
175 }
176
Ray Milkey501e0752015-03-04 16:11:50 -0800177 @Override
178 protected void execute() {
179 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700180 if (reportClientInfo) {
181 PartitionAdminService partitionAdminService = get(PartitionAdminService.class);
182 List<PartitionClientInfo> partitionClientInfo = partitionAdminService.partitionClientInfo();
183 if (outputJson()) {
184 print("%s", jsonForClientInfo(partitionClientInfo));
185 } else {
186 displayPartitionClients(partitionClientInfo);
187 }
Ray Milkey501e0752015-03-04 16:11:50 -0800188 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700189 List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
190 if (outputJson()) {
191 print("%s", json(partitionInfo));
192 } else {
193 displayPartitions(partitionInfo);
194 }
Ray Milkey501e0752015-03-04 16:11:50 -0800195 }
196 }
Jonathan Hart054da972015-02-18 17:30:28 -0800197}