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