blob: 59d2be73a60eae3afa0c6fed8969aea423bd1b4d [file] [log] [blame]
tom6d2a43e2014-09-08 01:50:20 -07001package org.onlab.onos.cli.net;
2
tom32085cf2014-10-16 00:04:33 -07003import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
6import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -07007import org.apache.karaf.shell.commands.Argument;
8import org.apache.karaf.shell.commands.Command;
9import org.onlab.onos.cli.AbstractShellCommand;
tom32085cf2014-10-16 00:04:33 -070010import org.onlab.onos.net.ConnectPoint;
tom6d2a43e2014-09-08 01:50:20 -070011import org.onlab.onos.net.Link;
12import org.onlab.onos.net.link.LinkService;
13
14import static org.onlab.onos.net.DeviceId.deviceId;
15
16/**
17 * Lists all infrastructure links.
18 */
19@Command(scope = "onos", name = "links",
20 description = "Lists all infrastructure links")
21public class LinksListCommand extends AbstractShellCommand {
22
23 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s";
tom9eb57fb2014-09-11 19:42:38 -070024 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070025
tomc290a122014-09-08 14:27:13 -070026 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070027 required = false, multiValued = false)
tomc290a122014-09-08 14:27:13 -070028 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070029
tom6d2a43e2014-09-08 01:50:20 -070030 @Override
tom0872a172014-09-23 11:24:26 -070031 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070032 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070033 Iterable<Link> links = uri != null ?
34 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom32085cf2014-10-16 00:04:33 -070035 if (outputJson()) {
36 print("%s", json(links));
37 } else {
38 for (Link link : links) {
39 print(linkString(link));
40 }
tom6d2a43e2014-09-08 01:50:20 -070041 }
tom6d2a43e2014-09-08 01:50:20 -070042 }
tom13cb4852014-09-11 12:44:17 -070043
44 /**
tom32085cf2014-10-16 00:04:33 -070045 * Produces a JSON array containing the specified links.
46 *
47 * @param links collection of links
48 * @return JSON array
49 */
50 public static JsonNode json(Iterable<Link> links) {
51 ObjectMapper mapper = new ObjectMapper();
52 ArrayNode result = mapper.createArrayNode();
53 for (Link link : links) {
54 result.add(json(mapper, link));
55 }
56 return result;
57 }
58
59 /**
60 * Produces a JSON object for the specified link.
61 *
62 * @param mapper object mapper
63 * @param link link to encode
64 * @return JSON object
65 */
66 public static ObjectNode json(ObjectMapper mapper, Link link) {
67 ObjectNode result = mapper.createObjectNode();
68 result.set("src", json(mapper, link.src()));
69 result.set("dst", json(mapper, link.src()));
70 return result;
71 }
72
73 /**
74 * Produces a JSON object for the specified connect point.
75 *
76 * @param mapper object mapper
77 * @param connectPoint connection point to encode
78 * @return JSON object
79 */
80 public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) {
81 return mapper.createObjectNode()
82 .put("device", connectPoint.deviceId().toString())
83 .put("port", connectPoint.port().toString());
84 }
85
86 /**
tom613d8142014-09-11 15:09:37 -070087 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -070088 *
89 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -070090 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -070091 */
92 public static String linkString(Link link) {
93 return String.format(FMT, link.src().deviceId(), link.src().port(),
94 link.dst().deviceId(), link.dst().port(), link.type());
tom13cb4852014-09-11 12:44:17 -070095 }
tom9eb57fb2014-09-11 19:42:38 -070096
97 /**
98 * Returns a compact string representing the given link.
99 *
100 * @param link infrastructure link
101 * @return formatted link string
102 */
103 public static String compactLinkString(Link link) {
104 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
105 link.dst().deviceId(), link.dst().port());
106 }
107
tom6d2a43e2014-09-08 01:50:20 -0700108}