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 | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 24 | import org.onosproject.incubator.net.virtual.VirtualHost; |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 25 | import org.onosproject.incubator.net.virtual.VirtualLink; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 26 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 27 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.DefaultDevice; |
| 32 | import org.onosproject.net.DefaultPort; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Port; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.rest.AbstractWebResource; |
| 38 | |
| 39 | import javax.ws.rs.Consumes; |
| 40 | import javax.ws.rs.DELETE; |
| 41 | import javax.ws.rs.GET; |
| 42 | import javax.ws.rs.POST; |
| 43 | import javax.ws.rs.Path; |
| 44 | import javax.ws.rs.PathParam; |
| 45 | import javax.ws.rs.Produces; |
| 46 | import javax.ws.rs.core.Context; |
| 47 | import javax.ws.rs.core.MediaType; |
| 48 | import javax.ws.rs.core.Response; |
| 49 | import javax.ws.rs.core.UriBuilder; |
| 50 | import javax.ws.rs.core.UriInfo; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
| 53 | import java.util.Collection; |
| 54 | import java.util.List; |
| 55 | import java.util.Set; |
| 56 | import java.util.stream.Collectors; |
| 57 | |
| 58 | /** |
| 59 | * Query and Manage Virtual Network elements. |
| 60 | */ |
| 61 | @Path("vnets") |
| 62 | public 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 71 | private UriInfo uriInfo; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 72 | |
| 73 | // VirtualNetwork |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Returns all virtual networks. |
| 77 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 78 | * @return 200 OK with set of virtual networks |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 79 | * @onos.rsModel VirtualNetworks |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 80 | */ |
| 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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 93 | * Returns the virtual networks with the specified tenant identifier. |
| 94 | * |
| 95 | * @param tenantId tenant identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 96 | * @return 200 OK with a virtual network, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 97 | * @onos.rsModel VirtualNetworks |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 98 | */ |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 104 | TenantId.tenantId(tenantId)); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 105 | Set<VirtualNetwork> vnets = vnetService.getVirtualNetworks(existingTid); |
| 106 | return ok(encodeArray(VirtualNetwork.class, "vnets", vnets)).build(); |
| 107 | } |
| 108 | |
| 109 | /** |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 110 | * Creates a virtual network from the JSON input stream. |
| 111 | * |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 112 | * @param stream tenant identifier JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 113 | * @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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 139 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 140 | */ |
| 141 | @DELETE |
| 142 | @Path("{networkId}") |
| 143 | public Response removeVirtualNetwork(@PathParam("networkId") long networkId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 144 | NetworkId nid = NetworkId.networkId(networkId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 145 | vnetAdminService.removeVirtualNetwork(nid); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 146 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // VirtualDevice |
| 150 | |
| 151 | /** |
| 152 | * Returns all virtual network devices in a virtual network. |
| 153 | * |
| 154 | * @param networkId network identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 155 | * @return 200 OK with set of virtual devices, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 156 | * @onos.rsModel VirtualDevices |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 157 | */ |
| 158 | @GET |
| 159 | @Produces(MediaType.APPLICATION_JSON) |
| 160 | @Path("{networkId}/devices") |
| 161 | public Response getVirtualDevices(@PathParam("networkId") long networkId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 162 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 163 | Set<VirtualDevice> vdevs = vnetService.getVirtualDevices(nid); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 164 | 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 Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 171 | * @param stream virtual device JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 172 | * @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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 177 | @Path("{networkId}/devices") |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 178 | @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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 185 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 186 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 187 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 188 | } |
| 189 | final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(), |
| 190 | vdevReq.id()); |
| 191 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 192 | .path("vnets").path(specifiedNetworkId.asText()) |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 193 | .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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 206 | * @param deviceId device identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 207 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 208 | */ |
| 209 | @DELETE |
| 210 | @Path("{networkId}/devices/{deviceId}") |
| 211 | public Response removeVirtualDevice(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 212 | @PathParam("deviceId") String deviceId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 213 | NetworkId nid = NetworkId.networkId(networkId); |
| 214 | DeviceId did = DeviceId.deviceId(deviceId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 215 | vnetAdminService.removeVirtualDevice(nid, did); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 216 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 217 | } |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 225 | * @param deviceId virtual device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 226 | * @return 200 OK with set of virtual ports, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 227 | * @onos.rsModel VirtualPorts |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 228 | */ |
| 229 | @GET |
| 230 | @Produces(MediaType.APPLICATION_JSON) |
| 231 | @Path("{networkId}/devices/{deviceId}/ports") |
| 232 | public Response getVirtualPorts(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 233 | @PathParam("deviceId") String deviceId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 234 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 235 | Iterable<VirtualPort> vports = vnetService.getVirtualPorts(nid, DeviceId.deviceId(deviceId)); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 236 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 242 | * @param networkId network identifier |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 243 | * @param virtDeviceId virtual device identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 244 | * @param stream virtual port JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 245 | * @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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 254 | @PathParam("deviceId") String virtDeviceId, |
| 255 | InputStream stream) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 256 | 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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 261 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 262 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 263 | } |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 264 | if (specifiedDeviceId == null || !specifiedDeviceId.asText().equals(virtDeviceId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 265 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 274 | null, null, null, null, null, null, annotations); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 275 | Port realizedBy = new DefaultPort(physDevice, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 276 | PortNumber.portNumber(specifiedPhysPortNum.asText()), true); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 277 | VirtualPort vport = vnetAdminService.createVirtualPort(nid, vdevId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 278 | PortNumber.portNumber(specifiedPortNum.asText()), realizedBy); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 279 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 295 | * @param deviceId virtual device identifier |
| 296 | * @param portNum virtual port number |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 297 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 298 | */ |
| 299 | @DELETE |
| 300 | @Path("{networkId}/devices/{deviceId}/ports/{portNum}") |
| 301 | public Response removeVirtualPort(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 302 | @PathParam("deviceId") String deviceId, |
| 303 | @PathParam("portNum") long portNum) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 304 | NetworkId nid = NetworkId.networkId(networkId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 305 | vnetAdminService.removeVirtualPort(nid, DeviceId.deviceId(deviceId), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 306 | PortNumber.portNumber(portNum)); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 307 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 310 | // VirtualLink |
| 311 | |
| 312 | /** |
| 313 | * Returns all virtual network links in a virtual network. |
| 314 | * |
| 315 | * @param networkId network identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 316 | * @return 200 OK with set of virtual network links |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 317 | * @onos.rsModel VirtualLinks |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 318 | */ |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 324 | Set<VirtualLink> vlinks = vnetService.getVirtualLinks(nid); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 325 | return ok(encodeArray(VirtualLink.class, "links", vlinks)).build(); |
| 326 | } |
| 327 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 328 | /** |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 329 | * Creates a virtual network link from the JSON input stream. |
| 330 | * |
| 331 | * @param networkId network identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 332 | * @param stream virtual link JSON stream |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 333 | * @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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 350 | vnetAdminService.createVirtualLink(vlinkReq.networkId(), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 351 | vlinkReq.src(), vlinkReq.dst()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 352 | 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 Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 367 | * @param stream virtual link JSON stream |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 368 | * @return 204 NO CONTENT |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 369 | * @onos.rsModel VirtualLink |
| 370 | */ |
| 371 | @DELETE |
| 372 | @Path("{networkId}/links") |
| 373 | @Consumes(MediaType.APPLICATION_JSON) |
| 374 | public Response removeVirtualLink(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 375 | InputStream stream) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 376 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 385 | vlinkReq.src(), vlinkReq.dst()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 386 | } catch (IOException e) { |
| 387 | throw new IllegalArgumentException(e); |
| 388 | } |
| 389 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 390 | return Response.noContent().build(); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 394 | * 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 Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 475 | * Get the tenant identifier from the JSON stream. |
| 476 | * |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 477 | * @param stream TenantId JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 478 | * @param jsonFieldName field name |
| 479 | * @return JsonNode |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 480 | * @throws IOException if unable to parse the request |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 481 | */ |
| 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 | } |