Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.Path; |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceService; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.Set; |
| 34 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import static org.onosproject.cli.net.LinksListCommand.compactLinkString; |
| 36 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Lists all shortest-paths paths between the specified source and |
| 40 | * destination devices. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | @Service |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 43 | @Command(scope = "onos", name = "paths", |
| 44 | description = "Lists all shortest-paths paths between the specified source and destination devices") |
| 45 | public class PathListCommand extends TopologyCommand { |
| 46 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 47 | private static final String SEP = "==>"; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 48 | |
| 49 | @Argument(index = 0, name = "src", description = "Source device ID", |
| 50 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 51 | @Completion(DeviceIdCompleter.class) |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 52 | String src = null; |
| 53 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 54 | @Argument(index = 1, name = "dst", description = "Destination device ID", |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 55 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 56 | @Completion(DeviceIdCompleter.class) |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 57 | String dst = null; |
| 58 | |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 59 | @Option(name = "--disjoint", description = "Show disjoint Paths") |
| 60 | boolean disjoint = false; |
| 61 | |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 62 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | protected void doExecute() { |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 64 | init(); |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 65 | DeviceService deviceService = get(DeviceService.class); |
| 66 | DeviceId srcDid = deviceId(src); |
| 67 | if (deviceService.getDevice(srcDid) == null) { |
| 68 | print("Unknown device %s", src); |
Ayaka Koshibe | e018a92 | 2016-01-19 15:53:37 -0800 | [diff] [blame] | 69 | return; |
| 70 | } |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 71 | DeviceId dstDid = deviceId(dst); |
| 72 | if (deviceService.getDevice(dstDid) == null) { |
| 73 | print("Unknown device %s", dst); |
| 74 | return; |
| 75 | } |
| 76 | Set<? extends Path> paths; |
| 77 | if (disjoint) { |
| 78 | paths = service.getDisjointPaths(topology, srcDid, dstDid); |
| 79 | } else { |
| 80 | paths = service.getPaths(topology, srcDid, dstDid); |
| 81 | } |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 82 | if (outputJson()) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 83 | print("%s", json(this, paths)); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 84 | } else { |
| 85 | for (Path path : paths) { |
| 86 | print(pathString(path)); |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 87 | if (path instanceof DisjointPath) { |
| 88 | // print backup right after primary |
| 89 | print(pathString(((DisjointPath) path).backup())); |
| 90 | } |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 91 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 92 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 95 | /** |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 96 | * Produces a JSON array containing the specified paths. |
| 97 | * |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 98 | * @param context context to use for looking up codecs |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 99 | * @param paths collection of paths |
| 100 | * @return JSON array |
| 101 | */ |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 102 | public static JsonNode json(AbstractShellCommand context, |
| 103 | Iterable<? extends Path> paths) { |
| 104 | ObjectMapper mapper = context.mapper(); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 105 | ArrayNode result = mapper.createArrayNode(); |
| 106 | for (Path path : paths) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 107 | result.add(LinksListCommand.json(context, path) |
| 108 | .put("cost", path.cost()) |
| 109 | .set("links", LinksListCommand.json(context, path.links()))); |
Yuta HIGUCHI | 33485fb | 2017-01-25 20:13:30 -0800 | [diff] [blame] | 110 | |
| 111 | if (path instanceof DisjointPath) { |
| 112 | // [ (primay), (backup), ...] |
| 113 | DisjointPath backup = (DisjointPath) path; |
| 114 | result.add(LinksListCommand.json(context, backup.backup()) |
| 115 | .put("cost", backup.cost()) |
| 116 | .set("links", LinksListCommand.json(context, backup.links()))); |
| 117 | } |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | /** |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 123 | * Produces a formatted string representing the specified path. |
| 124 | * |
| 125 | * @param path network path |
| 126 | * @return formatted path string |
| 127 | */ |
| 128 | protected String pathString(Path path) { |
| 129 | StringBuilder sb = new StringBuilder(); |
| 130 | for (Link link : path.links()) { |
| 131 | sb.append(compactLinkString(link)).append(SEP); |
| 132 | } |
| 133 | sb.delete(sb.lastIndexOf(SEP), sb.length()); |
| 134 | sb.append("; cost=").append(path.cost()); |
| 135 | return sb.toString(); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } |