blob: dca328d448673d137ff6df1d1d2f89726812cdcb [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;
30import org.onosproject.net.DefaultAnnotations;
31import org.onosproject.net.DefaultDevice;
32import org.onosproject.net.DefaultPort;
33import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Port;
36import org.onosproject.net.PortNumber;
37import org.onosproject.rest.AbstractWebResource;
38
39import javax.ws.rs.Consumes;
40import javax.ws.rs.DELETE;
41import javax.ws.rs.GET;
42import javax.ws.rs.POST;
43import javax.ws.rs.Path;
44import javax.ws.rs.PathParam;
45import javax.ws.rs.Produces;
46import javax.ws.rs.core.Context;
47import javax.ws.rs.core.MediaType;
48import javax.ws.rs.core.Response;
49import javax.ws.rs.core.UriBuilder;
50import javax.ws.rs.core.UriInfo;
51import java.io.IOException;
52import java.io.InputStream;
53import java.util.Collection;
54import java.util.List;
55import java.util.Set;
56import java.util.stream.Collectors;
57
58/**
59 * Query and Manage Virtual Network elements.
60 */
61@Path("vnets")
62public class VirtualNetworkWebResource extends AbstractWebResource {
63
64 private static final String MISSING_FIELD = "Missing ";
65 private static final String INVALID_FIELD = "Invalid ";
66
67 private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class);
68 private final VirtualNetworkService vnetService = get(VirtualNetworkService.class);
69
70 @Context
Jian Licc730a62016-05-10 16:36:16 -070071 private UriInfo uriInfo;
Claudine Chiufb8b8162016-04-01 23:50:51 +000072
73 // VirtualNetwork
Claudine Chiufb8b8162016-04-01 23:50:51 +000074
75 /**
76 * Returns all virtual networks.
77 *
Jian Licc730a62016-05-10 16:36:16 -070078 * @return 200 OK with set of virtual networks
Claudine Chiu1decd532016-04-19 18:30:01 +000079 * @onos.rsModel VirtualNetworks
Claudine Chiufb8b8162016-04-01 23:50:51 +000080 */
81 @GET
82 @Produces(MediaType.APPLICATION_JSON)
83 public Response getVirtualNetworks() {
84 Set<TenantId> tenantIds = vnetAdminService.getTenantIds();
85 List<VirtualNetwork> allVnets = tenantIds.stream()
86 .map(tenantId -> vnetService.getVirtualNetworks(tenantId))
87 .flatMap(Collection::stream)
88 .collect(Collectors.toList());
89 return ok(encodeArray(VirtualNetwork.class, "vnets", allVnets)).build();
90 }
91
92 /**
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000093 * Returns the virtual networks with the specified tenant identifier.
94 *
95 * @param tenantId tenant identifier
Jian Licc730a62016-05-10 16:36:16 -070096 * @return 200 OK with a virtual network, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +000097 * @onos.rsModel VirtualNetworks
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000098 */
99 @GET
100 @Produces(MediaType.APPLICATION_JSON)
101 @Path("{tenantId}")
102 public Response getVirtualNetworkById(@PathParam("tenantId") String tenantId) {
103 final TenantId existingTid = TenantWebResource.getExistingTenantId(vnetAdminService,
Brian Stanke9a108972016-04-11 15:25:17 -0400104 TenantId.tenantId(tenantId));
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000105 Set<VirtualNetwork> vnets = vnetService.getVirtualNetworks(existingTid);
106 return ok(encodeArray(VirtualNetwork.class, "vnets", vnets)).build();
107 }
108
109 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000110 * Creates a virtual network from the JSON input stream.
111 *
Claudine Chiu1decd532016-04-19 18:30:01 +0000112 * @param stream tenant identifier JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000113 * @return status of the request - CREATED if the JSON is correct,
114 * BAD_REQUEST if the JSON is invalid
115 * @onos.rsModel TenantId
116 */
117 @POST
118 @Consumes(MediaType.APPLICATION_JSON)
119 @Produces(MediaType.APPLICATION_JSON)
120 public Response createVirtualNetwork(InputStream stream) {
121 try {
122 final TenantId tid = TenantId.tenantId(getFromJsonStream(stream, "id").asText());
123 VirtualNetwork newVnet = vnetAdminService.createVirtualNetwork(tid);
124 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
125 .path("vnets")
126 .path(newVnet.id().toString());
127 return Response
128 .created(locationBuilder.build())
129 .build();
130 } catch (IOException e) {
131 throw new IllegalArgumentException(e);
132 }
133 }
134
135 /**
136 * Removes the virtual network with the specified network identifier.
137 *
138 * @param networkId network identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700139 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000140 */
141 @DELETE
142 @Path("{networkId}")
143 public Response removeVirtualNetwork(@PathParam("networkId") long networkId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000144 NetworkId nid = NetworkId.networkId(networkId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000145 vnetAdminService.removeVirtualNetwork(nid);
Jian Lic2a542b2016-05-10 11:48:19 -0700146 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000147 }
148
149 // VirtualDevice
150
151 /**
152 * Returns all virtual network devices in a virtual network.
153 *
154 * @param networkId network identifier
Jian Licc730a62016-05-10 16:36:16 -0700155 * @return 200 OK with set of virtual devices, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +0000156 * @onos.rsModel VirtualDevices
Claudine Chiufb8b8162016-04-01 23:50:51 +0000157 */
158 @GET
159 @Produces(MediaType.APPLICATION_JSON)
160 @Path("{networkId}/devices")
161 public Response getVirtualDevices(@PathParam("networkId") long networkId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000162 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400163 Set<VirtualDevice> vdevs = vnetService.getVirtualDevices(nid);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000164 return ok(encodeArray(VirtualDevice.class, "devices", vdevs)).build();
165 }
166
167 /**
168 * Creates a virtual device from the JSON input stream.
169 *
170 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000171 * @param stream virtual device JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000172 * @return status of the request - CREATED if the JSON is correct,
173 * BAD_REQUEST if the JSON is invalid
174 * @onos.rsModel VirtualDevice
175 */
176 @POST
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000177 @Path("{networkId}/devices")
Claudine Chiufb8b8162016-04-01 23:50:51 +0000178 @Consumes(MediaType.APPLICATION_JSON)
179 @Produces(MediaType.APPLICATION_JSON)
180 public Response createVirtualDevice(@PathParam("networkId") long networkId,
181 InputStream stream) {
182 try {
183 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
184 final VirtualDevice vdevReq = codec(VirtualDevice.class).decode(jsonTree, this);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000185 JsonNode specifiedNetworkId = jsonTree.get("networkId");
186 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000187 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
188 }
189 final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(),
190 vdevReq.id());
191 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000192 .path("vnets").path(specifiedNetworkId.asText())
Claudine Chiufb8b8162016-04-01 23:50:51 +0000193 .path("devices").path(vdevRes.id().toString());
194 return Response
195 .created(locationBuilder.build())
196 .build();
197 } catch (IOException e) {
198 throw new IllegalArgumentException(e);
199 }
200 }
201
202 /**
203 * Removes the virtual network device from the virtual network.
204 *
205 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400206 * @param deviceId device identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700207 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000208 */
209 @DELETE
210 @Path("{networkId}/devices/{deviceId}")
211 public Response removeVirtualDevice(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400212 @PathParam("deviceId") String deviceId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000213 NetworkId nid = NetworkId.networkId(networkId);
214 DeviceId did = DeviceId.deviceId(deviceId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000215 vnetAdminService.removeVirtualDevice(nid, did);
Jian Lic2a542b2016-05-10 11:48:19 -0700216 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000217 }
218
219 // VirtualPort
220
221 /**
222 * Returns all virtual network ports in a virtual device in a virtual network.
223 *
224 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400225 * @param deviceId virtual device identifier
Jian Licc730a62016-05-10 16:36:16 -0700226 * @return 200 OK with set of virtual ports, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +0000227 * @onos.rsModel VirtualPorts
Claudine Chiufb8b8162016-04-01 23:50:51 +0000228 */
229 @GET
230 @Produces(MediaType.APPLICATION_JSON)
231 @Path("{networkId}/devices/{deviceId}/ports")
232 public Response getVirtualPorts(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400233 @PathParam("deviceId") String deviceId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000234 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400235 Iterable<VirtualPort> vports = vnetService.getVirtualPorts(nid, DeviceId.deviceId(deviceId));
Claudine Chiufb8b8162016-04-01 23:50:51 +0000236 return ok(encodeArray(VirtualPort.class, "ports", vports)).build();
237 }
238
239 /**
240 * Creates a virtual network port in a virtual device in a virtual network.
241 *
Brian Stanke9a108972016-04-11 15:25:17 -0400242 * @param networkId network identifier
Claudine Chiufb8b8162016-04-01 23:50:51 +0000243 * @param virtDeviceId virtual device identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000244 * @param stream virtual port JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000245 * @return status of the request - CREATED if the JSON is correct,
246 * BAD_REQUEST if the JSON is invalid
247 * @onos.rsModel VirtualPort
248 */
249 @POST
250 @Consumes(MediaType.APPLICATION_JSON)
251 @Produces(MediaType.APPLICATION_JSON)
252 @Path("{networkId}/devices/{deviceId}/ports")
253 public Response createVirtualPort(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400254 @PathParam("deviceId") String virtDeviceId,
255 InputStream stream) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000256 try {
257 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
258// final VirtualPort vportReq = codec(VirtualPort.class).decode(jsonTree, this);
259 JsonNode specifiedNetworkId = jsonTree.get("networkId");
260 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000261 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000262 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
263 }
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000264 if (specifiedDeviceId == null || !specifiedDeviceId.asText().equals(virtDeviceId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000265 throw new IllegalArgumentException(INVALID_FIELD + "deviceId");
266 }
267 JsonNode specifiedPortNum = jsonTree.get("portNum");
268 JsonNode specifiedPhysDeviceId = jsonTree.get("physDeviceId");
269 JsonNode specifiedPhysPortNum = jsonTree.get("physPortNum");
270 final NetworkId nid = NetworkId.networkId(networkId);
271 DeviceId vdevId = DeviceId.deviceId(virtDeviceId);
272 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
273 Device physDevice = new DefaultDevice(null, DeviceId.deviceId(specifiedPhysDeviceId.asText()),
Brian Stanke9a108972016-04-11 15:25:17 -0400274 null, null, null, null, null, null, annotations);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000275 Port realizedBy = new DefaultPort(physDevice,
Brian Stanke9a108972016-04-11 15:25:17 -0400276 PortNumber.portNumber(specifiedPhysPortNum.asText()), true);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000277 VirtualPort vport = vnetAdminService.createVirtualPort(nid, vdevId,
Brian Stanke9a108972016-04-11 15:25:17 -0400278 PortNumber.portNumber(specifiedPortNum.asText()), realizedBy);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000279 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
280 .path("vnets").path(specifiedNetworkId.asText())
281 .path("devices").path(specifiedDeviceId.asText())
282 .path("ports").path(vport.number().toString());
283 return Response
284 .created(locationBuilder.build())
285 .build();
286 } catch (IOException e) {
287 throw new IllegalArgumentException(e);
288 }
289 }
290
291 /**
292 * Removes the virtual network port from the virtual device in a virtual network.
293 *
294 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400295 * @param deviceId virtual device identifier
296 * @param portNum virtual port number
Jian Lic2a542b2016-05-10 11:48:19 -0700297 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000298 */
299 @DELETE
300 @Path("{networkId}/devices/{deviceId}/ports/{portNum}")
301 public Response removeVirtualPort(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400302 @PathParam("deviceId") String deviceId,
303 @PathParam("portNum") long portNum) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000304 NetworkId nid = NetworkId.networkId(networkId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000305 vnetAdminService.removeVirtualPort(nid, DeviceId.deviceId(deviceId),
Brian Stanke9a108972016-04-11 15:25:17 -0400306 PortNumber.portNumber(portNum));
Jian Lic2a542b2016-05-10 11:48:19 -0700307 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000308 }
309
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000310 // VirtualLink
311
312 /**
313 * Returns all virtual network links in a virtual network.
314 *
315 * @param networkId network identifier
Jian Licc730a62016-05-10 16:36:16 -0700316 * @return 200 OK with set of virtual network links
Claudine Chiu1decd532016-04-19 18:30:01 +0000317 * @onos.rsModel VirtualLinks
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000318 */
319 @GET
320 @Produces(MediaType.APPLICATION_JSON)
321 @Path("{networkId}/links")
322 public Response getVirtualLinks(@PathParam("networkId") long networkId) {
323 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400324 Set<VirtualLink> vlinks = vnetService.getVirtualLinks(nid);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000325 return ok(encodeArray(VirtualLink.class, "links", vlinks)).build();
326 }
327
Brian Stanke9a108972016-04-11 15:25:17 -0400328 /**
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000329 * Creates a virtual network link from the JSON input stream.
330 *
331 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000332 * @param stream virtual link JSON stream
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000333 * @return status of the request - CREATED if the JSON is correct,
334 * BAD_REQUEST if the JSON is invalid
335 * @onos.rsModel VirtualLink
336 */
337 @POST
338 @Path("{networkId}/links")
339 @Consumes(MediaType.APPLICATION_JSON)
340 @Produces(MediaType.APPLICATION_JSON)
341 public Response createVirtualLink(@PathParam("networkId") long networkId,
342 InputStream stream) {
343 try {
344 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
345 JsonNode specifiedNetworkId = jsonTree.get("networkId");
346 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
347 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
348 }
349 final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000350 vnetAdminService.createVirtualLink(vlinkReq.networkId(),
Brian Stanke9a108972016-04-11 15:25:17 -0400351 vlinkReq.src(), vlinkReq.dst());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000352 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
353 .path("vnets").path(specifiedNetworkId.asText())
354 .path("links");
355 return Response
356 .created(locationBuilder.build())
357 .build();
358 } catch (IOException e) {
359 throw new IllegalArgumentException(e);
360 }
361 }
362
363 /**
364 * Removes the virtual network link from the JSON input stream.
365 *
366 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000367 * @param stream virtual link JSON stream
Jian Lic2a542b2016-05-10 11:48:19 -0700368 * @return 204 NO CONTENT
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000369 * @onos.rsModel VirtualLink
370 */
371 @DELETE
372 @Path("{networkId}/links")
373 @Consumes(MediaType.APPLICATION_JSON)
374 public Response removeVirtualLink(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400375 InputStream stream) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000376 try {
377 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
378 JsonNode specifiedNetworkId = jsonTree.get("networkId");
379 if (specifiedNetworkId != null &&
380 specifiedNetworkId.asLong() != (networkId)) {
381 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
382 }
383 final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this);
384 vnetAdminService.removeVirtualLink(vlinkReq.networkId(),
Brian Stanke9a108972016-04-11 15:25:17 -0400385 vlinkReq.src(), vlinkReq.dst());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000386 } catch (IOException e) {
387 throw new IllegalArgumentException(e);
388 }
389
Jian Lic2a542b2016-05-10 11:48:19 -0700390 return Response.noContent().build();
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000391 }
392
393 /**
Claudine Chiu25f07be2016-06-27 16:21:21 +0000394 * Returns all virtual network hosts in a virtual network.
395 *
396 * @param networkId network identifier
397 * @return 200 OK with set of virtual network hosts
398 * @onos.rsModel VirtualHosts
399 */
400 @GET
401 @Produces(MediaType.APPLICATION_JSON)
402 @Path("{networkId}/hosts")
403 public Response getVirtualHosts(@PathParam("networkId") long networkId) {
404 NetworkId nid = NetworkId.networkId(networkId);
405 Set<VirtualHost> vhosts = vnetService.getVirtualHosts(nid);
406 return ok(encodeArray(VirtualHost.class, "hosts", vhosts)).build();
407 }
408
409 /**
410 * Creates a virtual network host from the JSON input stream.
411 *
412 * @param networkId network identifier
413 * @param stream virtual host JSON stream
414 * @return status of the request - CREATED if the JSON is correct,
415 * BAD_REQUEST if the JSON is invalid
416 * @onos.rsModel VirtualHostPut
417 */
418 @POST
419 @Path("{networkId}/hosts")
420 @Consumes(MediaType.APPLICATION_JSON)
421 @Produces(MediaType.APPLICATION_JSON)
422 public Response createVirtualHost(@PathParam("networkId") long networkId,
423 InputStream stream) {
424 try {
425 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
426 JsonNode specifiedNetworkId = jsonTree.get("networkId");
427 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
428 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
429 }
430 final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
431 vnetAdminService.createVirtualHost(vhostReq.networkId(), vhostReq.id(),
432 vhostReq.mac(), vhostReq.vlan(),
433 vhostReq.location(), vhostReq.ipAddresses());
434 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
435 .path("vnets").path(specifiedNetworkId.asText())
436 .path("hosts");
437 return Response
438 .created(locationBuilder.build())
439 .build();
440 } catch (IOException e) {
441 throw new IllegalArgumentException(e);
442 }
443 }
444
445 /**
446 * Removes the virtual network host from the JSON input stream.
447 *
448 * @param networkId network identifier
449 * @param stream virtual host JSON stream
450 * @return 204 NO CONTENT
451 * @onos.rsModel VirtualHost
452 */
453 @DELETE
454 @Path("{networkId}/hosts")
455 @Consumes(MediaType.APPLICATION_JSON)
456 public Response removeVirtualHost(@PathParam("networkId") long networkId,
457 InputStream stream) {
458 try {
459 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
460 JsonNode specifiedNetworkId = jsonTree.get("networkId");
461 if (specifiedNetworkId != null &&
462 specifiedNetworkId.asLong() != (networkId)) {
463 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
464 }
465 final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
466 vnetAdminService.removeVirtualHost(vhostReq.networkId(), vhostReq.id());
467 } catch (IOException e) {
468 throw new IllegalArgumentException(e);
469 }
470
471 return Response.noContent().build();
472 }
473
474 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000475 * Get the tenant identifier from the JSON stream.
476 *
Brian Stanke9a108972016-04-11 15:25:17 -0400477 * @param stream TenantId JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000478 * @param jsonFieldName field name
479 * @return JsonNode
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000480 * @throws IOException if unable to parse the request
Claudine Chiufb8b8162016-04-01 23:50:51 +0000481 */
482 private JsonNode getFromJsonStream(InputStream stream, String jsonFieldName) throws IOException {
483 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
484 JsonNode jsonNode = jsonTree.get(jsonFieldName);
485
486 if (jsonNode == null) {
487 throw new IllegalArgumentException(MISSING_FIELD + jsonFieldName);
488 }
489 return jsonNode;
490 }
491}