Ray Milkey
Committed by Gerrit Code Review

ONOS-1219 - Add JSON output to the leaders command

Change-Id: I180b39c16dcbc9d71f857507c1f04ed01e11a240
...@@ -24,6 +24,10 @@ import org.onosproject.cli.AbstractShellCommand; ...@@ -24,6 +24,10 @@ import org.onosproject.cli.AbstractShellCommand;
24 import org.onosproject.cluster.Leadership; 24 import org.onosproject.cluster.Leadership;
25 import org.onosproject.cluster.LeadershipService; 25 import org.onosproject.cluster.LeadershipService;
26 26
27 +import com.fasterxml.jackson.databind.JsonNode;
28 +import com.fasterxml.jackson.databind.ObjectMapper;
29 +import com.fasterxml.jackson.databind.node.ArrayNode;
30 +
27 /** 31 /**
28 * Prints the leader for every topic. 32 * Prints the leader for every topic.
29 */ 33 */
...@@ -33,16 +37,10 @@ public class LeaderCommand extends AbstractShellCommand { ...@@ -33,16 +37,10 @@ public class LeaderCommand extends AbstractShellCommand {
33 37
34 private static final String FMT = "%-20s | %-15s | %-6s | %-10s |"; 38 private static final String FMT = "%-20s | %-15s | %-6s | %-10s |";
35 39
36 - @Override 40 + /**
37 - protected void execute() { 41 + * Compares leaders, sorting by toString() output.
38 - LeadershipService leaderService = get(LeadershipService.class); 42 + */
39 - Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard(); 43 + private Comparator<Leadership> leadershipComparator =
40 - print("--------------------------------------------------------------");
41 - print(FMT, "Topic", "Leader", "Epoch", "Elected");
42 - print("--------------------------------------------------------------");
43 -
44 -
45 - Comparator<Leadership> leadershipComparator =
46 (e1, e2) -> { 44 (e1, e2) -> {
47 if (e1.leader() == null && e2.leader() == null) { 45 if (e1.leader() == null && e2.leader() == null) {
48 return 0; 46 return 0;
...@@ -56,6 +54,16 @@ public class LeaderCommand extends AbstractShellCommand { ...@@ -56,6 +54,16 @@ public class LeaderCommand extends AbstractShellCommand {
56 return e1.leader().toString().compareTo(e2.leader().toString()); 54 return e1.leader().toString().compareTo(e2.leader().toString());
57 }; 55 };
58 56
57 + /**
58 + * Displays text representing the leaders.
59 + *
60 + * @param leaderBoard map of leaders
61 + */
62 + private void displayLeaders(Map<String, Leadership> leaderBoard) {
63 + print("--------------------------------------------------------------");
64 + print(FMT, "Topic", "Leader", "Epoch", "Elected");
65 + print("--------------------------------------------------------------");
66 +
59 leaderBoard.values() 67 leaderBoard.values()
60 .stream() 68 .stream()
61 .sorted(leadershipComparator) 69 .sorted(leadershipComparator)
...@@ -66,4 +74,39 @@ public class LeaderCommand extends AbstractShellCommand { ...@@ -66,4 +74,39 @@ public class LeaderCommand extends AbstractShellCommand {
66 Tools.timeAgo(l.electedTime()))); 74 Tools.timeAgo(l.electedTime())));
67 print("--------------------------------------------------------------"); 75 print("--------------------------------------------------------------");
68 } 76 }
77 +
78 + /**
79 + * Returns JSON node representing the leaders.
80 + *
81 + * @param leaderBoard map of leaders
82 + */
83 + private JsonNode json(Map<String, Leadership> leaderBoard) {
84 + ObjectMapper mapper = new ObjectMapper();
85 + ArrayNode result = mapper.createArrayNode();
86 + leaderBoard.values()
87 + .stream()
88 + .sorted(leadershipComparator)
89 + .forEach(l ->
90 + result.add(
91 + mapper.createObjectNode()
92 + .put("topic", l.topic())
93 + .put("leader", l.leader().toString())
94 + .put("epoch", l.epoch())
95 + .put("electedTime", Tools.timeAgo(l.electedTime()))));
96 +
97 + return result;
98 + }
99 +
100 +
101 + @Override
102 + protected void execute() {
103 + LeadershipService leaderService = get(LeadershipService.class);
104 + Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard();
105 +
106 + if (outputJson()) {
107 + print("%s", json(leaderBoard));
108 + } else {
109 + displayLeaders(leaderBoard);
110 + }
111 + }
69 } 112 }
......