blob: 47e72a2810a8a0fe45685457b5cba76532107bae [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080020import org.onlab.util.Tools;
Ray Milkey3078fc02015-05-06 16:14:14 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.Link;
23import org.onosproject.net.link.LinkService;
24
tom32085cf2014-10-16 00:04:33 -070025import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ArrayNode;
28import com.fasterxml.jackson.databind.node.ObjectNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import static org.onosproject.net.DeviceId.deviceId;
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080030import static org.onosproject.net.LinkKey.linkKey;
31
32import java.util.Comparator;
tom6d2a43e2014-09-08 01:50:20 -070033
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
Ray Milkeyb7f0f642016-01-22 16:08:14 -080041 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s, expected=%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()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070054 print("%s", json(this, links));
tom32085cf2014-10-16 00:04:33 -070055 } else {
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080056 Tools.stream(links)
57 .sorted(Comparator.comparing(link -> linkKey(link).toString()))
58 .forEach(link -> {
tom32085cf2014-10-16 00:04:33 -070059 print(linkString(link));
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080060 });
tom6d2a43e2014-09-08 01:50:20 -070061 }
tom6d2a43e2014-09-08 01:50:20 -070062 }
tom13cb4852014-09-11 12:44:17 -070063
64 /**
tom32085cf2014-10-16 00:04:33 -070065 * Produces a JSON array containing the specified links.
66 *
Ray Milkey3078fc02015-05-06 16:14:14 -070067 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070068 * @param links collection of links
69 * @return JSON array
70 */
Ray Milkey3078fc02015-05-06 16:14:14 -070071 public static JsonNode json(AbstractShellCommand context, Iterable<Link> links) {
tom32085cf2014-10-16 00:04:33 -070072 ObjectMapper mapper = new ObjectMapper();
73 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -070074
75 links.forEach(link -> result.add(context.jsonForEntity(link, Link.class)));
76
tom32085cf2014-10-16 00:04:33 -070077 return result;
78 }
79
80 /**
81 * Produces a JSON object for the specified link.
82 *
Ray Milkey3078fc02015-05-06 16:14:14 -070083 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070084 * @param link link to encode
85 * @return JSON object
86 */
Ray Milkey3078fc02015-05-06 16:14:14 -070087 public static ObjectNode json(AbstractShellCommand context, Link link) {
88 return context.jsonForEntity(link, Link.class);
tom32085cf2014-10-16 00:04:33 -070089 }
90
91 /**
tom613d8142014-09-11 15:09:37 -070092 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -070093 *
94 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -070095 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -070096 */
97 public static String linkString(Link link) {
98 return String.format(FMT, link.src().deviceId(), link.src().port(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -080099 link.dst().deviceId(), link.dst().port(),
100 link.type(), link.state(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800101 annotations(link.annotations()),
102 link.isExpected());
tom13cb4852014-09-11 12:44:17 -0700103 }
tom9eb57fb2014-09-11 19:42:38 -0700104
105 /**
106 * Returns a compact string representing the given link.
107 *
108 * @param link infrastructure link
109 * @return formatted link string
110 */
111 public static String compactLinkString(Link link) {
112 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
113 link.dst().deviceId(), link.dst().port());
114 }
115
tom6d2a43e2014-09-08 01:50:20 -0700116}