blob: 7d5dfad6a4c66fe249f9ed4065b31185e93ba0a8 [file] [log] [blame]
Ray Milkey1f95bd32014-12-10 11:11:00 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 Milkey1f95bd32014-12-10 11:11:00 -080060import static org.onosproject.net.HostId.hostId;
61
62/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 * Manage inventory of end-station hosts.
Ray Milkey1f95bd32014-12-10 11:11:00 -080064 */
65@Path("hosts")
66public class HostsWebResource extends AbstractWebResource {
67
Kedar Gupta47bd4802015-07-15 09:43:26 -070068 @Context
Jian Licc730a62016-05-10 16:36:16 -070069 private UriInfo uriInfo;
70 private static final String HOST_NOT_FOUND = "Host is not found";
Jian Li43c956b2015-11-26 03:02:46 -080071 private static final String[] REMOVAL_KEYS = {"mac", "vlan", "location", "ipAddresses"};
Ray Milkey1f95bd32014-12-10 11:11:00 -080072
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070073 /**
74 * Get all end-station hosts.
75 * Returns array of all known end-station hosts.
76 *
Jian Licc730a62016-05-10 16:36:16 -070077 * @return 200 OK with array of all known end-station hosts.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080078 * @onos.rsModel Hosts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080080 @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 Milkey3f025692015-01-26 11:15:41 -080085 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080086 }
87
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070088 /**
89 * Get details of end-station host.
90 * Returns detailed properties of the specified end-station host.
91 *
92 * @param id host identifier
Jian Licc730a62016-05-10 16:36:16 -070093 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -080094 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070095 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080096 @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 Gupta47bd4802015-07-15 09:43:26 -0700101 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800102 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800103 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800104 }
105
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700106 /**
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 Licc730a62016-05-10 16:36:16 -0700112 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800113 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700114 */
Ray Milkey1f95bd32014-12-10 11:11:00 -0800115 @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 Gupta47bd4802015-07-15 09:43:26 -0700121 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800122 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800123 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800124 }
Kedar Gupta47bd4802015-07-15 09:43:26 -0700125
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 Campanella10c4adc2015-12-03 15:27:54 -0800133 * @onos.rsModel HostPut
Kedar Gupta47bd4802015-07-15 09:43:26 -0700134 */
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 Gupta7c4d1962015-08-03 10:46:04 -0700145 InternalHostProvider hostProvider = new InternalHostProvider();
146 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
147 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700148 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 Gupta7c4d1962015-08-03 10:46:04 -0700155 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700156
157 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700158 throw new IllegalArgumentException(ex);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700159 }
160 return Response
161 .created(location)
162 .build();
163 }
164
Jian Licc730a62016-05-10 16:36:16 -0700165 /**
Thomas Vachuska58a7c342016-12-13 01:10:56 -0800166 * 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 Licc730a62016-05-10 16:36:16 -0700185 * Internal host provider that provides host events.
186 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700187 private final class InternalHostProvider implements HostProvider {
188 private final ProviderId providerId =
189 new ProviderId("host", "org.onosproject.rest", true);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700190 private HostProviderService hostProviderService;
191
Jian Licc730a62016-05-10 16:36:16 -0700192 /**
193 * Prevents from instantiation.
194 */
Ray Milkey5d915f42015-08-13 10:27:53 -0700195 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700196 }
197
Ray Milkey5d915f42015-08-13 10:27:53 -0700198 public void triggerProbe(Host host) {
199 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700200 }
201
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700202 public void setHostProviderService(HostProviderService service) {
203 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700204 }
205
Ray Milkey5d915f42015-08-13 10:27:53 -0700206 /*
207 * Return the ProviderId of "this"
208 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700209 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 Vachuska0fa2aa12015-08-18 12:53:04 -0700215 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700216 * @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 Milkey5d915f42015-08-13 10:27:53 -0700221 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700222 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 Li43c956b2015-11-26 03:02:46 -0800232
233 // try to remove elements from json node after reading them
234 SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700235 // Update host inventory
236
237 HostId hostId = HostId.hostId(mac, vlanId);
sdn5e935452016-08-30 04:12:54 -0700238 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, true, annotations);
Ray Milkeydc083442016-02-22 11:27:57 -0800239 hostProviderService.hostDetected(hostId, desc, false);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700240 return hostId;
241 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700242
243 /**
Jian Li43c956b2015-11-26 03:02:46 -0800244 * 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 Gupta7c4d1962015-08-03 10:46:04 -0700260 * 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 Gupta47bd4802015-07-15 09:43:26 -0700280 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800281}
282