blob: 95afe99e79149c0db07af8354f05982d7bf1d502 [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);
115 InternalHostProvider hostProvider = new InternalHostProvider(hostProviderRegistry);
116 hostProvider.register();
117 HostId hostId = hostProvider.parseHost(root);
118
119 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
120 .path("hosts")
121 .path(hostId.mac().toString())
122 .path(hostId.vlanId().toString());
123 location = locationBuilder.build();
124 hostProvider.unregister();
125
126 } catch (IOException ex) {
127 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
128 }
129 return Response
130 .created(location)
131 .build();
132 }
133
134 /**
135 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
136 * class for use in the POST method.
137 *
138 * @param node node to be annotated
139 * @return SparseAnnotations object with information about node
140 */
141 private SparseAnnotations annotations(JsonNode node) {
142 if (node == null) {
143 return DefaultAnnotations.EMPTY;
144 }
145
146 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
147 Iterator<String> it = node.fieldNames();
148 while (it.hasNext()) {
149 String k = it.next();
150 builder.set(k, node.get(k).asText());
151 }
152 return builder.build();
153 }
154
155 private final class InternalHostProvider implements HostProvider {
156 private final ProviderId providerId =
157 new ProviderId("host", "org.onosproject.rest", true);
158 private HostProviderRegistry hostProviderRegistry;
159 private HostProviderService hostProviderService;
160
161 public void triggerProbe(Host host) {
162 // No need to implement since we don't need to check if the host exists
163 }
164
165 private InternalHostProvider(HostProviderRegistry hostProviderRegistry) {
166 this.hostProviderRegistry = hostProviderRegistry;
167 }
168
169 private void register() {
170 this.hostProviderService = hostProviderRegistry.register(this);
171 }
172
173 private void unregister() {
174 hostProviderRegistry.unregister(this);
175 }
176
177 public ProviderId id() {
178 return providerId;
179 }
180
181 /**
182 * Creates and adds new host based on given data and returns its host ID.
183 * @param node JsonNode containing host information
184 * @return host ID of new host created
185 */
186 private HostId parseHost(JsonNode node) {
187 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
188 VlanId vlanId = VlanId.vlanId(((short) node.get("vlan").asInt((VlanId.UNTAGGED))));
189 JsonNode locationNode = node.get("location");
190 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
191 locationNode.get("port").asText();
192 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
193
194 Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements();
195 Set<IpAddress> ips = new HashSet<>();
196 while (ipStrings.hasNext()) {
197 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
198 }
199 SparseAnnotations annotations = annotations(node);
200 // Update host inventory
201
202 HostId hostId = HostId.hostId(mac, vlanId);
203 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
204 hostProviderService.hostDetected(hostId, desc);
205 return hostId;
206 }
207 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800208}
209