blob: e6867f66a710f63473e84b38127fc433c692c175 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom6d2a43e2014-09-08 01:50:20 -070019package org.onlab.onos.cli.net;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070025import org.apache.karaf.shell.commands.Argument;
26import org.apache.karaf.shell.commands.Command;
27import org.onlab.onos.cli.AbstractShellCommand;
tom32085cf2014-10-16 00:04:33 -070028import org.onlab.onos.net.ConnectPoint;
tom6d2a43e2014-09-08 01:50:20 -070029import org.onlab.onos.net.Link;
30import org.onlab.onos.net.link.LinkService;
31
32import static org.onlab.onos.net.DeviceId.deviceId;
33
34/**
35 * Lists all infrastructure links.
36 */
37@Command(scope = "onos", name = "links",
38 description = "Lists all infrastructure links")
39public class LinksListCommand extends AbstractShellCommand {
40
41 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s";
tom9eb57fb2014-09-11 19:42:38 -070042 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070043
tomc290a122014-09-08 14:27:13 -070044 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070045 required = false, multiValued = false)
tomc290a122014-09-08 14:27:13 -070046 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070047
tom6d2a43e2014-09-08 01:50:20 -070048 @Override
tom0872a172014-09-23 11:24:26 -070049 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070050 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070051 Iterable<Link> links = uri != null ?
52 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom32085cf2014-10-16 00:04:33 -070053 if (outputJson()) {
54 print("%s", json(links));
55 } else {
56 for (Link link : links) {
57 print(linkString(link));
58 }
tom6d2a43e2014-09-08 01:50:20 -070059 }
tom6d2a43e2014-09-08 01:50:20 -070060 }
tom13cb4852014-09-11 12:44:17 -070061
62 /**
tom32085cf2014-10-16 00:04:33 -070063 * Produces a JSON array containing the specified links.
64 *
65 * @param links collection of links
66 * @return JSON array
67 */
68 public static JsonNode json(Iterable<Link> links) {
69 ObjectMapper mapper = new ObjectMapper();
70 ArrayNode result = mapper.createArrayNode();
71 for (Link link : links) {
72 result.add(json(mapper, link));
73 }
74 return result;
75 }
76
77 /**
78 * Produces a JSON object for the specified link.
79 *
80 * @param mapper object mapper
81 * @param link link to encode
82 * @return JSON object
83 */
84 public static ObjectNode json(ObjectMapper mapper, Link link) {
85 ObjectNode result = mapper.createObjectNode();
86 result.set("src", json(mapper, link.src()));
tomc65fa112014-10-16 07:48:47 -070087 result.set("dst", json(mapper, link.dst()));
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(),
112 link.dst().deviceId(), link.dst().port(), link.type());
tom13cb4852014-09-11 12:44:17 -0700113 }
tom9eb57fb2014-09-11 19:42:38 -0700114
115 /**
116 * Returns a compact string representing the given link.
117 *
118 * @param link infrastructure link
119 * @return formatted link string
120 */
121 public static String compactLinkString(Link link) {
122 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
123 link.dst().deviceId(), link.dst().port());
124 }
125
tom6d2a43e2014-09-08 01:50:20 -0700126}