tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
| 3 | import org.apache.karaf.shell.commands.Argument; |
| 4 | import org.apache.karaf.shell.commands.Command; |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 5 | import org.onlab.onos.net.Link; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.Path; |
| 7 | |
| 8 | import java.util.Set; |
| 9 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 10 | import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 11 | import 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") |
| 19 | public class PathListCommand extends TopologyCommand { |
| 20 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 21 | private static final String SEP = "==>"; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 22 | |
| 23 | @Argument(index = 0, name = "src", description = "Source device ID", |
| 24 | required = true, multiValued = false) |
| 25 | String src = null; |
| 26 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 27 | @Argument(index = 1, name = "dst", description = "Destination device ID", |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 28 | required = true, multiValued = false) |
| 29 | String dst = null; |
| 30 | |
| 31 | @Override |
| 32 | protected Object doExecute() throws Exception { |
| 33 | init(); |
| 34 | Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); |
| 35 | for (Path path : paths) { |
| 36 | print(pathString(path)); |
| 37 | } |
| 38 | return null; |
| 39 | } |
| 40 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Produces a formatted string representing the specified path. |
| 43 | * |
| 44 | * @param path network path |
| 45 | * @return formatted path string |
| 46 | */ |
| 47 | protected String pathString(Path path) { |
| 48 | StringBuilder sb = new StringBuilder(); |
| 49 | for (Link link : path.links()) { |
| 50 | sb.append(compactLinkString(link)).append(SEP); |
| 51 | } |
| 52 | sb.delete(sb.lastIndexOf(SEP), sb.length()); |
| 53 | sb.append("; cost=").append(path.cost()); |
| 54 | return sb.toString(); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } |