Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 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; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.Path; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Set; |
| 28 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import static org.onosproject.cli.net.LinksListCommand.compactLinkString; |
| 30 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Lists all shortest-paths paths between the specified source and |
| 34 | * destination devices. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "paths", |
| 37 | description = "Lists all shortest-paths paths between the specified source and destination devices") |
| 38 | public class PathListCommand extends TopologyCommand { |
| 39 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 40 | private static final String SEP = "==>"; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 41 | |
| 42 | @Argument(index = 0, name = "src", description = "Source device ID", |
| 43 | required = true, multiValued = false) |
| 44 | String src = null; |
| 45 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 46 | @Argument(index = 1, name = "dst", description = "Destination device ID", |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 47 | required = true, multiValued = false) |
| 48 | String dst = null; |
| 49 | |
| 50 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 51 | protected void execute() { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 52 | init(); |
Ayaka Koshibe | e018a92 | 2016-01-19 15:53:37 -0800 | [diff] [blame] | 53 | if (src.split("/").length != 1 || dst.split("/").length != 1) { |
| 54 | print("Expected device IDs as arguments"); |
| 55 | return; |
| 56 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 57 | Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 58 | if (outputJson()) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 59 | print("%s", json(this, paths)); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 60 | } else { |
| 61 | for (Path path : paths) { |
| 62 | print(pathString(path)); |
| 63 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 64 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 67 | /** |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 68 | * Produces a JSON array containing the specified paths. |
| 69 | * |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 70 | * @param context context to use for looking up codecs |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 71 | * @param paths collection of paths |
| 72 | * @return JSON array |
| 73 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 74 | public static JsonNode json(AbstractShellCommand context, Iterable<Path> paths) { |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 75 | ObjectMapper mapper = new ObjectMapper(); |
| 76 | ArrayNode result = mapper.createArrayNode(); |
| 77 | for (Path path : paths) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 78 | result.add(LinksListCommand.json(context, path) |
| 79 | .put("cost", path.cost()) |
| 80 | .set("links", LinksListCommand.json(context, path.links()))); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | /** |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 86 | * Produces a formatted string representing the specified path. |
| 87 | * |
| 88 | * @param path network path |
| 89 | * @return formatted path string |
| 90 | */ |
| 91 | protected String pathString(Path path) { |
| 92 | StringBuilder sb = new StringBuilder(); |
| 93 | for (Link link : path.links()) { |
| 94 | sb.append(compactLinkString(link)).append(SEP); |
| 95 | } |
| 96 | sb.delete(sb.lastIndexOf(SEP), sb.length()); |
| 97 | sb.append("; cost=").append(path.cost()); |
| 98 | return sb.toString(); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } |