Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 54 | import static org.onlab.util.Tools.readTreeFromStream; |
| 55 | |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 56 | /** |
| 57 | * Query and Manage Virtual Network elements. |
| 58 | */ |
| 59 | @Path("vnets") |
| 60 | public 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 69 | private UriInfo uriInfo; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 70 | |
| 71 | // VirtualNetwork |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Returns all virtual networks. |
| 75 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 76 | * @return 200 OK with set of virtual networks |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 77 | * @onos.rsModel VirtualNetworks |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 78 | */ |
| 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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 91 | * Returns the virtual networks with the specified tenant identifier. |
| 92 | * |
| 93 | * @param tenantId tenant identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 94 | * @return 200 OK with a virtual network, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 95 | * @onos.rsModel VirtualNetworks |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 96 | */ |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 102 | TenantId.tenantId(tenantId)); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 103 | Set<VirtualNetwork> vnets = vnetService.getVirtualNetworks(existingTid); |
| 104 | return ok(encodeArray(VirtualNetwork.class, "vnets", vnets)).build(); |
| 105 | } |
| 106 | |
| 107 | /** |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 108 | * Creates a virtual network from the JSON input stream. |
| 109 | * |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 110 | * @param stream tenant identifier JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 111 | * @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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 137 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 138 | */ |
| 139 | @DELETE |
| 140 | @Path("{networkId}") |
| 141 | public Response removeVirtualNetwork(@PathParam("networkId") long networkId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 142 | NetworkId nid = NetworkId.networkId(networkId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 143 | vnetAdminService.removeVirtualNetwork(nid); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 144 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // VirtualDevice |
| 148 | |
| 149 | /** |
| 150 | * Returns all virtual network devices in a virtual network. |
| 151 | * |
| 152 | * @param networkId network identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 153 | * @return 200 OK with set of virtual devices, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 154 | * @onos.rsModel VirtualDevices |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 155 | */ |
| 156 | @GET |
| 157 | @Produces(MediaType.APPLICATION_JSON) |
| 158 | @Path("{networkId}/devices") |
| 159 | public Response getVirtualDevices(@PathParam("networkId") long networkId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 160 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 161 | Set<VirtualDevice> vdevs = vnetService.getVirtualDevices(nid); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 162 | 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 Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 169 | * @param stream virtual device JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 170 | * @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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 175 | @Path("{networkId}/devices") |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 176 | @Consumes(MediaType.APPLICATION_JSON) |
| 177 | @Produces(MediaType.APPLICATION_JSON) |
| 178 | public Response createVirtualDevice(@PathParam("networkId") long networkId, |
| 179 | InputStream stream) { |
| 180 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 181 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 182 | final VirtualDevice vdevReq = codec(VirtualDevice.class).decode(jsonTree, this); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 183 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 184 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 185 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 186 | } |
| 187 | final VirtualDevice vdevRes = vnetAdminService.createVirtualDevice(vdevReq.networkId(), |
| 188 | vdevReq.id()); |
| 189 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 190 | .path("vnets").path(specifiedNetworkId.asText()) |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 191 | .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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 204 | * @param deviceId device identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 205 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 206 | */ |
| 207 | @DELETE |
| 208 | @Path("{networkId}/devices/{deviceId}") |
| 209 | public Response removeVirtualDevice(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 210 | @PathParam("deviceId") String deviceId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 211 | NetworkId nid = NetworkId.networkId(networkId); |
| 212 | DeviceId did = DeviceId.deviceId(deviceId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 213 | vnetAdminService.removeVirtualDevice(nid, did); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 214 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 215 | } |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 223 | * @param deviceId virtual device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 224 | * @return 200 OK with set of virtual ports, 404 not found |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 225 | * @onos.rsModel VirtualPorts |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 226 | */ |
| 227 | @GET |
| 228 | @Produces(MediaType.APPLICATION_JSON) |
| 229 | @Path("{networkId}/devices/{deviceId}/ports") |
| 230 | public Response getVirtualPorts(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 231 | @PathParam("deviceId") String deviceId) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 232 | NetworkId nid = NetworkId.networkId(networkId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 233 | Iterable<VirtualPort> vports = vnetService.getVirtualPorts(nid, DeviceId.deviceId(deviceId)); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 234 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 240 | * @param networkId network identifier |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 241 | * @param virtDeviceId virtual device identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 242 | * @param stream virtual port JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 243 | * @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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 252 | @PathParam("deviceId") String virtDeviceId, |
| 253 | InputStream stream) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 254 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 255 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 256 | // final VirtualPort vportReq = codec(VirtualPort.class).decode(jsonTree, this); |
| 257 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 258 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 259 | if (specifiedNetworkId == null || specifiedNetworkId.asLong() != (networkId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 260 | throw new IllegalArgumentException(INVALID_FIELD + "networkId"); |
| 261 | } |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 262 | if (specifiedDeviceId == null || !specifiedDeviceId.asText().equals(virtDeviceId)) { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 263 | 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 Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 270 | |
| 271 | ConnectPoint realizedBy = new ConnectPoint(DeviceId.deviceId(specifiedPhysDeviceId.asText()), |
| 272 | PortNumber.portNumber(specifiedPhysPortNum.asText())); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 273 | VirtualPort vport = vnetAdminService.createVirtualPort(nid, vdevId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 274 | PortNumber.portNumber(specifiedPortNum.asText()), realizedBy); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 275 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 291 | * @param deviceId virtual device identifier |
| 292 | * @param portNum virtual port number |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 293 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 294 | */ |
| 295 | @DELETE |
| 296 | @Path("{networkId}/devices/{deviceId}/ports/{portNum}") |
| 297 | public Response removeVirtualPort(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 298 | @PathParam("deviceId") String deviceId, |
| 299 | @PathParam("portNum") long portNum) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 300 | NetworkId nid = NetworkId.networkId(networkId); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 301 | vnetAdminService.removeVirtualPort(nid, DeviceId.deviceId(deviceId), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 302 | PortNumber.portNumber(portNum)); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 303 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 306 | // VirtualLink |
| 307 | |
| 308 | /** |
| 309 | * Returns all virtual network links in a virtual network. |
| 310 | * |
| 311 | * @param networkId network identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 312 | * @return 200 OK with set of virtual network links |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 313 | * @onos.rsModel VirtualLinks |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 314 | */ |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 320 | Set<VirtualLink> vlinks = vnetService.getVirtualLinks(nid); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 321 | return ok(encodeArray(VirtualLink.class, "links", vlinks)).build(); |
| 322 | } |
| 323 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 324 | /** |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 325 | * Creates a virtual network link from the JSON input stream. |
| 326 | * |
| 327 | * @param networkId network identifier |
Claudine Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 328 | * @param stream virtual link JSON stream |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 329 | * @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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 340 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 341 | 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 Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 346 | vnetAdminService.createVirtualLink(vlinkReq.networkId(), |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 347 | vlinkReq.src(), vlinkReq.dst()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 348 | 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 Chiu | 1decd53 | 2016-04-19 18:30:01 +0000 | [diff] [blame] | 363 | * @param stream virtual link JSON stream |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 364 | * @return 204 NO CONTENT |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 365 | * @onos.rsModel VirtualLink |
| 366 | */ |
| 367 | @DELETE |
| 368 | @Path("{networkId}/links") |
| 369 | @Consumes(MediaType.APPLICATION_JSON) |
| 370 | public Response removeVirtualLink(@PathParam("networkId") long networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 371 | InputStream stream) { |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 372 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 373 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 374 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 381 | vlinkReq.src(), vlinkReq.dst()); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 382 | } catch (IOException e) { |
| 383 | throw new IllegalArgumentException(e); |
| 384 | } |
| 385 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 386 | return Response.noContent().build(); |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 390 | * 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 421 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 422 | 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 455 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 456 | 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 Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 471 | * Get the tenant identifier from the JSON stream. |
| 472 | * |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 473 | * @param stream TenantId JSON stream |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 474 | * @param jsonFieldName field name |
| 475 | * @return JsonNode |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 476 | * @throws IOException if unable to parse the request |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 477 | */ |
| 478 | private JsonNode getFromJsonStream(InputStream stream, String jsonFieldName) throws IOException { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 479 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 480 | JsonNode jsonNode = jsonTree.get(jsonFieldName); |
| 481 | |
| 482 | if (jsonNode == null) { |
| 483 | throw new IllegalArgumentException(MISSING_FIELD + jsonFieldName); |
| 484 | } |
| 485 | return jsonNode; |
| 486 | } |
| 487 | } |