blob: c15251cc4719c946dc5657e71e172c58ea57c100 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom613d8142014-09-11 15:09:37 -070017
tomc65fa112014-10-16 07:48:47 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
tom613d8142014-09-11 15:09:37 -070021import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
Ray Milkey3078fc02015-05-06 16:14:14 -070023import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.Link;
25import org.onosproject.net.Path;
tom613d8142014-09-11 15:09:37 -070026
27import java.util.Set;
28
Brian O'Connorabafb502014-12-02 22:26:20 -080029import static org.onosproject.cli.net.LinksListCommand.compactLinkString;
30import static org.onosproject.net.DeviceId.deviceId;
tom613d8142014-09-11 15:09:37 -070031
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")
38public class PathListCommand extends TopologyCommand {
39
tom9eb57fb2014-09-11 19:42:38 -070040 private static final String SEP = "==>";
tom613d8142014-09-11 15:09:37 -070041
42 @Argument(index = 0, name = "src", description = "Source device ID",
43 required = true, multiValued = false)
44 String src = null;
45
tom9eb57fb2014-09-11 19:42:38 -070046 @Argument(index = 1, name = "dst", description = "Destination device ID",
tom613d8142014-09-11 15:09:37 -070047 required = true, multiValued = false)
48 String dst = null;
49
50 @Override
tom0872a172014-09-23 11:24:26 -070051 protected void execute() {
tom613d8142014-09-11 15:09:37 -070052 init();
Ayaka Koshibee018a922016-01-19 15:53:37 -080053 if (src.split("/").length != 1 || dst.split("/").length != 1) {
54 print("Expected device IDs as arguments");
55 return;
56 }
tom613d8142014-09-11 15:09:37 -070057 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
tomc65fa112014-10-16 07:48:47 -070058 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070059 print("%s", json(this, paths));
tomc65fa112014-10-16 07:48:47 -070060 } else {
61 for (Path path : paths) {
62 print(pathString(path));
63 }
tom613d8142014-09-11 15:09:37 -070064 }
tom613d8142014-09-11 15:09:37 -070065 }
66
tom9eb57fb2014-09-11 19:42:38 -070067 /**
tomc65fa112014-10-16 07:48:47 -070068 * Produces a JSON array containing the specified paths.
69 *
Ray Milkey3078fc02015-05-06 16:14:14 -070070 * @param context context to use for looking up codecs
tomc65fa112014-10-16 07:48:47 -070071 * @param paths collection of paths
72 * @return JSON array
73 */
Ray Milkey3078fc02015-05-06 16:14:14 -070074 public static JsonNode json(AbstractShellCommand context, Iterable<Path> paths) {
tomc65fa112014-10-16 07:48:47 -070075 ObjectMapper mapper = new ObjectMapper();
76 ArrayNode result = mapper.createArrayNode();
77 for (Path path : paths) {
Ray Milkey3078fc02015-05-06 16:14:14 -070078 result.add(LinksListCommand.json(context, path)
79 .put("cost", path.cost())
80 .set("links", LinksListCommand.json(context, path.links())));
tomc65fa112014-10-16 07:48:47 -070081 }
82 return result;
83 }
84
85 /**
tom9eb57fb2014-09-11 19:42:38 -070086 * 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();
tom613d8142014-09-11 15:09:37 -070099 }
100
101}