blob: 24f5c7b27a584fdec54b307f44afb6e6c6485d71 [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 */
tom6d2a43e2014-09-08 01:50:20 -070016package org.onlab.onos.cli.net;
17
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.onlab.onos.cli.AbstractShellCommand;
tom32085cf2014-10-16 00:04:33 -070025import org.onlab.onos.net.ConnectPoint;
tom6d2a43e2014-09-08 01:50:20 -070026import org.onlab.onos.net.Link;
27import org.onlab.onos.net.link.LinkService;
28
29import static org.onlab.onos.net.DeviceId.deviceId;
30
31/**
32 * Lists all infrastructure links.
33 */
34@Command(scope = "onos", name = "links",
35 description = "Lists all infrastructure links")
36public class LinksListCommand extends AbstractShellCommand {
37
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070038 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s%s";
tom9eb57fb2014-09-11 19:42:38 -070039 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070040
tomc290a122014-09-08 14:27:13 -070041 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070042 required = false, multiValued = false)
tomc290a122014-09-08 14:27:13 -070043 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070044
tom6d2a43e2014-09-08 01:50:20 -070045 @Override
tom0872a172014-09-23 11:24:26 -070046 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070047 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070048 Iterable<Link> links = uri != null ?
49 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom32085cf2014-10-16 00:04:33 -070050 if (outputJson()) {
51 print("%s", json(links));
52 } else {
53 for (Link link : links) {
54 print(linkString(link));
55 }
tom6d2a43e2014-09-08 01:50:20 -070056 }
tom6d2a43e2014-09-08 01:50:20 -070057 }
tom13cb4852014-09-11 12:44:17 -070058
59 /**
tom32085cf2014-10-16 00:04:33 -070060 * Produces a JSON array containing the specified links.
61 *
62 * @param links collection of links
63 * @return JSON array
64 */
65 public static JsonNode json(Iterable<Link> links) {
66 ObjectMapper mapper = new ObjectMapper();
67 ArrayNode result = mapper.createArrayNode();
68 for (Link link : links) {
69 result.add(json(mapper, link));
70 }
71 return result;
72 }
73
74 /**
75 * Produces a JSON object for the specified link.
76 *
77 * @param mapper object mapper
78 * @param link link to encode
79 * @return JSON object
80 */
81 public static ObjectNode json(ObjectMapper mapper, Link link) {
82 ObjectNode result = mapper.createObjectNode();
83 result.set("src", json(mapper, link.src()));
tomc65fa112014-10-16 07:48:47 -070084 result.set("dst", json(mapper, link.dst()));
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070085 result.set("annotations", annotations(mapper, link.annotations()));
tom32085cf2014-10-16 00:04:33 -070086 return result;
87 }
88
89 /**
90 * Produces a JSON object for the specified connect point.
91 *
92 * @param mapper object mapper
93 * @param connectPoint connection point to encode
94 * @return JSON object
95 */
96 public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) {
97 return mapper.createObjectNode()
98 .put("device", connectPoint.deviceId().toString())
99 .put("port", connectPoint.port().toString());
100 }
101
102 /**
tom613d8142014-09-11 15:09:37 -0700103 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -0700104 *
105 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -0700106 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -0700107 */
108 public static String linkString(Link link) {
109 return String.format(FMT, link.src().deviceId(), link.src().port(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700110 link.dst().deviceId(), link.dst().port(), link.type(),
111 annotations(link.annotations()));
tom13cb4852014-09-11 12:44:17 -0700112 }
tom9eb57fb2014-09-11 19:42:38 -0700113
114 /**
115 * Returns a compact string representing the given link.
116 *
117 * @param link infrastructure link
118 * @return formatted link string
119 */
120 public static String compactLinkString(Link link) {
121 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
122 link.dst().deviceId(), link.dst().port());
123 }
124
tom6d2a43e2014-09-08 01:50:20 -0700125}