blob: 141ae26673bf9ef6e33e9e27e7f697eb99db6cf6 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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();
53 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
tomc65fa112014-10-16 07:48:47 -070054 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070055 print("%s", json(this, paths));
tomc65fa112014-10-16 07:48:47 -070056 } else {
57 for (Path path : paths) {
58 print(pathString(path));
59 }
tom613d8142014-09-11 15:09:37 -070060 }
tom613d8142014-09-11 15:09:37 -070061 }
62
tom9eb57fb2014-09-11 19:42:38 -070063 /**
tomc65fa112014-10-16 07:48:47 -070064 * Produces a JSON array containing the specified paths.
65 *
Ray Milkey3078fc02015-05-06 16:14:14 -070066 * @param context context to use for looking up codecs
tomc65fa112014-10-16 07:48:47 -070067 * @param paths collection of paths
68 * @return JSON array
69 */
Ray Milkey3078fc02015-05-06 16:14:14 -070070 public static JsonNode json(AbstractShellCommand context, Iterable<Path> paths) {
tomc65fa112014-10-16 07:48:47 -070071 ObjectMapper mapper = new ObjectMapper();
72 ArrayNode result = mapper.createArrayNode();
73 for (Path path : paths) {
Ray Milkey3078fc02015-05-06 16:14:14 -070074 result.add(LinksListCommand.json(context, path)
75 .put("cost", path.cost())
76 .set("links", LinksListCommand.json(context, path.links())));
tomc65fa112014-10-16 07:48:47 -070077 }
78 return result;
79 }
80
81 /**
tom9eb57fb2014-09-11 19:42:38 -070082 * Produces a formatted string representing the specified path.
83 *
84 * @param path network path
85 * @return formatted path string
86 */
87 protected String pathString(Path path) {
88 StringBuilder sb = new StringBuilder();
89 for (Link link : path.links()) {
90 sb.append(compactLinkString(link)).append(SEP);
91 }
92 sb.delete(sb.lastIndexOf(SEP), sb.length());
93 sb.append("; cost=").append(path.cost());
94 return sb.toString();
tom613d8142014-09-11 15:09:37 -070095 }
96
97}