blob: b34d4ebcefe0c58355a3deb9855963f662528f06 [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;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Kedar Gupta47bd4802015-07-15 09:43:26 -070020import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DefaultAnnotations;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070025import org.onosproject.net.Host;
Kedar Gupta47bd4802015-07-15 09:43:26 -070026import org.onosproject.net.HostId;
27import org.onosproject.net.HostLocation;
28import org.onosproject.net.SparseAnnotations;
29import org.onosproject.net.host.DefaultHostDescription;
30import org.onosproject.net.host.HostProvider;
31import org.onosproject.net.host.HostProviderRegistry;
32import org.onosproject.net.host.HostProviderService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070033import org.onosproject.net.host.HostService;
Kedar Gupta47bd4802015-07-15 09:43:26 -070034import org.onosproject.net.provider.ProviderId;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070035import org.onosproject.rest.AbstractWebResource;
Ray Milkey1f95bd32014-12-10 11:11:00 -080036
Kedar Gupta47bd4802015-07-15 09:43:26 -070037import javax.ws.rs.Consumes;
Ray Milkey1f95bd32014-12-10 11:11:00 -080038import javax.ws.rs.GET;
Kedar Gupta47bd4802015-07-15 09:43:26 -070039import javax.ws.rs.POST;
Ray Milkey1f95bd32014-12-10 11:11:00 -080040import javax.ws.rs.Path;
41import javax.ws.rs.PathParam;
42import javax.ws.rs.Produces;
Kedar Gupta47bd4802015-07-15 09:43:26 -070043import javax.ws.rs.core.Context;
Ray Milkey1f95bd32014-12-10 11:11:00 -080044import javax.ws.rs.core.MediaType;
45import javax.ws.rs.core.Response;
Kedar Gupta47bd4802015-07-15 09:43:26 -070046import javax.ws.rs.core.UriBuilder;
47import javax.ws.rs.core.UriInfo;
48import java.io.IOException;
49import java.io.InputStream;
50import java.net.URI;
51import java.util.HashSet;
52import java.util.Iterator;
53import java.util.Set;
Ray Milkey1f95bd32014-12-10 11:11:00 -080054
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070055import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey1f95bd32014-12-10 11:11:00 -080056import static org.onosproject.net.HostId.hostId;
57
58/**
59 * REST resource for interacting with the inventory of hosts.
60 */
61@Path("hosts")
62public class HostsWebResource extends AbstractWebResource {
63
Kedar Gupta47bd4802015-07-15 09:43:26 -070064 @Context
65 UriInfo uriInfo;
Ray Milkey1f95bd32014-12-10 11:11:00 -080066 public static final String HOST_NOT_FOUND = "Host is not found";
67
68 @GET
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response getHosts() {
71 final Iterable<Host> hosts = get(HostService.class).getHosts();
72 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080073 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080074 }
75
76 @GET
77 @Produces(MediaType.APPLICATION_JSON)
78 @Path("{id}")
79 public Response getHostById(@PathParam("id") String id) {
80 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
Kedar Gupta47bd4802015-07-15 09:43:26 -070081 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -080082 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -080083 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080084 }
85
86 @GET
87 @Produces(MediaType.APPLICATION_JSON)
88 @Path("{mac}/{vlan}")
89 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
90 @PathParam("vlan") String vlan) {
91 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
Kedar Gupta47bd4802015-07-15 09:43:26 -070092 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -080093 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -080094 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080095 }
Kedar Gupta47bd4802015-07-15 09:43:26 -070096
97 /**
98 * Creates a new host based on JSON input and adds it to the current
99 * host inventory.
100 *
101 * @param stream input JSON
102 * @return status of the request - CREATED if the JSON is correct,
103 * BAD_REQUEST if the JSON is invalid
104 */
105 @POST
106 @Consumes(MediaType.APPLICATION_JSON)
107 @Produces(MediaType.APPLICATION_JSON)
108 public Response createAndAddHost(InputStream stream) {
109 URI location;
110 try {
111 // Parse the input stream
112 ObjectNode root = (ObjectNode) mapper().readTree(stream);
113
114 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700115 InternalHostProvider hostProvider = new InternalHostProvider();
116 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
117 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700118 HostId hostId = hostProvider.parseHost(root);
119
120 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
121 .path("hosts")
122 .path(hostId.mac().toString())
123 .path(hostId.vlanId().toString());
124 location = locationBuilder.build();
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700125 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700126
127 } catch (IOException ex) {
128 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
129 }
130 return Response
131 .created(location)
132 .build();
133 }
134
Kedar Gupta47bd4802015-07-15 09:43:26 -0700135 private final class InternalHostProvider implements HostProvider {
136 private final ProviderId providerId =
137 new ProviderId("host", "org.onosproject.rest", true);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700138 private HostProviderService hostProviderService;
139
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700140 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700141 public void triggerProbe(Host host) {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700142 }
143
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700144 // Creates new InternalHostProvider with a HostProviderRegistry param.
145 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700146 }
147
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700148 public void setHostProviderService(HostProviderService service) {
149 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700150 }
151
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700152 // Return the ProviderId of "this"
Kedar Gupta47bd4802015-07-15 09:43:26 -0700153 public ProviderId id() {
154 return providerId;
155 }
156
157 /**
158 * Creates and adds new host based on given data and returns its host ID.
159 * @param node JsonNode containing host information
160 * @return host ID of new host created
161 */
162 private HostId parseHost(JsonNode node) {
163 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
164 VlanId vlanId = VlanId.vlanId(((short) node.get("vlan").asInt((VlanId.UNTAGGED))));
165 JsonNode locationNode = node.get("location");
166 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
167 locationNode.get("port").asText();
168 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
169
170 Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements();
171 Set<IpAddress> ips = new HashSet<>();
172 while (ipStrings.hasNext()) {
173 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
174 }
175 SparseAnnotations annotations = annotations(node);
176 // Update host inventory
177
178 HostId hostId = HostId.hostId(mac, vlanId);
179 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
180 hostProviderService.hostDetected(hostId, desc);
181 return hostId;
182 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700183
184 /**
185 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
186 * class for use in the POST method.
187 *
188 * @param node node to be annotated
189 * @return SparseAnnotations object with information about node
190 */
191 private SparseAnnotations annotations(JsonNode node) {
192 if (node == null) {
193 return DefaultAnnotations.EMPTY;
194 }
195
196 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
197 Iterator<String> it = node.fieldNames();
198 while (it.hasNext()) {
199 String k = it.next();
200 builder.set(k, node.get(k).asText());
201 }
202 return builder.build();
203 }
204
Kedar Gupta47bd4802015-07-15 09:43:26 -0700205 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800206}
207