Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 18 | import java.util.List; |
| 19 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ControllerNode; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.store.primitives.PartitionAdminService; |
| 28 | import org.onosproject.store.service.PartitionClientInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 29 | import org.onosproject.store.service.PartitionInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 30 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 31 | import com.fasterxml.jackson.databind.JsonNode; |
| 32 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 33 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 34 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Madan Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame] | 35 | import com.google.common.collect.Ordering; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Command to list the database partitions in the system. |
| 39 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | @Service |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 41 | @Command(scope = "onos", name = "partitions", |
| 42 | description = "Lists information about partitions in the system") |
| 43 | public class PartitionsListCommand extends AbstractShellCommand { |
| 44 | |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 45 | @Option(name = "-c", aliases = "--clients", |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | description = "Show information about partition clients", |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 47 | required = false, multiValued = false) |
| 48 | private boolean reportClientInfo = false; |
| 49 | |
| 50 | private static final String SERVER_FMT = "%-20s %8s %25s %s"; |
Jordan Halterman | 7ff7fef | 2017-07-31 16:08:40 -0700 | [diff] [blame] | 51 | private static final String CLIENT_FMT = "%-20s %8s"; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 52 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Displays partition info as text. |
| 55 | * |
| 56 | * @param partitionInfo partition descriptions |
| 57 | */ |
| 58 | private void displayPartitions(List<PartitionInfo> partitionInfo) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 59 | if (partitionInfo.isEmpty()) { |
| 60 | return; |
| 61 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 62 | print("----------------------------------------------------------"); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 63 | print(SERVER_FMT, "Name", "Term", "Members", ""); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 64 | print("----------------------------------------------------------"); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 65 | |
| 66 | for (PartitionInfo info : partitionInfo) { |
| 67 | boolean first = true; |
Madan Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame] | 68 | for (String member : Ordering.natural().sortedCopy(info.members())) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 69 | if (first) { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 70 | print(SERVER_FMT, info.id(), info.term(), member, |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 71 | member.equals(info.leader()) ? "*" : ""); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 72 | first = false; |
| 73 | } else { |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 74 | print(SERVER_FMT, "", "", member, |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 75 | member.equals(info.leader()) ? "*" : ""); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 78 | if (!first) { |
| 79 | print("----------------------------------------------------------"); |
| 80 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 85 | * 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 95 | print(CLIENT_FMT, "Name", "Servers"); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 96 | 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 104 | print(CLIENT_FMT, info.partitionId(), serverString); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 105 | first = false; |
| 106 | } else { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 107 | print(CLIENT_FMT, "", serverString); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | if (!first) { |
| 111 | print("-------------------------------------------------------------------"); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 117 | * 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 SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 126 | partitionInfo.forEach(info -> { |
| 127 | ObjectNode partition = mapper.createObjectNode(); |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 128 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 129 | // Add each member to the "members" array for this partition |
| 130 | ArrayNode members = partition.putArray("members"); |
| 131 | info.members().forEach(members::add); |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 132 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 133 | // Complete the partition attributes and add it to the array |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 134 | partition.put("name", info.id().toString()) |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 135 | .put("term", info.term()) |
| 136 | .put("leader", info.leader()); |
| 137 | partitions.add(partition); |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 138 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 139 | }); |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 140 | |
| 141 | return partitions; |
| 142 | } |
| 143 | |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 144 | /** |
| 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 SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 155 | partitionClientInfo.forEach(info -> { |
| 156 | ObjectNode partition = mapper.createObjectNode(); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 157 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 158 | // 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 Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 165 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 166 | // Complete the partition attributes and add it to the array |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 167 | partition.put("partitionId", info.partitionId().toString()); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 168 | partitions.add(partition); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 169 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 170 | }); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 171 | |
| 172 | return partitions; |
| 173 | } |
| 174 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 175 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 176 | protected void doExecute() { |
Ray Milkey | 8efe278 | 2019-02-08 12:29:39 -0800 | [diff] [blame] | 177 | PartitionAdminService partitionAdminService = get(PartitionAdminService.class); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 178 | if (reportClientInfo) { |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 179 | List<PartitionClientInfo> partitionClientInfo = partitionAdminService.partitionClientInfo(); |
| 180 | if (outputJson()) { |
| 181 | print("%s", jsonForClientInfo(partitionClientInfo)); |
| 182 | } else { |
| 183 | displayPartitionClients(partitionClientInfo); |
| 184 | } |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 185 | } else { |
Ray Milkey | 8efe278 | 2019-02-08 12:29:39 -0800 | [diff] [blame] | 186 | List<PartitionInfo> partitionInfo = partitionAdminService.partitionInfo(); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 187 | if (outputJson()) { |
| 188 | print("%s", json(partitionInfo)); |
| 189 | } else { |
| 190 | displayPartitions(partitionInfo); |
| 191 | } |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 194 | } |