blob: 67776259bd0e1c16258f137636d4e2282adf51f5 [file] [log] [blame]
tom613d8142014-09-11 15:09:37 -07001package org.onlab.onos.cli.net;
2
tomc65fa112014-10-16 07:48:47 -07003import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
tom613d8142014-09-11 15:09:37 -07006import org.apache.karaf.shell.commands.Argument;
7import org.apache.karaf.shell.commands.Command;
tom9eb57fb2014-09-11 19:42:38 -07008import org.onlab.onos.net.Link;
tom613d8142014-09-11 15:09:37 -07009import org.onlab.onos.net.Path;
10
11import java.util.Set;
12
tom9eb57fb2014-09-11 19:42:38 -070013import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString;
tom613d8142014-09-11 15:09:37 -070014import static org.onlab.onos.net.DeviceId.deviceId;
15
16/**
17 * Lists all shortest-paths paths between the specified source and
18 * destination devices.
19 */
20@Command(scope = "onos", name = "paths",
21 description = "Lists all shortest-paths paths between the specified source and destination devices")
22public class PathListCommand extends TopologyCommand {
23
tom9eb57fb2014-09-11 19:42:38 -070024 private static final String SEP = "==>";
tom613d8142014-09-11 15:09:37 -070025
26 @Argument(index = 0, name = "src", description = "Source device ID",
27 required = true, multiValued = false)
28 String src = null;
29
tom9eb57fb2014-09-11 19:42:38 -070030 @Argument(index = 1, name = "dst", description = "Destination device ID",
tom613d8142014-09-11 15:09:37 -070031 required = true, multiValued = false)
32 String dst = null;
33
34 @Override
tom0872a172014-09-23 11:24:26 -070035 protected void execute() {
tom613d8142014-09-11 15:09:37 -070036 init();
37 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
tomc65fa112014-10-16 07:48:47 -070038 if (outputJson()) {
39 print("%s", json(paths));
40 } else {
41 for (Path path : paths) {
42 print(pathString(path));
43 }
tom613d8142014-09-11 15:09:37 -070044 }
tom613d8142014-09-11 15:09:37 -070045 }
46
tom9eb57fb2014-09-11 19:42:38 -070047 /**
tomc65fa112014-10-16 07:48:47 -070048 * 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 }
63
64 /**
tom9eb57fb2014-09-11 19:42:38 -070065 * Produces a formatted string representing the specified path.
66 *
67 * @param path network path
68 * @return formatted path string
69 */
70 protected String pathString(Path path) {
71 StringBuilder sb = new StringBuilder();
72 for (Link link : path.links()) {
73 sb.append(compactLinkString(link)).append(SEP);
74 }
75 sb.delete(sb.lastIndexOf(SEP), sb.length());
76 sb.append("; cost=").append(path.cost());
77 return sb.toString();
tom613d8142014-09-11 15:09:37 -070078 }
79
80}