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