blob: 1dcbb5de2b85d4efa1714ace8daec1e20e627d98 [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;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.net.Host;
Kedar Gupta47bd4802015-07-15 09:43:26 -070021import org.onosproject.net.HostId;
Kedar Gupta47bd4802015-07-15 09:43:26 -070022import org.onosproject.net.SparseAnnotations;
23import org.onosproject.net.host.DefaultHostDescription;
Thomas Vachuska58a7c342016-12-13 01:10:56 -080024import org.onosproject.net.host.HostAdminService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070025import org.onosproject.net.host.HostProvider;
26import org.onosproject.net.host.HostProviderRegistry;
27import org.onosproject.net.host.HostProviderService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070028import org.onosproject.net.host.HostService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070029import org.onosproject.net.provider.ProviderId;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070030import org.onosproject.rest.AbstractWebResource;
Ray Milkey1f95bd32014-12-10 11:11:00 -080031
Kedar Gupta47bd4802015-07-15 09:43:26 -070032import javax.ws.rs.Consumes;
Thomas Vachuska58a7c342016-12-13 01:10:56 -080033import javax.ws.rs.DELETE;
Ray Milkey1f95bd32014-12-10 11:11:00 -080034import javax.ws.rs.GET;
Kedar Gupta47bd4802015-07-15 09:43:26 -070035import javax.ws.rs.POST;
Ray Milkey1f95bd32014-12-10 11:11:00 -080036import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
Kedar Gupta47bd4802015-07-15 09:43:26 -070039import javax.ws.rs.core.Context;
Ray Milkey1f95bd32014-12-10 11:11:00 -080040import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
Kedar Gupta47bd4802015-07-15 09:43:26 -070042import javax.ws.rs.core.UriBuilder;
43import javax.ws.rs.core.UriInfo;
44import java.io.IOException;
45import java.io.InputStream;
46import java.net.URI;
Ray Milkey1f95bd32014-12-10 11:11:00 -080047
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070048import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070049import static org.onlab.util.Tools.readTreeFromStream;
Ray Milkey1f95bd32014-12-10 11:11:00 -080050import static org.onosproject.net.HostId.hostId;
51
52/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070053 * Manage inventory of end-station hosts.
Ray Milkey1f95bd32014-12-10 11:11:00 -080054 */
55@Path("hosts")
56public class HostsWebResource extends AbstractWebResource {
57
Kedar Gupta47bd4802015-07-15 09:43:26 -070058 @Context
Jian Licc730a62016-05-10 16:36:16 -070059 private UriInfo uriInfo;
60 private static final String HOST_NOT_FOUND = "Host is not found";
Ray Milkey1f95bd32014-12-10 11:11:00 -080061
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 /**
63 * Get all end-station hosts.
64 * Returns array of all known end-station hosts.
65 *
Jian Licc730a62016-05-10 16:36:16 -070066 * @return 200 OK with array of all known end-station hosts.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080067 * @onos.rsModel Hosts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070068 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080069 @GET
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response getHosts() {
72 final Iterable<Host> hosts = get(HostService.class).getHosts();
73 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080074 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080075 }
76
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070077 /**
78 * Get details of end-station host.
79 * Returns detailed properties of the specified end-station host.
80 *
81 * @param id host identifier
Jian Licc730a62016-05-10 16:36:16 -070082 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -080083 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070084 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080085 @GET
86 @Produces(MediaType.APPLICATION_JSON)
87 @Path("{id}")
88 public Response getHostById(@PathParam("id") String id) {
89 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
Kedar Gupta47bd4802015-07-15 09:43:26 -070090 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -080091 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -080092 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080093 }
94
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070095 /**
96 * Get details of end-station host with MAC/VLAN.
97 * Returns detailed properties of the specified end-station host.
98 *
99 * @param mac host MAC address
100 * @param vlan host VLAN identifier
Jian Licc730a62016-05-10 16:36:16 -0700101 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800102 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700103 */
Ray Milkey1f95bd32014-12-10 11:11:00 -0800104 @GET
105 @Produces(MediaType.APPLICATION_JSON)
106 @Path("{mac}/{vlan}")
107 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
108 @PathParam("vlan") String vlan) {
109 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700110 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800111 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800112 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800113 }
Kedar Gupta47bd4802015-07-15 09:43:26 -0700114
115 /**
116 * Creates a new host based on JSON input and adds it to the current
117 * host inventory.
118 *
119 * @param stream input JSON
120 * @return status of the request - CREATED if the JSON is correct,
121 * BAD_REQUEST if the JSON is invalid
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800122 * @onos.rsModel HostPut
Kedar Gupta47bd4802015-07-15 09:43:26 -0700123 */
124 @POST
125 @Consumes(MediaType.APPLICATION_JSON)
126 @Produces(MediaType.APPLICATION_JSON)
127 public Response createAndAddHost(InputStream stream) {
128 URI location;
macauley_chengc4b6b412018-11-12 21:09:25 +0800129 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
130 InternalHostProvider hostProvider = new InternalHostProvider();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700131 try {
132 // Parse the input stream
Ray Milkey86ee5e82018-04-02 15:33:07 -0700133 ObjectNode root = readTreeFromStream(mapper(), stream);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700134
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700135 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
136 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700137 HostId hostId = hostProvider.parseHost(root);
138
139 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
140 .path("hosts")
141 .path(hostId.mac().toString())
142 .path(hostId.vlanId().toString());
143 location = locationBuilder.build();
Kedar Gupta47bd4802015-07-15 09:43:26 -0700144 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700145 throw new IllegalArgumentException(ex);
macauley_chengc4b6b412018-11-12 21:09:25 +0800146 } finally {
147 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700148 }
macauley_chengc4b6b412018-11-12 21:09:25 +0800149
Kedar Gupta47bd4802015-07-15 09:43:26 -0700150 return Response
151 .created(location)
152 .build();
153 }
154
Jian Licc730a62016-05-10 16:36:16 -0700155 /**
Thomas Vachuska58a7c342016-12-13 01:10:56 -0800156 * Removes infrastructure device.
157 * Administratively deletes the specified device from the inventory of
158 * known devices.
159 *
160 * @param mac host MAC address
161 * @param vlan host VLAN identifier
162 * @return 204 OK
163 * @onos.rsModel Host
164 */
165 @DELETE
166 @Path("{mac}/{vlan}")
167 @Produces(MediaType.APPLICATION_JSON)
168 public Response removeHost(@PathParam("mac") String mac,
169 @PathParam("vlan") String vlan) {
170 get(HostAdminService.class).removeHost(hostId(mac + "/" + vlan));
171 return Response.noContent().build();
172 }
173
174 /**
Jian Licc730a62016-05-10 16:36:16 -0700175 * Internal host provider that provides host events.
176 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700177 private final class InternalHostProvider implements HostProvider {
178 private final ProviderId providerId =
Ray Milkey584fa132019-03-06 10:03:53 -0800179 new ProviderId("host", "org.onosproject.rest");
Kedar Gupta47bd4802015-07-15 09:43:26 -0700180 private HostProviderService hostProviderService;
181
Jian Licc730a62016-05-10 16:36:16 -0700182 /**
183 * Prevents from instantiation.
184 */
Ray Milkey5d915f42015-08-13 10:27:53 -0700185 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700186 }
187
Ray Milkey5d915f42015-08-13 10:27:53 -0700188 public void triggerProbe(Host host) {
189 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700190 }
191
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700192 public void setHostProviderService(HostProviderService service) {
193 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700194 }
195
Ray Milkey5d915f42015-08-13 10:27:53 -0700196 /*
197 * Return the ProviderId of "this"
198 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700199 public ProviderId id() {
200 return providerId;
201 }
202
203 /**
204 * Creates and adds new host based on given data and returns its host ID.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700205 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700206 * @param node JsonNode containing host information
207 * @return host ID of new host created
208 */
209 private HostId parseHost(JsonNode node) {
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -0800210 Host host = codec(Host.class).decode((ObjectNode) node, HostsWebResource.this);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700211
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -0800212 HostId hostId = host.id();
213 DefaultHostDescription desc = new DefaultHostDescription(
214 host.mac(), host.vlan(), host.locations(), host.ipAddresses(), host.innerVlan(),
215 host.tpid(), host.configured(), (SparseAnnotations) host.annotations());
Ray Milkeydc083442016-02-22 11:27:57 -0800216 hostProviderService.hostDetected(hostId, desc, false);
Seyeon Jeong9ceeacb2020-02-28 01:46:39 -0800217
Kedar Gupta47bd4802015-07-15 09:43:26 -0700218 return hostId;
219 }
220 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800221}
222