blob: 9d3fd7fef1d4edfac929e19e7bf5a3acdc08c1a4 [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) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700128 throw new IllegalArgumentException(ex);
Kedar Gupta47bd4802015-07-15 09:43:26 -0700129 }
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
Ray Milkey5d915f42015-08-13 10:27:53 -0700140 private InternalHostProvider() {
Kedar Gupta47bd4802015-07-15 09:43:26 -0700141 }
142
Ray Milkey5d915f42015-08-13 10:27:53 -0700143 public void triggerProbe(Host host) {
144 // Not implemented since there is no need to check for hosts on network
Kedar Gupta47bd4802015-07-15 09:43:26 -0700145 }
146
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700147 public void setHostProviderService(HostProviderService service) {
148 this.hostProviderService = service;
Kedar Gupta47bd4802015-07-15 09:43:26 -0700149 }
150
Ray Milkey5d915f42015-08-13 10:27:53 -0700151 /*
152 * Return the ProviderId of "this"
153 */
Kedar Gupta47bd4802015-07-15 09:43:26 -0700154 public ProviderId id() {
155 return providerId;
156 }
157
158 /**
159 * Creates and adds new host based on given data and returns its host ID.
160 * @param node JsonNode containing host information
161 * @return host ID of new host created
162 */
163 private HostId parseHost(JsonNode node) {
164 MacAddress mac = MacAddress.valueOf(node.get("mac").asText());
Ray Milkey5d915f42015-08-13 10:27:53 -0700165 VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED));
Kedar Gupta47bd4802015-07-15 09:43:26 -0700166 JsonNode locationNode = node.get("location");
167 String deviceAndPort = locationNode.get("elementId").asText() + "/" +
168 locationNode.get("port").asText();
169 HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0);
170
171 Iterator<JsonNode> ipStrings = node.get("ipAddresses").elements();
172 Set<IpAddress> ips = new HashSet<>();
173 while (ipStrings.hasNext()) {
174 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
175 }
176 SparseAnnotations annotations = annotations(node);
177 // Update host inventory
178
179 HostId hostId = HostId.hostId(mac, vlanId);
180 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
181 hostProviderService.hostDetected(hostId, desc);
182 return hostId;
183 }
Kedar Gupta7c4d1962015-08-03 10:46:04 -0700184
185 /**
186 * Produces annotations from specified JsonNode. Copied from the ConfigProvider
187 * class for use in the POST method.
188 *
189 * @param node node to be annotated
190 * @return SparseAnnotations object with information about node
191 */
192 private SparseAnnotations annotations(JsonNode node) {
193 if (node == null) {
194 return DefaultAnnotations.EMPTY;
195 }
196
197 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
198 Iterator<String> it = node.fieldNames();
199 while (it.hasNext()) {
200 String k = it.next();
201 builder.set(k, node.get(k).asText());
202 }
203 return builder.build();
204 }
205
Kedar Gupta47bd4802015-07-15 09:43:26 -0700206 }
Ray Milkey1f95bd32014-12-10 11:11:00 -0800207}
208