Committed by
Gerrit Code Review
ONOS-1219 - Add JSON output to the leaders command
Change-Id: I180b39c16dcbc9d71f857507c1f04ed01e11a240
Showing
1 changed file
with
63 additions
and
20 deletions
... | @@ -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,29 +37,33 @@ public class LeaderCommand extends AbstractShellCommand { | ... | @@ -33,29 +37,33 @@ 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 = |
44 | + (e1, e2) -> { | ||
45 | + if (e1.leader() == null && e2.leader() == null) { | ||
46 | + return 0; | ||
47 | + } | ||
48 | + if (e1.leader() == null) { | ||
49 | + return 1; | ||
50 | + } | ||
51 | + if (e2.leader() == null) { | ||
52 | + return -1; | ||
53 | + } | ||
54 | + return e1.leader().toString().compareTo(e2.leader().toString()); | ||
55 | + }; | ||
56 | + | ||
57 | + /** | ||
58 | + * Displays text representing the leaders. | ||
59 | + * | ||
60 | + * @param leaderBoard map of leaders | ||
61 | + */ | ||
62 | + private void displayLeaders(Map<String, Leadership> leaderBoard) { | ||
40 | print("--------------------------------------------------------------"); | 63 | print("--------------------------------------------------------------"); |
41 | print(FMT, "Topic", "Leader", "Epoch", "Elected"); | 64 | print(FMT, "Topic", "Leader", "Epoch", "Elected"); |
42 | print("--------------------------------------------------------------"); | 65 | print("--------------------------------------------------------------"); |
43 | 66 | ||
44 | - | ||
45 | - Comparator<Leadership> leadershipComparator = | ||
46 | - (e1, e2) -> { | ||
47 | - if (e1.leader() == null && e2.leader() == null) { | ||
48 | - return 0; | ||
49 | - } | ||
50 | - if (e1.leader() == null) { | ||
51 | - return 1; | ||
52 | - } | ||
53 | - if (e2.leader() == null) { | ||
54 | - return -1; | ||
55 | - } | ||
56 | - return e1.leader().toString().compareTo(e2.leader().toString()); | ||
57 | - }; | ||
58 | - | ||
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 | } |
69 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + } | ||
112 | +} | ... | ... |
-
Please register or login to post a comment