20198d15434798bb9e0dd77d9e824581c9f9d84e
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / openecomp / mso / adapters / catalogdb / CatalogDbAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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 package org.openecomp.mso.adapters.catalogdb;
21
22 /*
23 Create an initial query to retrieve a VNF Resource definition (including a list of possible module types)
24 within the context of a given service. Input is a vnf resource model customization ID (new field for 1702),
25 or a composite key (from 1610) of service name, service version, vnf instance name
26
27 Returns a structure (JSON?) containing VNF RESOURCE attributes, plus a list of VF Module structures.
28
29 Query a NETWORK_RESOURCE from the MSO Catalog, based on a networkModelCustomizationUUID (new for 1702),
30 a network type (unique type identifier in 1610), or based on network role within a service.
31
32 Create Adapter framework for access to Catalog DB, including connection management,
33 login/password access, transaction logic, etc. This can be modeled after the Request DB Adapter
34
35 Update the MSO Catalog DB schema to include the new fields defined in this user story.
36
37 Note that the resourceModelCustomizationUUID (or vfModuleModelCustomizationUUID) will be unique keys (indexes)
38 on the VNF_RESOURCE and VF_MODULE tables respectively.
39 The previously constructed "vnf-type" and "vf-module-type" field may continue to be populated,
40 but should no longer be needed and can deprecate in future release.
41
42 For migration, a new randomly generated UUID field may be generated for the *ModelCustomizationUUID" fields
43 until such time that the model is redistributed from ASDC.
44
45 All other fields Check with Mike Z for appropriate value for the vfModuleLabel.
46 We might be able to derive it's value from the current vnf-type (using the "middle" piece that identifies the module type).
47
48 min and initial counts can be 0. max can be null to indicate no maximum.
49
50 Once the network-level distribution artifacts are defined, similar updates can be made to the NETWORK_RESOURCE table.
51 */
52
53 import java.util.Map;
54 import java.util.List;
55
56 import javax.ws.rs.Consumes;
57 import javax.ws.rs.DELETE;
58 import javax.ws.rs.GET;
59 import javax.ws.rs.HEAD;
60 import javax.ws.rs.POST;
61 import javax.ws.rs.PUT;
62 import javax.ws.rs.Path;
63 import javax.ws.rs.PathParam;
64 import javax.ws.rs.Produces;
65 import javax.ws.rs.QueryParam;
66 import javax.ws.rs.core.Response;
67 import javax.ws.rs.core.GenericEntity;
68 import javax.ws.rs.core.HttpHeaders;
69 import javax.ws.rs.core.MediaType;
70 import javax.xml.ws.Holder;
71
72 import org.apache.http.HttpStatus;
73
74 import org.openecomp.mso.logger.MessageEnum;
75 import org.openecomp.mso.logger.MsoLogger;
76 import org.openecomp.mso.adapters.catalogdb.catalogrest.CatalogQueryException;
77 import org.openecomp.mso.adapters.catalogdb.catalogrest.CatalogQueryExceptionCategory;
78 import org.openecomp.mso.adapters.catalogdb.catalogrest.CatalogQuery;
79 import org.openecomp.mso.adapters.catalogdb.catalogrest.QueryServiceVnfs;
80 import org.openecomp.mso.adapters.catalogdb.catalogrest.QueryServiceNetworks;
81 import org.openecomp.mso.adapters.catalogdb.catalogrest.QueryServiceMacroHolder;
82 import org.openecomp.mso.adapters.catalogdb.catalogrest.QueryAllottedResourceCustomization;
83 import org.openecomp.mso.db.catalog.CatalogDatabase;
84 import org.openecomp.mso.db.catalog.beans.VnfResource;
85 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
86 import org.openecomp.mso.db.catalog.beans.ServiceMacroHolder;
87 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
88
89 /**
90  * This class services calls to the REST interface for VF Modules (http://host:port/ecomp/mso/catalog/v1)
91  * Both XML and JSON can be produced/consumed.  Set Accept: and Content-Type: headers appropriately.  XML is the default.
92  * Requests respond synchronously only
93  */
94 @Path("/{version: v[0-9]+}")
95 public class CatalogDbAdapterRest {
96         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
97         private static final boolean IS_ARRAY = true;
98
99         public Response respond(String version, int respStatus, boolean isArray, CatalogQuery qryResp) {
100                 return Response
101                                 .status(respStatus)
102                                 //.entity(new GenericEntity<QueryServiceVnfs>(qryResp) {})
103                                 .entity(qryResp.toJsonString(version, isArray))
104                                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
105                                 .build();
106         }
107
108         @HEAD
109         @GET
110         @Path("healthcheck")
111         @Produces(MediaType.TEXT_HTML)
112         public Response healthcheck (
113                         @PathParam("version") String version
114         ) {
115                 String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title>Health Check</title></head><body>Application "+ version+ " ready</body></html>";
116                 return Response.ok().entity(CHECK_HTML).build();
117         }
118
119         /*
120          * GET {http-catalog-adapter-root}/v1/serviceVnfs?vnfModelCustomizationUuid=<vnf-model-customization-uuid>
121          * URL:http://localhost:8080/ecomp/mso/catalog/v1/getVfModuleType?vnfType=Test/vSAMP10&vfModuleType=vSAMP10::base::module-0
122          * RESP:
123          * {"queryVfModule":{"version":1,"asdcUuid":"MANUAL RECORD","created":{"nanos":0},"description":"vSAMP10","environmentId":15184,"id":2312,"isBase":1,"modelName":"vSAMP10::base::module-0","modelVersion":1,"templateId":15123,"type":"Test\/vSAMP10::vSAMP10::base::module-0","vnfResourceId":15187}}
124          */
125
126         @GET
127         @Path("vnfs/{vnfModelCustomizationUuid}")
128         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
129         public Response serviceVnfs (
130                         @PathParam("version") String version,
131                         @PathParam("vnfModelCustomizationUuid") String vnfUuid
132         ) {
133                 return serviceVnfsImpl (version, !IS_ARRAY, vnfUuid, null, null, null, null);
134         }
135
136         @GET
137         @Path("serviceVnfs")
138         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
139         public Response serviceVnfs(
140                         @PathParam("version") String version,
141                         @QueryParam("vnfModelCustomizationUuid") String vnfUuid,
142                         @QueryParam("serviceModelUuid") String smUuid,
143                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
144                         @QueryParam("serviceModelVersion") String smVer,
145                         @QueryParam("serviceModelName") String smName
146         ) {
147                 return serviceVnfsImpl (version, IS_ARRAY, vnfUuid, smUuid, smiUuid, smVer, smName);
148         }
149
150         public Response serviceVnfsImpl(String version, boolean isArray, String vnfUuid, String smUuid, String smiUuid, String smVer, String smName) {
151                 QueryServiceVnfs qryResp;
152                 int respStatus = HttpStatus.SC_OK;
153                 String uuid = "";
154                 List<VnfResource> ret;
155
156                 try (CatalogDatabase db = new CatalogDatabase()) {
157                         if (vnfUuid != null && !vnfUuid.equals("")) {
158                                 uuid = vnfUuid;
159                                 LOGGER.debug ("Query serviceVnfs getAllVnfsByVnfModelCustomizationUuid vnfModelCustomizationUuid: " + uuid);
160                                 ret = db.getAllVnfsByVnfModelCustomizationUuid(uuid);
161                         }
162                         else if (smUuid != null && !smUuid.equals("")) {
163                                 uuid = smUuid;
164                                 LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceModelUuid serviceModelUuid: " + uuid);
165                                 ret = db.getAllVnfsByServiceModelUuid(uuid);
166                         }
167                         else if (smiUuid != null && !smiUuid.equals("")) {
168                                 uuid = smiUuid;
169                                 if (smVer != null && !smVer.equals("")) {
170                                         LOGGER.debug ("Query serviceVnfs getAllNetworksByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
171                                         ret = db.getAllVnfsByServiceModelInvariantUuid(uuid, smVer);
172                                 }
173                                 else {
174                                         LOGGER.debug ("Query serviceVnfs getAllNetworksByServiceModelInvariantUuid serviceModelUuid: " + uuid);
175                                         ret = db.getAllVnfsByServiceModelInvariantUuid(uuid);
176                                 }
177                         }
178                         else if (smName != null && !smName.equals("")) {
179                                 if (smVer != null && !smVer.equals("")) {
180                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelInvariantName: " + smName+ " serviceModelVersion: "+ smVer);
181                                         ret = db.getAllVnfsByServiceName(smName, smVer);
182                                 }
183                                 else {
184                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelName: " + smName);
185                                         ret = db.getAllVnfsByServiceName(smName);
186                                 }
187                         }
188                         else if (smName != null && !smName.equals("")) {
189                                 if (smVer != null && !smVer.equals("")) {
190                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelInvariantName: " + smName+ " serviceModelVersion: "+ smVer);
191                                         ret = db.getAllVnfsByServiceName(smName, smVer);
192                                 }
193                                 else {
194                                         LOGGER.debug ("Query serviceVnfs getAllVnfsByServiceName serviceModelName: " + smName);
195                                         ret = db.getAllVnfsByServiceName(smName);
196                                 }
197                         }
198                         else {
199                                 throw(new Exception("no matching parameters"));
200                         }
201
202                         if (ret == null || ret.isEmpty()) {
203                                 LOGGER.debug ("serviceVnfs not found");
204                                 respStatus = HttpStatus.SC_NOT_FOUND;
205                                 qryResp = new QueryServiceVnfs();
206                         } else {
207                                 LOGGER.debug ("serviceVnfs found");
208                                 qryResp = new QueryServiceVnfs(ret);
209                                 LOGGER.debug ("serviceVnfs qryResp="+ qryResp);
210                         }
211                         LOGGER.debug ("Query serviceVnfs exit");
212                         return respond(version, respStatus, isArray, qryResp);
213                 } catch (Exception e) {
214                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryServiceVnfs", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryServiceVnfs", e);
215                         CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
216                         return Response
217                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
218                                 .entity(new GenericEntity<CatalogQueryException>(excResp) {})
219                                 .build();
220                 }
221         }
222
223         @GET
224         @Path("networks/{networkModelCustomizationUuid}")
225         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
226         public Response serviceNetworks (
227                         @PathParam("version") String version,
228                         @PathParam("networkModelCustomizationUuid") String nUuid
229         ) {
230                 return serviceNetworksImpl (version, !IS_ARRAY, nUuid, null, null, null, null);
231         }
232
233         @GET
234         @Path("serviceNetworks")
235         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
236         public Response serviceNetworks (
237                         @PathParam("version") String version,
238                         @QueryParam("networkModelCustomizationUuid") String nUuid,
239                         @QueryParam("networkType") String nType,
240                         @QueryParam("serviceModelUuid") String smUuid,
241                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
242                         @QueryParam("serviceModelVersion") String smVer
243         ) {
244                 return serviceNetworksImpl (version, IS_ARRAY, nUuid, nType, smUuid, smiUuid, smVer);
245         }
246
247         public Response serviceNetworksImpl (String version, boolean isArray, String nUuid, String nType, String smUuid, String smiUuid, String smVer) {
248                 QueryServiceNetworks qryResp;
249                 int respStatus = HttpStatus.SC_OK;
250                 String uuid = "";
251                 List<NetworkResourceCustomization> ret;
252
253                 try (CatalogDatabase db = new CatalogDatabase()) {
254                         if (nUuid != null && !nUuid.equals("")) {
255                                 uuid = nUuid;
256                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByNetworkModelCustomizationUuid networkModelCustomizationUuid: " + uuid);
257                                 ret = db.getAllNetworksByNetworkModelCustomizationUuid(uuid);
258                         }
259                         else if (smUuid != null && !smUuid.equals("")) {
260                                 uuid = smUuid;
261                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelUuid serviceModelUuid: " + uuid);
262                                 ret = db.getAllNetworksByServiceModelUuid(uuid);
263                         }
264                         else if (nType != null && !nType.equals("")) {
265                                 uuid = nType;
266                                 LOGGER.debug ("Query serviceNetworks getAllNetworksByNetworkType serviceModelUuid: " + uuid);
267                                 ret = db.getAllNetworksByNetworkType(uuid);
268                         }
269                         else if (smiUuid != null && !smiUuid.equals("")) {
270                                 uuid = smiUuid;
271                                 if (smVer != null && !smVer.equals("")) {
272                                         LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
273                                         ret = db.getAllNetworksByServiceModelInvariantUuid(uuid, smVer);
274                                 }
275                                 else {
276                                         LOGGER.debug ("Query serviceNetworks getAllNetworksByServiceModelInvariantUuid serviceModelUuid: " + uuid);
277                                         ret = db.getAllNetworksByServiceModelInvariantUuid(uuid);
278                                 }
279                         }
280                         else {
281                                 throw(new Exception("no matching parameters"));
282                         }
283
284                         if (ret == null || ret.isEmpty()) {
285                                 LOGGER.debug ("serviceNetworks not found");
286                                 respStatus = HttpStatus.SC_NOT_FOUND;
287                                 qryResp = new QueryServiceNetworks();
288                         } else {
289                                 LOGGER.debug ("serviceNetworks found");
290                                 qryResp = new QueryServiceNetworks(ret);
291                                 LOGGER.debug ("serviceNetworks qryResp="+ qryResp);
292                         }
293                         LOGGER.debug ("Query serviceNetworks exit");
294                         return respond(version, respStatus, isArray, qryResp);
295                 } catch (Exception e) {
296                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryServiceNetworks", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryServiceNetworks", e);
297                         CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
298                         return Response
299                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
300                                 .entity(new GenericEntity<CatalogQueryException>(excResp) {})
301                                 .build();
302                 }
303         }
304
305         @GET
306         @Path("serviceResources")
307         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
308         public Response serviceResources(
309                         @PathParam("version") String version,
310                         @QueryParam("serviceModelUuid") String smUuid,
311                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
312                         @QueryParam("serviceModelVersion") String smVer) {
313                 QueryServiceMacroHolder qryResp;
314                 int respStatus = HttpStatus.SC_OK;
315                 String uuid = "";
316                 ServiceMacroHolder ret;
317
318                 try (CatalogDatabase db = new CatalogDatabase()) {
319                         if (smUuid != null && !smUuid.equals("")) {
320                                 uuid = smUuid;
321                                 LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: " + uuid);
322                                 ret = db.getAllResourcesByServiceModelUuid(uuid);
323                         }
324                         else if (smiUuid != null && !smiUuid.equals("")) {
325                                 uuid = smiUuid;
326                                 if (smVer != null && !smVer.equals("")) {
327                                         LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
328                                         ret = db.getAllResourcesByServiceModelInvariantUuid(uuid, smVer);
329                                 }
330                                 else {
331                                         LOGGER.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: " + uuid);
332                                         ret = db.getAllResourcesByServiceModelInvariantUuid(uuid);
333                                 }
334                         }
335                         else {
336                                 throw(new Exception("no matching parameters"));
337                         }
338
339                         if (ret == null) {
340                                 LOGGER.debug ("serviceMacroHolder not found");
341                                 respStatus = HttpStatus.SC_NOT_FOUND;
342                                 qryResp = new QueryServiceMacroHolder();
343                         } else {
344                                 LOGGER.debug ("serviceMacroHolder found");
345                                 qryResp = new QueryServiceMacroHolder(ret);
346                                 LOGGER.debug ("serviceMacroHolder qryResp="+ qryResp);
347                         }
348                         LOGGER.debug ("Query serviceMacroHolder exit");
349                         return respond(version, respStatus, IS_ARRAY, qryResp);
350                 } catch (Exception e) {
351                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryServiceMacroHolder", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryServiceMacroHolder", e);
352                         CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
353                         return Response
354                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
355                                 .entity(new GenericEntity<CatalogQueryException>(excResp) {})
356                                 .build();
357                 }
358         }
359
360         @GET
361         @Path("allottedResources/{arModelCustomizationUuid}")
362         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
363         public Response serviceAllottedResources (
364                         @PathParam("version") String version,
365                         @PathParam("arModelCustomizationUuid") String aUuid
366         ) {
367                 return serviceAllottedResourcesImpl(version, !IS_ARRAY, aUuid, null, null, null);
368         }
369
370         @GET
371         @Path("serviceAllottedResources")
372         @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
373         public Response serviceAllottedResources(
374                         @PathParam("version") String version,
375                         @QueryParam("serviceModelUuid") String smUuid,
376                         @QueryParam("serviceModelInvariantUuid") String smiUuid,
377                         @QueryParam("serviceModelVersion") String smVer,
378                         @QueryParam("arModelCustomizationUuid") String aUuid
379         ) {
380                 return serviceAllottedResourcesImpl(version, IS_ARRAY, aUuid, smUuid, smiUuid, smVer);
381         }
382
383         public Response serviceAllottedResourcesImpl(String version, boolean isArray, String aUuid, String smUuid, String smiUuid, String smVer) {
384                 QueryAllottedResourceCustomization qryResp;
385                 int respStatus = HttpStatus.SC_OK;
386                 String uuid = "";
387                 List<AllottedResourceCustomization > ret;
388
389                 try (CatalogDatabase db = new CatalogDatabase()) {
390                         if (smUuid != null && !smUuid.equals("")) {
391                                 uuid = smUuid;
392                                 LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelUuid serviceModelUuid: " + uuid);
393                                 ret = db.getAllAllottedResourcesByServiceModelUuid(uuid);
394                         }
395                         else if (smiUuid != null && !smiUuid.equals("")) {
396                                 uuid = smiUuid;
397                                 if (smVer != null && !smVer.equals("")) {
398                                         LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: " + uuid+ " serviceModelVersion: "+ smVer);
399                                         ret = db.getAllAllottedResourcesByServiceModelInvariantUuid(uuid, smVer);
400                                 }
401                                 else {
402                                         LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByServiceModelInvariantUuid serviceModelUuid: " + uuid);
403                                         ret = db.getAllAllottedResourcesByServiceModelInvariantUuid(uuid);
404                                 }
405                         }
406                         else if (aUuid != null && !aUuid.equals("")) {
407                                 uuid = aUuid;
408                                 LOGGER.debug ("Query AllottedResourceCustomization getAllAllottedResourcesByArModelCustomizationUuid serviceModelUuid: " + uuid);
409                                 ret = db.getAllAllottedResourcesByArModelCustomizationUuid(uuid);
410                         }
411                         else {
412                                 throw(new Exception("no matching parameters"));
413                         }
414
415                         if (ret == null || ret.isEmpty()) {
416                                 LOGGER.debug ("AllottedResourceCustomization not found");
417                                 respStatus = HttpStatus.SC_NOT_FOUND;
418                                 qryResp = new QueryAllottedResourceCustomization();
419                         } else {
420                                 LOGGER.debug ("AllottedResourceCustomization found");
421                                 qryResp = new QueryAllottedResourceCustomization(ret);
422                                 LOGGER.debug ("AllottedResourceCustomization qryResp="+ qryResp);
423                         }
424                         LOGGER.debug ("Query AllottedResourceCustomization exit");
425                         return respond(version, respStatus, isArray, qryResp);
426                 } catch (Exception e) {
427                         LOGGER.error (MessageEnum.RA_QUERY_VNF_ERR,  uuid, "", "queryAllottedResourceCustomization", MsoLogger.ErrorCode.BusinessProcesssError, "Exception - queryAllottedResourceCustomization", e);
428                         CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
429                         return Response
430                                 .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
431                                 .entity(new GenericEntity<CatalogQueryException>(excResp) {})
432                                 .build();
433                 }
434         }
435 }