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