Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
| 17 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Host; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 27 | import org.onosproject.net.HostId; |
| 28 | import org.onosproject.net.HostLocation; |
| 29 | import org.onosproject.net.SparseAnnotations; |
| 30 | import org.onosproject.net.host.DefaultHostDescription; |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 31 | import org.onosproject.net.host.HostAdminService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.host.HostProvider; |
| 33 | import org.onosproject.net.host.HostProviderRegistry; |
| 34 | import org.onosproject.net.host.HostProviderService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 35 | import org.onosproject.net.host.HostService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 36 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 37 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 38 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 39 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 40 | import javax.ws.rs.DELETE; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 41 | import javax.ws.rs.GET; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 42 | import javax.ws.rs.POST; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 43 | import javax.ws.rs.Path; |
| 44 | import javax.ws.rs.PathParam; |
| 45 | import javax.ws.rs.Produces; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 46 | import javax.ws.rs.core.Context; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 47 | import javax.ws.rs.core.MediaType; |
| 48 | import javax.ws.rs.core.Response; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 49 | import javax.ws.rs.core.UriBuilder; |
| 50 | import javax.ws.rs.core.UriInfo; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
| 53 | import java.net.URI; |
| 54 | import java.util.HashSet; |
| 55 | import java.util.Iterator; |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 56 | import java.util.Map; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 57 | import java.util.Set; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 58 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 59 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 60 | import static org.onosproject.net.HostId.hostId; |
| 61 | |
| 62 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 63 | * Manage inventory of end-station hosts. |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 64 | */ |
| 65 | @Path("hosts") |
| 66 | public class HostsWebResource extends AbstractWebResource { |
| 67 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 68 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 69 | private UriInfo uriInfo; |
| 70 | private static final String HOST_NOT_FOUND = "Host is not found"; |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 71 | private static final String[] REMOVAL_KEYS = {"mac", "vlan", "location", "ipAddresses"}; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 72 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Get all end-station hosts. |
| 75 | * Returns array of all known end-station hosts. |
| 76 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 77 | * @return 200 OK with array of all known end-station hosts. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 78 | * @onos.rsModel Hosts |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 79 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 80 | @GET |
| 81 | @Produces(MediaType.APPLICATION_JSON) |
| 82 | public Response getHosts() { |
| 83 | final Iterable<Host> hosts = get(HostService.class).getHosts(); |
| 84 | final ObjectNode root = encodeArray(Host.class, "hosts", hosts); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 85 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Get details of end-station host. |
| 90 | * Returns detailed properties of the specified end-station host. |
| 91 | * |
| 92 | * @param id host identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 93 | * @return 200 OK with detailed properties of the specified end-station host |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 94 | * @onos.rsModel Host |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 95 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 96 | @GET |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
| 98 | @Path("{id}") |
| 99 | public Response getHostById(@PathParam("id") String id) { |
| 100 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)), |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 101 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 102 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 103 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Get details of end-station host with MAC/VLAN. |
| 108 | * Returns detailed properties of the specified end-station host. |
| 109 | * |
| 110 | * @param mac host MAC address |
| 111 | * @param vlan host VLAN identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 112 | * @return 200 OK with detailed properties of the specified end-station host |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 113 | * @onos.rsModel Host |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 114 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 115 | @GET |
| 116 | @Produces(MediaType.APPLICATION_JSON) |
| 117 | @Path("{mac}/{vlan}") |
| 118 | public Response getHostByMacAndVlan(@PathParam("mac") String mac, |
| 119 | @PathParam("vlan") String vlan) { |
| 120 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)), |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 121 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 122 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 123 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 124 | } |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Creates a new host based on JSON input and adds it to the current |
| 128 | * host inventory. |
| 129 | * |
| 130 | * @param stream input JSON |
| 131 | * @return status of the request - CREATED if the JSON is correct, |
| 132 | * BAD_REQUEST if the JSON is invalid |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 133 | * @onos.rsModel HostPut |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 134 | */ |
| 135 | @POST |
| 136 | @Consumes(MediaType.APPLICATION_JSON) |
| 137 | @Produces(MediaType.APPLICATION_JSON) |
| 138 | public Response createAndAddHost(InputStream stream) { |
| 139 | URI location; |
| 140 | try { |
| 141 | // Parse the input stream |
| 142 | ObjectNode root = (ObjectNode) mapper().readTree(stream); |
| 143 | |
| 144 | HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class); |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 145 | InternalHostProvider hostProvider = new InternalHostProvider(); |
| 146 | HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider); |
| 147 | hostProvider.setHostProviderService(hostProviderService); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 148 | HostId hostId = hostProvider.parseHost(root); |
| 149 | |
| 150 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 151 | .path("hosts") |
| 152 | .path(hostId.mac().toString()) |
| 153 | .path(hostId.vlanId().toString()); |
| 154 | location = locationBuilder.build(); |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 155 | hostProviderRegistry.unregister(hostProvider); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 156 | |
| 157 | } catch (IOException ex) { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 158 | throw new IllegalArgumentException(ex); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 159 | } |
| 160 | return Response |
| 161 | .created(location) |
| 162 | .build(); |
| 163 | } |
| 164 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 165 | /** |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 166 | * Removes infrastructure device. |
| 167 | * Administratively deletes the specified device from the inventory of |
| 168 | * known devices. |
| 169 | * |
| 170 | * @param mac host MAC address |
| 171 | * @param vlan host VLAN identifier |
| 172 | * @return 204 OK |
| 173 | * @onos.rsModel Host |
| 174 | */ |
| 175 | @DELETE |
| 176 | @Path("{mac}/{vlan}") |
| 177 | @Produces(MediaType.APPLICATION_JSON) |
| 178 | public Response removeHost(@PathParam("mac") String mac, |
| 179 | @PathParam("vlan") String vlan) { |
| 180 | get(HostAdminService.class).removeHost(hostId(mac + "/" + vlan)); |
| 181 | return Response.noContent().build(); |
| 182 | } |
| 183 | |
| 184 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 185 | * Internal host provider that provides host events. |
| 186 | */ |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 187 | private final class InternalHostProvider implements HostProvider { |
| 188 | private final ProviderId providerId = |
| 189 | new ProviderId("host", "org.onosproject.rest", true); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 190 | private HostProviderService hostProviderService; |
| 191 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Prevents from instantiation. |
| 194 | */ |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 195 | private InternalHostProvider() { |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 198 | public void triggerProbe(Host host) { |
| 199 | // Not implemented since there is no need to check for hosts on network |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 202 | public void setHostProviderService(HostProviderService service) { |
| 203 | this.hostProviderService = service; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 206 | /* |
| 207 | * Return the ProviderId of "this" |
| 208 | */ |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 209 | public ProviderId id() { |
| 210 | return providerId; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Creates and adds new host based on given data and returns its host ID. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 215 | * |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 216 | * @param node JsonNode containing host information |
| 217 | * @return host ID of new host created |
| 218 | */ |
| 219 | private HostId parseHost(JsonNode node) { |
| 220 | MacAddress mac = MacAddress.valueOf(node.get("mac").asText()); |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 221 | VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 222 | JsonNode locationNode = node.get("location"); |
| 223 | String deviceAndPort = locationNode.get("elementId").asText() + "/" + |
| 224 | locationNode.get("port").asText(); |
| 225 | HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0); |
| 226 | |
| 227 | Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements(); |
| 228 | Set<IpAddress> ips = new HashSet<>(); |
| 229 | while (ipStrings.hasNext()) { |
| 230 | ips.add(IpAddress.valueOf(ipStrings.next().asText())); |
| 231 | } |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 232 | |
| 233 | // try to remove elements from json node after reading them |
| 234 | SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS)); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 235 | // Update host inventory |
| 236 | |
| 237 | HostId hostId = HostId.hostId(mac, vlanId); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 238 | DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, true, annotations); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 239 | hostProviderService.hostDetected(hostId, desc, false); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 240 | return hostId; |
| 241 | } |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 242 | |
| 243 | /** |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 244 | * Remove a set of elements from JsonNode by specifying keys. |
| 245 | * |
| 246 | * @param node JsonNode containing host information |
| 247 | * @param removalKeys key of elements that need to be removed |
| 248 | * @return removal keys |
| 249 | */ |
| 250 | private JsonNode removeElements(JsonNode node, String[] removalKeys) { |
| 251 | ObjectMapper mapper = new ObjectMapper(); |
| 252 | Map<String, Object> map = mapper.convertValue(node, Map.class); |
| 253 | for (String key : removalKeys) { |
| 254 | map.remove(key); |
| 255 | } |
| 256 | return mapper.convertValue(map, JsonNode.class); |
| 257 | } |
| 258 | |
| 259 | /** |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 260 | * Produces annotations from specified JsonNode. Copied from the ConfigProvider |
| 261 | * class for use in the POST method. |
| 262 | * |
| 263 | * @param node node to be annotated |
| 264 | * @return SparseAnnotations object with information about node |
| 265 | */ |
| 266 | private SparseAnnotations annotations(JsonNode node) { |
| 267 | if (node == null) { |
| 268 | return DefaultAnnotations.EMPTY; |
| 269 | } |
| 270 | |
| 271 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 272 | Iterator<String> it = node.fieldNames(); |
| 273 | while (it.hasNext()) { |
| 274 | String k = it.next(); |
| 275 | builder.set(k, node.get(k).asText()); |
| 276 | } |
| 277 | return builder.build(); |
| 278 | } |
| 279 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 280 | } |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 281 | } |
| 282 | |