Adding JSON format to the CLI. Intents and flows still need to be done.
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/LinksListCommand.java b/cli/src/main/java/org/onlab/onos/cli/net/LinksListCommand.java
index f5226b1..59d2be7 100644
--- a/cli/src/main/java/org/onlab/onos/cli/net/LinksListCommand.java
+++ b/cli/src/main/java/org/onlab/onos/cli/net/LinksListCommand.java
@@ -1,8 +1,13 @@
package org.onlab.onos.cli.net;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.link.LinkService;
@@ -27,12 +32,58 @@
LinkService service = get(LinkService.class);
Iterable<Link> links = uri != null ?
service.getDeviceLinks(deviceId(uri)) : service.getLinks();
- for (Link link : links) {
- print(linkString(link));
+ if (outputJson()) {
+ print("%s", json(links));
+ } else {
+ for (Link link : links) {
+ print(linkString(link));
+ }
}
}
/**
+ * Produces a JSON array containing the specified links.
+ *
+ * @param links collection of links
+ * @return JSON array
+ */
+ public static JsonNode json(Iterable<Link> links) {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode result = mapper.createArrayNode();
+ for (Link link : links) {
+ result.add(json(mapper, link));
+ }
+ return result;
+ }
+
+ /**
+ * Produces a JSON object for the specified link.
+ *
+ * @param mapper object mapper
+ * @param link link to encode
+ * @return JSON object
+ */
+ public static ObjectNode json(ObjectMapper mapper, Link link) {
+ ObjectNode result = mapper.createObjectNode();
+ result.set("src", json(mapper, link.src()));
+ result.set("dst", json(mapper, link.src()));
+ return result;
+ }
+
+ /**
+ * Produces a JSON object for the specified connect point.
+ *
+ * @param mapper object mapper
+ * @param connectPoint connection point to encode
+ * @return JSON object
+ */
+ public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) {
+ return mapper.createObjectNode()
+ .put("device", connectPoint.deviceId().toString())
+ .put("port", connectPoint.port().toString());
+ }
+
+ /**
* Returns a formatted string representing the given link.
*
* @param link infrastructure link