blob: dc15be58d835583259540d2411d350ba665d9327 [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)
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
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000134 * @throws IOException if unable to parse the request
Claudine Chiufb8b8162016-04-01 23:50:51 +0000135 */
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 *
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000149 * @param vnetAdminSvc virtual network administration service
Claudine Chiufb8b8162016-04-01 23:50:51 +0000150 * @param tidIn tenant identifier
151 * @return TenantId
152 */
Claudine Chiuf6bf8d52016-04-08 01:31:54 +0000153 static TenantId getExistingTenantId(VirtualNetworkAdminService vnetAdminSvc,
Claudine Chiufb8b8162016-04-01 23:50:51 +0000154 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}