blob: 32a745285cae9943d6b19cf21567379beef232d3 [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;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.service.PartitionInfo;
23import org.onosproject.store.service.StorageAdminService;
24
Ray Milkey501e0752015-03-04 16:11:50 -080025import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ArrayNode;
28import com.fasterxml.jackson.databind.node.ObjectNode;
Madan Jampani630c8822016-02-24 10:38:21 -080029import com.google.common.collect.Ordering;
Jonathan Hart054da972015-02-18 17:30:28 -080030
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")
36public class PartitionsListCommand extends AbstractShellCommand {
37
38 private static final String FMT = "%-20s %8s %25s %s";
39
Ray Milkey501e0752015-03-04 16:11:50 -080040 /**
41 * Displays partition info as text.
42 *
43 * @param partitionInfo partition descriptions
44 */
45 private void displayPartitions(List<PartitionInfo> partitionInfo) {
Madan Jampanif172d402016-03-04 00:56:38 -080046 if (partitionInfo.isEmpty()) {
47 return;
48 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070049 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080050 print(FMT, "Name", "Term", "Members", "");
Madan Jampanif1b8e172015-03-23 11:42:02 -070051 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080052
53 for (PartitionInfo info : partitionInfo) {
54 boolean first = true;
Madan Jampani630c8822016-02-24 10:38:21 -080055 for (String member : Ordering.natural().sortedCopy(info.members())) {
Jonathan Hart054da972015-02-18 17:30:28 -080056 if (first) {
57 print(FMT, info.name(), info.term(), member,
Ray Milkey501e0752015-03-04 16:11:50 -080058 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080059 first = false;
60 } else {
61 print(FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080062 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080063 }
64 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070065 if (!first) {
66 print("----------------------------------------------------------");
67 }
Jonathan Hart054da972015-02-18 17:30:28 -080068 }
69 }
Ray Milkey501e0752015-03-04 16:11:50 -080070
71 /**
72 * Converts partition info into a JSON object.
73 *
74 * @param partitionInfo partition descriptions
75 */
76 private JsonNode json(List<PartitionInfo> partitionInfo) {
77 ObjectMapper mapper = new ObjectMapper();
78 ArrayNode partitions = mapper.createArrayNode();
79
80 // Create a JSON node for each partition
81 partitionInfo.stream()
82 .forEach(info -> {
83 ObjectNode partition = mapper.createObjectNode();
84
85 // Add each member to the "members" array for this partition
86 ArrayNode members = partition.putArray("members");
87 info.members()
88 .stream()
89 .forEach(members::add);
90
91 // Complete the partition attributes and add it to the array
92 partition.put("name", info.name())
93 .put("term", info.term())
94 .put("leader", info.leader());
95 partitions.add(partition);
96
97 });
98
99 return partitions;
100 }
101
102 @Override
103 protected void execute() {
104 StorageAdminService storageAdminService = get(StorageAdminService.class);
105 List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
106
107 if (outputJson()) {
108 print("%s", json(partitionInfo));
109 } else {
110 displayPartitions(partitionInfo);
111 }
112 }
Jonathan Hart054da972015-02-18 17:30:28 -0800113}