ONOS-1219 - Add JSON output to the leaders command

Change-Id: I180b39c16dcbc9d71f857507c1f04ed01e11a240
diff --git a/cli/src/main/java/org/onosproject/cli/net/LeaderCommand.java b/cli/src/main/java/org/onosproject/cli/net/LeaderCommand.java
index c4605fb..a60da76 100644
--- a/cli/src/main/java/org/onosproject/cli/net/LeaderCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/LeaderCommand.java
@@ -24,6 +24,10 @@
 import org.onosproject.cluster.Leadership;
 import org.onosproject.cluster.LeadershipService;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+
 /**
  * Prints the leader for every topic.
  */
@@ -33,29 +37,33 @@
 
     private static final String FMT = "%-20s | %-15s | %-6s | %-10s |";
 
-    @Override
-    protected void execute() {
-        LeadershipService leaderService = get(LeadershipService.class);
-        Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard();
+    /**
+     * Compares leaders, sorting by toString() output.
+     */
+    private Comparator<Leadership> leadershipComparator =
+            (e1, e2) -> {
+                if (e1.leader() == null && e2.leader() == null) {
+                    return 0;
+                }
+                if (e1.leader() == null) {
+                    return 1;
+                }
+                if (e2.leader() == null) {
+                    return -1;
+                }
+                return e1.leader().toString().compareTo(e2.leader().toString());
+            };
+
+    /**
+     * Displays text representing the leaders.
+     *
+     * @param leaderBoard map of leaders
+     */
+    private void displayLeaders(Map<String, Leadership> leaderBoard) {
         print("--------------------------------------------------------------");
         print(FMT, "Topic", "Leader", "Epoch", "Elected");
         print("--------------------------------------------------------------");
 
-
-        Comparator<Leadership> leadershipComparator =
-                (e1, e2) -> {
-                    if (e1.leader() == null && e2.leader() == null) {
-                        return 0;
-                    }
-                    if (e1.leader() == null) {
-                        return 1;
-                    }
-                    if (e2.leader() == null) {
-                        return -1;
-                    }
-                    return e1.leader().toString().compareTo(e2.leader().toString());
-                };
-
         leaderBoard.values()
                 .stream()
                 .sorted(leadershipComparator)
@@ -66,4 +74,39 @@
                         Tools.timeAgo(l.electedTime())));
         print("--------------------------------------------------------------");
     }
-}
\ No newline at end of file
+
+    /**
+     * Returns JSON node representing the leaders.
+     *
+     * @param leaderBoard map of leaders
+     */
+    private JsonNode json(Map<String, Leadership> leaderBoard) {
+        ObjectMapper mapper = new ObjectMapper();
+        ArrayNode result = mapper.createArrayNode();
+        leaderBoard.values()
+                .stream()
+                .sorted(leadershipComparator)
+                .forEach(l ->
+                        result.add(
+                            mapper.createObjectNode()
+                                .put("topic", l.topic())
+                                .put("leader", l.leader().toString())
+                                .put("epoch", l.epoch())
+                                .put("electedTime", Tools.timeAgo(l.electedTime()))));
+
+        return result;
+    }
+
+
+    @Override
+    protected void execute() {
+        LeadershipService leaderService = get(LeadershipService.class);
+        Map<String, Leadership> leaderBoard = leaderService.getLeaderBoard();
+
+        if (outputJson()) {
+            print("%s", json(leaderBoard));
+        } else {
+            displayLeaders(leaderBoard);
+        }
+    }
+}