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