blob: 74375dd72b9eeac5fcfdc7b7a07eb1965ef460bc [file] [log] [blame]
Jonathan Hart054da972015-02-18 17:30:28 -08001/*
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 */
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 Jampanif1b8e172015-03-23 11:42:02 -070046 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080047 print(FMT, "Name", "Term", "Members", "");
Madan Jampanif1b8e172015-03-23 11:42:02 -070048 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080049
50 for (PartitionInfo info : partitionInfo) {
51 boolean first = true;
Madan Jampani630c8822016-02-24 10:38:21 -080052 for (String member : Ordering.natural().sortedCopy(info.members())) {
Jonathan Hart054da972015-02-18 17:30:28 -080053 if (first) {
54 print(FMT, info.name(), info.term(), member,
Ray Milkey501e0752015-03-04 16:11:50 -080055 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080056 first = false;
57 } else {
58 print(FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080059 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080060 }
61 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070062 if (!first) {
63 print("----------------------------------------------------------");
64 }
Jonathan Hart054da972015-02-18 17:30:28 -080065 }
66 }
Ray Milkey501e0752015-03-04 16:11:50 -080067
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 Hart054da972015-02-18 17:30:28 -0800110}