blob: 9fb92db92179e1da1953e72513f847d882f62574 [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
126 partitionInfo.stream()
127 .forEach(info -> {
128 ObjectNode partition = mapper.createObjectNode();
129
130 // Add each member to the "members" array for this partition
131 ArrayNode members = partition.putArray("members");
132 info.members()
133 .stream()
134 .forEach(members::add);
135
136 // Complete the partition attributes and add it to the array
137 partition.put("name", info.name())
138 .put("term", info.term())
139 .put("leader", info.leader());
140 partitions.add(partition);
141
142 });
143
144 return partitions;
145 }
146
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700147 /**
148 * Converts partition client info into a JSON object.
149 *
150 * @param partitionClientInfo partition client descriptions
151 */
152 private JsonNode jsonForClientInfo(List<PartitionClientInfo> partitionClientInfo) {
153 ObjectMapper mapper = new ObjectMapper();
154 ArrayNode partitions = mapper.createArrayNode();
155 ClusterService clusterService = get(ClusterService.class);
156
157 // Create a JSON node for each partition client
158 partitionClientInfo.stream()
159 .forEach(info -> {
160 ObjectNode partition = mapper.createObjectNode();
161
162 // Add each member to the "servers" array for this partition
163 ArrayNode servers = partition.putArray("servers");
164 info.servers()
165 .stream()
166 .map(clusterService::getNode)
167 .map(node -> String.format("%s:%d", node.ip(), node.tcpPort()))
168 .forEach(servers::add);
169
170 // Complete the partition attributes and add it to the array
171 partition.put("partitionId", info.partitionId().toString())
172 .put("sessionId", info.sessionId())
173 .put("status", info.status().toString());
174 partitions.add(partition);
175
176 });
177
178 return partitions;
179 }
180
Ray Milkey501e0752015-03-04 16:11:50 -0800181 @Override
182 protected void execute() {
183 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700184 if (reportClientInfo) {
185 PartitionAdminService partitionAdminService = get(PartitionAdminService.class);
186 List<PartitionClientInfo> partitionClientInfo = partitionAdminService.partitionClientInfo();
187 if (outputJson()) {
188 print("%s", jsonForClientInfo(partitionClientInfo));
189 } else {
190 displayPartitionClients(partitionClientInfo);
191 }
Ray Milkey501e0752015-03-04 16:11:50 -0800192 } else {
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700193 List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
194 if (outputJson()) {
195 print("%s", json(partitionInfo));
196 } else {
197 displayPartitions(partitionInfo);
198 }
Ray Milkey501e0752015-03-04 16:11:50 -0800199 }
200 }
Jonathan Hart054da972015-02-18 17:30:28 -0800201}