Merge "Reorder modifiers"
[so.git] / adapters / mso-tenant-adapter / src / main / java / org / openecomp / mso / adapters / tenant / TenantAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.mso.adapters.tenant;
23
24
25 import java.util.Map;
26
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;
40
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;
58
59 /**
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.
62  */
63 @Path("/v1/tenants")
64 public class TenantAdapterRest {
65         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
66
67         //RAA? No logging in wrappers
68
69         @HEAD
70         @GET
71         @Path("/healthcheck")
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 ();
76         }
77
78         /*
79         URL:
80         EP: http://host:8080/tenants/rest
81         Resource: v1/tenants
82         REQ - metadata?
83         {
84         "cloudSiteId": "DAN",
85         "tenantName": "RAA_1",
86         "failIfExists": true,
87         "msoRequest": {
88         "requestId": "ra1",
89         "serviceInstanceId": "sa1"
90         }}
91         RESP-
92         {
93    "cloudSiteId": "DAN",
94    "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
95    "tenantCreated": true,
96    "tenantRollback":    {
97       "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
98       "cloudId": "DAN", // RAA? cloudId instead of cloudSiteId
99       "tenantCreated": true,
100       "msoRequest":       {
101          "requestId": "ra1",
102          "serviceInstanceId": "sa1"
103       }
104          }
105         }
106         */
107         @POST
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());
112
113                 String newTenantId = null;
114                 TenantRollback tenantRollback = new TenantRollback ();
115
116                 try {
117                         Holder<String> htenant = new Holder<>();
118                         Holder<TenantRollback> hrollback = new Holder<>();
119                         MsoTenantAdapter impl = new MsoTenantAdapterImpl();
120                     impl.createTenant(
121                         req.getCloudSiteId(),
122                         req.getTenantName(),
123                         req.getMetadata(),
124                         req.getFailIfExists(),
125                 req.getBackout(),
126                 req.getMsoRequest(),
127                 htenant,
128                 hrollback);
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(),
135 //                                                                                              req.getBackout(),
136 //                                                                                              req.getMetadata(),
137 //                                                                                              req.getMsoRequest(),
138 //                                                                                              tenantRollback);
139                 }
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();
144                 }
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();
149                 }
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();
154                 }
155
156                 CreateTenantResponse resp = new CreateTenantResponse (req.getCloudSiteId(), newTenantId, tenantRollback.getTenantCreated(), tenantRollback);
157                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
158         }
159
160         /*
161         URL:
162         http://host:8080/tenants/rest
163         Resource: v1/tenant/tennatId
164         REQ:
165         {"cloudSiteId": "DAN",
166         "tenantId": "ca84cd3d3df44272845da554656b3ace",
167         "msoRequest": {
168         "requestId": "ra1",
169         "serviceInstanceId": "sa1"
170         }
171         }
172         RESP:
173         {"tenantDeleted": true}
174          */
175         @DELETE
176         @Path("{tenantId}")
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)
182         {
183                 boolean tenantDeleted = false;
184
185                 try {
186                         Holder<Boolean> deleted = new Holder<>();
187                         MsoTenantAdapter impl = new MsoTenantAdapterImpl();
188                     impl.deleteTenant(
189                         req.getCloudSiteId(),
190                         req.getTenantId(),
191                         req.getMsoRequest(),
192                         deleted);
193                     tenantDeleted = deleted.value;
194                 }
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();
199                 }
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();
204                 }
205                 DeleteTenantResponse resp = new DeleteTenantResponse();
206                 resp.setTenantDeleted(tenantDeleted);
207                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
208         }
209
210         /*
211         URL
212         EP://http://host:8080/tenants/rest
213         Resource: /v1/tenants
214         Params:?tenantNameOrId=RAA_1&cloudSiteId=DAN
215         RESP
216         {
217                    "tenantId": "214b428a1f554c02935e66330f6a5409",
218                    "tenantName": "RAA_1",
219                    "metadata": {}
220         }
221         */
222         @GET
223         @Path("{tenantId}")
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)
231         {
232                 MsoTenant tenant = null;
233                 try {
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();
238                     impl.queryTenant(
239                         cloudSiteId,
240                         tenantId,
241                         null,
242                         htenant,
243                         tenantName,
244                         metadata
245                         );
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);
250                 }
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();
255                 }
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();
260                 }
261                 QueryTenantResponse resp = new QueryTenantResponse(tenant.getTenantId(), tenant.getTenantName(), tenant.getMetadata());
262                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
263         }
264
265         /*
266         URL
267         EP: //http://host:8080/tenants/rest
268         Resource: /v1/tenants/rollback
269         REQ
270         {"cloudSiteId": "DAN",
271         "tenantId": "f58abb05041d4ff384d4d22d1ccd2a6c",
272         "msoRequest": {
273         "requestId": "ra1",
274         "serviceInstanceId": "sa1"
275         }
276         }
277         RESP:
278         {"tenantDeleted": true}
279          */
280         @DELETE
281         @Path("")
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)
287         {
288                 try {
289                         MsoTenantAdapter impl = new MsoTenantAdapterImpl();
290                     impl.rollbackTenant(req.getTenantRollback());
291                 }
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();
296                 }
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();
301                 }
302
303                 RollbackTenantResponse resp = new RollbackTenantResponse ();
304                 resp.setTenantRolledback(req != null);
305                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
306         }
307 }