blob: b7c8ef72b1b89d1faee010a2cfa2995b84d2f566 [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;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080021import org.onlab.util.Tools;
Ray Milkey3078fc02015-05-06 16:14:14 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.Link;
24import org.onosproject.net.link.LinkService;
25
tom32085cf2014-10-16 00:04:33 -070026import com.fasterxml.jackson.databind.JsonNode;
27import com.fasterxml.jackson.databind.ObjectMapper;
28import com.fasterxml.jackson.databind.node.ArrayNode;
29import com.fasterxml.jackson.databind.node.ObjectNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import static org.onosproject.net.DeviceId.deviceId;
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080031import static org.onosproject.net.LinkKey.linkKey;
32
33import java.util.Comparator;
tom6d2a43e2014-09-08 01:50:20 -070034
35/**
36 * Lists all infrastructure links.
37 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038@Service
tom6d2a43e2014-09-08 01:50:20 -070039@Command(scope = "onos", name = "links",
40 description = "Lists all infrastructure links")
41public class LinksListCommand extends AbstractShellCommand {
42
Ray Milkeyb7f0f642016-01-22 16:08:14 -080043 private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s, expected=%s";
tom9eb57fb2014-09-11 19:42:38 -070044 private static final String COMPACT = "%s/%s-%s/%s";
tom6d2a43e2014-09-08 01:50:20 -070045
tomc290a122014-09-08 14:27:13 -070046 @Argument(index = 0, name = "uri", description = "Device ID",
tom6d2a43e2014-09-08 01:50:20 -070047 required = false, multiValued = false)
tomc290a122014-09-08 14:27:13 -070048 String uri = null;
tom6d2a43e2014-09-08 01:50:20 -070049
tom6d2a43e2014-09-08 01:50:20 -070050 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 protected void doExecute() {
tomcaf3bf72014-09-23 13:20:53 -070052 LinkService service = get(LinkService.class);
tomc290a122014-09-08 14:27:13 -070053 Iterable<Link> links = uri != null ?
54 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
tom32085cf2014-10-16 00:04:33 -070055 if (outputJson()) {
Ray Milkey3078fc02015-05-06 16:14:14 -070056 print("%s", json(this, links));
tom32085cf2014-10-16 00:04:33 -070057 } else {
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080058 Tools.stream(links)
59 .sorted(Comparator.comparing(link -> linkKey(link).toString()))
60 .forEach(link -> {
tom32085cf2014-10-16 00:04:33 -070061 print(linkString(link));
Yuta HIGUCHI8bda4d82017-02-17 14:45:21 -080062 });
tom6d2a43e2014-09-08 01:50:20 -070063 }
tom6d2a43e2014-09-08 01:50:20 -070064 }
tom13cb4852014-09-11 12:44:17 -070065
66 /**
tom32085cf2014-10-16 00:04:33 -070067 * Produces a JSON array containing the specified links.
68 *
Ray Milkey3078fc02015-05-06 16:14:14 -070069 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070070 * @param links collection of links
71 * @return JSON array
72 */
Ray Milkey3078fc02015-05-06 16:14:14 -070073 public static JsonNode json(AbstractShellCommand context, Iterable<Link> links) {
tom32085cf2014-10-16 00:04:33 -070074 ObjectMapper mapper = new ObjectMapper();
75 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -070076
77 links.forEach(link -> result.add(context.jsonForEntity(link, Link.class)));
78
tom32085cf2014-10-16 00:04:33 -070079 return result;
80 }
81
82 /**
83 * Produces a JSON object for the specified link.
84 *
Ray Milkey3078fc02015-05-06 16:14:14 -070085 * @param context context to use for looking up codecs
tom32085cf2014-10-16 00:04:33 -070086 * @param link link to encode
87 * @return JSON object
88 */
Ray Milkey3078fc02015-05-06 16:14:14 -070089 public static ObjectNode json(AbstractShellCommand context, Link link) {
90 return context.jsonForEntity(link, Link.class);
tom32085cf2014-10-16 00:04:33 -070091 }
92
93 /**
tom613d8142014-09-11 15:09:37 -070094 * Returns a formatted string representing the given link.
tom13cb4852014-09-11 12:44:17 -070095 *
96 * @param link infrastructure link
tom613d8142014-09-11 15:09:37 -070097 * @return formatted link string
tom13cb4852014-09-11 12:44:17 -070098 */
99 public static String linkString(Link link) {
100 return String.format(FMT, link.src().deviceId(), link.src().port(),
Thomas Vachuskae4cebaf2014-11-15 18:49:34 -0800101 link.dst().deviceId(), link.dst().port(),
102 link.type(), link.state(),
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800103 annotations(link.annotations()),
104 link.isExpected());
tom13cb4852014-09-11 12:44:17 -0700105 }
tom9eb57fb2014-09-11 19:42:38 -0700106
107 /**
108 * Returns a compact string representing the given link.
109 *
110 * @param link infrastructure link
111 * @return formatted link string
112 */
113 public static String compactLinkString(Link link) {
114 return String.format(COMPACT, link.src().deviceId(), link.src().port(),
115 link.dst().deviceId(), link.dst().port());
116 }
117
tom6d2a43e2014-09-08 01:50:20 -0700118}