blob: 768c3b980517bc1f0a8f07a187e7697d2f79ca8a [file] [log] [blame]
Claudine Chiufb8b8162016-04-01 23:50:51 +00001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-present Open Networking Foundation
Claudine Chiufb8b8162016-04-01 23:50:51 +00003 *
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
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080017package org.onosproject.incubator.net.virtual.rest;
Claudine Chiufb8b8162016-04-01 23:50:51 +000018
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.util.ItemNotFoundException;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080022import org.onosproject.net.TenantId;
Claudine Chiufb8b8162016-04-01 23:50:51 +000023import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24import org.onosproject.rest.AbstractWebResource;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.Context;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import javax.ws.rs.core.UriBuilder;
37import javax.ws.rs.core.UriInfo;
38import java.io.IOException;
39import java.io.InputStream;
40
Ray Milkey86ee5e82018-04-02 15:33:07 -070041import static org.onlab.util.Tools.readTreeFromStream;
42
Claudine Chiufb8b8162016-04-01 23:50:51 +000043/**
44 * Query and manage tenants of virtual networks.
45 */
46@Path("tenants")
47public class TenantWebResource extends AbstractWebResource {
48
49 private static final String MISSING_TENANTID = "Missing tenant identifier";
50 private static final String TENANTID_NOT_FOUND = "Tenant identifier not found";
51 private static final String INVALID_TENANTID = "Invalid tenant identifier ";
52
53 @Context
Jian Licc730a62016-05-10 16:36:16 -070054 private UriInfo uriInfo;
Claudine Chiufb8b8162016-04-01 23:50:51 +000055
56 private final VirtualNetworkAdminService vnetAdminService = get(VirtualNetworkAdminService.class);
57
58 /**
Jian Licc730a62016-05-10 16:36:16 -070059 * Returns all tenant identifiers.
Claudine Chiufb8b8162016-04-01 23:50:51 +000060 *
Jian Licc730a62016-05-10 16:36:16 -070061 * @return 200 OK with set of tenant identifiers
Claudine Chiufb8b8162016-04-01 23:50:51 +000062 * @onos.rsModel TenantIds
63 */
64 @GET
65 @Produces(MediaType.APPLICATION_JSON)
Claudine Chiua98c2ea2016-04-19 16:09:18 +000066 public Response getVirtualNetworkTenantIds() {
Claudine Chiufb8b8162016-04-01 23:50:51 +000067 Iterable<TenantId> tenantIds = vnetAdminService.getTenantIds();
68 return ok(encodeArray(TenantId.class, "tenants", tenantIds)).build();
69 }
70
71 /**
72 * Creates a tenant with the given tenant identifier.
73 *
74 * @param stream TenantId JSON stream
75 * @return status of the request - CREATED if the JSON is correct,
76 * BAD_REQUEST if the JSON is invalid
77 * @onos.rsModel TenantId
78 */
79 @POST
80 @Consumes(MediaType.APPLICATION_JSON)
81 @Produces(MediaType.APPLICATION_JSON)
82 public Response addTenantId(InputStream stream) {
83 try {
84 final TenantId tid = getTenantIdFromJsonStream(stream);
85 vnetAdminService.registerTenantId(tid);
86 final TenantId resultTid = getExistingTenantId(vnetAdminService, tid);
87 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
88 .path("tenants")
89 .path(resultTid.id());
90 return Response
91 .created(locationBuilder.build())
92 .build();
93 } catch (IOException e) {
94 throw new IllegalArgumentException(e);
95 }
96 }
97
98 /**
99 * Removes the specified tenant with the specified tenant identifier.
100 *
101 * @param tenantId tenant identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700102 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000103 */
104 @DELETE
105 @Path("{tenantId}")
106 public Response removeTenantId(@PathParam("tenantId") String tenantId) {
107 final TenantId tid = TenantId.tenantId(tenantId);
108 final TenantId existingTid = getExistingTenantId(vnetAdminService, tid);
109 vnetAdminService.unregisterTenantId(existingTid);
Jian Lic2a542b2016-05-10 11:48:19 -0700110 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000111 }
112
113 /**
Jian Licc730a62016-05-10 16:36:16 -0700114 * Gets the tenant identifier from the JSON stream.
Claudine Chiufb8b8162016-04-01 23:50:51 +0000115 *
116 * @param stream TenantId JSON stream
117 * @return TenantId
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000118 * @throws IOException if unable to parse the request
Claudine Chiufb8b8162016-04-01 23:50:51 +0000119 */
120 private TenantId getTenantIdFromJsonStream(InputStream stream) throws IOException {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700121 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Claudine Chiufb8b8162016-04-01 23:50:51 +0000122 JsonNode specifiedTenantId = jsonTree.get("id");
123
124 if (specifiedTenantId == null) {
125 throw new IllegalArgumentException(MISSING_TENANTID);
126 }
127 return TenantId.tenantId(specifiedTenantId.asText());
128 }
129
130 /**
131 * Get the matching tenant identifier from existing tenant identifiers in system.
132 *
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000133 * @param vnetAdminSvc virtual network administration service
Jian Licc730a62016-05-10 16:36:16 -0700134 * @param tidIn tenant identifier
Claudine Chiufb8b8162016-04-01 23:50:51 +0000135 * @return TenantId
136 */
Jian Licc730a62016-05-10 16:36:16 -0700137 protected static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc,
138 TenantId tidIn) {
139 return vnetAdminSvc
Claudine Chiufb8b8162016-04-01 23:50:51 +0000140 .getTenantIds()
141 .stream()
142 .filter(tenantId -> tenantId.equals(tidIn))
143 .findFirst()
144 .orElseThrow(() -> new ItemNotFoundException(TENANTID_NOT_FOUND));
Claudine Chiufb8b8162016-04-01 23:50:51 +0000145 }
146}