Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Argument; |
| 26 | import org.apache.karaf.shell.commands.Command; |
| 27 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.ConnectPoint; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.Link; |
| 30 | import org.onlab.onos.net.link.LinkService; |
| 31 | |
| 32 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 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 | |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 41 | private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s%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()) { |
| 54 | print("%s", json(links)); |
| 55 | } else { |
| 56 | for (Link link : links) { |
| 57 | print(linkString(link)); |
| 58 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 59 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 60 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 63 | * Produces a JSON array containing the specified links. |
| 64 | * |
| 65 | * @param links collection of links |
| 66 | * @return JSON array |
| 67 | */ |
| 68 | public static JsonNode json(Iterable<Link> links) { |
| 69 | ObjectMapper mapper = new ObjectMapper(); |
| 70 | ArrayNode result = mapper.createArrayNode(); |
| 71 | for (Link link : links) { |
| 72 | result.add(json(mapper, link)); |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Produces a JSON object for the specified link. |
| 79 | * |
| 80 | * @param mapper object mapper |
| 81 | * @param link link to encode |
| 82 | * @return JSON object |
| 83 | */ |
| 84 | public static ObjectNode json(ObjectMapper mapper, Link link) { |
| 85 | ObjectNode result = mapper.createObjectNode(); |
| 86 | result.set("src", json(mapper, link.src())); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 87 | result.set("dst", json(mapper, link.dst())); |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 88 | result.set("annotations", annotations(mapper, link.annotations())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 89 | return result; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Produces a JSON object for the specified connect point. |
| 94 | * |
| 95 | * @param mapper object mapper |
| 96 | * @param connectPoint connection point to encode |
| 97 | * @return JSON object |
| 98 | */ |
| 99 | public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) { |
| 100 | return mapper.createObjectNode() |
| 101 | .put("device", connectPoint.deviceId().toString()) |
| 102 | .put("port", connectPoint.port().toString()); |
| 103 | } |
| 104 | |
| 105 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 106 | * Returns a formatted string representing the given link. |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 107 | * |
| 108 | * @param link infrastructure link |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 109 | * @return formatted link string |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 110 | */ |
| 111 | public static String linkString(Link link) { |
| 112 | return String.format(FMT, link.src().deviceId(), link.src().port(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 113 | link.dst().deviceId(), link.dst().port(), link.type(), |
| 114 | annotations(link.annotations())); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 115 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Returns a compact string representing the given link. |
| 119 | * |
| 120 | * @param link infrastructure link |
| 121 | * @return formatted link string |
| 122 | */ |
| 123 | public static String compactLinkString(Link link) { |
| 124 | return String.format(COMPACT, link.src().deviceId(), link.src().port(), |
| 125 | link.dst().deviceId(), link.dst().port()); |
| 126 | } |
| 127 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 128 | } |