2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.mso.adapters.tenant;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.HEAD;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.QueryParam;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import javax.xml.ws.Holder;
41 import org.openecomp.mso.adapters.tenant.exceptions.TenantAlreadyExists;
42 import org.openecomp.mso.adapters.tenant.exceptions.TenantException;
43 import org.openecomp.mso.adapters.tenantrest.CreateTenantError;
44 import org.openecomp.mso.adapters.tenantrest.CreateTenantRequest;
45 import org.openecomp.mso.adapters.tenantrest.CreateTenantResponse;
46 import org.openecomp.mso.adapters.tenantrest.DeleteTenantError;
47 import org.openecomp.mso.adapters.tenantrest.DeleteTenantRequest;
48 import org.openecomp.mso.adapters.tenantrest.DeleteTenantResponse;
49 import org.openecomp.mso.adapters.tenantrest.QueryTenantError;
50 import org.openecomp.mso.adapters.tenantrest.QueryTenantResponse;
51 import org.openecomp.mso.adapters.tenantrest.RollbackTenantError;
52 import org.openecomp.mso.adapters.tenantrest.RollbackTenantRequest;
53 import org.openecomp.mso.adapters.tenantrest.RollbackTenantResponse;
54 import org.openecomp.mso.adapters.tenantrest.TenantRollback;
55 import org.openecomp.mso.logger.MsoLogger;
56 import org.openecomp.mso.openstack.beans.MsoTenant;
57 import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;
60 * This class services calls to the REST interface for Tenants (http://host:port/vnfs/rest/v1/tenants)
61 * Both XML and JSON can be produced/consumed. Set Accept: and Content-Type: headers appropriately. XML is the default.
64 public class TenantAdapterRest {
65 private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
67 //RAA? No logging in wrappers
72 @Produces(MediaType.TEXT_HTML)
73 public Response healthcheck () {
74 String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application ready</body></html>";
75 return Response.status (HttpServletResponse.SC_OK).entity (CHECK_HTML).build ();
80 EP: http://host:8080/tenants/rest
85 "tenantName": "RAA_1",
89 "serviceInstanceId": "sa1"
94 "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
95 "tenantCreated": true,
97 "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
98 "cloudId": "DAN", // RAA? cloudId instead of cloudSiteId
99 "tenantCreated": true,
102 "serviceInstanceId": "sa1"
108 @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
109 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
110 public Response createTenant(CreateTenantRequest req) {
111 LOGGER.debug("createTenant enter: " + req.toJsonString());
113 String newTenantId = null;
114 TenantRollback tenantRollback = new TenantRollback ();
117 Holder<String> htenant = new Holder<>();
118 Holder<TenantRollback> hrollback = new Holder<>();
119 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
121 req.getCloudSiteId(),
124 req.getFailIfExists(),
129 newTenantId = htenant.value;
130 tenantRollback = hrollback.value;
131 // TenantAdapterCore TAImpl = new TenantAdapterCore();
132 // newTenantId = TAImpl.createTenant (req.getCloudSiteId(),
133 // req.getTenantName(),
134 // req.getFailIfExists(),
136 // req.getMetadata(),
137 // req.getMsoRequest(),
140 catch (TenantAlreadyExists tae) {
141 LOGGER.debug("Exception :",tae);
142 CreateTenantError exc = new CreateTenantError(tae.getMessage(), tae.getFaultInfo().getCategory(), Boolean.TRUE);
143 return Response.status(HttpServletResponse.SC_NOT_IMPLEMENTED).entity(exc).build();
145 catch (TenantException te) {
146 LOGGER.debug("Exception :",te);
147 CreateTenantError exc = new CreateTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
148 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
150 catch (Exception e) {
151 LOGGER.debug("Exception :",e);
152 CreateTenantError exc = new CreateTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
153 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
156 CreateTenantResponse resp = new CreateTenantResponse (req.getCloudSiteId(), newTenantId, tenantRollback.getTenantCreated(), tenantRollback);
157 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
162 http://host:8080/tenants/rest
163 Resource: v1/tenant/tennatId
165 {"cloudSiteId": "DAN",
166 "tenantId": "ca84cd3d3df44272845da554656b3ace",
169 "serviceInstanceId": "sa1"
173 {"tenantDeleted": true}
177 @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
178 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
179 public Response deleteTenant(
180 @PathParam("tenantId") String tenantId,
181 DeleteTenantRequest req)
183 boolean tenantDeleted = false;
186 Holder<Boolean> deleted = new Holder<>();
187 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
189 req.getCloudSiteId(),
193 tenantDeleted = deleted.value;
195 catch (TenantException te) {
196 LOGGER.debug("Exception :",te);
197 DeleteTenantError exc = new DeleteTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
198 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
200 catch (Exception e) {
201 LOGGER.debug("Exception :",e);
202 DeleteTenantError exc = new DeleteTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
203 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
205 DeleteTenantResponse resp = new DeleteTenantResponse();
206 resp.setTenantDeleted(tenantDeleted);
207 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
212 EP://http://host:8080/tenants/rest
213 Resource: /v1/tenants
214 Params:?tenantNameOrId=RAA_1&cloudSiteId=DAN
217 "tenantId": "214b428a1f554c02935e66330f6a5409",
218 "tenantName": "RAA_1",
224 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
225 public Response queryTenant(
226 @PathParam("tenantId") String tenantId,
227 // @QueryParam("tenantNameOrId") String tenantNameOrId, //RAA? diff from doc
228 @QueryParam("cloudSiteId") String cloudSiteId,
229 @QueryParam("msoRequest.requestId") String requestId,
230 @QueryParam("msoRequest.serviceInstanceId") String serviceInstanceId)
232 MsoTenant tenant = null;
234 Holder<String> htenant = new Holder<>();
235 Holder<String> tenantName = new Holder<>();
236 Holder<Map<String,String>> metadata = new Holder<>();
237 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
246 tenant = new MsoTenant(htenant.value, tenantName.value, metadata.value);
247 // TenantAdapterCore TAImpl = new TenantAdapterCore();
248 // MsoRequest msoReq = new MsoRequest();
249 // tenant = TAImpl.queryTenant (cloudSiteId, tenantId, msoReq);
251 catch (TenantException te) {
252 LOGGER.debug("Exception :",te);
253 QueryTenantError exc = new QueryTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory());
254 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
256 catch (Exception e) {
257 LOGGER.debug("Exception :",e);
258 QueryTenantError exc = new QueryTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL);
259 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
261 QueryTenantResponse resp = new QueryTenantResponse(tenant.getTenantId(), tenant.getTenantName(), tenant.getMetadata());
262 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
267 EP: //http://host:8080/tenants/rest
268 Resource: /v1/tenants/rollback
270 {"cloudSiteId": "DAN",
271 "tenantId": "f58abb05041d4ff384d4d22d1ccd2a6c",
274 "serviceInstanceId": "sa1"
278 {"tenantDeleted": true}
282 @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
283 @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
284 public Response rollbackTenant(
285 @QueryParam("rollback") String action, // WTF?
286 RollbackTenantRequest req)
289 MsoTenantAdapter impl = new MsoTenantAdapterImpl();
290 impl.rollbackTenant(req.getTenantRollback());
292 catch (TenantException te) {
293 LOGGER.debug("Exception :",te);
294 RollbackTenantError exc = new RollbackTenantError(te.getFaultInfo().getMessage(), te.getFaultInfo().getCategory(), Boolean.TRUE);
295 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
297 catch (Exception e) {
298 LOGGER.debug("Exception :",e);
299 RollbackTenantError exc = new RollbackTenantError(e.getMessage(), MsoExceptionCategory.INTERNAL, Boolean.TRUE);
300 return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(exc).build();
303 RollbackTenantResponse resp = new RollbackTenantResponse ();
304 resp.setTenantRolledback(req != null);
305 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();