Enahnced path list command to show JSON.

This commit is contained in:
tom 2014-10-16 07:48:47 -07:00
parent 8350ba5108
commit c65fa11ca2
2 changed files with 27 additions and 3 deletions

View File

@ -66,7 +66,7 @@ public class LinksListCommand extends AbstractShellCommand {
public static ObjectNode json(ObjectMapper mapper, Link link) { public static ObjectNode json(ObjectMapper mapper, Link link) {
ObjectNode result = mapper.createObjectNode(); ObjectNode result = mapper.createObjectNode();
result.set("src", json(mapper, link.src())); result.set("src", json(mapper, link.src()));
result.set("dst", json(mapper, link.src())); result.set("dst", json(mapper, link.dst()));
return result; return result;
} }

View File

@ -1,5 +1,8 @@
package org.onlab.onos.cli.net; 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 org.apache.karaf.shell.commands.Argument; import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command; import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.net.Link; import org.onlab.onos.net.Link;
@ -32,10 +35,31 @@ public class PathListCommand extends TopologyCommand {
protected void execute() { protected void execute() {
init(); init();
Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
if (outputJson()) {
print("%s", json(paths));
} else {
for (Path path : paths) { for (Path path : paths) {
print(pathString(path)); print(pathString(path));
} }
} }
}
/**
* Produces a JSON array containing the specified paths.
*
* @param paths collection of paths
* @return JSON array
*/
public static JsonNode json(Iterable<Path> paths) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (Path path : paths) {
result.add(LinksListCommand.json(mapper, path)
.put("cost", path.cost())
.set("links", LinksListCommand.json(path.links())));
}
return result;
}
/** /**
* Produces a formatted string representing the specified path. * Produces a formatted string representing the specified path.