blob: 75ab02ce87e98e3bbf80aabb2d0d8fcc7c890e2d [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
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 */
tom613d8142014-09-11 15:09:37 -070019package org.onlab.onos.cli.net;
20
tomc65fa112014-10-16 07:48:47 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
tom613d8142014-09-11 15:09:37 -070024import org.apache.karaf.shell.commands.Argument;
25import org.apache.karaf.shell.commands.Command;
tom9eb57fb2014-09-11 19:42:38 -070026import org.onlab.onos.net.Link;
tom613d8142014-09-11 15:09:37 -070027import org.onlab.onos.net.Path;
28
29import java.util.Set;
30
tom9eb57fb2014-09-11 19:42:38 -070031import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString;
tom613d8142014-09-11 15:09:37 -070032import 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")
40public class PathListCommand extends TopologyCommand {
41
tom9eb57fb2014-09-11 19:42:38 -070042 private static final String SEP = "==>";
tom613d8142014-09-11 15:09:37 -070043
44 @Argument(index = 0, name = "src", description = "Source device ID",
45 required = true, multiValued = false)
46 String src = null;
47
tom9eb57fb2014-09-11 19:42:38 -070048 @Argument(index = 1, name = "dst", description = "Destination device ID",
tom613d8142014-09-11 15:09:37 -070049 required = true, multiValued = false)
50 String dst = null;
51
52 @Override
tom0872a172014-09-23 11:24:26 -070053 protected void execute() {
tom613d8142014-09-11 15:09:37 -070054 init();
55 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
tomc65fa112014-10-16 07:48:47 -070056 if (outputJson()) {
57 print("%s", json(paths));
58 } else {
59 for (Path path : paths) {
60 print(pathString(path));
61 }
tom613d8142014-09-11 15:09:37 -070062 }
tom613d8142014-09-11 15:09:37 -070063 }
64
tom9eb57fb2014-09-11 19:42:38 -070065 /**
tomc65fa112014-10-16 07:48:47 -070066 * 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 /**
tom9eb57fb2014-09-11 19:42:38 -070083 * 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();
tom613d8142014-09-11 15:09:37 -070096 }
97
98}