blob: 6bdd4836a3af81726b6c3ce8e11d1d26502078a0 [file] [log] [blame]
Ray Milkey1f95bd32014-12-10 11:11:00 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey1f95bd32014-12-10 11:11:00 -08003 *
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 Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
17
Kedar Gupta47bd4802015-07-15 09:43:26 -070018import com.fasterxml.jackson.databind.JsonNode;
Jian Li43c956b2015-11-26 03:02:46 -080019import com.fasterxml.jackson.databind.ObjectMapper;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
Charles Chan7becb122020-01-14 16:29:43 -080021import org.onlab.packet.EthType;
Kedar Gupta47bd4802015-07-15 09:43:26 -070022import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DefaultAnnotations;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070027import org.onosproject.net.Host;
Kedar Gupta47bd4802015-07-15 09:43:26 -070028import org.onosproject.net.HostId;
29import org.onosproject.net.HostLocation;
30import org.onosproject.net.SparseAnnotations;
31import org.onosproject.net.host.DefaultHostDescription;
Thomas Vachuska58a7c342016-12-13 01:10:56 -080032import org.onosproject.net.host.HostAdminService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070033import org.onosproject.net.host.HostProvider;
34import org.onosproject.net.host.HostProviderRegistry;
35import org.onosproject.net.host.HostProviderService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070036import org.onosproject.net.host.HostService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070037import org.onosproject.net.provider.ProviderId;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070038import org.onosproject.rest.AbstractWebResource;
Ray Milkey1f95bd32014-12-10 11:11:00 -080039
Kedar Gupta47bd4802015-07-15 09:43:26 -070040import javax.ws.rs.Consumes;
Thomas Vachuska58a7c342016-12-13 01:10:56 -080041import javax.ws.rs.DELETE;
Ray Milkey1f95bd32014-12-10 11:11:00 -080042import javax.ws.rs.GET;
Kedar Gupta47bd4802015-07-15 09:43:26 -070043import javax.ws.rs.POST;
Ray Milkey1f95bd32014-12-10 11:11:00 -080044import javax.ws.rs.Path;
45import javax.ws.rs.PathParam;
46import javax.ws.rs.Produces;
Kedar Gupta47bd4802015-07-15 09:43:26 -070047import javax.ws.rs.core.Context;
Ray Milkey1f95bd32014-12-10 11:11:00 -080048import javax.ws.rs.core.MediaType;
49import javax.ws.rs.core.Response;
Kedar Gupta47bd4802015-07-15 09:43:26 -070050import javax.ws.rs.core.UriBuilder;
51import javax.ws.rs.core.UriInfo;
52import java.io.IOException;
53import java.io.InputStream;
54import java.net.URI;
55import java.util.HashSet;
56import java.util.Iterator;
Jian Li43c956b2015-11-26 03:02:46 -080057import java.util.Map;
Kedar Gupta47bd4802015-07-15 09:43:26 -070058import java.util.Set;
Ray Milkey1f95bd32014-12-10 11:11:00 -080059
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070060import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070061import static org.onlab.util.Tools.readTreeFromStream;
Ray Milkey1f95bd32014-12-10 11:11:00 -080062import static org.onosproject.net.HostId.hostId;
63
64/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 * Manage inventory of end-station hosts.
Ray Milkey1f95bd32014-12-10 11:11:00 -080066 */
67@Path("hosts")
68public class HostsWebResource extends AbstractWebResource {
69
Kedar Gupta47bd4802015-07-15 09:43:26 -070070 @Context
Jian Licc730a62016-05-10 16:36:16 -070071 private UriInfo uriInfo;
72 private static final String HOST_NOT_FOUND = "Host is not found";
Charles Chan7becb122020-01-14 16:29:43 -080073 private static final String[] REMOVAL_KEYS = {"mac", "vlan", "locations", "ipAddresses",
74 "auxLocations", "innerVlan", "outerTpid"};
Ray Milkey1f95bd32014-12-10 11:11:00 -080075
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070076 /**
77 * Get all end-station hosts.
78 * Returns array of all known end-station hosts.
79 *
Jian Licc730a62016-05-10 16:36:16 -070080 * @return 200 OK with array of all known end-station hosts.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080081 * @onos.rsModel Hosts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070082 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080083 @GET
84 @Produces(MediaType.APPLICATION_JSON)
85 public Response getHosts() {
86 final Iterable<Host> hosts = get(HostService.class).getHosts();
87 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080088 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080089 }
90
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070091 /**
92 * Get details of end-station host.
93 * Returns detailed properties of the specified end-station host.
94 *
95 * @param id host identifier
Jian Licc730a62016-05-10 16:36:16 -070096 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -080097 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070098 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080099 @GET
100 @Produces(MediaType.APPLICATION_JSON)
101 @Path("{id}")
102 public Response getHostById(@PathParam("id") String id) {
103 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700104 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800105 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800106 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800107 }
108
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700109 /**
110 * Get details of end-station host with MAC/VLAN.
111 * Returns detailed properties of the specified end-station host.
112 *
113 * @param mac host MAC address
114 * @param vlan host VLAN identifier
Jian Licc730a62016-05-10 16:36:16 -0700115 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800116 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700117 */
Ray Milkey1f95bd32014-12-10 11:11:00 -0800118 @GET
119 @Produces(MediaType.APPLICATION_JSON)
120 @Path("{mac}/{vlan}")
121 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
122 @PathParam("vlan") String vlan) {
123 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700124 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800125 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800126 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800127 }
Kedar Gupta47bd4802015-07-15 09:43:26 -0700128
129 /**
130 * Creates a new host based on JSON input and adds it to the current
131 * host inventory.
132 *
133 * @param stream input JSON
134 * @return status of the request - CREATED if the JSON is correct,
135 * BAD_REQUEST if the JSON is invalid
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800136 * @onos.rsModel HostPut
Kedar Gupta47bd4802015-07-15 09:43:26 -0700137 */
138 @POST
139 @Consumes(MediaType.APPLICATION_JSON)
140 @Produces(MediaType.APPLICATION_JSON)
141 public Response createAndAddHost(InputStream stream) {
142 URI location;
macauley_chengc4b6b412018-11-12 21:09:25 +0800143 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
144 InternalHostProvider hostProvider = new InternalHostProvider();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700145 try {
146 // Parse the input stream
Ray Milkey86ee5e82018-04-02 15:33:07 -0700147 ObjectNode root = readTreeFromStream(mapper(), stream);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700148
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700149 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
150 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700151 HostId hostId = hostProvider.parseHost(root);
152
153 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
154 .path("hosts")
155 .path(hostId.mac().toString())
156 .path(hostId.vlanId().toString());
157 location = locationBuilder.build();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700158 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700159 throw new IllegalArgumentException(ex);
macauley_chengc4b6b412018-11-12 21:09:25 +0800160 } finally {
161 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700162 }
macauley_chengc4b6b412018-11-12 21:09:25 +0800163
Kedar Gupta47bd4802015-07-15 09:43:26 -0700164 return Response
165 .created(location)
166 .build();
167 }
168
Jian Licc730a62016-05-10 16:36:16 -0700169 /**
Thomas Vachuska58a7c342016-12-13 01:10:56 -0800170 * Removes infrastructure device.
171 * Administratively deletes the specified device from the inventory of
172 * known devices.
173 *
174 * @param mac host MAC address
175 * @param vlan host VLAN identifier
176 * @return 204 OK
177 * @onos.rsModel Host
178 */
179 @DELETE
180 @Path("{mac}/{vlan}")
181 @Produces(MediaType.APPLICATION_JSON)
182 public Response removeHost(@PathParam("mac") String mac,
183 @PathParam("vlan") String vlan) {
184 get(HostAdminService.class).removeHost(hostId(mac + "/" + vlan));
185 return Response.noContent().build();
186 }
187
188 /**
Jian Licc730a62016-05-10 16:36:16 -0700189 * Internal host provider that provides host events.
190 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700191 private final class InternalHostProvider implements HostProvider {
192 private final ProviderId providerId =
Ray Milkey584fa132019-03-06 10:03:53 -0800193 new ProviderId("host", "org.onosproject.rest");
Kedar Gupta47bd4802015-07-15 09:43:26 -0700194 private HostProviderService hostProviderService;
195
Jian Licc730a62016-05-10 16:36:16 -0700196 /**
197 * Prevents from instantiation.
198 */
Ray Milkey5d915f42015-08-13 10:27:53 -0700199 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700200 }
201
Ray Milkey5d915f42015-08-13 10:27:53 -0700202 public void triggerProbe(Host host) {
203 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700204 }
205
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700206 public void setHostProviderService(HostProviderService service) {
207 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700208 }
209
Ray Milkey5d915f42015-08-13 10:27:53 -0700210 /*
211 * Return the ProviderId of "this"
212 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700213 public ProviderId id() {
214 return providerId;
215 }
216
217 /**
218 * Creates and adds new host based on given data and returns its host ID.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700219 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700220 * @param node JsonNode containing host information
221 * @return host ID of new host created
222 */
223 private HostId parseHost(JsonNode node) {
224 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
Ray Milkey5d915f42015-08-13 10:27:53 -0700225 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700226
Charles Chan7becb122020-01-14 16:29:43 -0800227 // Parse locations
macauley_chengc4b6b412018-11-12 21:09:25 +0800228 if (null == node.get("locations")) {
229 throw new IllegalArgumentException("location isn't specified");
230 }
Charles Chancd06c692017-04-27 20:46:06 -0700231 Iterator<JsonNode> locationNodes = node.get("locations").elements();
232 Set<HostLocation> locations = new HashSet<>();
233 while (locationNodes.hasNext()) {
234 JsonNode locationNode = locationNodes.next();
235 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
236 locationNode.get("port").asText();
237 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
238 locations.add(hostLocation);
239 }
240
Charles Chan7becb122020-01-14 16:29:43 -0800241 // Parse ipAddresses
macauley_chengc4b6b412018-11-12 21:09:25 +0800242 if (null == node.get("ipAddresses")) {
243 throw new IllegalArgumentException("ipAddress isn't specified");
244 }
Charles Chancd06c692017-04-27 20:46:06 -0700245 Iterator<JsonNode> ipNodes = node.get("ipAddresses").elements();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700246 Set<IpAddress> ips = new HashSet<>();
Charles Chancd06c692017-04-27 20:46:06 -0700247 while (ipNodes.hasNext()) {
248 ips.add(IpAddress.valueOf(ipNodes.next().asText()));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700249 }
Jian Li43c956b2015-11-26 03:02:46 -0800250
Charles Chan7becb122020-01-14 16:29:43 -0800251 // Parse auxLocations
252 Set<HostLocation> auxLocations;
253 JsonNode auxLocationsNode = node.get("auxLocations");
254 if (null == auxLocationsNode) {
255 auxLocations = null;
256 } else {
257 Iterator<JsonNode> auxLocationNodes = auxLocationsNode.elements();
258 auxLocations = new HashSet<>();
259 while (auxLocationNodes.hasNext()) {
260 JsonNode auxLocationNode = auxLocationNodes.next();
261 String deviceAndPort = auxLocationNode.get("elementId").asText() + "/" +
262 auxLocationNode.get("port").asText();
263 HostLocation auxLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
264 auxLocations.add(auxLocation);
265 }
266 }
267
268 // Parse innerVlan
269 JsonNode innerVlanNode = node.get("innerVlan");
270 VlanId innerVlan = (null == innerVlanNode) ? VlanId.NONE : VlanId.vlanId(innerVlanNode.asText());
271
272 // Parse outerTpid
273 JsonNode outerTpidNode = node.get("outerTpid");
274 EthType outerTpid = (null == outerTpidNode) ? EthType.EtherType.UNKNOWN.ethType() :
275 EthType.EtherType.lookup((short) (Integer.decode(outerTpidNode.asText()) & 0xFFFF)).ethType();
276
Jian Li43c956b2015-11-26 03:02:46 -0800277 // try to remove elements from json node after reading them
278 SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700279 // Update host inventory
280
281 HostId hostId = HostId.hostId(mac, vlanId);
Charles Chan7becb122020-01-14 16:29:43 -0800282 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, locations, auxLocations,
283 ips, innerVlan, outerTpid, true, annotations);
Ray Milkeydc083442016-02-22 11:27:57 -0800284 hostProviderService.hostDetected(hostId, desc, false);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700285 return hostId;
286 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700287
288 /**
Jian Li43c956b2015-11-26 03:02:46 -0800289 * Remove a set of elements from JsonNode by specifying keys.
290 *
291 * @param node JsonNode containing host information
292 * @param removalKeys key of elements that need to be removed
293 * @return removal keys
294 */
295 private JsonNode removeElements(JsonNode node, String[] removalKeys) {
296 ObjectMapper mapper = new ObjectMapper();
297 Map<String, Object> map = mapper.convertValue(node, Map.class);
298 for (String key : removalKeys) {
299 map.remove(key);
300 }
301 return mapper.convertValue(map, JsonNode.class);
302 }
303
304 /**
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700305 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
306 * class for use in the POST method.
307 *
308 * @param node node to be annotated
309 * @return SparseAnnotations object with information about node
310 */
311 private SparseAnnotations annotations(JsonNode node) {
312 if (node == null) {
313 return DefaultAnnotations.EMPTY;
314 }
315
316 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
317 Iterator<String> it = node.fieldNames();
318 while (it.hasNext()) {
319 String k = it.next();
320 builder.set(k, node.get(k).asText());
321 }
322 return builder.build();
323 }
324
Kedar Gupta47bd4802015-07-15 09:43:26 -0700325 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800326}
327