blob: 7c32129a9c8b416a818a668733c604e5960ace5b [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 Vachuskae4cebaf2014-11-15 18:49:34 -080038 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%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 Vachuskae4cebaf2014-11-15 18:49:34 -080085 result.put("type", link.type().toString());
86 result.put("state", link.state().toString());
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070087 result.set("annotations", annotations(mapper, link.annotations()));
tom32085cf2014-10-16 00:04:33 -070088 return result;
89 }
90
91 /**
92 * Produces a JSON object for the specified connect point.
93 *
94 * @param mapper object mapper
95 * @param connectPoint connection point to encode
96 * @return JSON object
97 */
98 public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) {
99 return mapper.createObjectNode()
100 .put("device", connectPoint.deviceId().toString())
101 .put("port", connectPoint.port().toString());
102 }
103
104 /**
tom613d8142014-09-11 15:09:37 -0700105 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -0700106 *
107 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -0700108 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -0700109 */
110 public static String linkString(Link link) {
111 return String.format(FMT, link.src().deviceId(), link.src().port(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800112 link.dst().deviceId(), link.dst().port(),
113 link.type(), link.state(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700114 annotations(link.annotations()));
tom13cb4852014-09-11 12:44:17 -0700115 }
tom9eb57fb2014-09-11 19:42:38 -0700116
117 /**
118 * Returns a compact string representing the given link.
119 *
120 * @param link infrastructure link
121 * @return formatted link string
122 */
123 public static String compactLinkString(Link link) {
124 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
125 link.dst().deviceId(), link.dst().port());
126 }
127
tom6d2a43e2014-09-08 01:50:20 -0700128}