blob: 350fe7961f9e140a5617aaf2ba2a838fd05e9d72 [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;
31import org.onosproject.net.host.HostProvider;
32import org.onosproject.net.host.HostProviderRegistry;
33import org.onosproject.net.host.HostProviderService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070034import org.onosproject.net.host.HostService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070035import org.onosproject.net.provider.ProviderId;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070036import org.onosproject.rest.AbstractWebResource;
Ray Milkey1f95bd32014-12-10 11:11:00 -080037
Kedar Gupta47bd4802015-07-15 09:43:26 -070038import javax.ws.rs.Consumes;
Ray Milkey1f95bd32014-12-10 11:11:00 -080039import javax.ws.rs.GET;
Kedar Gupta47bd4802015-07-15 09:43:26 -070040import javax.ws.rs.POST;
Ray Milkey1f95bd32014-12-10 11:11:00 -080041import javax.ws.rs.Path;
42import javax.ws.rs.PathParam;
43import javax.ws.rs.Produces;
Kedar Gupta47bd4802015-07-15 09:43:26 -070044import javax.ws.rs.core.Context;
Ray Milkey1f95bd32014-12-10 11:11:00 -080045import javax.ws.rs.core.MediaType;
46import javax.ws.rs.core.Response;
Kedar Gupta47bd4802015-07-15 09:43:26 -070047import javax.ws.rs.core.UriBuilder;
48import javax.ws.rs.core.UriInfo;
49import java.io.IOException;
50import java.io.InputStream;
51import java.net.URI;
52import java.util.HashSet;
53import java.util.Iterator;
Jian Li43c956b2015-11-26 03:02:46 -080054import java.util.Map;
Kedar Gupta47bd4802015-07-15 09:43:26 -070055import java.util.Set;
Ray Milkey1f95bd32014-12-10 11:11:00 -080056
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070057import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey1f95bd32014-12-10 11:11:00 -080058import static org.onosproject.net.HostId.hostId;
59
60/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070061 * Manage inventory of end-station hosts.
Ray Milkey1f95bd32014-12-10 11:11:00 -080062 */
63@Path("hosts")
64public class HostsWebResource extends AbstractWebResource {
65
Kedar Gupta47bd4802015-07-15 09:43:26 -070066 @Context
Jian Licc730a62016-05-10 16:36:16 -070067 private UriInfo uriInfo;
68 private static final String HOST_NOT_FOUND = "Host is not found";
Jian Li43c956b2015-11-26 03:02:46 -080069 private static final String[] REMOVAL_KEYS = {"mac", "vlan", "location", "ipAddresses"};
Ray Milkey1f95bd32014-12-10 11:11:00 -080070
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 /**
72 * Get all end-station hosts.
73 * Returns array of all known end-station hosts.
74 *
Jian Licc730a62016-05-10 16:36:16 -070075 * @return 200 OK with array of all known end-station hosts.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080076 * @onos.rsModel Hosts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070077 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080078 @GET
79 @Produces(MediaType.APPLICATION_JSON)
80 public Response getHosts() {
81 final Iterable<Host> hosts = get(HostService.class).getHosts();
82 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080083 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080084 }
85
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070086 /**
87 * Get details of end-station host.
88 * Returns detailed properties of the specified end-station host.
89 *
90 * @param id host identifier
Jian Licc730a62016-05-10 16:36:16 -070091 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -080092 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070093 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080094 @GET
95 @Produces(MediaType.APPLICATION_JSON)
96 @Path("{id}")
97 public Response getHostById(@PathParam("id") String id) {
98 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
Kedar Gupta47bd4802015-07-15 09:43:26 -070099 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800100 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800101 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800102 }
103
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700104 /**
105 * Get details of end-station host with MAC/VLAN.
106 * Returns detailed properties of the specified end-station host.
107 *
108 * @param mac host MAC address
109 * @param vlan host VLAN identifier
Jian Licc730a62016-05-10 16:36:16 -0700110 * @return 200 OK with detailed properties of the specified end-station host
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800111 * @onos.rsModel Host
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700112 */
Ray Milkey1f95bd32014-12-10 11:11:00 -0800113 @GET
114 @Produces(MediaType.APPLICATION_JSON)
115 @Path("{mac}/{vlan}")
116 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
117 @PathParam("vlan") String vlan) {
118 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700119 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800120 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800121 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800122 }
Kedar Gupta47bd4802015-07-15 09:43:26 -0700123
124 /**
125 * Creates a new host based on JSON input and adds it to the current
126 * host inventory.
127 *
128 * @param stream input JSON
129 * @return status of the request - CREATED if the JSON is correct,
130 * BAD_REQUEST if the JSON is invalid
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800131 * @onos.rsModel HostPut
Kedar Gupta47bd4802015-07-15 09:43:26 -0700132 */
133 @POST
134 @Consumes(MediaType.APPLICATION_JSON)
135 @Produces(MediaType.APPLICATION_JSON)
136 public Response createAndAddHost(InputStream stream) {
137 URI location;
138 try {
139 // Parse the input stream
140 ObjectNode root = (ObjectNode) mapper().readTree(stream);
141
142 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700143 InternalHostProvider hostProvider = new InternalHostProvider();
144 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
145 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700146 HostId hostId = hostProvider.parseHost(root);
147
148 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
149 .path("hosts")
150 .path(hostId.mac().toString())
151 .path(hostId.vlanId().toString());
152 location = locationBuilder.build();
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700153 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700154
155 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700156 throw new IllegalArgumentException(ex);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700157 }
158 return Response
159 .created(location)
160 .build();
161 }
162
Jian Licc730a62016-05-10 16:36:16 -0700163 /**
164 * Internal host provider that provides host events.
165 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700166 private final class InternalHostProvider implements HostProvider {
167 private final ProviderId providerId =
168 new ProviderId("host", "org.onosproject.rest", true);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700169 private HostProviderService hostProviderService;
170
Jian Licc730a62016-05-10 16:36:16 -0700171 /**
172 * Prevents from instantiation.
173 */
Ray Milkey5d915f42015-08-13 10:27:53 -0700174 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700175 }
176
Ray Milkey5d915f42015-08-13 10:27:53 -0700177 public void triggerProbe(Host host) {
178 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700179 }
180
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700181 public void setHostProviderService(HostProviderService service) {
182 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700183 }
184
Ray Milkey5d915f42015-08-13 10:27:53 -0700185 /*
186 * Return the ProviderId of "this"
187 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700188 public ProviderId id() {
189 return providerId;
190 }
191
192 /**
193 * Creates and adds new host based on given data and returns its host ID.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700194 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700195 * @param node JsonNode containing host information
196 * @return host ID of new host created
197 */
198 private HostId parseHost(JsonNode node) {
199 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
Ray Milkey5d915f42015-08-13 10:27:53 -0700200 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700201 JsonNode locationNode = node.get("location");
202 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
203 locationNode.get("port").asText();
204 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
205
206 Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements();
207 Set<IpAddress> ips = new HashSet<>();
208 while (ipStrings.hasNext()) {
209 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
210 }
Jian Li43c956b2015-11-26 03:02:46 -0800211
212 // try to remove elements from json node after reading them
213 SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700214 // Update host inventory
215
216 HostId hostId = HostId.hostId(mac, vlanId);
sdn5e935452016-08-30 04:12:54 -0700217 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, true, annotations);
Ray Milkeydc083442016-02-22 11:27:57 -0800218 hostProviderService.hostDetected(hostId, desc, false);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700219 return hostId;
220 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700221
222 /**
Jian Li43c956b2015-11-26 03:02:46 -0800223 * Remove a set of elements from JsonNode by specifying keys.
224 *
225 * @param node JsonNode containing host information
226 * @param removalKeys key of elements that need to be removed
227 * @return removal keys
228 */
229 private JsonNode removeElements(JsonNode node, String[] removalKeys) {
230 ObjectMapper mapper = new ObjectMapper();
231 Map<String, Object> map = mapper.convertValue(node, Map.class);
232 for (String key : removalKeys) {
233 map.remove(key);
234 }
235 return mapper.convertValue(map, JsonNode.class);
236 }
237
238 /**
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700239 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
240 * class for use in the POST method.
241 *
242 * @param node node to be annotated
243 * @return SparseAnnotations object with information about node
244 */
245 private SparseAnnotations annotations(JsonNode node) {
246 if (node == null) {
247 return DefaultAnnotations.EMPTY;
248 }
249
250 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
251 Iterator<String> it = node.fieldNames();
252 while (it.hasNext()) {
253 String k = it.next();
254 builder.set(k, node.get(k).asText());
255 }
256 return builder.build();
257 }
258
Kedar Gupta47bd4802015-07-15 09:43:26 -0700259 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800260}
261