blob: 3acd21fcacc90c280d905770e89b4d45b29991f8 [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) {
Jonathan Hart054da972015-02-18 17:30:28 -080045 print(FMT, "Name", "Term", "Members", "");
46
47 for (PartitionInfo info : partitionInfo) {
48 boolean first = true;
49 for (String member : info.members()) {
50 if (first) {
51 print(FMT, info.name(), info.term(), member,
Ray Milkey501e0752015-03-04 16:11:50 -080052 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080053 first = false;
54 } else {
55 print(FMT, "", "", member,
Ray Milkey501e0752015-03-04 16:11:50 -080056 member.equals(info.leader()) ? "*" : "");
Jonathan Hart054da972015-02-18 17:30:28 -080057 }
58 }
59 }
60 }
Ray Milkey501e0752015-03-04 16:11:50 -080061
62 /**
63 * Converts partition info into a JSON object.
64 *
65 * @param partitionInfo partition descriptions
66 */
67 private JsonNode json(List<PartitionInfo> partitionInfo) {
68 ObjectMapper mapper = new ObjectMapper();
69 ArrayNode partitions = mapper.createArrayNode();
70
71 // Create a JSON node for each partition
72 partitionInfo.stream()
73 .forEach(info -> {
74 ObjectNode partition = mapper.createObjectNode();
75
76 // Add each member to the "members" array for this partition
77 ArrayNode members = partition.putArray("members");
78 info.members()
79 .stream()
80 .forEach(members::add);
81
82 // Complete the partition attributes and add it to the array
83 partition.put("name", info.name())
84 .put("term", info.term())
85 .put("leader", info.leader());
86 partitions.add(partition);
87
88 });
89
90 return partitions;
91 }
92
93 @Override
94 protected void execute() {
95 StorageAdminService storageAdminService = get(StorageAdminService.class);
96 List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
97
98 if (outputJson()) {
99 print("%s", json(partitionInfo));
100 } else {
101 displayPartitions(partitionInfo);
102 }
103 }
Jonathan Hart054da972015-02-18 17:30:28 -0800104}