Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.service.PartitionInfo; |
| 23 | import org.onosproject.store.service.StorageAdminService; |
| 24 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 25 | import com.fasterxml.jackson.databind.JsonNode; |
| 26 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Madan Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame] | 29 | import com.google.common.collect.Ordering; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Command to list the database partitions in the system. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "partitions", |
| 35 | description = "Lists information about partitions in the system") |
| 36 | public class PartitionsListCommand extends AbstractShellCommand { |
| 37 | |
| 38 | private static final String FMT = "%-20s %8s %25s %s"; |
| 39 | |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Displays partition info as text. |
| 42 | * |
| 43 | * @param partitionInfo partition descriptions |
| 44 | */ |
| 45 | private void displayPartitions(List<PartitionInfo> partitionInfo) { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 46 | print("----------------------------------------------------------"); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 47 | print(FMT, "Name", "Term", "Members", ""); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 48 | print("----------------------------------------------------------"); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 49 | |
| 50 | for (PartitionInfo info : partitionInfo) { |
| 51 | boolean first = true; |
Madan Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame] | 52 | for (String member : Ordering.natural().sortedCopy(info.members())) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 53 | if (first) { |
| 54 | print(FMT, info.name(), info.term(), member, |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 55 | member.equals(info.leader()) ? "*" : ""); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 56 | first = false; |
| 57 | } else { |
| 58 | print(FMT, "", "", member, |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 59 | member.equals(info.leader()) ? "*" : ""); |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 62 | if (!first) { |
| 63 | print("----------------------------------------------------------"); |
| 64 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
Ray Milkey | 501e075 | 2015-03-04 16:11:50 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Converts partition info into a JSON object. |
| 70 | * |
| 71 | * @param partitionInfo partition descriptions |
| 72 | */ |
| 73 | private JsonNode json(List<PartitionInfo> partitionInfo) { |
| 74 | ObjectMapper mapper = new ObjectMapper(); |
| 75 | ArrayNode partitions = mapper.createArrayNode(); |
| 76 | |
| 77 | // Create a JSON node for each partition |
| 78 | partitionInfo.stream() |
| 79 | .forEach(info -> { |
| 80 | ObjectNode partition = mapper.createObjectNode(); |
| 81 | |
| 82 | // Add each member to the "members" array for this partition |
| 83 | ArrayNode members = partition.putArray("members"); |
| 84 | info.members() |
| 85 | .stream() |
| 86 | .forEach(members::add); |
| 87 | |
| 88 | // Complete the partition attributes and add it to the array |
| 89 | partition.put("name", info.name()) |
| 90 | .put("term", info.term()) |
| 91 | .put("leader", info.leader()); |
| 92 | partitions.add(partition); |
| 93 | |
| 94 | }); |
| 95 | |
| 96 | return partitions; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected void execute() { |
| 101 | StorageAdminService storageAdminService = get(StorageAdminService.class); |
| 102 | List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo(); |
| 103 | |
| 104 | if (outputJson()) { |
| 105 | print("%s", json(partitionInfo)); |
| 106 | } else { |
| 107 | displayPartitions(partitionInfo); |
| 108 | } |
| 109 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 110 | } |