Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.Link; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.Path; |
| 25 | |
| 26 | import java.util.Set; |
| 27 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 28 | import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 29 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 30 | |
| 31 | /** |
| 32 | * Lists all shortest-paths paths between the specified source and |
| 33 | * destination devices. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "paths", |
| 36 | description = "Lists all shortest-paths paths between the specified source and destination devices") |
| 37 | public class PathListCommand extends TopologyCommand { |
| 38 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 39 | private static final String SEP = "==>"; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 40 | |
| 41 | @Argument(index = 0, name = "src", description = "Source device ID", |
| 42 | required = true, multiValued = false) |
| 43 | String src = null; |
| 44 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 45 | @Argument(index = 1, name = "dst", description = "Destination device ID", |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 46 | required = true, multiValued = false) |
| 47 | String dst = null; |
| 48 | |
| 49 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 50 | protected void execute() { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 51 | init(); |
| 52 | Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 53 | if (outputJson()) { |
| 54 | print("%s", json(paths)); |
| 55 | } else { |
| 56 | for (Path path : paths) { |
| 57 | print(pathString(path)); |
| 58 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 59 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 60 | } |
| 61 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 62 | /** |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 63 | * Produces a JSON array containing the specified paths. |
| 64 | * |
| 65 | * @param paths collection of paths |
| 66 | * @return JSON array |
| 67 | */ |
| 68 | public static JsonNode json(Iterable<Path> paths) { |
| 69 | ObjectMapper mapper = new ObjectMapper(); |
| 70 | ArrayNode result = mapper.createArrayNode(); |
| 71 | for (Path path : paths) { |
| 72 | result.add(LinksListCommand.json(mapper, path) |
| 73 | .put("cost", path.cost()) |
| 74 | .set("links", LinksListCommand.json(path.links()))); |
| 75 | } |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /** |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 80 | * Produces a formatted string representing the specified path. |
| 81 | * |
| 82 | * @param path network path |
| 83 | * @return formatted path string |
| 84 | */ |
| 85 | protected String pathString(Path path) { |
| 86 | StringBuilder sb = new StringBuilder(); |
| 87 | for (Link link : path.links()) { |
| 88 | sb.append(compactLinkString(link)).append(SEP); |
| 89 | } |
| 90 | sb.delete(sb.lastIndexOf(SEP), sb.length()); |
| 91 | sb.append("; cost=").append(path.cost()); |
| 92 | return sb.toString(); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } |