Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 3 | * |
| 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
| 17 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Host; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.HostId; |
| 27 | import org.onosproject.net.HostLocation; |
| 28 | import org.onosproject.net.SparseAnnotations; |
| 29 | import org.onosproject.net.host.DefaultHostDescription; |
| 30 | import org.onosproject.net.host.HostProvider; |
| 31 | import org.onosproject.net.host.HostProviderRegistry; |
| 32 | import org.onosproject.net.host.HostProviderService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 33 | import org.onosproject.net.host.HostService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 35 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 36 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 37 | import javax.ws.rs.Consumes; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 38 | import javax.ws.rs.GET; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 39 | import javax.ws.rs.POST; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 40 | import javax.ws.rs.Path; |
| 41 | import javax.ws.rs.PathParam; |
| 42 | import javax.ws.rs.Produces; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 43 | import javax.ws.rs.core.Context; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 44 | import javax.ws.rs.core.MediaType; |
| 45 | import javax.ws.rs.core.Response; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 46 | import javax.ws.rs.core.UriBuilder; |
| 47 | import javax.ws.rs.core.UriInfo; |
| 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
| 50 | import java.net.URI; |
| 51 | import java.util.HashSet; |
| 52 | import java.util.Iterator; |
| 53 | import java.util.Set; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 54 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 55 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 56 | import static org.onosproject.net.HostId.hostId; |
| 57 | |
| 58 | /** |
| 59 | * REST resource for interacting with the inventory of hosts. |
| 60 | */ |
| 61 | @Path("hosts") |
| 62 | public class HostsWebResource extends AbstractWebResource { |
| 63 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 64 | @Context |
| 65 | UriInfo uriInfo; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 66 | 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 Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 73 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 74 | } |
| 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 Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 81 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 82 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 83 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 84 | } |
| 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 Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 92 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 93 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 94 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 95 | } |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 96 | |
| 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 Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 208 | } |
| 209 | |