blob: 8f69c24dab9be48a33c21b9e6a76441cf058ec4e [file] [log] [blame]
Ray Milkey1f95bd32014-12-10 11:11:00 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
67 UriInfo uriInfo;
Ray Milkey1f95bd32014-12-10 11:11:00 -080068 public 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 *
75 * @return 200 OK
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
91 * @return 200 OK
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
110 * @return 200 OK
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
Kedar Gupta47bd4802015-07-15 09:43:26 -0700163 private final class InternalHostProvider implements HostProvider {
164 private final ProviderId providerId =
165 new ProviderId("host", "org.onosproject.rest", true);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700166 private HostProviderService hostProviderService;
167
Ray Milkey5d915f42015-08-13 10:27:53 -0700168 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700169 }
170
Ray Milkey5d915f42015-08-13 10:27:53 -0700171 public void triggerProbe(Host host) {
172 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700173 }
174
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700175 public void setHostProviderService(HostProviderService service) {
176 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700177 }
178
Ray Milkey5d915f42015-08-13 10:27:53 -0700179 /*
180 * Return the ProviderId of "this"
181 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700182 public ProviderId id() {
183 return providerId;
184 }
185
186 /**
187 * Creates and adds new host based on given data and returns its host ID.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700188 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700189 * @param node JsonNode containing host information
190 * @return host ID of new host created
191 */
192 private HostId parseHost(JsonNode node) {
193 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
Ray Milkey5d915f42015-08-13 10:27:53 -0700194 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700195 JsonNode locationNode = node.get("location");
196 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
197 locationNode.get("port").asText();
198 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
199
200 Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements();
201 Set<IpAddress> ips = new HashSet<>();
202 while (ipStrings.hasNext()) {
203 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
204 }
Jian Li43c956b2015-11-26 03:02:46 -0800205
206 // try to remove elements from json node after reading them
207 SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700208 // Update host inventory
209
210 HostId hostId = HostId.hostId(mac, vlanId);
211 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
212 hostProviderService.hostDetected(hostId, desc);
213 return hostId;
214 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700215
216 /**
Jian Li43c956b2015-11-26 03:02:46 -0800217 * Remove a set of elements from JsonNode by specifying keys.
218 *
219 * @param node JsonNode containing host information
220 * @param removalKeys key of elements that need to be removed
221 * @return removal keys
222 */
223 private JsonNode removeElements(JsonNode node, String[] removalKeys) {
224 ObjectMapper mapper = new ObjectMapper();
225 Map<String, Object> map = mapper.convertValue(node, Map.class);
226 for (String key : removalKeys) {
227 map.remove(key);
228 }
229 return mapper.convertValue(map, JsonNode.class);
230 }
231
232 /**
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700233 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
234 * class for use in the POST method.
235 *
236 * @param node node to be annotated
237 * @return SparseAnnotations object with information about node
238 */
239 private SparseAnnotations annotations(JsonNode node) {
240 if (node == null) {
241 return DefaultAnnotations.EMPTY;
242 }
243
244 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
245 Iterator<String> it = node.fieldNames();
246 while (it.hasNext()) {
247 String k = it.next();
248 builder.set(k, node.get(k).asText());
249 }
250 return builder.build();
251 }
252
Kedar Gupta47bd4802015-07-15 09:43:26 -0700253 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800254}
255