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 |
| 52 | UriInfo uriInfo; |
| 53 | |
| 54 | private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class); |
| 55 | |
| 56 | /** |
| 57 | * Returns all tenants. |
| 58 | * |
| 59 | * @return 200 OK |
| 60 | * @onos.rsModel TenantIds |
| 61 | */ |
| 62 | @GET |
| 63 | @Produces(MediaType.APPLICATION_JSON) |
| 64 | public Response getVirtualNetworkTenants() { |
| 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 |
| 100 | * @return 200 OK, 404 not found |
| 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); |
| 108 | return Response.ok().build(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Removes the specified tenant with the specified tenant identifier. |
| 113 | * |
| 114 | * @param stream deviceIds JSON stream |
| 115 | * @return 200 OK, 404 not found |
| 116 | * @onos.rsModel TenantId |
| 117 | */ |
| 118 | @DELETE |
| 119 | public Response removeTenantId(InputStream stream) { |
| 120 | try { |
| 121 | final TenantId tid = getTenantIdFromJsonStream(stream); |
| 122 | vnetAdminService.unregisterTenantId(tid); |
| 123 | } catch (IOException e) { |
| 124 | throw new IllegalArgumentException(e); |
| 125 | } |
| 126 | return Response.ok().build(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the tenant identifier from the JSON stream. |
| 131 | * |
| 132 | * @param stream TenantId JSON stream |
| 133 | * @return TenantId |
| 134 | * @throws IOException |
| 135 | */ |
| 136 | private TenantId getTenantIdFromJsonStream(InputStream stream) throws IOException { |
| 137 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 138 | JsonNode specifiedTenantId = jsonTree.get("id"); |
| 139 | |
| 140 | if (specifiedTenantId == null) { |
| 141 | throw new IllegalArgumentException(MISSING_TENANTID); |
| 142 | } |
| 143 | return TenantId.tenantId(specifiedTenantId.asText()); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the matching tenant identifier from existing tenant identifiers in system. |
| 148 | * |
| 149 | * @param vnetAdminSvc |
| 150 | * @param tidIn tenant identifier |
| 151 | * @return TenantId |
| 152 | */ |
| 153 | private static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc, |
| 154 | TenantId tidIn) { |
| 155 | final TenantId resultTid = vnetAdminSvc |
| 156 | .getTenantIds() |
| 157 | .stream() |
| 158 | .filter(tenantId -> tenantId.equals(tidIn)) |
| 159 | .findFirst() |
| 160 | .orElseThrow(() -> new ItemNotFoundException(TENANTID_NOT_FOUND)); |
| 161 | return resultTid; |
| 162 | } |
| 163 | } |