blob: 34e13ca1b2201c45470e29dc567ccad50d63e6d0 [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;
Kedar Gupta47bd4802015-07-15 09:43:26 -070021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultAnnotations;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070026import org.onosproject.net.Host;
Kedar Gupta47bd4802015-07-15 09:43:26 -070027import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
29import org.onosproject.net.SparseAnnotations;
30import org.onosproject.net.host.DefaultHostDescription;
Thomas Vachuska58a7c342016-12-13 01:10:56 -080031import org.onosproject.net.host.HostAdminService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070032import org.onosproject.net.host.HostProvider;
33import org.onosproject.net.host.HostProviderRegistry;
34import org.onosproject.net.host.HostProviderService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070035import org.onosproject.net.host.HostService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070036import org.onosproject.net.provider.ProviderId;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070037import org.onosproject.rest.AbstractWebResource;
Ray Milkey1f95bd32014-12-10 11:11:00 -080038
Kedar Gupta47bd4802015-07-15 09:43:26 -070039import javax.ws.rs.Consumes;
Thomas Vachuska58a7c342016-12-13 01:10:56 -080040import javax.ws.rs.DELETE;
Ray Milkey1f95bd32014-12-10 11:11:00 -080041import javax.ws.rs.GET;
Kedar Gupta47bd4802015-07-15 09:43:26 -070042import javax.ws.rs.POST;
Ray Milkey1f95bd32014-12-10 11:11:00 -080043import javax.ws.rs.Path;
44import javax.ws.rs.PathParam;
45import javax.ws.rs.Produces;
Kedar Gupta47bd4802015-07-15 09:43:26 -070046import javax.ws.rs.core.Context;
Ray Milkey1f95bd32014-12-10 11:11:00 -080047import javax.ws.rs.core.MediaType;
48import javax.ws.rs.core.Response;
Kedar Gupta47bd4802015-07-15 09:43:26 -070049import javax.ws.rs.core.UriBuilder;
50import javax.ws.rs.core.UriInfo;
51import java.io.IOException;
52import java.io.InputStream;
53import java.net.URI;
54import java.util.HashSet;
55import java.util.Iterator;
Jian Li43c956b2015-11-26 03:02:46 -080056import java.util.Map;
Kedar Gupta47bd4802015-07-15 09:43:26 -070057import java.util.Set;
Ray Milkey1f95bd32014-12-10 11:11:00 -080058
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070059import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070060import static org.onlab.util.Tools.readTreeFromStream;
Ray Milkey1f95bd32014-12-10 11:11:00 -080061import static org.onosproject.net.HostId.hostId;
62
63/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 * Manage inventory of end-station hosts.
Ray Milkey1f95bd32014-12-10 11:11:00 -080065 */
66@Path("hosts")
67public class HostsWebResource extends AbstractWebResource {
68
Kedar Gupta47bd4802015-07-15 09:43:26 -070069 @Context
Jian Licc730a62016-05-10 16:36:16 -070070 private UriInfo uriInfo;
71 private static final String HOST_NOT_FOUND = "Host is not found";
Charles Chancd06c692017-04-27 20:46:06 -070072 private static final String[] REMOVAL_KEYS = {"mac", "vlan", "locations", "ipAddresses"};
Ray Milkey1f95bd32014-12-10 11:11:00 -080073
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070074 /**
75 * Get all end-station hosts.
76 * Returns array of all known end-station hosts.
77 *
Jian Licc730a62016-05-10 16:36:16 -070078 * @return 200 OK with array of all known end-station hosts.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080079 * @onos.rsModel Hosts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070080 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080081 @GET
82 @Produces(MediaType.APPLICATION_JSON)
83 public Response getHosts() {
84 final Iterable<Host> hosts = get(HostService.class).getHosts();
85 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080086 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080087 }
88
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070089 /**
90 * Get details of end-station host.
91 * Returns detailed properties of the specified end-station host.
92 *
93 * @param id host identifier
Jian Licc730a62016-05-10 16:36:16 -070094 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -080095 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070096 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080097 @GET
98 @Produces(MediaType.APPLICATION_JSON)
99 @Path("{id}")
100 public Response getHostById(@PathParam("id") String id) {
101 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700102 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800103 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800104 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800105 }
106
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700107 /**
108 * Get details of end-station host with MAC/VLAN.
109 * Returns detailed properties of the specified end-station host.
110 *
111 * @param mac host MAC address
112 * @param vlan host VLAN identifier
Jian Licc730a62016-05-10 16:36:16 -0700113 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800114 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700115 */
Ray Milkey1f95bd32014-12-10 11:11:00 -0800116 @GET
117 @Produces(MediaType.APPLICATION_JSON)
118 @Path("{mac}/{vlan}")
119 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
120 @PathParam("vlan") String vlan) {
121 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700122 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800123 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800124 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800125 }
Kedar Gupta47bd4802015-07-15 09:43:26 -0700126
127 /**
128 * Creates a new host based on JSON input and adds it to the current
129 * host inventory.
130 *
131 * @param stream input JSON
132 * @return status of the request - CREATED if the JSON is correct,
133 * BAD_REQUEST if the JSON is invalid
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800134 * @onos.rsModel HostPut
Kedar Gupta47bd4802015-07-15 09:43:26 -0700135 */
136 @POST
137 @Consumes(MediaType.APPLICATION_JSON)
138 @Produces(MediaType.APPLICATION_JSON)
139 public Response createAndAddHost(InputStream stream) {
140 URI location;
macauley_chengc4b6b412018-11-12 21:09:25 +0800141 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
142 InternalHostProvider hostProvider = new InternalHostProvider();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700143 try {
144 // Parse the input stream
Ray Milkey86ee5e82018-04-02 15:33:07 -0700145 ObjectNode root = readTreeFromStream(mapper(), stream);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700146
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700147 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
148 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700149 HostId hostId = hostProvider.parseHost(root);
150
151 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
152 .path("hosts")
153 .path(hostId.mac().toString())
154 .path(hostId.vlanId().toString());
155 location = locationBuilder.build();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700156 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700157 throw new IllegalArgumentException(ex);
macauley_chengc4b6b412018-11-12 21:09:25 +0800158 } finally {
159 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700160 }
macauley_chengc4b6b412018-11-12 21:09:25 +0800161
Kedar Gupta47bd4802015-07-15 09:43:26 -0700162 return Response
163 .created(location)
164 .build();
165 }
166
Jian Licc730a62016-05-10 16:36:16 -0700167 /**
Thomas Vachuska58a7c342016-12-13 01:10:56 -0800168 * Removes infrastructure device.
169 * Administratively deletes the specified device from the inventory of
170 * known devices.
171 *
172 * @param mac host MAC address
173 * @param vlan host VLAN identifier
174 * @return 204 OK
175 * @onos.rsModel Host
176 */
177 @DELETE
178 @Path("{mac}/{vlan}")
179 @Produces(MediaType.APPLICATION_JSON)
180 public Response removeHost(@PathParam("mac") String mac,
181 @PathParam("vlan") String vlan) {
182 get(HostAdminService.class).removeHost(hostId(mac + "/" + vlan));
183 return Response.noContent().build();
184 }
185
186 /**
Jian Licc730a62016-05-10 16:36:16 -0700187 * Internal host provider that provides host events.
188 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700189 private final class InternalHostProvider implements HostProvider {
190 private final ProviderId providerId =
Ray Milkey584fa132019-03-06 10:03:53 -0800191 new ProviderId("host", "org.onosproject.rest");
Kedar Gupta47bd4802015-07-15 09:43:26 -0700192 private HostProviderService hostProviderService;
193
Jian Licc730a62016-05-10 16:36:16 -0700194 /**
195 * Prevents from instantiation.
196 */
Ray Milkey5d915f42015-08-13 10:27:53 -0700197 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700198 }
199
Ray Milkey5d915f42015-08-13 10:27:53 -0700200 public void triggerProbe(Host host) {
201 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700202 }
203
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700204 public void setHostProviderService(HostProviderService service) {
205 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700206 }
207
Ray Milkey5d915f42015-08-13 10:27:53 -0700208 /*
209 * Return the ProviderId of "this"
210 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700211 public ProviderId id() {
212 return providerId;
213 }
214
215 /**
216 * Creates and adds new host based on given data and returns its host ID.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700217 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700218 * @param node JsonNode containing host information
219 * @return host ID of new host created
220 */
221 private HostId parseHost(JsonNode node) {
222 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
Ray Milkey5d915f42015-08-13 10:27:53 -0700223 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700224
macauley_chengc4b6b412018-11-12 21:09:25 +0800225 if (null == node.get("locations")) {
226 throw new IllegalArgumentException("location isn't specified");
227 }
228
Charles Chancd06c692017-04-27 20:46:06 -0700229 Iterator<JsonNode> locationNodes = node.get("locations").elements();
230 Set<HostLocation> locations = new HashSet<>();
231 while (locationNodes.hasNext()) {
232 JsonNode locationNode = locationNodes.next();
233 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
234 locationNode.get("port").asText();
235 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
236 locations.add(hostLocation);
237 }
238
macauley_chengc4b6b412018-11-12 21:09:25 +0800239 if (null == node.get("ipAddresses")) {
240 throw new IllegalArgumentException("ipAddress isn't specified");
241 }
242
Charles Chancd06c692017-04-27 20:46:06 -0700243 Iterator<JsonNode> ipNodes = node.get("ipAddresses").elements();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700244 Set<IpAddress> ips = new HashSet<>();
Charles Chancd06c692017-04-27 20:46:06 -0700245 while (ipNodes.hasNext()) {
246 ips.add(IpAddress.valueOf(ipNodes.next().asText()));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700247 }
Jian Li43c956b2015-11-26 03:02:46 -0800248
249 // try to remove elements from json node after reading them
250 SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700251 // Update host inventory
252
253 HostId hostId = HostId.hostId(mac, vlanId);
Charles Chancd06c692017-04-27 20:46:06 -0700254 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, locations, ips, true, annotations);
Ray Milkeydc083442016-02-22 11:27:57 -0800255 hostProviderService.hostDetected(hostId, desc, false);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700256 return hostId;
257 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700258
259 /**
Jian Li43c956b2015-11-26 03:02:46 -0800260 * Remove a set of elements from JsonNode by specifying keys.
261 *
262 * @param node JsonNode containing host information
263 * @param removalKeys key of elements that need to be removed
264 * @return removal keys
265 */
266 private JsonNode removeElements(JsonNode node, String[] removalKeys) {
267 ObjectMapper mapper = new ObjectMapper();
268 Map<String, Object> map = mapper.convertValue(node, Map.class);
269 for (String key : removalKeys) {
270 map.remove(key);
271 }
272 return mapper.convertValue(map, JsonNode.class);
273 }
274
275 /**
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700276 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
277 * class for use in the POST method.
278 *
279 * @param node node to be annotated
280 * @return SparseAnnotations object with information about node
281 */
282 private SparseAnnotations annotations(JsonNode node) {
283 if (node == null) {
284 return DefaultAnnotations.EMPTY;
285 }
286
287 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
288 Iterator<String> it = node.fieldNames();
289 while (it.hasNext()) {
290 String k = it.next();
291 builder.set(k, node.get(k).asText());
292 }
293 return builder.build();
294 }
295
Kedar Gupta47bd4802015-07-15 09:43:26 -0700296 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800297}
298