blob: 83c6c4d444eb1ee8dae7244eb78e52089e0000f7 [file] [log] [blame]
Claudine Chiufb8b8162016-04-01 23:50:51 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.rest.resources;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.util.ItemNotFoundException;
22import org.onosproject.incubator.net.virtual.TenantId;
23import 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
41/**
42 * Query and manage tenants of virtual networks.
43 */
44@Path("tenants")
45public 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)
Claudine Chiua98c2ea2016-04-19 16:09:18 +000064 public Response getVirtualNetworkTenantIds() {
Claudine Chiufb8b8162016-04-01 23:50:51 +000065 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 Lic2a542b2016-05-10 11:48:19 -0700100 * @return 204 NO CONTENT
Claudine Chiufb8b8162016-04-01 23:50:51 +0000101 */
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 Lic2a542b2016-05-10 11:48:19 -0700108 return Response.noContent().build();
Claudine Chiufb8b8162016-04-01 23:50:51 +0000109 }
110
111 /**
Claudine Chiufb8b8162016-04-01 23:50:51 +0000112 * Get the tenant identifier from the JSON stream.
113 *
114 * @param stream TenantId JSON stream
115 * @return TenantId
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000116 * @throws IOException if unable to parse the request
Claudine Chiufb8b8162016-04-01 23:50:51 +0000117 */
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 Chiuf6bf8d52016-04-08 01:31:54 +0000131 * @param vnetAdminSvc virtual network administration service
Claudine Chiufb8b8162016-04-01 23:50:51 +0000132 * @param tidIn tenant identifier
133 * @return TenantId
134 */
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000135 static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc,
Claudine Chiufb8b8162016-04-01 23:50:51 +0000136 TenantId tidIn) {
137 final TenantId resultTid = vnetAdminSvc
138 .getTenantIds()
139 .stream()
140 .filter(tenantId -> tenantId.equals(tidIn))
141 .findFirst()
142 .orElseThrow(() -> new ItemNotFoundException(TENANTID_NOT_FOUND));
143 return resultTid;
144 }
145}