Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.rest.resources; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 22 | import org.onosproject.incubator.net.virtual.TenantId; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 24 | import org.onosproject.incubator.net.virtual.VirtualLink; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 25 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 26 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 27 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.DefaultDevice; |
| 31 | import org.onosproject.net.DefaultPort; |
| 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Port; |
| 35 | import org.onosproject.net.PortNumber; |
| 36 | import org.onosproject.rest.AbstractWebResource; |
| 37 | |
| 38 | import javax.ws.rs.Consumes; |
| 39 | import javax.ws.rs.DELETE; |
| 40 | import javax.ws.rs.GET; |
| 41 | import javax.ws.rs.POST; |
| 42 | import javax.ws.rs.Path; |
| 43 | import javax.ws.rs.PathParam; |
| 44 | import javax.ws.rs.Produces; |
| 45 | import javax.ws.rs.core.Context; |
| 46 | import javax.ws.rs.core.MediaType; |
| 47 | import javax.ws.rs.core.Response; |
| 48 | import javax.ws.rs.core.UriBuilder; |
| 49 | import javax.ws.rs.core.UriInfo; |
| 50 | import java.io.IOException; |
| 51 | import java.io.InputStream; |
| 52 | import java.util.Collection; |
| 53 | import java.util.List; |
| 54 | import java.util.Set; |
| 55 | import java.util.stream.Collectors; |
| 56 | |
| 57 | /** |
| 58 | * Query and Manage Virtual Network elements. |
| 59 | */ |
| 60 | @Path("vnets") |
| 61 | public class VirtualNetworkWebResource extends AbstractWebResource { |
| 62 | |
| 63 | private static final String MISSING_FIELD = "Missing "; |
| 64 | private static final String INVALID_FIELD = "Invalid "; |
| 65 | |
| 66 | private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class); |
| 67 | private final VirtualNetworkService vnetService = get(VirtualNetworkService.class); |
| 68 | |
| 69 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 70 | private UriInfo uriInfo; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 71 | |
| 72 | // VirtualNetwork |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Returns all virtual networks. |
| 76 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 77 | * @return 200 OK with set of virtual networks |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 78 | * @onos.rsModel VirtualNetworks |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 79 | */ |
| 80 | @GET |
| 81 | @Produces(MediaType.APPLICATION_JSON) |
| 82 | public Response getVirtualNetworks() { |
| 83 | Set<TenantId> tenantIds = vnetAdminService.getTenantIds(); |
| 84 | List<VirtualNetwork> allVnets = tenantIds.stream() |
| 85 | .map(tenantId -> vnetService.getVirtualNetworks(tenantId)) |
| 86 | .flatMap(Collection::stream) |
| 87 | .collect(Collectors.toList()); |
| 88 | return ok(encodeArray(VirtualNetwork.class, "vnets", allVnets)).build(); |
| 89 | } |
| 90 | |
| 91 | /** |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 92 | * Returns the virtual networks with the specified tenant identifier. |
| 93 | * |
| 94 | * @param tenantId tenant identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 95 | * @return 200 OK with a virtual network, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 96 | * @onos.rsModel VirtualNetworks |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 97 | */ |
| 98 | @GET |
| 99 | @Produces(MediaType.APPLICATION_JSON) |
| 100 | @Path("{tenantId}") |
| 101 | public Response getVirtualNetworkById(@PathParam("tenantId") String tenantId) { |
| 102 | final TenantId existingTid = TenantWebResource.getExistingTenantId(vnetAdminService, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 103 | TenantId.tenantId(tenantId)); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 104 | Set<VirtualNetwork> vnets = vnetService.getVirtualNetworks(existingTid); |
| 105 | return ok(encodeArray(VirtualNetwork.class, "vnets", vnets)).build(); |
| 106 | } |
| 107 | |
| 108 | /** |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 109 | * Creates a virtual network from the JSON input stream. |
| 110 | * |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 111 | * @param stream tenant identifier JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 112 | * @return status of the request - CREATED if the JSON is correct, |
| 113 | * BAD_REQUEST if the JSON is invalid |
| 114 | * @onos.rsModel TenantId |
| 115 | */ |
| 116 | @POST |
| 117 | @Consumes(MediaType.APPLICATION_JSON) |
| 118 | @Produces(MediaType.APPLICATION_JSON) |
| 119 | public Response createVirtualNetwork(InputStream stream) { |
| 120 | try { |
| 121 | final TenantId tid = TenantId.tenantId(getFromJsonStream(stream, "id").asText()); |
| 122 | VirtualNetwork newVnet = vnetAdminService.createVirtualNetwork(tid); |
| 123 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 124 | .path("vnets") |
| 125 | .path(newVnet.id().toString()); |
| 126 | return Response |
| 127 | .created(locationBuilder.build()) |
| 128 | .build(); |
| 129 | } catch (IOException e) { |
| 130 | throw new IllegalArgumentException(e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Removes the virtual network with the specified network identifier. |
| 136 | * |
| 137 | * @param networkId network identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 138 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 139 | */ |
| 140 | @DELETE |
| 141 | @Path("{networkId}") |
| 142 | public Response removeVirtualNetwork(@PathParam("networkId") long networkId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 143 | NetworkId nid = NetworkId.networkId(networkId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 144 | vnetAdminService.removeVirtualNetwork(nid); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 145 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // VirtualDevice |
| 149 | |
| 150 | /** |
| 151 | * Returns all virtual network devices in a virtual network. |
| 152 | * |
| 153 | * @param networkId network identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 154 | * @return 200 OK with set of virtual devices, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 155 | * @onos.rsModel VirtualDevices |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 156 | */ |
| 157 | @GET |
| 158 | @Produces(MediaType.APPLICATION_JSON) |
| 159 | @Path("{networkId}/devices") |
| 160 | public Response getVirtualDevices(@PathParam("networkId") long networkId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 161 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 162 | Set<VirtualDevice> vdevs = vnetService.getVirtualDevices(nid); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 163 | return ok(encodeArray(VirtualDevice.class, "devices", vdevs)).build(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Creates a virtual device from the JSON input stream. |
| 168 | * |
| 169 | * @param networkId network identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 170 | * @param stream virtual device JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 171 | * @return status of the request - CREATED if the JSON is correct, |
| 172 | * BAD_REQUEST if the JSON is invalid |
| 173 | * @onos.rsModel VirtualDevice |
| 174 | */ |
| 175 | @POST |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 176 | @Path("{networkId}/devices") |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 177 | @Consumes(MediaType.APPLICATION_JSON) |
| 178 | @Produces(MediaType.APPLICATION_JSON) |
| 179 | public Response createVirtualDevice(@PathParam("networkId") long networkId, |
| 180 | InputStream stream) { |
| 181 | try { |
| 182 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 183 | final VirtualDevice vdevReq = codec(VirtualDevice.class).decode(jsonTree, this); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 184 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 185 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 186 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 187 | } |
| 188 | final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(), |
| 189 | vdevReq.id()); |
| 190 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 191 | .path("vnets").path(specifiedNetworkId.asText()) |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 192 | .path("devices").path(vdevRes.id().toString()); |
| 193 | return Response |
| 194 | .created(locationBuilder.build()) |
| 195 | .build(); |
| 196 | } catch (IOException e) { |
| 197 | throw new IllegalArgumentException(e); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Removes the virtual network device from the virtual network. |
| 203 | * |
| 204 | * @param networkId network identifier |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 205 | * @param deviceId device identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 206 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 207 | */ |
| 208 | @DELETE |
| 209 | @Path("{networkId}/devices/{deviceId}") |
| 210 | public Response removeVirtualDevice(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 211 | @PathParam("deviceId") String deviceId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 212 | NetworkId nid = NetworkId.networkId(networkId); |
| 213 | DeviceId did = DeviceId.deviceId(deviceId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 214 | vnetAdminService.removeVirtualDevice(nid, did); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 215 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // VirtualPort |
| 219 | |
| 220 | /** |
| 221 | * Returns all virtual network ports in a virtual device in a virtual network. |
| 222 | * |
| 223 | * @param networkId network identifier |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 224 | * @param deviceId virtual device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 225 | * @return 200 OK with set of virtual ports, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 226 | * @onos.rsModel VirtualPorts |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 227 | */ |
| 228 | @GET |
| 229 | @Produces(MediaType.APPLICATION_JSON) |
| 230 | @Path("{networkId}/devices/{deviceId}/ports") |
| 231 | public Response getVirtualPorts(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 232 | @PathParam("deviceId") String deviceId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 233 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 234 | Iterable<VirtualPort> vports = vnetService.getVirtualPorts(nid, DeviceId.deviceId(deviceId)); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 235 | return ok(encodeArray(VirtualPort.class, "ports", vports)).build(); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Creates a virtual network port in a virtual device in a virtual network. |
| 240 | * |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 241 | * @param networkId network identifier |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 242 | * @param virtDeviceId virtual device identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 243 | * @param stream virtual port JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 244 | * @return status of the request - CREATED if the JSON is correct, |
| 245 | * BAD_REQUEST if the JSON is invalid |
| 246 | * @onos.rsModel VirtualPort |
| 247 | */ |
| 248 | @POST |
| 249 | @Consumes(MediaType.APPLICATION_JSON) |
| 250 | @Produces(MediaType.APPLICATION_JSON) |
| 251 | @Path("{networkId}/devices/{deviceId}/ports") |
| 252 | public Response createVirtualPort(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 253 | @PathParam("deviceId") String virtDeviceId, |
| 254 | InputStream stream) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 255 | try { |
| 256 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 257 | // final VirtualPort vportReq = codec(VirtualPort.class).decode(jsonTree, this); |
| 258 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 259 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 260 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 261 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 262 | } |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 263 | if (specifiedDeviceId == null || !specifiedDeviceId.asText().equals(virtDeviceId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 264 | throw new IllegalArgumentException(INVALID_FIELD + "deviceId"); |
| 265 | } |
| 266 | JsonNode specifiedPortNum = jsonTree.get("portNum"); |
| 267 | JsonNode specifiedPhysDeviceId = jsonTree.get("physDeviceId"); |
| 268 | JsonNode specifiedPhysPortNum = jsonTree.get("physPortNum"); |
| 269 | final NetworkId nid = NetworkId.networkId(networkId); |
| 270 | DeviceId vdevId = DeviceId.deviceId(virtDeviceId); |
| 271 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 272 | Device physDevice = new DefaultDevice(null, DeviceId.deviceId(specifiedPhysDeviceId.asText()), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 273 | null, null, null, null, null, null, annotations); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 274 | Port realizedBy = new DefaultPort(physDevice, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 275 | PortNumber.portNumber(specifiedPhysPortNum.asText()), true); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 276 | VirtualPort vport = vnetAdminService.createVirtualPort(nid, vdevId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 277 | PortNumber.portNumber(specifiedPortNum.asText()), realizedBy); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 278 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 279 | .path("vnets").path(specifiedNetworkId.asText()) |
| 280 | .path("devices").path(specifiedDeviceId.asText()) |
| 281 | .path("ports").path(vport.number().toString()); |
| 282 | return Response |
| 283 | .created(locationBuilder.build()) |
| 284 | .build(); |
| 285 | } catch (IOException e) { |
| 286 | throw new IllegalArgumentException(e); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Removes the virtual network port from the virtual device in a virtual network. |
| 292 | * |
| 293 | * @param networkId network identifier |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 294 | * @param deviceId virtual device identifier |
| 295 | * @param portNum virtual port number |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 296 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 297 | */ |
| 298 | @DELETE |
| 299 | @Path("{networkId}/devices/{deviceId}/ports/{portNum}") |
| 300 | public Response removeVirtualPort(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 301 | @PathParam("deviceId") String deviceId, |
| 302 | @PathParam("portNum") long portNum) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 303 | NetworkId nid = NetworkId.networkId(networkId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 304 | vnetAdminService.removeVirtualPort(nid, DeviceId.deviceId(deviceId), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 305 | PortNumber.portNumber(portNum)); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 306 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 309 | // VirtualLink |
| 310 | |
| 311 | /** |
| 312 | * Returns all virtual network links in a virtual network. |
| 313 | * |
| 314 | * @param networkId network identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 315 | * @return 200 OK with set of virtual network links |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 316 | * @onos.rsModel VirtualLinks |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 317 | */ |
| 318 | @GET |
| 319 | @Produces(MediaType.APPLICATION_JSON) |
| 320 | @Path("{networkId}/links") |
| 321 | public Response getVirtualLinks(@PathParam("networkId") long networkId) { |
| 322 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 323 | Set<VirtualLink> vlinks = vnetService.getVirtualLinks(nid); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 324 | return ok(encodeArray(VirtualLink.class, "links", vlinks)).build(); |
| 325 | } |
| 326 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 327 | /** |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 328 | * Creates a virtual network link from the JSON input stream. |
| 329 | * |
| 330 | * @param networkId network identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 331 | * @param stream virtual link JSON stream |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 332 | * @return status of the request - CREATED if the JSON is correct, |
| 333 | * BAD_REQUEST if the JSON is invalid |
| 334 | * @onos.rsModel VirtualLink |
| 335 | */ |
| 336 | @POST |
| 337 | @Path("{networkId}/links") |
| 338 | @Consumes(MediaType.APPLICATION_JSON) |
| 339 | @Produces(MediaType.APPLICATION_JSON) |
| 340 | public Response createVirtualLink(@PathParam("networkId") long networkId, |
| 341 | InputStream stream) { |
| 342 | try { |
| 343 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 344 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 345 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
| 346 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 347 | } |
| 348 | final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 349 | vnetAdminService.createVirtualLink(vlinkReq.networkId(), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 350 | vlinkReq.src(), vlinkReq.dst()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 351 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 352 | .path("vnets").path(specifiedNetworkId.asText()) |
| 353 | .path("links"); |
| 354 | return Response |
| 355 | .created(locationBuilder.build()) |
| 356 | .build(); |
| 357 | } catch (IOException e) { |
| 358 | throw new IllegalArgumentException(e); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Removes the virtual network link from the JSON input stream. |
| 364 | * |
| 365 | * @param networkId network identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 366 | * @param stream virtual link JSON stream |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 367 | * @return 204 NO CONTENT |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 368 | * @onos.rsModel VirtualLink |
| 369 | */ |
| 370 | @DELETE |
| 371 | @Path("{networkId}/links") |
| 372 | @Consumes(MediaType.APPLICATION_JSON) |
| 373 | public Response removeVirtualLink(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 374 | InputStream stream) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 375 | try { |
| 376 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 377 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 378 | if (specifiedNetworkId != null && |
| 379 | specifiedNetworkId.asLong() != (networkId)) { |
| 380 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 381 | } |
| 382 | final VirtualLink vlinkReq = codec(VirtualLink.class).decode(jsonTree, this); |
| 383 | vnetAdminService.removeVirtualLink(vlinkReq.networkId(), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 384 | vlinkReq.src(), vlinkReq.dst()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 385 | } catch (IOException e) { |
| 386 | throw new IllegalArgumentException(e); |
| 387 | } |
| 388 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 389 | return Response.noContent().build(); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 393 | * Get the tenant identifier from the JSON stream. |
| 394 | * |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 395 | * @param stream TenantId JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 396 | * @param jsonFieldName field name |
| 397 | * @return JsonNode |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 398 | * @throws IOException if unable to parse the request |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 399 | */ |
| 400 | private JsonNode getFromJsonStream(InputStream stream, String jsonFieldName) throws IOException { |
| 401 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 402 | JsonNode jsonNode = jsonTree.get(jsonFieldName); |
| 403 | |
| 404 | if (jsonNode == null) { |
| 405 | throw new IllegalArgumentException(MISSING_FIELD + jsonFieldName); |
| 406 | } |
| 407 | return jsonNode; |
| 408 | } |
| 409 | } |