blob: 0e3504c8dd30f9694c58ed9ed3d35207a23d26bb [file] [log] [blame]
Claudine Chiufb8b8162016-04-01 23:50:51 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Ray Milkey86ee5e82018-04-02 15:33:07 -070054import static org.onlab.util.Tools.readTreeFromStream;
55
Claudine Chiufb8b8162016-04-01 23:50:51 +000056/**
57 * Query and Manage Virtual Network elements.
58 */
59@Path("vnets")
60public class VirtualNetworkWebResource extends AbstractWebResource {
61
62 private static final String MISSING_FIELD = "Missing ";
63 private static final String INVALID_FIELD = "Invalid ";
64
65 private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class);
66 private final VirtualNetworkService vnetService = get(VirtualNetworkService.class);
67
68 @Context
Jian Licc730a62016-05-10 16:36:16 -070069 private UriInfo uriInfo;
Claudine Chiufb8b8162016-04-01 23:50:51 +000070
71 // VirtualNetwork
Claudine Chiufb8b8162016-04-01 23:50:51 +000072
73 /**
74 * Returns all virtual networks.
75 *
Jian Licc730a62016-05-10 16:36:16 -070076 * @return 200 OK with set of virtual networks
Claudine Chiu1decd532016-04-19 18:30:01 +000077 * @onos.rsModel VirtualNetworks
Claudine Chiufb8b8162016-04-01 23:50:51 +000078 */
79 @GET
80 @Produces(MediaType.APPLICATION_JSON)
81 public Response getVirtualNetworks() {
82 Set<TenantId> tenantIds = vnetAdminService.getTenantIds();
83 List<VirtualNetwork> allVnets = tenantIds.stream()
84 .map(tenantId -> vnetService.getVirtualNetworks(tenantId))
85 .flatMap(Collection::stream)
86 .collect(Collectors.toList());
87 return ok(encodeArray(VirtualNetwork.class, "vnets", allVnets)).build();
88 }
89
90 /**
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000091 * Returns the virtual networks with the specified tenant identifier.
92 *
93 * @param tenantId tenant identifier
Jian Licc730a62016-05-10 16:36:16 -070094 * @return 200 OK with a virtual network, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +000095 * @onos.rsModel VirtualNetworks
Claudine Chiuf6bf8d52016-04-08 01:31:54 +000096 */
97 @GET
98 @Produces(MediaType.APPLICATION_JSON)
99 @Path("{tenantId}")
100 public Response getVirtualNetworkById(@PathParam("tenantId") String tenantId) {
101 final TenantId existingTid = TenantWebResource.getExistingTenantId(vnetAdminService,
Brian Stanke9a108972016-04-11 15:25:17 -0400102 TenantId.tenantId(tenantId));
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000103 Set<VirtualNetwork> vnets = vnetService.getVirtualNetworks(existingTid);
104 return ok(encodeArray(VirtualNetwork.class, "vnets", vnets)).build();
105 }
106
107 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000108 * Creates a virtual network from the JSON input stream.
109 *
Claudine Chiu1decd532016-04-19 18:30:01 +0000110 * @param stream tenant identifier JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000111 * @return status of the request - CREATED if the JSON is correct,
112 * BAD_REQUEST if the JSON is invalid
113 * @onos.rsModel TenantId
114 */
115 @POST
116 @Consumes(MediaType.APPLICATION_JSON)
117 @Produces(MediaType.APPLICATION_JSON)
118 public Response createVirtualNetwork(InputStream stream) {
119 try {
120 final TenantId tid = TenantId.tenantId(getFromJsonStream(stream, "id").asText());
121 VirtualNetwork newVnet = vnetAdminService.createVirtualNetwork(tid);
122 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
123 .path("vnets")
124 .path(newVnet.id().toString());
125 return Response
126 .created(locationBuilder.build())
127 .build();
128 } catch (IOException e) {
129 throw new IllegalArgumentException(e);
130 }
131 }
132
133 /**
134 * Removes the virtual network with the specified network identifier.
135 *
136 * @param networkId network identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700137 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000138 */
139 @DELETE
140 @Path("{networkId}")
141 public Response removeVirtualNetwork(@PathParam("networkId") long networkId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000142 NetworkId nid = NetworkId.networkId(networkId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000143 vnetAdminService.removeVirtualNetwork(nid);
Jian Lic2a542b2016-05-10 11:48:19 -0700144 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000145 }
146
147 // VirtualDevice
148
149 /**
150 * Returns all virtual network devices in a virtual network.
151 *
152 * @param networkId network identifier
Jian Licc730a62016-05-10 16:36:16 -0700153 * @return 200 OK with set of virtual devices, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +0000154 * @onos.rsModel VirtualDevices
Claudine Chiufb8b8162016-04-01 23:50:51 +0000155 */
156 @GET
157 @Produces(MediaType.APPLICATION_JSON)
158 @Path("{networkId}/devices")
159 public Response getVirtualDevices(@PathParam("networkId") long networkId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000160 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400161 Set<VirtualDevice> vdevs = vnetService.getVirtualDevices(nid);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000162 return ok(encodeArray(VirtualDevice.class, "devices", vdevs)).build();
163 }
164
165 /**
166 * Creates a virtual device from the JSON input stream.
167 *
168 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000169 * @param stream virtual device JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000170 * @return status of the request - CREATED if the JSON is correct,
171 * BAD_REQUEST if the JSON is invalid
172 * @onos.rsModel VirtualDevice
173 */
174 @POST
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000175 @Path("{networkId}/devices")
Claudine Chiufb8b8162016-04-01 23:50:51 +0000176 @Consumes(MediaType.APPLICATION_JSON)
177 @Produces(MediaType.APPLICATION_JSON)
178 public Response createVirtualDevice(@PathParam("networkId") long networkId,
179 InputStream stream) {
180 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700181 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000182 final VirtualDevice vdevReq = codec(VirtualDevice.class).decode(jsonTree, this);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000183 JsonNode specifiedNetworkId = jsonTree.get("networkId");
184 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000185 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
186 }
187 final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(),
188 vdevReq.id());
189 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000190 .path("vnets").path(specifiedNetworkId.asText())
Claudine Chiufb8b8162016-04-01 23:50:51 +0000191 .path("devices").path(vdevRes.id().toString());
192 return Response
193 .created(locationBuilder.build())
194 .build();
195 } catch (IOException e) {
196 throw new IllegalArgumentException(e);
197 }
198 }
199
200 /**
201 * Removes the virtual network device from the virtual network.
202 *
203 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400204 * @param deviceId device identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700205 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000206 */
207 @DELETE
208 @Path("{networkId}/devices/{deviceId}")
209 public Response removeVirtualDevice(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400210 @PathParam("deviceId") String deviceId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000211 NetworkId nid = NetworkId.networkId(networkId);
212 DeviceId did = DeviceId.deviceId(deviceId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000213 vnetAdminService.removeVirtualDevice(nid, did);
Jian Lic2a542b2016-05-10 11:48:19 -0700214 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000215 }
216
217 // VirtualPort
218
219 /**
220 * Returns all virtual network ports in a virtual device in a virtual network.
221 *
222 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400223 * @param deviceId virtual device identifier
Jian Licc730a62016-05-10 16:36:16 -0700224 * @return 200 OK with set of virtual ports, 404 not found
Claudine Chiu1decd532016-04-19 18:30:01 +0000225 * @onos.rsModel VirtualPorts
Claudine Chiufb8b8162016-04-01 23:50:51 +0000226 */
227 @GET
228 @Produces(MediaType.APPLICATION_JSON)
229 @Path("{networkId}/devices/{deviceId}/ports")
230 public Response getVirtualPorts(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400231 @PathParam("deviceId") String deviceId) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000232 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400233 Iterable<VirtualPort> vports = vnetService.getVirtualPorts(nid, DeviceId.deviceId(deviceId));
Claudine Chiufb8b8162016-04-01 23:50:51 +0000234 return ok(encodeArray(VirtualPort.class, "ports", vports)).build();
235 }
236
237 /**
238 * Creates a virtual network port in a virtual device in a virtual network.
239 *
Brian Stanke9a108972016-04-11 15:25:17 -0400240 * @param networkId network identifier
Claudine Chiufb8b8162016-04-01 23:50:51 +0000241 * @param virtDeviceId virtual device identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000242 * @param stream virtual port JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000243 * @return status of the request - CREATED if the JSON is correct,
244 * BAD_REQUEST if the JSON is invalid
245 * @onos.rsModel VirtualPort
246 */
247 @POST
248 @Consumes(MediaType.APPLICATION_JSON)
249 @Produces(MediaType.APPLICATION_JSON)
250 @Path("{networkId}/devices/{deviceId}/ports")
251 public Response createVirtualPort(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400252 @PathParam("deviceId") String virtDeviceId,
253 InputStream stream) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000254 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700255 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000256// final VirtualPort vportReq = codec(VirtualPort.class).decode(jsonTree, this);
257 JsonNode specifiedNetworkId = jsonTree.get("networkId");
258 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000259 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000260 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
261 }
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000262 if (specifiedDeviceId == null || !specifiedDeviceId.asText().equals(virtDeviceId)) {
Claudine Chiufb8b8162016-04-01 23:50:51 +0000263 throw new IllegalArgumentException(INVALID_FIELD + "deviceId");
264 }
265 JsonNode specifiedPortNum = jsonTree.get("portNum");
266 JsonNode specifiedPhysDeviceId = jsonTree.get("physDeviceId");
267 JsonNode specifiedPhysPortNum = jsonTree.get("physPortNum");
268 final NetworkId nid = NetworkId.networkId(networkId);
269 DeviceId vdevId = DeviceId.deviceId(virtDeviceId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700270
271 ConnectPoint realizedBy = new ConnectPoint(DeviceId.deviceId(specifiedPhysDeviceId.asText()),
272 PortNumber.portNumber(specifiedPhysPortNum.asText()));
Claudine Chiufb8b8162016-04-01 23:50:51 +0000273 VirtualPort vport = vnetAdminService.createVirtualPort(nid, vdevId,
Brian Stanke9a108972016-04-11 15:25:17 -0400274 PortNumber.portNumber(specifiedPortNum.asText()), realizedBy);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000275 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
276 .path("vnets").path(specifiedNetworkId.asText())
277 .path("devices").path(specifiedDeviceId.asText())
278 .path("ports").path(vport.number().toString());
279 return Response
280 .created(locationBuilder.build())
281 .build();
282 } catch (IOException e) {
283 throw new IllegalArgumentException(e);
284 }
285 }
286
287 /**
288 * Removes the virtual network port from the virtual device in a virtual network.
289 *
290 * @param networkId network identifier
Brian Stanke9a108972016-04-11 15:25:17 -0400291 * @param deviceId virtual device identifier
292 * @param portNum virtual port number
Jian Lic2a542b2016-05-10 11:48:19 -0700293 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000294 */
295 @DELETE
296 @Path("{networkId}/devices/{deviceId}/ports/{portNum}")
297 public Response removeVirtualPort(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400298 @PathParam("deviceId") String deviceId,
299 @PathParam("portNum") long portNum) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000300 NetworkId nid = NetworkId.networkId(networkId);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000301 vnetAdminService.removeVirtualPort(nid, DeviceId.deviceId(deviceId),
Brian Stanke9a108972016-04-11 15:25:17 -0400302 PortNumber.portNumber(portNum));
Jian Lic2a542b2016-05-10 11:48:19 -0700303 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000304 }
305
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000306 // VirtualLink
307
308 /**
309 * Returns all virtual network links in a virtual network.
310 *
311 * @param networkId network identifier
Jian Licc730a62016-05-10 16:36:16 -0700312 * @return 200 OK with set of virtual network links
Claudine Chiu1decd532016-04-19 18:30:01 +0000313 * @onos.rsModel VirtualLinks
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000314 */
315 @GET
316 @Produces(MediaType.APPLICATION_JSON)
317 @Path("{networkId}/links")
318 public Response getVirtualLinks(@PathParam("networkId") long networkId) {
319 NetworkId nid = NetworkId.networkId(networkId);
Brian Stanke9a108972016-04-11 15:25:17 -0400320 Set<VirtualLink> vlinks = vnetService.getVirtualLinks(nid);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000321 return ok(encodeArray(VirtualLink.class, "links", vlinks)).build();
322 }
323
Brian Stanke9a108972016-04-11 15:25:17 -0400324 /**
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000325 * Creates a virtual network link from the JSON input stream.
326 *
327 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000328 * @param stream virtual link JSON stream
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000329 * @return status of the request - CREATED if the JSON is correct,
330 * BAD_REQUEST if the JSON is invalid
331 * @onos.rsModel VirtualLink
332 */
333 @POST
334 @Path("{networkId}/links")
335 @Consumes(MediaType.APPLICATION_JSON)
336 @Produces(MediaType.APPLICATION_JSON)
337 public Response createVirtualLink(@PathParam("networkId") long networkId,
338 InputStream stream) {
339 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700340 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000341 JsonNode specifiedNetworkId = jsonTree.get("networkId");
342 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
343 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
344 }
345 final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000346 vnetAdminService.createVirtualLink(vlinkReq.networkId(),
Brian Stanke9a108972016-04-11 15:25:17 -0400347 vlinkReq.src(), vlinkReq.dst());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000348 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
349 .path("vnets").path(specifiedNetworkId.asText())
350 .path("links");
351 return Response
352 .created(locationBuilder.build())
353 .build();
354 } catch (IOException e) {
355 throw new IllegalArgumentException(e);
356 }
357 }
358
359 /**
360 * Removes the virtual network link from the JSON input stream.
361 *
362 * @param networkId network identifier
Claudine Chiu1decd532016-04-19 18:30:01 +0000363 * @param stream virtual link JSON stream
Jian Lic2a542b2016-05-10 11:48:19 -0700364 * @return 204 NO CONTENT
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000365 * @onos.rsModel VirtualLink
366 */
367 @DELETE
368 @Path("{networkId}/links")
369 @Consumes(MediaType.APPLICATION_JSON)
370 public Response removeVirtualLink(@PathParam("networkId") long networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400371 InputStream stream) {
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000372 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700373 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000374 JsonNode specifiedNetworkId = jsonTree.get("networkId");
375 if (specifiedNetworkId != null &&
376 specifiedNetworkId.asLong() != (networkId)) {
377 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
378 }
379 final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this);
380 vnetAdminService.removeVirtualLink(vlinkReq.networkId(),
Brian Stanke9a108972016-04-11 15:25:17 -0400381 vlinkReq.src(), vlinkReq.dst());
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000382 } catch (IOException e) {
383 throw new IllegalArgumentException(e);
384 }
385
Jian Lic2a542b2016-05-10 11:48:19 -0700386 return Response.noContent().build();
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000387 }
388
389 /**
Claudine Chiu25f07be2016-06-27 16:21:21 +0000390 * Returns all virtual network hosts in a virtual network.
391 *
392 * @param networkId network identifier
393 * @return 200 OK with set of virtual network hosts
394 * @onos.rsModel VirtualHosts
395 */
396 @GET
397 @Produces(MediaType.APPLICATION_JSON)
398 @Path("{networkId}/hosts")
399 public Response getVirtualHosts(@PathParam("networkId") long networkId) {
400 NetworkId nid = NetworkId.networkId(networkId);
401 Set<VirtualHost> vhosts = vnetService.getVirtualHosts(nid);
402 return ok(encodeArray(VirtualHost.class, "hosts", vhosts)).build();
403 }
404
405 /**
406 * Creates a virtual network host from the JSON input stream.
407 *
408 * @param networkId network identifier
409 * @param stream virtual host JSON stream
410 * @return status of the request - CREATED if the JSON is correct,
411 * BAD_REQUEST if the JSON is invalid
412 * @onos.rsModel VirtualHostPut
413 */
414 @POST
415 @Path("{networkId}/hosts")
416 @Consumes(MediaType.APPLICATION_JSON)
417 @Produces(MediaType.APPLICATION_JSON)
418 public Response createVirtualHost(@PathParam("networkId") long networkId,
419 InputStream stream) {
420 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700421 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiu25f07be2016-06-27 16:21:21 +0000422 JsonNode specifiedNetworkId = jsonTree.get("networkId");
423 if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) {
424 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
425 }
426 final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
427 vnetAdminService.createVirtualHost(vhostReq.networkId(), vhostReq.id(),
428 vhostReq.mac(), vhostReq.vlan(),
429 vhostReq.location(), vhostReq.ipAddresses());
430 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
431 .path("vnets").path(specifiedNetworkId.asText())
432 .path("hosts");
433 return Response
434 .created(locationBuilder.build())
435 .build();
436 } catch (IOException e) {
437 throw new IllegalArgumentException(e);
438 }
439 }
440
441 /**
442 * Removes the virtual network host from the JSON input stream.
443 *
444 * @param networkId network identifier
445 * @param stream virtual host JSON stream
446 * @return 204 NO CONTENT
447 * @onos.rsModel VirtualHost
448 */
449 @DELETE
450 @Path("{networkId}/hosts")
451 @Consumes(MediaType.APPLICATION_JSON)
452 public Response removeVirtualHost(@PathParam("networkId") long networkId,
453 InputStream stream) {
454 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700455 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiu25f07be2016-06-27 16:21:21 +0000456 JsonNode specifiedNetworkId = jsonTree.get("networkId");
457 if (specifiedNetworkId != null &&
458 specifiedNetworkId.asLong() != (networkId)) {
459 throw new IllegalArgumentException(INVALID_FIELD + "networkId");
460 }
461 final VirtualHost vhostReq = codec(VirtualHost.class).decode(jsonTree, this);
462 vnetAdminService.removeVirtualHost(vhostReq.networkId(), vhostReq.id());
463 } catch (IOException e) {
464 throw new IllegalArgumentException(e);
465 }
466
467 return Response.noContent().build();
468 }
469
470 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000471 * Get the tenant identifier from the JSON stream.
472 *
Brian Stanke9a108972016-04-11 15:25:17 -0400473 * @param stream TenantId JSON stream
Claudine Chiufb8b8162016-04-01 23:50:51 +0000474 * @param jsonFieldName field name
475 * @return JsonNode
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000476 * @throws IOException if unable to parse the request
Claudine Chiufb8b8162016-04-01 23:50:51 +0000477 */
478 private JsonNode getFromJsonStream(InputStream stream, String jsonFieldName) throws IOException {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700479 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000480 JsonNode jsonNode = jsonTree.get(jsonFieldName);
481
482 if (jsonNode == null) {
483 throw new IllegalArgumentException(MISSING_FIELD + jsonFieldName);
484 }
485 return jsonNode;
486 }
487}