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; |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Charles Chan | 7becb12 | 2020-01-14 16:29:43 -0800 | [diff] [blame] | 21 | import org.onlab.packet.EthType; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.HostLocation; |
| 30 | import org.onosproject.net.SparseAnnotations; |
| 31 | import org.onosproject.net.host.DefaultHostDescription; |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 32 | import org.onosproject.net.host.HostAdminService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 33 | import org.onosproject.net.host.HostProvider; |
| 34 | import org.onosproject.net.host.HostProviderRegistry; |
| 35 | import org.onosproject.net.host.HostProviderService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 36 | import org.onosproject.net.host.HostService; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 37 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 38 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 39 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 40 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 41 | import javax.ws.rs.DELETE; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 42 | import javax.ws.rs.GET; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 43 | import javax.ws.rs.POST; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 44 | import javax.ws.rs.Path; |
| 45 | import javax.ws.rs.PathParam; |
| 46 | import javax.ws.rs.Produces; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 47 | import javax.ws.rs.core.Context; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 48 | import javax.ws.rs.core.MediaType; |
| 49 | import javax.ws.rs.core.Response; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 50 | import javax.ws.rs.core.UriBuilder; |
| 51 | import javax.ws.rs.core.UriInfo; |
| 52 | import java.io.IOException; |
| 53 | import java.io.InputStream; |
| 54 | import java.net.URI; |
| 55 | import java.util.HashSet; |
| 56 | import java.util.Iterator; |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 57 | import java.util.Map; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 58 | import java.util.Set; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 59 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 60 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 61 | import static org.onlab.util.Tools.readTreeFromStream; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 62 | import static org.onosproject.net.HostId.hostId; |
| 63 | |
| 64 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 65 | * Manage inventory of end-station hosts. |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 66 | */ |
| 67 | @Path("hosts") |
| 68 | public class HostsWebResource extends AbstractWebResource { |
| 69 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 70 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 71 | private UriInfo uriInfo; |
| 72 | private static final String HOST_NOT_FOUND = "Host is not found"; |
Charles Chan | 7becb12 | 2020-01-14 16:29:43 -0800 | [diff] [blame] | 73 | private static final String[] REMOVAL_KEYS = {"mac", "vlan", "locations", "ipAddresses", |
| 74 | "auxLocations", "innerVlan", "outerTpid"}; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 75 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Get all end-station hosts. |
| 78 | * Returns array of all known end-station hosts. |
| 79 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 80 | * @return 200 OK with array of all known end-station hosts. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 81 | * @onos.rsModel Hosts |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 82 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 83 | @GET |
| 84 | @Produces(MediaType.APPLICATION_JSON) |
| 85 | public Response getHosts() { |
| 86 | final Iterable<Host> hosts = get(HostService.class).getHosts(); |
| 87 | final ObjectNode root = encodeArray(Host.class, "hosts", hosts); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 88 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Get details of end-station host. |
| 93 | * Returns detailed properties of the specified end-station host. |
| 94 | * |
| 95 | * @param id host identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 96 | * @return 200 OK with detailed properties of the specified end-station host |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 97 | * @onos.rsModel Host |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 98 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 99 | @GET |
| 100 | @Produces(MediaType.APPLICATION_JSON) |
| 101 | @Path("{id}") |
| 102 | public Response getHostById(@PathParam("id") String id) { |
| 103 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(id)), |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 104 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 105 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 106 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Get details of end-station host with MAC/VLAN. |
| 111 | * Returns detailed properties of the specified end-station host. |
| 112 | * |
| 113 | * @param mac host MAC address |
| 114 | * @param vlan host VLAN identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 115 | * @return 200 OK with detailed properties of the specified end-station host |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 116 | * @onos.rsModel Host |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 117 | */ |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 118 | @GET |
| 119 | @Produces(MediaType.APPLICATION_JSON) |
| 120 | @Path("{mac}/{vlan}") |
| 121 | public Response getHostByMacAndVlan(@PathParam("mac") String mac, |
| 122 | @PathParam("vlan") String vlan) { |
| 123 | final Host host = nullIsNotFound(get(HostService.class).getHost(hostId(mac + "/" + vlan)), |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 124 | HOST_NOT_FOUND); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 125 | final ObjectNode root = codec(Host.class).encode(host, this); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 126 | return ok(root).build(); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 127 | } |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Creates a new host based on JSON input and adds it to the current |
| 131 | * host inventory. |
| 132 | * |
| 133 | * @param stream input JSON |
| 134 | * @return status of the request - CREATED if the JSON is correct, |
| 135 | * BAD_REQUEST if the JSON is invalid |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 136 | * @onos.rsModel HostPut |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 137 | */ |
| 138 | @POST |
| 139 | @Consumes(MediaType.APPLICATION_JSON) |
| 140 | @Produces(MediaType.APPLICATION_JSON) |
| 141 | public Response createAndAddHost(InputStream stream) { |
| 142 | URI location; |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 143 | HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class); |
| 144 | InternalHostProvider hostProvider = new InternalHostProvider(); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 145 | try { |
| 146 | // Parse the input stream |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 147 | ObjectNode root = readTreeFromStream(mapper(), stream); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 148 | |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 149 | HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider); |
| 150 | hostProvider.setHostProviderService(hostProviderService); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 151 | HostId hostId = hostProvider.parseHost(root); |
| 152 | |
| 153 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 154 | .path("hosts") |
| 155 | .path(hostId.mac().toString()) |
| 156 | .path(hostId.vlanId().toString()); |
| 157 | location = locationBuilder.build(); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 158 | } catch (IOException ex) { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 159 | throw new IllegalArgumentException(ex); |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 160 | } finally { |
| 161 | hostProviderRegistry.unregister(hostProvider); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 162 | } |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 163 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 164 | return Response |
| 165 | .created(location) |
| 166 | .build(); |
| 167 | } |
| 168 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 169 | /** |
Thomas Vachuska | 58a7c34 | 2016-12-13 01:10:56 -0800 | [diff] [blame] | 170 | * Removes infrastructure device. |
| 171 | * Administratively deletes the specified device from the inventory of |
| 172 | * known devices. |
| 173 | * |
| 174 | * @param mac host MAC address |
| 175 | * @param vlan host VLAN identifier |
| 176 | * @return 204 OK |
| 177 | * @onos.rsModel Host |
| 178 | */ |
| 179 | @DELETE |
| 180 | @Path("{mac}/{vlan}") |
| 181 | @Produces(MediaType.APPLICATION_JSON) |
| 182 | public Response removeHost(@PathParam("mac") String mac, |
| 183 | @PathParam("vlan") String vlan) { |
| 184 | get(HostAdminService.class).removeHost(hostId(mac + "/" + vlan)); |
| 185 | return Response.noContent().build(); |
| 186 | } |
| 187 | |
| 188 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 189 | * Internal host provider that provides host events. |
| 190 | */ |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 191 | private final class InternalHostProvider implements HostProvider { |
| 192 | private final ProviderId providerId = |
Ray Milkey | 584fa13 | 2019-03-06 10:03:53 -0800 | [diff] [blame] | 193 | new ProviderId("host", "org.onosproject.rest"); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 194 | private HostProviderService hostProviderService; |
| 195 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Prevents from instantiation. |
| 198 | */ |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 199 | private InternalHostProvider() { |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 202 | public void triggerProbe(Host host) { |
| 203 | // 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] | 204 | } |
| 205 | |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 206 | public void setHostProviderService(HostProviderService service) { |
| 207 | this.hostProviderService = service; |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 210 | /* |
| 211 | * Return the ProviderId of "this" |
| 212 | */ |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 213 | public ProviderId id() { |
| 214 | return providerId; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * 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] | 219 | * |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 220 | * @param node JsonNode containing host information |
| 221 | * @return host ID of new host created |
| 222 | */ |
| 223 | private HostId parseHost(JsonNode node) { |
| 224 | MacAddress mac = MacAddress.valueOf(node.get("mac").asText()); |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 225 | VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 226 | |
Charles Chan | 7becb12 | 2020-01-14 16:29:43 -0800 | [diff] [blame] | 227 | // Parse locations |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 228 | if (null == node.get("locations")) { |
| 229 | throw new IllegalArgumentException("location isn't specified"); |
| 230 | } |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 231 | Iterator<JsonNode> locationNodes = node.get("locations").elements(); |
| 232 | Set<HostLocation> locations = new HashSet<>(); |
| 233 | while (locationNodes.hasNext()) { |
| 234 | JsonNode locationNode = locationNodes.next(); |
| 235 | String deviceAndPort = locationNode.get("elementId").asText() + "/" + |
| 236 | locationNode.get("port").asText(); |
| 237 | HostLocation hostLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0); |
| 238 | locations.add(hostLocation); |
| 239 | } |
| 240 | |
Charles Chan | 7becb12 | 2020-01-14 16:29:43 -0800 | [diff] [blame] | 241 | // Parse ipAddresses |
macauley_cheng | c4b6b41 | 2018-11-12 21:09:25 +0800 | [diff] [blame] | 242 | if (null == node.get("ipAddresses")) { |
| 243 | throw new IllegalArgumentException("ipAddress isn't specified"); |
| 244 | } |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 245 | Iterator<JsonNode> ipNodes = node.get("ipAddresses").elements(); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 246 | Set<IpAddress> ips = new HashSet<>(); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 247 | while (ipNodes.hasNext()) { |
| 248 | ips.add(IpAddress.valueOf(ipNodes.next().asText())); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 249 | } |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 250 | |
Charles Chan | 7becb12 | 2020-01-14 16:29:43 -0800 | [diff] [blame] | 251 | // Parse auxLocations |
| 252 | Set<HostLocation> auxLocations; |
| 253 | JsonNode auxLocationsNode = node.get("auxLocations"); |
| 254 | if (null == auxLocationsNode) { |
| 255 | auxLocations = null; |
| 256 | } else { |
| 257 | Iterator<JsonNode> auxLocationNodes = auxLocationsNode.elements(); |
| 258 | auxLocations = new HashSet<>(); |
| 259 | while (auxLocationNodes.hasNext()) { |
| 260 | JsonNode auxLocationNode = auxLocationNodes.next(); |
| 261 | String deviceAndPort = auxLocationNode.get("elementId").asText() + "/" + |
| 262 | auxLocationNode.get("port").asText(); |
| 263 | HostLocation auxLocation = new HostLocation(ConnectPoint.deviceConnectPoint(deviceAndPort), 0); |
| 264 | auxLocations.add(auxLocation); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Parse innerVlan |
| 269 | JsonNode innerVlanNode = node.get("innerVlan"); |
| 270 | VlanId innerVlan = (null == innerVlanNode) ? VlanId.NONE : VlanId.vlanId(innerVlanNode.asText()); |
| 271 | |
| 272 | // Parse outerTpid |
| 273 | JsonNode outerTpidNode = node.get("outerTpid"); |
| 274 | EthType outerTpid = (null == outerTpidNode) ? EthType.EtherType.UNKNOWN.ethType() : |
| 275 | EthType.EtherType.lookup((short) (Integer.decode(outerTpidNode.asText()) & 0xFFFF)).ethType(); |
| 276 | |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 277 | // try to remove elements from json node after reading them |
| 278 | SparseAnnotations annotations = annotations(removeElements(node, REMOVAL_KEYS)); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 279 | // Update host inventory |
| 280 | |
| 281 | HostId hostId = HostId.hostId(mac, vlanId); |
Charles Chan | 7becb12 | 2020-01-14 16:29:43 -0800 | [diff] [blame] | 282 | DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, locations, auxLocations, |
| 283 | ips, innerVlan, outerTpid, true, annotations); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 284 | hostProviderService.hostDetected(hostId, desc, false); |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 285 | return hostId; |
| 286 | } |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 287 | |
| 288 | /** |
Jian Li | 43c956b | 2015-11-26 03:02:46 -0800 | [diff] [blame] | 289 | * Remove a set of elements from JsonNode by specifying keys. |
| 290 | * |
| 291 | * @param node JsonNode containing host information |
| 292 | * @param removalKeys key of elements that need to be removed |
| 293 | * @return removal keys |
| 294 | */ |
| 295 | private JsonNode removeElements(JsonNode node, String[] removalKeys) { |
| 296 | ObjectMapper mapper = new ObjectMapper(); |
| 297 | Map<String, Object> map = mapper.convertValue(node, Map.class); |
| 298 | for (String key : removalKeys) { |
| 299 | map.remove(key); |
| 300 | } |
| 301 | return mapper.convertValue(map, JsonNode.class); |
| 302 | } |
| 303 | |
| 304 | /** |
Kedar Gupta | 7c4d196 | 2015-08-03 10:46:04 -0700 | [diff] [blame] | 305 | * Produces annotations from specified JsonNode. Copied from the ConfigProvider |
| 306 | * class for use in the POST method. |
| 307 | * |
| 308 | * @param node node to be annotated |
| 309 | * @return SparseAnnotations object with information about node |
| 310 | */ |
| 311 | private SparseAnnotations annotations(JsonNode node) { |
| 312 | if (node == null) { |
| 313 | return DefaultAnnotations.EMPTY; |
| 314 | } |
| 315 | |
| 316 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 317 | Iterator<String> it = node.fieldNames(); |
| 318 | while (it.hasNext()) { |
| 319 | String k = it.next(); |
| 320 | builder.set(k, node.get(k).asText()); |
| 321 | } |
| 322 | return builder.build(); |
| 323 | } |
| 324 | |
Kedar Gupta | 47bd480 | 2015-07-15 09:43:26 -0700 | [diff] [blame] | 325 | } |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 326 | } |
| 327 | |