Ray Milkey
Committed by Gerrit Code Review

ONOS-1220 - JSON output for partitions command

Change-Id: I2d1418ed36dc06a2a17f05cc07e5ef221da84baa
...@@ -15,12 +15,17 @@ ...@@ -15,12 +15,17 @@
15 */ 15 */
16 package org.onosproject.cli.net; 16 package org.onosproject.cli.net;
17 17
18 +import java.util.List;
19 +
18 import org.apache.karaf.shell.commands.Command; 20 import org.apache.karaf.shell.commands.Command;
19 import org.onosproject.cli.AbstractShellCommand; 21 import org.onosproject.cli.AbstractShellCommand;
20 import org.onosproject.store.service.PartitionInfo; 22 import org.onosproject.store.service.PartitionInfo;
21 import org.onosproject.store.service.StorageAdminService; 23 import org.onosproject.store.service.StorageAdminService;
22 24
23 -import java.util.List; 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;
24 29
25 /** 30 /**
26 * Command to list the database partitions in the system. 31 * Command to list the database partitions in the system.
...@@ -31,11 +36,12 @@ public class PartitionsListCommand extends AbstractShellCommand { ...@@ -31,11 +36,12 @@ public class PartitionsListCommand extends AbstractShellCommand {
31 36
32 private static final String FMT = "%-20s %8s %25s %s"; 37 private static final String FMT = "%-20s %8s %25s %s";
33 38
34 - @Override 39 + /**
35 - protected void execute() { 40 + * Displays partition info as text.
36 - StorageAdminService storageAdminService = get(StorageAdminService.class); 41 + *
37 - List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo(); 42 + * @param partitionInfo partition descriptions
38 - 43 + */
44 + private void displayPartitions(List<PartitionInfo> partitionInfo) {
39 print(FMT, "Name", "Term", "Members", ""); 45 print(FMT, "Name", "Term", "Members", "");
40 46
41 for (PartitionInfo info : partitionInfo) { 47 for (PartitionInfo info : partitionInfo) {
...@@ -43,13 +49,56 @@ public class PartitionsListCommand extends AbstractShellCommand { ...@@ -43,13 +49,56 @@ public class PartitionsListCommand extends AbstractShellCommand {
43 for (String member : info.members()) { 49 for (String member : info.members()) {
44 if (first) { 50 if (first) {
45 print(FMT, info.name(), info.term(), member, 51 print(FMT, info.name(), info.term(), member,
46 - member.equals(info.leader()) ? "*" : ""); 52 + member.equals(info.leader()) ? "*" : "");
47 first = false; 53 first = false;
48 } else { 54 } else {
49 print(FMT, "", "", member, 55 print(FMT, "", "", member,
50 - member.equals(info.leader()) ? "*" : ""); 56 + member.equals(info.leader()) ? "*" : "");
51 } 57 }
52 } 58 }
53 } 59 }
54 } 60 }
61 +
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 + }
55 } 104 }
......