tom

Enahnced path list command to show JSON.

...@@ -66,7 +66,7 @@ public class LinksListCommand extends AbstractShellCommand { ...@@ -66,7 +66,7 @@ public class LinksListCommand extends AbstractShellCommand {
66 public static ObjectNode json(ObjectMapper mapper, Link link) { 66 public static ObjectNode json(ObjectMapper mapper, Link link) {
67 ObjectNode result = mapper.createObjectNode(); 67 ObjectNode result = mapper.createObjectNode();
68 result.set("src", json(mapper, link.src())); 68 result.set("src", json(mapper, link.src()));
69 - result.set("dst", json(mapper, link.src())); 69 + result.set("dst", json(mapper, link.dst()));
70 return result; 70 return result;
71 } 71 }
72 72
......
1 package org.onlab.onos.cli.net; 1 package org.onlab.onos.cli.net;
2 2
3 +import com.fasterxml.jackson.databind.JsonNode;
4 +import com.fasterxml.jackson.databind.ObjectMapper;
5 +import com.fasterxml.jackson.databind.node.ArrayNode;
3 import org.apache.karaf.shell.commands.Argument; 6 import org.apache.karaf.shell.commands.Argument;
4 import org.apache.karaf.shell.commands.Command; 7 import org.apache.karaf.shell.commands.Command;
5 import org.onlab.onos.net.Link; 8 import org.onlab.onos.net.Link;
...@@ -32,10 +35,31 @@ public class PathListCommand extends TopologyCommand { ...@@ -32,10 +35,31 @@ public class PathListCommand extends TopologyCommand {
32 protected void execute() { 35 protected void execute() {
33 init(); 36 init();
34 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); 37 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
38 + if (outputJson()) {
39 + print("%s", json(paths));
40 + } else {
35 for (Path path : paths) { 41 for (Path path : paths) {
36 print(pathString(path)); 42 print(pathString(path));
37 } 43 }
38 } 44 }
45 + }
46 +
47 + /**
48 + * Produces a JSON array containing the specified paths.
49 + *
50 + * @param paths collection of paths
51 + * @return JSON array
52 + */
53 + public static JsonNode json(Iterable<Path> paths) {
54 + ObjectMapper mapper = new ObjectMapper();
55 + ArrayNode result = mapper.createArrayNode();
56 + for (Path path : paths) {
57 + result.add(LinksListCommand.json(mapper, path)
58 + .put("cost", path.cost())
59 + .set("links", LinksListCommand.json(path.links())));
60 + }
61 + return result;
62 + }
39 63
40 /** 64 /**
41 * Produces a formatted string representing the specified path. 65 * Produces a formatted string representing the specified path.
......