blob: b9403a397a7c9c3df216f470832dde8d881693dd [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom6d2a43e2014-09-08 01:50:20 -070017
Ray Milkey3078fc02015-05-06 16:14:14 -070018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.net.Link;
22import org.onosproject.net.link.LinkService;
23
tom32085cf2014-10-16 00:04:33 -070024import com.fasterxml.jackson.databind.JsonNode;
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.ArrayNode;
27import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070028
Brian O'Connorabafb502014-12-02 22:26:20 -080029import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070030
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()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070051 print("%s", json(this, links));
tom32085cf2014-10-16 00:04:33 -070052 } 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 *
Ray Milkey3078fc02015-05-06 16:14:14 -070062 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070063 * @param links collection of links
64 * @return JSON array
65 */
Ray Milkey3078fc02015-05-06 16:14:14 -070066 public static JsonNode json(AbstractShellCommand context, Iterable<Link> links) {
tom32085cf2014-10-16 00:04:33 -070067 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -070069
70 links.forEach(link -> result.add(context.jsonForEntity(link, Link.class)));
71
tom32085cf2014-10-16 00:04:33 -070072 return result;
73 }
74
75 /**
76 * Produces a JSON object for the specified link.
77 *
Ray Milkey3078fc02015-05-06 16:14:14 -070078 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070079 * @param link link to encode
80 * @return JSON object
81 */
Ray Milkey3078fc02015-05-06 16:14:14 -070082 public static ObjectNode json(AbstractShellCommand context, Link link) {
83 return context.jsonForEntity(link, Link.class);
tom32085cf2014-10-16 00:04:33 -070084 }
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(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -080094 link.dst().deviceId(), link.dst().port(),
95 link.type(), link.state(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070096 annotations(link.annotations()));
tom13cb4852014-09-11 12:44:17 -070097 }
tom9eb57fb2014-09-11 19:42:38 -070098
99 /**
100 * Returns a compact string representing the given link.
101 *
102 * @param link infrastructure link
103 * @return formatted link string
104 */
105 public static String compactLinkString(Link link) {
106 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
107 link.dst().deviceId(), link.dst().port());
108 }
109
tom6d2a43e2014-09-08 01:50:20 -0700110}