Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
| 20 | import org.onosproject.net.Host; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 21 | import org.onosproject.net.HostId; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 22 | import org.onosproject.net.SparseAnnotations; |
| 23 | import org.onosproject.net.host.DefaultHostDescription; |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 24 | import org.onosproject.net.host.HostAdminService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.host.HostProvider; |
| 26 | import org.onosproject.net.host.HostProviderRegistry; |
| 27 | import org.onosproject.net.host.HostProviderService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 28 | import org.onosproject.net.host.HostService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 30 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 31 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 32 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 33 | import javax.ws.rs.DELETE; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 34 | import javax.ws.rs.GET; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 35 | import javax.ws.rs.POST; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.PathParam; |
| 38 | import javax.ws.rs.Produces; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 39 | import javax.ws.rs.core.Context; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
| 44 | import java.io.IOException; |
| 45 | import java.io.InputStream; |
| 46 | import java.net.URI; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 47 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 48 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 49 | import static org.onlab.util.Tools.readTreeFromStream; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 50 | import static org.onosproject.net.HostId.hostId; |
| 51 | |
| 52 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 53 | * Manage inventory of end-station hosts. |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 54 | */ |
| 55 | @Path("hosts") |
| 56 | public class HostsWebResource extends AbstractWebResource { |
| 57 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 58 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 59 | private UriInfo uriInfo; |
| 60 | private static final String HOST_NOT_FOUND = "Host is not found"; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 61 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Get all end-station hosts. |
| 64 | * Returns array of all known end-station hosts. |
| 65 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 66 | * @return 200 OK with array of all known end-station hosts. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 67 | * @onos.rsModel Hosts |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 68 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 69 | @GET |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | public Response getHosts() { |
| 72 | final Iterable<Host> hosts = get(HostService.class).getHosts(); |
| 73 | final ObjectNode root = encodeArray(Host.class, "hosts", hosts); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 74 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Get details of end-station host. |
| 79 | * Returns detailed properties of the specified end-station host. |
| 80 | * |
| 81 | * @param id host identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 82 | * @return 200 OK with detailed properties of the specified end-station host |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 83 | * @onos.rsModel Host |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 84 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 85 | @GET |
| 86 | @Produces(MediaType.APPLICATION_JSON) |
| 87 | @Path("{id}") |
| 88 | public Response getHostById(@PathParam("id") String id) { |
| 89 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)), |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 90 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 91 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 92 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Get details of end-station host with MAC/VLAN. |
| 97 | * Returns detailed properties of the specified end-station host. |
| 98 | * |
| 99 | * @param mac host MAC address |
| 100 | * @param vlan host VLAN identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 101 | * @return 200 OK with detailed properties of the specified end-station host |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 102 | * @onos.rsModel Host |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 103 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 104 | @GET |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
| 106 | @Path("{mac}/{vlan}") |
| 107 | public Response getHostByMacAndVlan(@PathParam("mac") String mac, |
| 108 | @PathParam("vlan") String vlan) { |
| 109 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)), |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 110 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 111 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 112 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 113 | } |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Creates a new host based on JSON input and adds it to the current |
| 117 | * host inventory. |
| 118 | * |
| 119 | * @param stream input JSON |
| 120 | * @return status of the request - CREATED if the JSON is correct, |
| 121 | * BAD_REQUEST if the JSON is invalid |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 122 | * @onos.rsModel HostPut |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 123 | */ |
| 124 | @POST |
| 125 | @Consumes(MediaType.APPLICATION_JSON) |
| 126 | @Produces(MediaType.APPLICATION_JSON) |
| 127 | public Response createAndAddHost(InputStream stream) { |
| 128 | URI location; |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 129 | HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class); |
| 130 | InternalHostProvider hostProvider = new InternalHostProvider(); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 131 | try { |
| 132 | // Parse the input stream |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 133 | ObjectNode root = readTreeFromStream(mapper(), stream); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 134 | |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 135 | HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider); |
| 136 | hostProvider.setHostProviderService(hostProviderService); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 137 | HostId hostId = hostProvider.parseHost(root); |
| 138 | |
| 139 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 140 | .path("hosts") |
| 141 | .path(hostId.mac().toString()) |
| 142 | .path(hostId.vlanId().toString()); |
| 143 | location = locationBuilder.build(); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 144 | } catch (IOException ex) { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 145 | throw new IllegalArgumentException(ex); |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 146 | } finally { |
| 147 | hostProviderRegistry.unregister(hostProvider); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 148 | } |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 149 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 150 | return Response |
| 151 | .created(location) |
| 152 | .build(); |
| 153 | } |
| 154 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 155 | /** |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 156 | * Removes infrastructure device. |
| 157 | * Administratively deletes the specified device from the inventory of |
| 158 | * known devices. |
| 159 | * |
| 160 | * @param mac host MAC address |
| 161 | * @param vlan host VLAN identifier |
| 162 | * @return 204 OK |
| 163 | * @onos.rsModel Host |
| 164 | */ |
| 165 | @DELETE |
| 166 | @Path("{mac}/{vlan}") |
| 167 | @Produces(MediaType.APPLICATION_JSON) |
| 168 | public Response removeHost(@PathParam("mac") String mac, |
| 169 | @PathParam("vlan") String vlan) { |
| 170 | get(HostAdminService.class).removeHost(hostId(mac + "/" + vlan)); |
| 171 | return Response.noContent().build(); |
| 172 | } |
| 173 | |
| 174 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 175 | * Internal host provider that provides host events. |
| 176 | */ |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 177 | private final class InternalHostProvider implements HostProvider { |
| 178 | private final ProviderId providerId = |
Ray Milkey | 584fa13 | 2019-03-06 10:03:53 -0800 | [diff] [blame] | 179 | new ProviderId("host", "org.onosproject.rest"); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 180 | private HostProviderService hostProviderService; |
| 181 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Prevents from instantiation. |
| 184 | */ |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 185 | private InternalHostProvider() { |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 188 | public void triggerProbe(Host host) { |
| 189 | // Not implemented since there is no need to check for hosts on network |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 192 | public void setHostProviderService(HostProviderService service) { |
| 193 | this.hostProviderService = service; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 196 | /* |
| 197 | * Return the ProviderId of "this" |
| 198 | */ |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 199 | public ProviderId id() { |
| 200 | return providerId; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Creates and adds new host based on given data and returns its host ID. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 205 | * |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 206 | * @param node JsonNode containing host information |
| 207 | * @return host ID of new host created |
| 208 | */ |
| 209 | private HostId parseHost(JsonNode node) { |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 210 | Host host = codec(Host.class).decode((ObjectNode) node, HostsWebResource.this); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 211 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 212 | HostId hostId = host.id(); |
| 213 | DefaultHostDescription desc = new DefaultHostDescription( |
| 214 | host.mac(), host.vlan(), host.locations(), host.ipAddresses(), host.innerVlan(), |
| 215 | host.tpid(), host.configured(), (SparseAnnotations) host.annotations()); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 216 | hostProviderService.hostDetected(hostId, desc, false); |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame] | 217 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 218 | return hostId; |
| 219 | } |
| 220 | } |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 221 | } |
| 222 | |