tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 6 | import org.apache.karaf.shell.commands.Argument; |
| 7 | import org.apache.karaf.shell.commands.Command; |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.Link; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.Path; |
| 10 | |
| 11 | import java.util.Set; |
| 12 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 13 | import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 14 | import 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") |
| 22 | public class PathListCommand extends TopologyCommand { |
| 23 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 24 | private static final String SEP = "==>"; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 25 | |
| 26 | @Argument(index = 0, name = "src", description = "Source device ID", |
| 27 | required = true, multiValued = false) |
| 28 | String src = null; |
| 29 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 30 | @Argument(index = 1, name = "dst", description = "Destination device ID", |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 31 | required = true, multiValued = false) |
| 32 | String dst = null; |
| 33 | |
| 34 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 35 | protected void execute() { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 36 | init(); |
| 37 | Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 38 | if (outputJson()) { |
| 39 | print("%s", json(paths)); |
| 40 | } else { |
| 41 | for (Path path : paths) { |
| 42 | print(pathString(path)); |
| 43 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 44 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 45 | } |
| 46 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 47 | /** |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 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 | } |
| 63 | |
| 64 | /** |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 65 | * 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(); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } |