blob: f5226b1d550076464ba797febc4991d700d65bc0 [file] [log] [blame]
tom6d2a43e2014-09-08 01:50:20 -07001package org.onlab.onos.cli.net;
2
3import org.apache.karaf.shell.commands.Argument;
4import org.apache.karaf.shell.commands.Command;
5import org.onlab.onos.cli.AbstractShellCommand;
6import org.onlab.onos.net.Link;
7import org.onlab.onos.net.link.LinkService;
8
9import static org.onlab.onos.net.DeviceId.deviceId;
10
11/**
12 * Lists all infrastructure links.
13 */
14@Command(scope = "onos", name = "links",
15 description = "Lists all infrastructure links")
16public class LinksListCommand extends AbstractShellCommand {
17
18 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s";
tom9eb57fb2014-09-11 19:42:38 -070019 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070020
tomc290a122014-09-08 14:27:13 -070021 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070022 required = false, multiValued = false)
tomc290a122014-09-08 14:27:13 -070023 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070024
tom6d2a43e2014-09-08 01:50:20 -070025 @Override
tom0872a172014-09-23 11:24:26 -070026 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070027 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070028 Iterable<Link> links = uri != null ?
29 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom6d2a43e2014-09-08 01:50:20 -070030 for (Link link : links) {
tom13cb4852014-09-11 12:44:17 -070031 print(linkString(link));
tom6d2a43e2014-09-08 01:50:20 -070032 }
tom6d2a43e2014-09-08 01:50:20 -070033 }
tom13cb4852014-09-11 12:44:17 -070034
35 /**
tom613d8142014-09-11 15:09:37 -070036 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -070037 *
38 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -070039 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -070040 */
41 public static String linkString(Link link) {
42 return String.format(FMT, link.src().deviceId(), link.src().port(),
43 link.dst().deviceId(), link.dst().port(), link.type());
tom13cb4852014-09-11 12:44:17 -070044 }
tom9eb57fb2014-09-11 19:42:38 -070045
46 /**
47 * Returns a compact string representing the given link.
48 *
49 * @param link infrastructure link
50 * @return formatted link string
51 */
52 public static String compactLinkString(Link link) {
53 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
54 link.dst().deviceId(), link.dst().port());
55 }
56
tom6d2a43e2014-09-08 01:50:20 -070057}