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