blob: 57167ead73947d35c2a94ec4655326ebfb3ed4d4 [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 */
tom613d8142014-09-11 15:09:37 -070016package org.onlab.onos.cli.net;
17
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;
tom9eb57fb2014-09-11 19:42:38 -070023import org.onlab.onos.net.Link;
tom613d8142014-09-11 15:09:37 -070024import org.onlab.onos.net.Path;
25
26import java.util.Set;
27
tom9eb57fb2014-09-11 19:42:38 -070028import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString;
tom613d8142014-09-11 15:09:37 -070029import static org.onlab.onos.net.DeviceId.deviceId;
30
31/**
32 * Lists all shortest-paths paths between the specified source and
33 * destination devices.
34 */
35@Command(scope = "onos", name = "paths",
36 description = "Lists all shortest-paths paths between the specified source and destination devices")
37public class PathListCommand extends TopologyCommand {
38
tom9eb57fb2014-09-11 19:42:38 -070039 private static final String SEP = "==>";
tom613d8142014-09-11 15:09:37 -070040
41 @Argument(index = 0, name = "src", description = "Source device ID",
42 required = true, multiValued = false)
43 String src = null;
44
tom9eb57fb2014-09-11 19:42:38 -070045 @Argument(index = 1, name = "dst", description = "Destination device ID",
tom613d8142014-09-11 15:09:37 -070046 required = true, multiValued = false)
47 String dst = null;
48
49 @Override
tom0872a172014-09-23 11:24:26 -070050 protected void execute() {
tom613d8142014-09-11 15:09:37 -070051 init();
52 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
tomc65fa112014-10-16 07:48:47 -070053 if (outputJson()) {
54 print("%s", json(paths));
55 } else {
56 for (Path path : paths) {
57 print(pathString(path));
58 }
tom613d8142014-09-11 15:09:37 -070059 }
tom613d8142014-09-11 15:09:37 -070060 }
61
tom9eb57fb2014-09-11 19:42:38 -070062 /**
tomc65fa112014-10-16 07:48:47 -070063 * Produces a JSON array containing the specified paths.
64 *
65 * @param paths collection of paths
66 * @return JSON array
67 */
68 public static JsonNode json(Iterable<Path> paths) {
69 ObjectMapper mapper = new ObjectMapper();
70 ArrayNode result = mapper.createArrayNode();
71 for (Path path : paths) {
72 result.add(LinksListCommand.json(mapper, path)
73 .put("cost", path.cost())
74 .set("links", LinksListCommand.json(path.links())));
75 }
76 return result;
77 }
78
79 /**
tom9eb57fb2014-09-11 19:42:38 -070080 * Produces a formatted string representing the specified path.
81 *
82 * @param path network path
83 * @return formatted path string
84 */
85 protected String pathString(Path path) {
86 StringBuilder sb = new StringBuilder();
87 for (Link link : path.links()) {
88 sb.append(compactLinkString(link)).append(SEP);
89 }
90 sb.delete(sb.lastIndexOf(SEP), sb.length());
91 sb.append("; cost=").append(path.cost());
92 return sb.toString();
tom613d8142014-09-11 15:09:37 -070093 }
94
95}