blob: b89f5addfea3d198f5267d1a973506b513877cfd [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/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070059 * Manage inventory of end-station hosts.
Ray Milkey1f95bd32014-12-10 11:11:00 -080060 */
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
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070068 /**
69 * Get all end-station hosts.
70 * Returns array of all known end-station hosts.
71 *
72 * @return 200 OK
73 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080074 @GET
75 @Produces(MediaType.APPLICATION_JSON)
76 public Response getHosts() {
77 final Iterable<Host> hosts = get(HostService.class).getHosts();
78 final ObjectNode root = encodeArray(Host.class, "hosts", hosts);
Ray Milkey3f025692015-01-26 11:15:41 -080079 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080080 }
81
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070082 /**
83 * Get details of end-station host.
84 * Returns detailed properties of the specified end-station host.
85 *
86 * @param id host identifier
87 * @return 200 OK
88 */
Ray Milkey1f95bd32014-12-10 11:11:00 -080089 @GET
90 @Produces(MediaType.APPLICATION_JSON)
91 @Path("{id}")
92 public Response getHostById(@PathParam("id") String id) {
93 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)),
Kedar Gupta47bd4802015-07-15 09:43:26 -070094 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -080095 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -080096 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -080097 }
98
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070099 /**
100 * Get details of end-station host with MAC/VLAN.
101 * Returns detailed properties of the specified end-station host.
102 *
103 * @param mac host MAC address
104 * @param vlan host VLAN identifier
105 * @return 200 OK
106 */
Ray Milkey1f95bd32014-12-10 11:11:00 -0800107 @GET
108 @Produces(MediaType.APPLICATION_JSON)
109 @Path("{mac}/{vlan}")
110 public Response getHostByMacAndVlan(@PathParam("mac") String mac,
111 @PathParam("vlan") String vlan) {
112 final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)),
Kedar Gupta47bd4802015-07-15 09:43:26 -0700113 HOST_NOT_FOUND);
Ray Milkey1f95bd32014-12-10 11:11:00 -0800114 final ObjectNode root = codec(Host.class).encode(host, this);
Ray Milkey3f025692015-01-26 11:15:41 -0800115 return ok(root).build();
Ray Milkey1f95bd32014-12-10 11:11:00 -0800116 }
Kedar Gupta47bd4802015-07-15 09:43:26 -0700117
118 /**
119 * Creates a new host based on JSON input and adds it to the current
120 * host inventory.
121 *
122 * @param stream input JSON
123 * @return status of the request - CREATED if the JSON is correct,
124 * BAD_REQUEST if the JSON is invalid
125 */
126 @POST
127 @Consumes(MediaType.APPLICATION_JSON)
128 @Produces(MediaType.APPLICATION_JSON)
129 public Response createAndAddHost(InputStream stream) {
130 URI location;
131 try {
132 // Parse the input stream
133 ObjectNode root = (ObjectNode) mapper().readTree(stream);
134
135 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700136 InternalHostProvider hostProvider = new InternalHostProvider();
137 HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
138 hostProvider.setHostProviderService(hostProviderService);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700139 HostId hostId = hostProvider.parseHost(root);
140
141 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
142 .path("hosts")
143 .path(hostId.mac().toString())
144 .path(hostId.vlanId().toString());
145 location = locationBuilder.build();
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700146 hostProviderRegistry.unregister(hostProvider);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700147
148 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700149 throw new IllegalArgumentException(ex);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700150 }
151 return Response
152 .created(location)
153 .build();
154 }
155
Kedar Gupta47bd4802015-07-15 09:43:26 -0700156 private final class InternalHostProvider implements HostProvider {
157 private final ProviderId providerId =
158 new ProviderId("host", "org.onosproject.rest", true);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700159 private HostProviderService hostProviderService;
160
Ray Milkey5d915f42015-08-13 10:27:53 -0700161 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700162 }
163
Ray Milkey5d915f42015-08-13 10:27:53 -0700164 public void triggerProbe(Host host) {
165 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700166 }
167
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700168 public void setHostProviderService(HostProviderService service) {
169 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700170 }
171
Ray Milkey5d915f42015-08-13 10:27:53 -0700172 /*
173 * Return the ProviderId of "this"
174 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700175 public ProviderId id() {
176 return providerId;
177 }
178
179 /**
180 * Creates and adds new host based on given data and returns its host ID.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700181 *
Kedar Gupta47bd4802015-07-15 09:43:26 -0700182 * @param node JsonNode containing host information
183 * @return host ID of new host created
184 */
185 private HostId parseHost(JsonNode node) {
186 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
Ray Milkey5d915f42015-08-13 10:27:53 -0700187 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700188 JsonNode locationNode = node.get("location");
189 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
190 locationNode.get("port").asText();
191 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
192
193 Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements();
194 Set<IpAddress> ips = new HashSet<>();
195 while (ipStrings.hasNext()) {
196 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
197 }
198 SparseAnnotations annotations = annotations(node);
199 // Update host inventory
200
201 HostId hostId = HostId.hostId(mac, vlanId);
202 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
203 hostProviderService.hostDetected(hostId, desc);
204 return hostId;
205 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700206
207 /**
208 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
209 * class for use in the POST method.
210 *
211 * @param node node to be annotated
212 * @return SparseAnnotations object with information about node
213 */
214 private SparseAnnotations annotations(JsonNode node) {
215 if (node == null) {
216 return DefaultAnnotations.EMPTY;
217 }
218
219 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
220 Iterator<String> it = node.fieldNames();
221 while (it.hasNext()) {
222 String k = it.next();
223 builder.set(k, node.get(k).asText());
224 }
225 return builder.build();
226 }
227
Kedar Gupta47bd4802015-07-15 09:43:26 -0700228 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800229}
230