blob: c1667e91853c57ef3bbae80d92f8ca90ed84929f [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 Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080022import org.onlab.util.Tools;
Ray Milkey3078fc02015-05-06 16:14:14 -070023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.Link;
25import org.onosproject.net.link.LinkService;
26
tom32085cf2014-10-16 00:04:33 -070027import com.fasterxml.jackson.databind.JsonNode;
28import com.fasterxml.jackson.databind.ObjectMapper;
29import com.fasterxml.jackson.databind.node.ArrayNode;
30import com.fasterxml.jackson.databind.node.ObjectNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import static org.onosproject.net.DeviceId.deviceId;
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080032import static org.onosproject.net.LinkKey.linkKey;
33
34import java.util.Comparator;
tom6d2a43e2014-09-08 01:50:20 -070035
36/**
37 * Lists all infrastructure links.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
tom6d2a43e2014-09-08 01:50:20 -070040@Command(scope = "onos", name = "links",
41 description = "Lists all infrastructure links")
42public class LinksListCommand extends AbstractShellCommand {
43
Ray Milkeyb7f0f642016-01-22 16:08:14 -080044 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s, expected=%s";
tom9eb57fb2014-09-11 19:42:38 -070045 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070046
tomc290a122014-09-08 14:27:13 -070047 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070048 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070049 @Completion(DeviceIdCompleter.class)
tomc290a122014-09-08 14:27:13 -070050 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070051
tom6d2a43e2014-09-08 01:50:20 -070052 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 protected void doExecute() {
tomcaf3bf72014-09-23 13:20:53 -070054 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070055 Iterable<Link> links = uri != null ?
56 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom32085cf2014-10-16 00:04:33 -070057 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070058 print("%s", json(this, links));
tom32085cf2014-10-16 00:04:33 -070059 } else {
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080060 Tools.stream(links)
61 .sorted(Comparator.comparing(link -> linkKey(link).toString()))
62 .forEach(link -> {
tom32085cf2014-10-16 00:04:33 -070063 print(linkString(link));
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080064 });
tom6d2a43e2014-09-08 01:50:20 -070065 }
tom6d2a43e2014-09-08 01:50:20 -070066 }
tom13cb4852014-09-11 12:44:17 -070067
68 /**
tom32085cf2014-10-16 00:04:33 -070069 * Produces a JSON array containing the specified links.
70 *
Ray Milkey3078fc02015-05-06 16:14:14 -070071 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070072 * @param links collection of links
73 * @return JSON array
74 */
Ray Milkey3078fc02015-05-06 16:14:14 -070075 public static JsonNode json(AbstractShellCommand context, Iterable<Link> links) {
tom32085cf2014-10-16 00:04:33 -070076 ObjectMapper mapper = new ObjectMapper();
77 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -070078
79 links.forEach(link -> result.add(context.jsonForEntity(link, Link.class)));
80
tom32085cf2014-10-16 00:04:33 -070081 return result;
82 }
83
84 /**
85 * Produces a JSON object for the specified link.
86 *
Ray Milkey3078fc02015-05-06 16:14:14 -070087 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070088 * @param link link to encode
89 * @return JSON object
90 */
Ray Milkey3078fc02015-05-06 16:14:14 -070091 public static ObjectNode json(AbstractShellCommand context, Link link) {
92 return context.jsonForEntity(link, Link.class);
tom32085cf2014-10-16 00:04:33 -070093 }
94
95 /**
tom613d8142014-09-11 15:09:37 -070096 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -070097 *
98 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -070099 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -0700100 */
101 public static String linkString(Link link) {
102 return String.format(FMT, link.src().deviceId(), link.src().port(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800103 link.dst().deviceId(), link.dst().port(),
104 link.type(), link.state(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800105 annotations(link.annotations()),
106 link.isExpected());
tom13cb4852014-09-11 12:44:17 -0700107 }
tom9eb57fb2014-09-11 19:42:38 -0700108
109 /**
110 * Returns a compact string representing the given link.
111 *
112 * @param link infrastructure link
113 * @return formatted link string
114 */
115 public static String compactLinkString(Link link) {
116 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
117 link.dst().deviceId(), link.dst().port());
118 }
119
tom6d2a43e2014-09-08 01:50:20 -0700120}