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 | */ |
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 | |
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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 26 | import org.onosproject.net.HostId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.link.LinkService; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Lists all infrastructure links. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "links", |
| 36 | description = "Lists all infrastructure links") |
| 37 | public class LinksListCommand extends AbstractShellCommand { |
| 38 | |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 39 | 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] | 40 | private static final String COMPACT = "%s/%s-%s/%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 42 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 43 | required = false, multiValued = false) |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 44 | String uri = null; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 45 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 46 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 47 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 48 | LinkService service = get(LinkService.class); |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 49 | Iterable<Link> links = uri != null ? |
| 50 | service.getDeviceLinks(deviceId(uri)) : service.getLinks(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 51 | if (outputJson()) { |
| 52 | print("%s", json(links)); |
| 53 | } else { |
| 54 | for (Link link : links) { |
| 55 | print(linkString(link)); |
| 56 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 57 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 58 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 61 | * Produces a JSON array containing the specified links. |
| 62 | * |
| 63 | * @param links collection of links |
| 64 | * @return JSON array |
| 65 | */ |
| 66 | public static JsonNode json(Iterable<Link> links) { |
| 67 | ObjectMapper mapper = new ObjectMapper(); |
| 68 | ArrayNode result = mapper.createArrayNode(); |
| 69 | for (Link link : links) { |
| 70 | result.add(json(mapper, link)); |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Produces a JSON object for the specified link. |
| 77 | * |
| 78 | * @param mapper object mapper |
| 79 | * @param link link to encode |
| 80 | * @return JSON object |
| 81 | */ |
| 82 | public static ObjectNode json(ObjectMapper mapper, Link link) { |
| 83 | ObjectNode result = mapper.createObjectNode(); |
| 84 | result.set("src", json(mapper, link.src())); |
tom | c65fa11 | 2014-10-16 07:48:47 -0700 | [diff] [blame] | 85 | result.set("dst", json(mapper, link.dst())); |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 86 | result.put("type", link.type().toString()); |
| 87 | result.put("state", link.state().toString()); |
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 | /** |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 93 | * Produces a JSON object for the specified host ID. |
| 94 | * |
| 95 | * @param mapper object mapper |
| 96 | * @param hostId host ID to encode |
| 97 | * @return JSON object |
| 98 | */ |
| 99 | public static ObjectNode json(ObjectMapper mapper, HostId hostId) { |
| 100 | return mapper.createObjectNode() |
| 101 | .put("mac", hostId.mac().toString()) |
| 102 | .put("vlanId", hostId.vlanId().toString()); |
| 103 | } |
| 104 | |
| 105 | /** |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 106 | * Produces a JSON object for the specified connect point. |
| 107 | * |
| 108 | * @param mapper object mapper |
| 109 | * @param connectPoint connection point to encode |
| 110 | * @return JSON object |
| 111 | */ |
| 112 | public static ObjectNode json(ObjectMapper mapper, ConnectPoint connectPoint) { |
| 113 | return mapper.createObjectNode() |
| 114 | .put("device", connectPoint.deviceId().toString()) |
| 115 | .put("port", connectPoint.port().toString()); |
| 116 | } |
| 117 | |
| 118 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 119 | * Returns a formatted string representing the given link. |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 120 | * |
| 121 | * @param link infrastructure link |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 122 | * @return formatted link string |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 123 | */ |
| 124 | public static String linkString(Link link) { |
| 125 | return String.format(FMT, link.src().deviceId(), link.src().port(), |
Thomas Vachuska | e4cebaf | 2014-11-15 18:49:34 -0800 | [diff] [blame] | 126 | link.dst().deviceId(), link.dst().port(), |
| 127 | link.type(), link.state(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 128 | annotations(link.annotations())); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 129 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Returns a compact string representing the given link. |
| 133 | * |
| 134 | * @param link infrastructure link |
| 135 | * @return formatted link string |
| 136 | */ |
| 137 | public static String compactLinkString(Link link) { |
| 138 | return String.format(COMPACT, link.src().deviceId(), link.src().port(), |
| 139 | link.dst().deviceId(), link.dst().port()); |
| 140 | } |
| 141 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 142 | } |