Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.store.primitives.PartitionAdminService; |
| 27 | import org.onosproject.store.service.PartitionClientInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 28 | import org.onosproject.store.service.PartitionInfo; |
| 29 | import org.onosproject.store.service.StorageAdminService; |
| 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 | */ |
| 40 | @Command(scope = "onos", name = "partitions", |
| 41 | description = "Lists information about partitions in the system") |
| 42 | public class PartitionsListCommand extends AbstractShellCommand { |
| 43 | |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 44 | @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 Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 51 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Displays partition info as text. |
| 54 | * |
| 55 | * @param partitionInfo partition descriptions |
| 56 | */ |
| 57 | private void displayPartitions(List<PartitionInfo> partitionInfo) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 58 | if (partitionInfo.isEmpty()) { |
| 59 | return; |
| 60 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 61 | print("----------------------------------------------------------"); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 62 | print(SERVER_FMT, "Name", "Term", "Members", ""); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 63 | print("----------------------------------------------------------"); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 64 | |
| 65 | for (PartitionInfo info : partitionInfo) { |
| 66 | boolean first = true; |
Madan Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame] | 67 | for (String member : Ordering.natural().sortedCopy(info.members())) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 68 | if (first) { |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 69 | print(SERVER_FMT, info.name(), info.term(), member, |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 70 | member.equals(info.leader()) ? "*" : ""); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 71 | first = false; |
| 72 | } else { |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 73 | print(SERVER_FMT, "", "", member, |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 74 | member.equals(info.leader()) ? "*" : ""); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 77 | if (!first) { |
| 78 | print("----------------------------------------------------------"); |
| 79 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 84 | * 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 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 |
| 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 Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 147 | /** |
| 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 Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 181 | @Override |
| 182 | protected void execute() { |
| 183 | StorageAdminService storageAdminService = get(StorageAdminService.class); |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 184 | 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 Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 192 | } else { |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 193 | List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo(); |
| 194 | if (outputJson()) { |
| 195 | print("%s", json(partitionInfo)); |
| 196 | } else { |
| 197 | displayPartitions(partitionInfo); |
| 198 | } |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 201 | } |