Support to print out k8s pod, service and endpoints in JSON format

Change-Id: I7f66fae528165e0570e416f6bdb75715cf0d38ab
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java
index ade0b01..1d6cf3d 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/cli/K8sServiceListCommand.java
@@ -15,15 +15,22 @@
  */
 package org.onosproject.k8snetworking.cli;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.Lists;
+import io.fabric8.kubernetes.client.utils.Serialization;
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.k8snetworking.api.K8sServiceService;
 
+import java.io.IOException;
 import java.util.Comparator;
 import java.util.List;
 
+import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.prettyJson;
+
 /**
  * Lists kubernetes services.
  */
@@ -42,20 +49,40 @@
                 Lists.newArrayList(service.services());
         services.sort(Comparator.comparing(s -> s.getMetadata().getName()));
 
-        print(FORMAT, "Name", "Cluster IP", "Ports");
+        if (outputJson()) {
+            print("%s", json(services));
+        } else {
+            print(FORMAT, "Name", "Cluster IP", "Ports");
 
-        for (io.fabric8.kubernetes.api.model.Service svc : services) {
+            for (io.fabric8.kubernetes.api.model.Service svc : services) {
 
-            List<String> portWithProtocol = Lists.newArrayList();
+                List<String> portWithProtocol = Lists.newArrayList();
 
-            svc.getSpec().getPorts().forEach(p ->
-                    portWithProtocol.add(p.getPort() +
-                    PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
+                svc.getSpec().getPorts().forEach(p ->
+                        portWithProtocol.add(p.getPort() +
+                                PORT_PROTOCOL_SEPARATOR + p.getProtocol()));
 
-            print(FORMAT,
-                    svc.getMetadata().getName(),
-                    svc.getSpec().getClusterIP(),
-                    portWithProtocol.isEmpty() ? "" : portWithProtocol);
+                print(FORMAT,
+                        svc.getMetadata().getName(),
+                        svc.getSpec().getClusterIP(),
+                        portWithProtocol.isEmpty() ? "" : portWithProtocol);
+            }
+        }
+    }
+
+    private String json(List<io.fabric8.kubernetes.api.model.Service> services) {
+        ObjectMapper mapper = new ObjectMapper();
+        ArrayNode result = mapper.createArrayNode();
+        try {
+            for (io.fabric8.kubernetes.api.model.Service service : services) {
+                ObjectNode json = (ObjectNode) new ObjectMapper()
+                        .readTree(Serialization.asJson(service));
+                result.add(json);
+            }
+            return prettyJson(mapper, result.toString());
+        } catch (IOException e) {
+            log.warn("Failed to parse service's JSON string.");
+            return "";
         }
     }
 }