tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 7 | import org.apache.karaf.shell.commands.Argument; |
| 8 | import org.apache.karaf.shell.commands.Command; |
| 9 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 10 | import org.onlab.onos.net.ConnectPoint; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.Link; |
| 12 | import org.onlab.onos.net.link.LinkService; |
| 13 | |
| 14 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 15 | |
| 16 | /** |
| 17 | * Lists all infrastructure links. |
| 18 | */ |
| 19 | @Command(scope = "onos", name = "links", |
| 20 | description = "Lists all infrastructure links") |
| 21 | public class LinksListCommand extends AbstractShellCommand { |
| 22 | |
| 23 | private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s"; |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 24 | private static final String COMPACT = "%s/%s-%s/%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 25 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 26 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 27 | required = false, multiValued = false) |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 28 | String uri = null; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 30 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 31 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 32 | LinkService service = get(LinkService.class); |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 33 | Iterable<Link> links = uri != null ? |
| 34 | service.getDeviceLinks(deviceId(uri)) : service.getLinks(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 35 | if (outputJson()) { |
| 36 | print("%s", json(links)); |
| 37 | } else { |
| 38 | for (Link link : links) { |
| 39 | print(linkString(link)); |
| 40 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 42 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame^] | 45 | * Produces a JSON array containing the specified links. |
| 46 | * |
| 47 | * @param links collection of links |
| 48 | * @return JSON array |
| 49 | */ |
| 50 | public static JsonNode json(Iterable<Link> links) { |
| 51 | ObjectMapper mapper = new ObjectMapper(); |
| 52 | ArrayNode result = mapper.createArrayNode(); |
| 53 | for (Link link : links) { |
| 54 | result.add(json(mapper, link)); |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Produces a JSON object for the specified link. |
| 61 | * |
| 62 | * @param mapper object mapper |
| 63 | * @param link link to encode |
| 64 | * @return JSON object |
| 65 | */ |
| 66 | public static ObjectNode json(ObjectMapper mapper, Link link) { |
| 67 | ObjectNode result = mapper.createObjectNode(); |
| 68 | result.set("src", json(mapper, link.src())); |
| 69 | result.set("dst", json(mapper, link.src())); |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Produces a JSON object for the specified connect point. |
| 75 | * |
| 76 | * @param mapper object mapper |
| 77 | * @param connectPoint connection point to encode |
| 78 | * @return JSON object |
| 79 | */ |
| 80 | public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) { |
| 81 | return mapper.createObjectNode() |
| 82 | .put("device", connectPoint.deviceId().toString()) |
| 83 | .put("port", connectPoint.port().toString()); |
| 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(), |
| 94 | link.dst().deviceId(), link.dst().port(), link.type()); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 95 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Returns a compact string representing the given link. |
| 99 | * |
| 100 | * @param link infrastructure link |
| 101 | * @return formatted link string |
| 102 | */ |
| 103 | public static String compactLinkString(Link link) { |
| 104 | return String.format(COMPACT, link.src().deviceId(), link.src().port(), |
| 105 | link.dst().deviceId(), link.dst().port()); |
| 106 | } |
| 107 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 108 | } |