blob: 073543a84687a9fa22897184a9d71d5e544dd736 [file] [log] [blame]
Claudine Chiufb8b8162016-04-01 23:50:51 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Claudine Chiufb8b8162016-04-01 23:50:51 +00003 *
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 */
16
17package org.onosproject.rest.resources;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.incubator.net.virtual.NetworkId;
22import org.onosproject.incubator.net.virtual.TenantId;
23import org.onosproject.incubator.net.virtual.VirtualDevice;
Claudine Chiu25f07be2016-06-27 16:21:21 +000024import org.onosproject.incubator.net.virtual.VirtualHost;
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000025import org.onosproject.incubator.net.virtual.VirtualLink;
Claudine Chiufb8b8162016-04-01 23:50:51 +000026import org.onosproject.incubator.net.virtual.VirtualNetwork;
27import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
28import org.onosproject.incubator.net.virtual.VirtualNetworkService;
29import org.onosproject.incubator.net.virtual.VirtualPort;
Yoonseon Han6c603892016-09-01 11:52:21 -070030import org.onosproject.net.ConnectPoint;
Claudine Chiufb8b8162016-04-01 23:50:51 +000031import org.onosproject.net.DeviceId;
Claudine Chiufb8b8162016-04-01 23:50:51 +000032import org.onosproject.net.PortNumber;
33import org.onosproject.rest.AbstractWebResource;
34
35import javax.ws.rs.Consumes;
36import javax.ws.rs.DELETE;
37import javax.ws.rs.GET;
38import javax.ws.rs.POST;
39import javax.ws.rs.Path;
40import javax.ws.rs.PathParam;
41import javax.ws.rs.Produces;
42import javax.ws.rs.core.Context;
43import javax.ws.rs.core.MediaType;
44import javax.ws.rs.core.Response;
45import javax.ws.rs.core.UriBuilder;
46import javax.ws.rs.core.UriInfo;
47import java.io.IOException;
48import java.io.InputStream;
49import java.util.Collection;
50import java.util.List;
51import java.util.Set;
52import java.util.stream.Collectors;
53
54/**
55 * Query and Manage Virtual Network elements.
56 */
57@Path("vnets")
58public class VirtualNetworkWebResource extends AbstractWebResource {
59
60 private static final String MISSING_FIELD = "Missing ";
61 private static final String INVALID_FIELD = "Invalid ";
62
63 private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class);
64 private final VirtualNetworkService vnetService = get(VirtualNetworkService.class);
65
66 @Context
Jian Licc730a62016-05-10 16:36:16 -070067 private UriInfo uriInfo;
Claudine Chiufb8b8162016-04-01 23:50:51 +000068
69 // VirtualNetwork
Claudine Chiufb8b8162016-04-01 23:50:51 +000070
71 /**
72 * Returns all virtual networks.
73 *
Jian Licc730a62016-05-10 16:36:16 -070074 * @return 200 OK with set of virtual networks
Claudine Chiu1decd532016-04-19 18:30:01 +000075 * @onos.rsModel VirtualNetworks
Claudine Chiufb8b8162016-04-01 23:50:51 +000076 */
77 @GET
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response getVirtualNetworks() {
80 Set<TenantId> tenantIds = vnetAdminService.getTenantIds();
81 List<VirtualNetwork> allVnets = tenantIds.stream()
82 .map(tenantId -> vnetService.getVirtualNetworks(tenantId))
83 .flatMap(Collection::stream)
84 .collect(Collectors.toList());
85 return ok(encodeArray(VirtualNetwork.class, "vnets", allVnets)).build();
86 }
87
88 /**
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000089 * Returns the virtual networks with the specified tenant identifier.
90 *
91 * @param tenantId tenant identifier
Jian Licc730a62016-05-10 16:36:16 -070092 * @return 200 OK with a virtual network, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +000093 * @onos.rsModel VirtualNetworks
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000094 */
95 @GET
96 @Produces(MediaType.APPLICATION_JSON)
97 @Path("{tenantId}")
98 public Response getVirtualNetworkById(@PathParam("tenantId") String tenantId) {
99 final TenantId existingTid = TenantWebResource.getExistingTenantId(vnetAdminService,
Brian Stanke9a108972016-04-11 15:25:17 -0400100 TenantId.tenantId(tenantId));
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000101 Set<VirtualNetwork> vnets = vnetService.getVirtualNetworks(existingTid);
102 return ok(encodeArray(VirtualNetwork.class, "vnets", vnets)).build();
103 }
104
105 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000106 * Creates a virtual network from the JSON input stream.
107 *
Claudine Chiu1decd532016-04-19 18:30:01 +0000108 * @param stream tenant identifier JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000109 * @return status of the request - CREATED if the JSON is correct,
110 * BAD_REQUEST if the JSON is invalid
111 * @onos.rsModel TenantId
112 */
113 @POST
114 @Consumes(MediaType.APPLICATION_JSON)
115 @Produces(MediaType.APPLICATION_JSON)
116 public Response createVirtualNetwork(InputStream stream) {
117 try {
118 final TenantId tid = TenantId.tenantId(getFromJsonStream(stream, "id").asText());
119 VirtualNetwork newVnet = vnetAdminService.createVirtualNetwork(tid);
120 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
121 .path("vnets")
122 .path(newVnet.id().toString());
123 return Response
124 .created(locationBuilder.build())
125 .build();
126 } catch (IOException e) {
127 throw new IllegalArgumentException(e);
128 }
129 }
130
131 /**
132 * Removes the virtual network with the specified network identifier.
133 *
134 * @param networkId network identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700135 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000136 */
137 @DELETE
138 @Path("{networkId}")
139 public Response removeVirtualNetwork(@PathParam("networkId") long networkId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000140 NetworkId nid = NetworkId.networkId(networkId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000141 vnetAdminService.removeVirtualNetwork(nid);
Jian Lic2a542b2016-05-10 11:48:19 -0700142 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000143 }
144
145 // VirtualDevice
146
147 /**
148 * Returns all virtual network devices in a virtual network.
149 *
150 * @param networkId network identifier
Jian Licc730a62016-05-10 16:36:16 -0700151 * @return 200 OK with set of virtual devices, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +0000152 * @onos.rsModel VirtualDevices
Claudine Chiufb8b8162016-04-01 23:50:51 +0000153 */
154 @GET
155 @Produces(MediaType.APPLICATION_JSON)
156 @Path("{networkId}/devices")
157 public Response getVirtualDevices(@PathParam("networkId") long networkId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000158 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400159 Set<VirtualDevice> vdevs = vnetService.getVirtualDevices(nid);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000160 return ok(encodeArray(VirtualDevice.class, "devices", vdevs)).build();
161 }
162
163 /**
164 * Creates a virtual device from the JSON input stream.
165 *
166 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000167 * @param stream virtual device JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000168 * @return status of the request - CREATED if the JSON is correct,
169 * BAD_REQUEST if the JSON is invalid
170 * @onos.rsModel VirtualDevice
171 */
172 @POST
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000173 @Path("{networkId}/devices")
Claudine Chiufb8b8162016-04-01 23:50:51 +0000174 @Consumes(MediaType.APPLICATION_JSON)
175 @Produces(MediaType.APPLICATION_JSON)
176 public Response createVirtualDevice(@PathParam("networkId") long networkId,
177 InputStream stream) {
178 try {
179 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
180 final VirtualDevice vdevReq = codec(VirtualDevice.class).decode(jsonTree, this);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000181 JsonNode specifiedNetworkId = jsonTree.get("networkId");
182 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000183 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
184 }
185 final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(),
186 vdevReq.id());
187 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000188 .path("vnets").path(specifiedNetworkId.asText())
Claudine Chiufb8b8162016-04-01 23:50:51 +0000189 .path("devices").path(vdevRes.id().toString());
190 return Response
191 .created(locationBuilder.build())
192 .build();
193 } catch (IOException e) {
194 throw new IllegalArgumentException(e);
195 }
196 }
197
198 /**
199 * Removes the virtual network device from the virtual network.
200 *
201 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400202 * @param deviceId device identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700203 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000204 */
205 @DELETE
206 @Path("{networkId}/devices/{deviceId}")
207 public Response removeVirtualDevice(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400208 @PathParam("deviceId") String deviceId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000209 NetworkId nid = NetworkId.networkId(networkId);
210 DeviceId did = DeviceId.deviceId(deviceId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000211 vnetAdminService.removeVirtualDevice(nid, did);
Jian Lic2a542b2016-05-10 11:48:19 -0700212 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000213 }
214
215 // VirtualPort
216
217 /**
218 * Returns all virtual network ports in a virtual device in a virtual network.
219 *
220 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400221 * @param deviceId virtual device identifier
Jian Licc730a62016-05-10 16:36:16 -0700222 * @return 200 OK with set of virtual ports, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +0000223 * @onos.rsModel VirtualPorts
Claudine Chiufb8b8162016-04-01 23:50:51 +0000224 */
225 @GET
226 @Produces(MediaType.APPLICATION_JSON)
227 @Path("{networkId}/devices/{deviceId}/ports")
228 public Response getVirtualPorts(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400229 @PathParam("deviceId") String deviceId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000230 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400231 Iterable<VirtualPort> vports = vnetService.getVirtualPorts(nid, DeviceId.deviceId(deviceId));
Claudine Chiufb8b8162016-04-01 23:50:51 +0000232 return ok(encodeArray(VirtualPort.class, "ports", vports)).build();
233 }
234
235 /**
236 * Creates a virtual network port in a virtual device in a virtual network.
237 *
Brian Stanke9a108972016-04-11 15:25:17 -0400238 * @param networkId network identifier
Claudine Chiufb8b8162016-04-01 23:50:51 +0000239 * @param virtDeviceId virtual device identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000240 * @param stream virtual port JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000241 * @return status of the request - CREATED if the JSON is correct,
242 * BAD_REQUEST if the JSON is invalid
243 * @onos.rsModel VirtualPort
244 */
245 @POST
246 @Consumes(MediaType.APPLICATION_JSON)
247 @Produces(MediaType.APPLICATION_JSON)
248 @Path("{networkId}/devices/{deviceId}/ports")
249 public Response createVirtualPort(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400250 @PathParam("deviceId") String virtDeviceId,
251 InputStream stream) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000252 try {
253 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
254// final VirtualPort vportReq = codec(VirtualPort.class).decode(jsonTree, this);
255 JsonNode specifiedNetworkId = jsonTree.get("networkId");
256 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000257 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000258 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
259 }
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000260 if (specifiedDeviceId == null || !specifiedDeviceId.asText().equals(virtDeviceId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000261 throw new IllegalArgumentException(INVALID_FIELD + "deviceId");
262 }
263 JsonNode specifiedPortNum = jsonTree.get("portNum");
264 JsonNode specifiedPhysDeviceId = jsonTree.get("physDeviceId");
265 JsonNode specifiedPhysPortNum = jsonTree.get("physPortNum");
266 final NetworkId nid = NetworkId.networkId(networkId);
267 DeviceId vdevId = DeviceId.deviceId(virtDeviceId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700268
269 ConnectPoint realizedBy = new ConnectPoint(DeviceId.deviceId(specifiedPhysDeviceId.asText()),
270 PortNumber.portNumber(specifiedPhysPortNum.asText()));
Claudine Chiufb8b8162016-04-01 23:50:51 +0000271 VirtualPort vport = vnetAdminService.createVirtualPort(nid, vdevId,
Brian Stanke9a108972016-04-11 15:25:17 -0400272 PortNumber.portNumber(specifiedPortNum.asText()), realizedBy);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000273 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
274 .path("vnets").path(specifiedNetworkId.asText())
275 .path("devices").path(specifiedDeviceId.asText())
276 .path("ports").path(vport.number().toString());
277 return Response
278 .created(locationBuilder.build())
279 .build();
280 } catch (IOException e) {
281 throw new IllegalArgumentException(e);
282 }
283 }
284
285 /**
286 * Removes the virtual network port from the virtual device in a virtual network.
287 *
288 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400289 * @param deviceId virtual device identifier
290 * @param portNum virtual port number
Jian Lic2a542b2016-05-10 11:48:19 -0700291 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000292 */
293 @DELETE
294 @Path("{networkId}/devices/{deviceId}/ports/{portNum}")
295 public Response removeVirtualPort(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400296 @PathParam("deviceId") String deviceId,
297 @PathParam("portNum") long portNum) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000298 NetworkId nid = NetworkId.networkId(networkId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000299 vnetAdminService.removeVirtualPort(nid, DeviceId.deviceId(deviceId),
Brian Stanke9a108972016-04-11 15:25:17 -0400300 PortNumber.portNumber(portNum));
Jian Lic2a542b2016-05-10 11:48:19 -0700301 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000302 }
303
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000304 // VirtualLink
305
306 /**
307 * Returns all virtual network links in a virtual network.
308 *
309 * @param networkId network identifier
Jian Licc730a62016-05-10 16:36:16 -0700310 * @return 200 OK with set of virtual network links
Claudine Chiu1decd532016-04-19 18:30:01 +0000311 * @onos.rsModel VirtualLinks
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000312 */
313 @GET
314 @Produces(MediaType.APPLICATION_JSON)
315 @Path("{networkId}/links")
316 public Response getVirtualLinks(@PathParam("networkId") long networkId) {
317 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400318 Set<VirtualLink> vlinks = vnetService.getVirtualLinks(nid);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000319 return ok(encodeArray(VirtualLink.class, "links", vlinks)).build();
320 }
321
Brian Stanke9a108972016-04-11 15:25:17 -0400322 /**
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000323 * Creates a virtual network link from the JSON input stream.
324 *
325 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000326 * @param stream virtual link JSON stream
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000327 * @return status of the request - CREATED if the JSON is correct,
328 * BAD_REQUEST if the JSON is invalid
329 * @onos.rsModel VirtualLink
330 */
331 @POST
332 @Path("{networkId}/links")
333 @Consumes(MediaType.APPLICATION_JSON)
334 @Produces(MediaType.APPLICATION_JSON)
335 public Response createVirtualLink(@PathParam("networkId") long networkId,
336 InputStream stream) {
337 try {
338 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
339 JsonNode specifiedNetworkId = jsonTree.get("networkId");
340 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
341 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
342 }
343 final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000344 vnetAdminService.createVirtualLink(vlinkReq.networkId(),
Brian Stanke9a108972016-04-11 15:25:17 -0400345 vlinkReq.src(), vlinkReq.dst());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000346 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
347 .path("vnets").path(specifiedNetworkId.asText())
348 .path("links");
349 return Response
350 .created(locationBuilder.build())
351 .build();
352 } catch (IOException e) {
353 throw new IllegalArgumentException(e);
354 }
355 }
356
357 /**
358 * Removes the virtual network link from the JSON input stream.
359 *
360 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000361 * @param stream virtual link JSON stream
Jian Lic2a542b2016-05-10 11:48:19 -0700362 * @return 204 NO CONTENT
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000363 * @onos.rsModel VirtualLink
364 */
365 @DELETE
366 @Path("{networkId}/links")
367 @Consumes(MediaType.APPLICATION_JSON)
368 public Response removeVirtualLink(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400369 InputStream stream) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000370 try {
371 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
372 JsonNode specifiedNetworkId = jsonTree.get("networkId");
373 if (specifiedNetworkId != null &&
374 specifiedNetworkId.asLong() != (networkId)) {
375 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
376 }
377 final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this);
378 vnetAdminService.removeVirtualLink(vlinkReq.networkId(),
Brian Stanke9a108972016-04-11 15:25:17 -0400379 vlinkReq.src(), vlinkReq.dst());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000380 } catch (IOException e) {
381 throw new IllegalArgumentException(e);
382 }
383
Jian Lic2a542b2016-05-10 11:48:19 -0700384 return Response.noContent().build();
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000385 }
386
387 /**
Claudine Chiu25f07be2016-06-27 16:21:21 +0000388 * Returns all virtual network hosts in a virtual network.
389 *
390 * @param networkId network identifier
391 * @return 200 OK with set of virtual network hosts
392 * @onos.rsModel VirtualHosts
393 */
394 @GET
395 @Produces(MediaType.APPLICATION_JSON)
396 @Path("{networkId}/hosts")
397 public Response getVirtualHosts(@PathParam("networkId") long networkId) {
398 NetworkId nid = NetworkId.networkId(networkId);
399 Set<VirtualHost> vhosts = vnetService.getVirtualHosts(nid);
400 return ok(encodeArray(VirtualHost.class, "hosts", vhosts)).build();
401 }
402
403 /**
404 * Creates a virtual network host from the JSON input stream.
405 *
406 * @param networkId network identifier
407 * @param stream virtual host JSON stream
408 * @return status of the request - CREATED if the JSON is correct,
409 * BAD_REQUEST if the JSON is invalid
410 * @onos.rsModel VirtualHostPut
411 */
412 @POST
413 @Path("{networkId}/hosts")
414 @Consumes(MediaType.APPLICATION_JSON)
415 @Produces(MediaType.APPLICATION_JSON)
416 public Response createVirtualHost(@PathParam("networkId") long networkId,
417 InputStream stream) {
418 try {
419 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
420 JsonNode specifiedNetworkId = jsonTree.get("networkId");
421 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
422 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
423 }
424 final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
425 vnetAdminService.createVirtualHost(vhostReq.networkId(), vhostReq.id(),
426 vhostReq.mac(), vhostReq.vlan(),
427 vhostReq.location(), vhostReq.ipAddresses());
428 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
429 .path("vnets").path(specifiedNetworkId.asText())
430 .path("hosts");
431 return Response
432 .created(locationBuilder.build())
433 .build();
434 } catch (IOException e) {
435 throw new IllegalArgumentException(e);
436 }
437 }
438
439 /**
440 * Removes the virtual network host from the JSON input stream.
441 *
442 * @param networkId network identifier
443 * @param stream virtual host JSON stream
444 * @return 204 NO CONTENT
445 * @onos.rsModel VirtualHost
446 */
447 @DELETE
448 @Path("{networkId}/hosts")
449 @Consumes(MediaType.APPLICATION_JSON)
450 public Response removeVirtualHost(@PathParam("networkId") long networkId,
451 InputStream stream) {
452 try {
453 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
454 JsonNode specifiedNetworkId = jsonTree.get("networkId");
455 if (specifiedNetworkId != null &&
456 specifiedNetworkId.asLong() != (networkId)) {
457 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
458 }
459 final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
460 vnetAdminService.removeVirtualHost(vhostReq.networkId(), vhostReq.id());
461 } catch (IOException e) {
462 throw new IllegalArgumentException(e);
463 }
464
465 return Response.noContent().build();
466 }
467
468 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000469 * Get the tenant identifier from the JSON stream.
470 *
Brian Stanke9a108972016-04-11 15:25:17 -0400471 * @param stream TenantId JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000472 * @param jsonFieldName field name
473 * @return JsonNode
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000474 * @throws IOException if unable to parse the request
Claudine Chiufb8b8162016-04-01 23:50:51 +0000475 */
476 private JsonNode getFromJsonStream(InputStream stream, String jsonFieldName) throws IOException {
477 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
478 JsonNode jsonNode = jsonTree.get(jsonFieldName);
479
480 if (jsonNode == null) {
481 throw new IllegalArgumentException(MISSING_FIELD + jsonFieldName);
482 }
483 return jsonNode;
484 }
485}