blob: 8b3d16609a850116c3e563675d691d29284c248c [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;
Jonathan Hart054da972015-02-18 17:30:28 -080029
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")
35public class PartitionsListCommand extends AbstractShellCommand {
36
37 private static final String FMT = "%-20s %8s %25s %s";
38
Ray Milkey501e0752015-03-04 16:11:50 -080039 /**
40 * Displays partition info as text.
41 *
42 * @param partitionInfo partition descriptions
43 */
44 private void displayPartitions(List<PartitionInfo> partitionInfo) {
Madan Jampanif1b8e172015-03-23 11:42:02 -070045 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080046 print(FMT, "Name", "Term", "Members", "");
Madan Jampanif1b8e172015-03-23 11:42:02 -070047 print("----------------------------------------------------------");
Jonathan Hart054da972015-02-18 17:30:28 -080048
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 Milkey501e0752015-03-04 16:11:50 -080054 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080055 first = false;
56 } else {
57 print(FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080058 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080059 }
60 }
Madan Jampanif1b8e172015-03-23 11:42:02 -070061 if (!first) {
62 print("----------------------------------------------------------");
63 }
Jonathan Hart054da972015-02-18 17:30:28 -080064 }
65 }
Ray Milkey501e0752015-03-04 16:11:50 -080066
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 Hart054da972015-02-18 17:30:28 -0800109}