blob: 0ee921458e855dce9056b19f1efd0d9db7f15483 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080023import org.apache.karaf.shell.commands.Option;
Ray Milkey3078fc02015-05-06 16:14:14 -070024import org.onosproject.cli.AbstractShellCommand;
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080025import org.onosproject.net.DeviceId;
26import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Link;
28import org.onosproject.net.Path;
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080029import org.onosproject.net.device.DeviceService;
tom613d8142014-09-11 15:09:37 -070030
31import java.util.Set;
32
Brian O'Connorabafb502014-12-02 22:26:20 -080033import static org.onosproject.cli.net.LinksListCommand.compactLinkString;
34import static org.onosproject.net.DeviceId.deviceId;
tom613d8142014-09-11 15:09:37 -070035
36/**
37 * Lists all shortest-paths paths between the specified source and
38 * destination devices.
39 */
40@Command(scope = "onos", name = "paths",
41 description = "Lists all shortest-paths paths between the specified source and destination devices")
42public class PathListCommand extends TopologyCommand {
43
tom9eb57fb2014-09-11 19:42:38 -070044 private static final String SEP = "==>";
tom613d8142014-09-11 15:09:37 -070045
46 @Argument(index = 0, name = "src", description = "Source device ID",
47 required = true, multiValued = false)
48 String src = null;
49
tom9eb57fb2014-09-11 19:42:38 -070050 @Argument(index = 1, name = "dst", description = "Destination device ID",
tom613d8142014-09-11 15:09:37 -070051 required = true, multiValued = false)
52 String dst = null;
53
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080054 @Option(name = "--disjoint", description = "Show disjoint Paths")
55 boolean disjoint = false;
56
tom613d8142014-09-11 15:09:37 -070057 @Override
tom0872a172014-09-23 11:24:26 -070058 protected void execute() {
tom613d8142014-09-11 15:09:37 -070059 init();
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080060 DeviceService deviceService = get(DeviceService.class);
61 DeviceId srcDid = deviceId(src);
62 if (deviceService.getDevice(srcDid) == null) {
63 print("Unknown device %s", src);
Ayaka Koshibee018a922016-01-19 15:53:37 -080064 return;
65 }
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080066 DeviceId dstDid = deviceId(dst);
67 if (deviceService.getDevice(dstDid) == null) {
68 print("Unknown device %s", dst);
69 return;
70 }
71 Set<? extends Path> paths;
72 if (disjoint) {
73 paths = service.getDisjointPaths(topology, srcDid, dstDid);
74 } else {
75 paths = service.getPaths(topology, srcDid, dstDid);
76 }
tomc65fa112014-10-16 07:48:47 -070077 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070078 print("%s", json(this, paths));
tomc65fa112014-10-16 07:48:47 -070079 } else {
80 for (Path path : paths) {
81 print(pathString(path));
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080082 if (path instanceof DisjointPath) {
83 // print backup right after primary
84 print(pathString(((DisjointPath) path).backup()));
85 }
tomc65fa112014-10-16 07:48:47 -070086 }
tom613d8142014-09-11 15:09:37 -070087 }
tom613d8142014-09-11 15:09:37 -070088 }
89
tom9eb57fb2014-09-11 19:42:38 -070090 /**
tomc65fa112014-10-16 07:48:47 -070091 * Produces a JSON array containing the specified paths.
92 *
Ray Milkey3078fc02015-05-06 16:14:14 -070093 * @param context context to use for looking up codecs
tomc65fa112014-10-16 07:48:47 -070094 * @param paths collection of paths
95 * @return JSON array
96 */
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -080097 public static JsonNode json(AbstractShellCommand context,
98 Iterable<? extends Path> paths) {
99 ObjectMapper mapper = context.mapper();
tomc65fa112014-10-16 07:48:47 -0700100 ArrayNode result = mapper.createArrayNode();
101 for (Path path : paths) {
Ray Milkey3078fc02015-05-06 16:14:14 -0700102 result.add(LinksListCommand.json(context, path)
103 .put("cost", path.cost())
104 .set("links", LinksListCommand.json(context, path.links())));
Yuta HIGUCHI33485fb2017-01-25 20:13:30 -0800105
106 if (path instanceof DisjointPath) {
107 // [ (primay), (backup), ...]
108 DisjointPath backup = (DisjointPath) path;
109 result.add(LinksListCommand.json(context, backup.backup())
110 .put("cost", backup.cost())
111 .set("links", LinksListCommand.json(context, backup.links())));
112 }
tomc65fa112014-10-16 07:48:47 -0700113 }
114 return result;
115 }
116
117 /**
tom9eb57fb2014-09-11 19:42:38 -0700118 * Produces a formatted string representing the specified path.
119 *
120 * @param path network path
121 * @return formatted path string
122 */
123 protected String pathString(Path path) {
124 StringBuilder sb = new StringBuilder();
125 for (Link link : path.links()) {
126 sb.append(compactLinkString(link)).append(SEP);
127 }
128 sb.delete(sb.lastIndexOf(SEP), sb.length());
129 sb.append("; cost=").append(path.cost());
130 return sb.toString();
tom613d8142014-09-11 15:09:37 -0700131 }
132
133}