blob: 8bb808aeab3a772b4cbf0e00f58e9a08b7e7281d [file] [log] [blame]
tom613d8142014-09-11 15:09:37 -07001package org.onlab.onos.cli.net;
2
3import org.apache.karaf.shell.commands.Argument;
4import org.apache.karaf.shell.commands.Command;
tom9eb57fb2014-09-11 19:42:38 -07005import org.onlab.onos.net.Link;
tom613d8142014-09-11 15:09:37 -07006import org.onlab.onos.net.Path;
7
8import java.util.Set;
9
tom9eb57fb2014-09-11 19:42:38 -070010import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString;
tom613d8142014-09-11 15:09:37 -070011import static org.onlab.onos.net.DeviceId.deviceId;
12
13/**
14 * Lists all shortest-paths paths between the specified source and
15 * destination devices.
16 */
17@Command(scope = "onos", name = "paths",
18 description = "Lists all shortest-paths paths between the specified source and destination devices")
19public class PathListCommand extends TopologyCommand {
20
tom9eb57fb2014-09-11 19:42:38 -070021 private static final String SEP = "==>";
tom613d8142014-09-11 15:09:37 -070022
23 @Argument(index = 0, name = "src", description = "Source device ID",
24 required = true, multiValued = false)
25 String src = null;
26
tom9eb57fb2014-09-11 19:42:38 -070027 @Argument(index = 1, name = "dst", description = "Destination device ID",
tom613d8142014-09-11 15:09:37 -070028 required = true, multiValued = false)
29 String dst = null;
30
31 @Override
tom0872a172014-09-23 11:24:26 -070032 protected void execute() {
tom613d8142014-09-11 15:09:37 -070033 init();
34 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
35 for (Path path : paths) {
36 print(pathString(path));
37 }
tom613d8142014-09-11 15:09:37 -070038 }
39
tom9eb57fb2014-09-11 19:42:38 -070040 /**
41 * Produces a formatted string representing the specified path.
42 *
43 * @param path network path
44 * @return formatted path string
45 */
46 protected String pathString(Path path) {
47 StringBuilder sb = new StringBuilder();
48 for (Link link : path.links()) {
49 sb.append(compactLinkString(link)).append(SEP);
50 }
51 sb.delete(sb.lastIndexOf(SEP), sb.length());
52 sb.append("; cost=").append(path.cost());
53 return sb.toString();
tom613d8142014-09-11 15:09:37 -070054 }
55
56}