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.onlab.util.ItemNotFoundException; |
| 22 | import org.onosproject.incubator.net.virtual.TenantId; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.Context; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import javax.ws.rs.core.UriBuilder; |
| 37 | import javax.ws.rs.core.UriInfo; |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | |
| 41 | /** |
| 42 | * Query and manage tenants of virtual networks. |
| 43 | */ |
| 44 | @Path("tenants") |
| 45 | public class TenantWebResource extends AbstractWebResource { |
| 46 | |
| 47 | private static final String MISSING_TENANTID = "Missing tenant identifier"; |
| 48 | private static final String TENANTID_NOT_FOUND = "Tenant identifier not found"; |
| 49 | private static final String INVALID_TENANTID = "Invalid tenant identifier "; |
| 50 | |
| 51 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 52 | private UriInfo uriInfo; |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 53 | |
| 54 | private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class); |
| 55 | |
| 56 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 57 | * Returns all tenant identifiers. |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 58 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 59 | * @return 200 OK with set of tenant identifiers |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 60 | * @onos.rsModel TenantIds |
| 61 | */ |
| 62 | @GET |
| 63 | @Produces(MediaType.APPLICATION_JSON) |
Claudine Chiu | a98c2ea | 2016-04-19 16:09:18 +0000 | [diff] [blame] | 64 | public Response getVirtualNetworkTenantIds() { |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 65 | Iterable<TenantId> tenantIds = vnetAdminService.getTenantIds(); |
| 66 | return ok(encodeArray(TenantId.class, "tenants", tenantIds)).build(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a tenant with the given tenant identifier. |
| 71 | * |
| 72 | * @param stream TenantId JSON stream |
| 73 | * @return status of the request - CREATED if the JSON is correct, |
| 74 | * BAD_REQUEST if the JSON is invalid |
| 75 | * @onos.rsModel TenantId |
| 76 | */ |
| 77 | @POST |
| 78 | @Consumes(MediaType.APPLICATION_JSON) |
| 79 | @Produces(MediaType.APPLICATION_JSON) |
| 80 | public Response addTenantId(InputStream stream) { |
| 81 | try { |
| 82 | final TenantId tid = getTenantIdFromJsonStream(stream); |
| 83 | vnetAdminService.registerTenantId(tid); |
| 84 | final TenantId resultTid = getExistingTenantId(vnetAdminService, tid); |
| 85 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 86 | .path("tenants") |
| 87 | .path(resultTid.id()); |
| 88 | return Response |
| 89 | .created(locationBuilder.build()) |
| 90 | .build(); |
| 91 | } catch (IOException e) { |
| 92 | throw new IllegalArgumentException(e); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Removes the specified tenant with the specified tenant identifier. |
| 98 | * |
| 99 | * @param tenantId tenant identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 100 | * @return 204 NO CONTENT |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 101 | */ |
| 102 | @DELETE |
| 103 | @Path("{tenantId}") |
| 104 | public Response removeTenantId(@PathParam("tenantId") String tenantId) { |
| 105 | final TenantId tid = TenantId.tenantId(tenantId); |
| 106 | final TenantId existingTid = getExistingTenantId(vnetAdminService, tid); |
| 107 | vnetAdminService.unregisterTenantId(existingTid); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 108 | return Response.noContent().build(); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 112 | * Gets the tenant identifier from the JSON stream. |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 113 | * |
| 114 | * @param stream TenantId JSON stream |
| 115 | * @return TenantId |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 116 | * @throws IOException if unable to parse the request |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 117 | */ |
| 118 | private TenantId getTenantIdFromJsonStream(InputStream stream) throws IOException { |
| 119 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 120 | JsonNode specifiedTenantId = jsonTree.get("id"); |
| 121 | |
| 122 | if (specifiedTenantId == null) { |
| 123 | throw new IllegalArgumentException(MISSING_TENANTID); |
| 124 | } |
| 125 | return TenantId.tenantId(specifiedTenantId.asText()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the matching tenant identifier from existing tenant identifiers in system. |
| 130 | * |
Claudine Chiu | f6bf8d5 | 2016-04-08 01:31:54 +0000 | [diff] [blame] | 131 | * @param vnetAdminSvc virtual network administration service |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 132 | * @param tidIn tenant identifier |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 133 | * @return TenantId |
| 134 | */ |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 135 | protected static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc, |
| 136 | TenantId tidIn) { |
| 137 | return vnetAdminSvc |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 138 | .getTenantIds() |
| 139 | .stream() |
| 140 | .filter(tenantId -> tenantId.equals(tidIn)) |
| 141 | .findFirst() |
| 142 | .orElseThrow(() -> new ItemNotFoundException(TENANTID_NOT_FOUND)); |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 143 | } |
| 144 | } |