Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Yuta HIGUCHI | 8bda4d8 | 2017-02-17 14:45:21 -0800 | [diff] [blame] | 21 | import org.onlab.util.Tools; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.Link; |
| 24 | import org.onosproject.net.link.LinkService; |
| 25 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.JsonNode; |
| 27 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 29 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.DeviceId.deviceId; |
Yuta HIGUCHI | 8bda4d8 | 2017-02-17 14:45:21 -0800 | [diff] [blame] | 31 | import static org.onosproject.net.LinkKey.linkKey; |
| 32 | |
| 33 | import java.util.Comparator; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Lists all infrastructure links. |
| 37 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 38 | @Service |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 39 | @Command(scope = "onos", name = "links", |
| 40 | description = "Lists all infrastructure links") |
| 41 | public class LinksListCommand extends AbstractShellCommand { |
| 42 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 43 | private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s, expected=%s"; |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 44 | private static final String COMPACT = "%s/%s-%s/%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 45 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 46 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 47 | required = false, multiValued = false) |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 48 | String uri = null; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 49 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 50 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 51 | protected void doExecute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 52 | LinkService service = get(LinkService.class); |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 53 | Iterable<Link> links = uri != null ? |
| 54 | service.getDeviceLinks(deviceId(uri)) : service.getLinks(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 55 | if (outputJson()) { |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 56 | print("%s", json(this, links)); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 57 | } else { |
Yuta HIGUCHI | 8bda4d8 | 2017-02-17 14:45:21 -0800 | [diff] [blame] | 58 | Tools.stream(links) |
| 59 | .sorted(Comparator.comparing(link -> linkKey(link).toString())) |
| 60 | .forEach(link -> { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 61 | print(linkString(link)); |
Yuta HIGUCHI | 8bda4d8 | 2017-02-17 14:45:21 -0800 | [diff] [blame] | 62 | }); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 63 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 64 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 67 | * Produces a JSON array containing the specified links. |
| 68 | * |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 69 | * @param context context to use for looking up codecs |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 70 | * @param links collection of links |
| 71 | * @return JSON array |
| 72 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 73 | public static JsonNode json(AbstractShellCommand context, Iterable<Link> links) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 74 | ObjectMapper mapper = new ObjectMapper(); |
| 75 | ArrayNode result = mapper.createArrayNode(); |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 76 | |
| 77 | links.forEach(link -> result.add(context.jsonForEntity(link, Link.class))); |
| 78 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 79 | return result; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Produces a JSON object for the specified link. |
| 84 | * |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 85 | * @param context context to use for looking up codecs |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 86 | * @param link link to encode |
| 87 | * @return JSON object |
| 88 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 89 | public static ObjectNode json(AbstractShellCommand context, Link link) { |
| 90 | return context.jsonForEntity(link, Link.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 94 | * Returns a formatted string representing the given link. |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 95 | * |
| 96 | * @param link infrastructure link |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 97 | * @return formatted link string |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 98 | */ |
| 99 | public static String linkString(Link link) { |
| 100 | return String.format(FMT, link.src().deviceId(), link.src().port(), |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 101 | link.dst().deviceId(), link.dst().port(), |
| 102 | link.type(), link.state(), |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 103 | annotations(link.annotations()), |
| 104 | link.isExpected()); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 105 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 106 | |
| 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 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 118 | } |