Add JSON to CLI commands

- Drivers
- Groups

Change-Id: Ib47dc75d9db839329e6cf8fc4439150848f604f5
diff --git a/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java
index 70fa2a7..afbcab8 100644
--- a/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java
@@ -15,10 +15,9 @@
  */
 package org.onosproject.cli.net;
 
-import static com.google.common.collect.Lists.newArrayList;
-
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
@@ -34,6 +33,11 @@
 import org.onosproject.net.group.GroupBucket;
 import org.onosproject.net.group.GroupService;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+
+import static com.google.common.collect.Lists.newArrayList;
+
 /**
  * Lists all groups in the system.
  */
@@ -54,6 +58,16 @@
             required = false, multiValued = false)
     String state;
 
+    private JsonNode json(Map<Device, List<Group>> sortedGroups) {
+        ArrayNode result = mapper().createArrayNode();
+
+        sortedGroups.forEach((device, groups) ->
+                groups.forEach(group ->
+                        result.add(jsonForEntity(group, Group.class))));
+
+        return result;
+    }
+
     @Override
     protected void execute() {
         DeviceService deviceService = get(DeviceService.class);
@@ -61,7 +75,11 @@
         SortedMap<Device, List<Group>> sortedGroups =
                 getSortedGroups(deviceService, groupService);
 
-        sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
+        if (outputJson()) {
+            print("%s", json(sortedGroups));
+        } else {
+            sortedGroups.forEach((device, groups) -> printGroups(device.id(), groups));
+        }
     }
 
     /**