Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.ConnectPoint; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.Link; |
| 27 | import org.onlab.onos.net.link.LinkService; |
| 28 | |
| 29 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 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 | |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 38 | private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%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()) { |
| 51 | print("%s", json(links)); |
| 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 | * |
| 62 | * @param links collection of links |
| 63 | * @return JSON array |
| 64 | */ |
| 65 | public static JsonNode json(Iterable<Link> links) { |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | ArrayNode result = mapper.createArrayNode(); |
| 68 | for (Link link : links) { |
| 69 | result.add(json(mapper, link)); |
| 70 | } |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Produces a JSON object for the specified link. |
| 76 | * |
| 77 | * @param mapper object mapper |
| 78 | * @param link link to encode |
| 79 | * @return JSON object |
| 80 | */ |
| 81 | public static ObjectNode json(ObjectMapper mapper, Link link) { |
| 82 | ObjectNode result = mapper.createObjectNode(); |
| 83 | result.set("src", json(mapper, link.src())); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 84 | result.set("dst", json(mapper, link.dst())); |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 85 | result.put("type", link.type().toString()); |
| 86 | result.put("state", link.state().toString()); |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 87 | result.set("annotations", annotations(mapper, link.annotations())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 88 | return result; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Produces a JSON object for the specified connect point. |
| 93 | * |
| 94 | * @param mapper object mapper |
| 95 | * @param connectPoint connection point to encode |
| 96 | * @return JSON object |
| 97 | */ |
| 98 | public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) { |
| 99 | return mapper.createObjectNode() |
| 100 | .put("device", connectPoint.deviceId().toString()) |
| 101 | .put("port", connectPoint.port().toString()); |
| 102 | } |
| 103 | |
| 104 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 105 | * Returns a formatted string representing the given link. |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 106 | * |
| 107 | * @param link infrastructure link |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 108 | * @return formatted link string |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 109 | */ |
| 110 | public static String linkString(Link link) { |
| 111 | return String.format(FMT, link.src().deviceId(), link.src().port(), |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 112 | link.dst().deviceId(), link.dst().port(), |
| 113 | link.type(), link.state(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 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 | } |