Change the header to SO
[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  * ================================================================================
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.adapters.tenant;
22
23
24 import java.util.Map;
25
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;
39
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;
57
58 /**
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.
61  */
62 @Path("/v1/tenants")
63 public class TenantAdapterRest {
64         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
65
66         //RAA? No logging in wrappers
67
68         @HEAD
69         @GET
70         @Path("/healthcheck")
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 ();
75         }
76
77         /*
78         URL:
79         EP: http://host:8080/tenants/rest
80         Resource: v1/tenants
81         REQ - metadata?
82         {
83         "cloudSiteId": "DAN",
84         "tenantName": "RAA_1",
85         "failIfExists": true,
86         "msoRequest": {
87         "requestId": "ra1",
88         "serviceInstanceId": "sa1"
89         }}
90         RESP-
91         {
92    "cloudSiteId": "DAN",
93    "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
94    "tenantCreated": true,
95    "tenantRollback":    {
96       "tenantId": "128e10b9996d43a7874f19bbc4eb6749",
97       "cloudId": "DAN", // RAA? cloudId instead of cloudSiteId
98       "tenantCreated": true,
99       "msoRequest":       {
100          "requestId": "ra1",
101          "serviceInstanceId": "sa1"
102       }
103          }
104         }
105         */
106         @POST
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());
111
112                 String newTenantId = null;
113                 TenantRollback tenantRollback = new TenantRollback ();
114
115                 try {
116                         Holder<String> htenant = new Holder<String>();
117                         Holder<TenantRollback> hrollback = new Holder<TenantRollback>();
118                         MsoTenantAdapter impl = new MsoTenantAdapterImpl();
119                     impl.createTenant(
120                         req.getCloudSiteId(),
121                         req.getTenantName(),
122                         req.getMetadata(),
123                         req.getFailIfExists(),
124                 req.getBackout(),
125                 req.getMsoRequest(),
126                 htenant,
127                 hrollback);
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(),
134 //                                                                                              req.getBackout(),
135 //                                                                                              req.getMetadata(),
136 //                                                                                              req.getMsoRequest(),
137 //                                                                                              tenantRollback);
138                 }
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();
142                 }
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();
146                 }
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();
150                 }
151
152                 CreateTenantResponse resp = new CreateTenantResponse (req.getCloudSiteId(), newTenantId, tenantRollback.getTenantCreated(), tenantRollback);
153                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
154         }
155
156         /*
157         URL:
158         http://host:8080/tenants/rest
159         Resource: v1/tenant/tennatId
160         REQ:
161         {"cloudSiteId": "DAN",
162         "tenantId": "ca84cd3d3df44272845da554656b3ace",
163         "msoRequest": {
164         "requestId": "ra1",
165         "serviceInstanceId": "sa1"
166         }
167         }
168         RESP:
169         {"tenantDeleted": true}
170          */
171         @DELETE
172         @Path("{tenantId}")
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)
178         {
179                 boolean tenantDeleted = false;
180
181                 try {
182                         Holder<Boolean> deleted = new Holder<Boolean>();
183                         MsoTenantAdapter impl = new MsoTenantAdapterImpl();
184                     impl.deleteTenant(
185                         req.getCloudSiteId(),
186                         req.getTenantId(),
187                         req.getMsoRequest(),
188                         deleted);
189                     tenantDeleted = deleted.value;
190                 }
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();
194                 }
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();
198                 }
199                 DeleteTenantResponse resp = new DeleteTenantResponse();
200                 resp.setTenantDeleted(tenantDeleted);
201                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
202         }
203
204         /*
205         URL
206         EP://http://host:8080/tenants/rest
207         Resource: /v1/tenants
208         Params:?tenantNameOrId=RAA_1&cloudSiteId=DAN
209         RESP
210         {
211                    "tenantId": "214b428a1f554c02935e66330f6a5409",
212                    "tenantName": "RAA_1",
213                    "metadata": {}
214         }
215         */
216         @GET
217         @Path("{tenantId}")
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)
225         {
226                 MsoTenant tenant = null;
227                 try {
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();
232                     impl.queryTenant(
233                         cloudSiteId,
234                         tenantId,
235                         null,
236                         htenant,
237                         tenantName,
238                         metadata
239                         );
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);
244                 }
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();
248                 }
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();
252                 }
253                 QueryTenantResponse resp = new QueryTenantResponse(tenant.getTenantId(), tenant.getTenantName(), tenant.getMetadata());
254                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
255         }
256
257         /*
258         URL
259         EP: //http://host:8080/tenants/rest
260         Resource: /v1/tenants/rollback
261         REQ
262         {"cloudSiteId": "DAN",
263         "tenantId": "f58abb05041d4ff384d4d22d1ccd2a6c",
264         "msoRequest": {
265         "requestId": "ra1",
266         "serviceInstanceId": "sa1"
267         }
268         }
269         RESP:
270         {"tenantDeleted": true}
271          */
272         @DELETE
273         @Path("")
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)
279         {
280                 try {
281                         MsoTenantAdapter impl = new MsoTenantAdapterImpl();
282                     impl.rollbackTenant(req.getTenantRollback());
283                 }
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();
287                 }
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();
291                 }
292
293                 RollbackTenantResponse resp = new RollbackTenantResponse ();
294                 resp.setTenantRolledback(req != null);
295                 return Response.status(HttpServletResponse.SC_OK).entity(resp).build();
296         }
297 }