2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.adapters.tenant;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.DELETE;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.HEAD;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import javax.xml.ws.Holder;
40 import org.openecomp.mso.adapters.tenant.exceptions.TenantAlreadyExists;
41 import org.openecomp.mso.adapters.tenant.exceptions.TenantException;
42 import org.openecomp.mso.adapters.tenantrest.CreateTenantError;
43 import org.openecomp.mso.adapters.tenantrest.CreateTenantRequest;
44 import org.openecomp.mso.adapters.tenantrest.CreateTenantResponse;
45 import org.openecomp.mso.adapters.tenantrest.DeleteTenantError;
46 import org.openecomp.mso.adapters.tenantrest.DeleteTenantRequest;
47 import org.openecomp.mso.adapters.tenantrest.DeleteTenantResponse;
48 import org.openecomp.mso.adapters.tenantrest.QueryTenantError;
49 import org.openecomp.mso.adapters.tenantrest.QueryTenantResponse;
50 import org.openecomp.mso.adapters.tenantrest.RollbackTenantError;
51 import org.openecomp.mso.adapters.tenantrest.RollbackTenantRequest;
52 import org.openecomp.mso.adapters.tenantrest.RollbackTenantResponse;
53 import org.openecomp.mso.adapters.tenantrest.TenantRollback;
54 import org.openecomp.mso.logger.MsoLogger;
55 import org.openecomp.mso.openstack.beans.MsoTenant;
56 import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;
59 * This class services calls to the REST interface for Tenants (http://host:port/vnfs/rest/v1/tenants)
60 * Both XML and JSON can be produced/consumed. Set Accept: and Content-Type: headers appropriately. XML is the default.
63 public class TenantAdapterRest {
64 private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
66 //RAA? No logging in wrappers
71 @Produces(MediaType.TEXT_HTML)
72 public Response healthcheck () {
73 String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application ready</body></html>";
74 return Response.status (HttpServletResponse.SC_OK).entity (CHECK_HTML).build ();
79 EP: http://host:8080/tenants/rest
84 "tenantName": "RAA_1",
88 "serviceInstanceId": "sa1"
93 "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
94 "tenantCreated": true,
96 "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
97 "cloudId": "DAN", // RAA? cloudId instead of cloudSiteId
98 "tenantCreated": true,
101 "serviceInstanceId": "sa1"
107 @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
108 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
109 public Response createTenant(CreateTenantRequest req) {
110 LOGGER.debug("createTenant enter: " + req.toJsonString());
112 String newTenantId = null;
113 TenantRollback tenantRollback = new TenantRollback ();
116 Holder<String> htenant = new Holder<String>();
117 Holder<TenantRollback> hrollback = new Holder<TenantRollback>();
118 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
120 req.getCloudSiteId(),
123 req.getFailIfExists(),
128 newTenantId = htenant.value;
129 tenantRollback = hrollback.value;
130 // TenantAdapterCore TAImpl = new TenantAdapterCore();
131 // newTenantId = TAImpl.createTenant (req.getCloudSiteId(),
132 // req.getTenantName(),
133 // req.getFailIfExists(),
135 // req.getMetadata(),
136 // req.getMsoRequest(),
139 catch (TenantAlreadyExists tae) {
140 CreateTenantError exc = new CreateTenantError(tae.getMessage(), tae.getFaultInfo().getCategory(), Boolean.TRUE);
141 return Response.status(HttpServletResponse.SC_NOT_IMPLEMENTED).entity(exc).build();
143 catch (TenantException te) {
144 CreateTenantError exc = new CreateTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
145 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
147 catch (Exception e) {
148 CreateTenantError exc = new CreateTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
149 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
152 CreateTenantResponse resp = new CreateTenantResponse (req.getCloudSiteId(), newTenantId, tenantRollback.getTenantCreated(), tenantRollback);
153 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
158 http://host:8080/tenants/rest
159 Resource: v1/tenant/tennatId
161 {"cloudSiteId": "DAN",
162 "tenantId": "ca84cd3d3df44272845da554656b3ace",
165 "serviceInstanceId": "sa1"
169 {"tenantDeleted": true}
173 @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
174 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
175 public Response deleteTenant(
176 @PathParam("tenantId") String tenantId,
177 DeleteTenantRequest req)
179 boolean tenantDeleted = false;
182 Holder<Boolean> deleted = new Holder<Boolean>();
183 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
185 req.getCloudSiteId(),
189 tenantDeleted = deleted.value;
191 catch (TenantException te) {
192 DeleteTenantError exc = new DeleteTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
193 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
195 catch (Exception e) {
196 DeleteTenantError exc = new DeleteTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
197 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
199 DeleteTenantResponse resp = new DeleteTenantResponse();
200 resp.setTenantDeleted(tenantDeleted);
201 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
206 EP://http://host:8080/tenants/rest
207 Resource: /v1/tenants
208 Params:?tenantNameOrId=RAA_1&cloudSiteId=DAN
211 "tenantId": "214b428a1f554c02935e66330f6a5409",
212 "tenantName": "RAA_1",
218 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
219 public Response queryTenant(
220 @PathParam("tenantId") String tenantId,
221 // @QueryParam("tenantNameOrId") String tenantNameOrId, //RAA? diff from doc
222 @QueryParam("cloudSiteId") String cloudSiteId,
223 @QueryParam("msoRequest.requestId") String requestId,
224 @QueryParam("msoRequest.serviceInstanceId") String serviceInstanceId)
226 MsoTenant tenant = null;
228 Holder<String> htenant = new Holder<String>();
229 Holder<String> tenantName = new Holder<String>();
230 Holder<Map<String,String>> metadata = new Holder<Map<String,String>>();
231 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
240 tenant = new MsoTenant(htenant.value, tenantName.value, metadata.value);
241 // TenantAdapterCore TAImpl = new TenantAdapterCore();
242 // MsoRequest msoReq = new MsoRequest();
243 // tenant = TAImpl.queryTenant (cloudSiteId, tenantId, msoReq);
245 catch (TenantException te) {
246 QueryTenantError exc = new QueryTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory());
247 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
249 catch (Exception e) {
250 QueryTenantError exc = new QueryTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL);
251 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
253 QueryTenantResponse resp = new QueryTenantResponse(tenant.getTenantId(), tenant.getTenantName(), tenant.getMetadata());
254 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
259 EP: //http://host:8080/tenants/rest
260 Resource: /v1/tenants/rollback
262 {"cloudSiteId": "DAN",
263 "tenantId": "f58abb05041d4ff384d4d22d1ccd2a6c",
266 "serviceInstanceId": "sa1"
270 {"tenantDeleted": true}
274 @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
275 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
276 public Response rollbackTenant(
277 @QueryParam("rollback") String action, // WTF?
278 RollbackTenantRequest req)
281 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
282 impl.rollbackTenant(req.getTenantRollback());
284 catch (TenantException te) {
285 RollbackTenantError exc = new RollbackTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
286 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
288 catch (Exception e) {
289 RollbackTenantError exc = new RollbackTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
290 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
293 RollbackTenantResponse resp = new RollbackTenantResponse ();
294 resp.setTenantRolledback(req != null);
295 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();