Fix the retrival of resource recipe
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / onap / so / adapters / catalogdb / rest / CatalogDbAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.catalogdb.rest;
24
25 /*
26 Create an initial query to retrieve a VNF Resource definition (including a list of possible module types)
27 within the context of a given service. Input is a vnf resource model customization ID (new field for 1702),
28 or a composite key (from 1610) of service name, service version, vnf instance name
29
30 Returns a structure (JSON?) containing VNF RESOURCE attributes, plus a list of VF Module structures.
31
32 Query a NETWORK_RESOURCE from the MSO Catalog, based on a networkModelCustomizationUUID (new for 1702),
33 a network type (unique type identifier in 1610), or based on network role within a service.
34
35 Create Adapter framework for access to Catalog DB, including connection management,
36 login/password access, transaction logic, etc. This can be modeled after the Request DB Adapter
37
38 Update the MSO Catalog DB schema to include the new fields defined in this user story.
39
40 Note that the resourceModelCustomizationUUID (or vfModuleModelCustomizationUUID) will be unique keys (indexes)
41 on the VNF_RESOURCE and VF_MODULE tables respectively.
42 The previously constructed "vnf-type" and "vf-module-type" field may continue to be populated,
43 but should no longer be needed and can deprecate in future release.
44
45 For migration, a new randomly generated UUID field may be generated for the *ModelCustomizationUUID" fields
46 until such time that the model is redistributed from ASDC.
47
48 All other fields Check with Mike Z for appropriate value for the vfModuleLabel.
49 We might be able to derive it's value from the current vnf-type (using the "middle" piece that identifies the module type).
50
51 min and initial counts can be 0. max can be null to indicate no maximum.
52
53 Once the network-level distribution artifacts are defined, similar updates can be made to the NETWORK_RESOURCE table.
54  */
55
56 import org.apache.http.HttpStatus;
57 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQuery;
58 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQueryException;
59 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQueryExceptionCategory;
60 import org.onap.so.adapters.catalogdb.catalogrest.QueryAllottedResourceCustomization;
61 import org.onap.so.adapters.catalogdb.catalogrest.QueryResourceRecipe;
62 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceCsar;
63 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceMacroHolder;
64 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceNetworks;
65 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceVnfs;
66 import org.onap.so.adapters.catalogdb.catalogrest.QueryVfModule;
67 import org.onap.so.db.catalog.beans.AllottedResource;
68 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
69 import org.onap.so.db.catalog.beans.NetworkResource;
70 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
71 import org.onap.so.db.catalog.beans.Recipe;
72 import org.onap.so.db.catalog.beans.Service;
73 import org.onap.so.db.catalog.beans.ToscaCsar;
74 import org.onap.so.db.catalog.beans.VfModule;
75 import org.onap.so.db.catalog.beans.VfModuleCustomization;
76 import org.onap.so.db.catalog.beans.VnfRecipe;
77 import org.onap.so.db.catalog.beans.VnfResource;
78 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
79 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
80 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
81 import org.onap.so.db.catalog.data.repository.ArRecipeRepository;
82 import org.onap.so.db.catalog.data.repository.NetworkRecipeRepository;
83 import org.onap.so.db.catalog.data.repository.NetworkResourceCustomizationRepository;
84 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
85 import org.onap.so.db.catalog.data.repository.ServiceRepository;
86 import org.onap.so.db.catalog.data.repository.ToscaCsarRepository;
87 import org.onap.so.db.catalog.data.repository.VFModuleRepository;
88 import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
89 import org.onap.so.db.catalog.data.repository.VnfRecipeRepository;
90 import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
91 import org.onap.so.db.catalog.rest.beans.ServiceMacroHolder;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
94 import org.springframework.beans.factory.annotation.Autowired;
95 import org.springframework.stereotype.Component;
96 import org.springframework.transaction.annotation.Transactional;
97
98 import javax.ws.rs.GET;
99 import javax.ws.rs.Path;
100 import javax.ws.rs.PathParam;
101 import javax.ws.rs.Produces;
102 import javax.ws.rs.QueryParam;
103 import javax.ws.rs.core.GenericEntity;
104 import javax.ws.rs.core.HttpHeaders;
105 import javax.ws.rs.core.MediaType;
106 import javax.ws.rs.core.Response;
107 import java.util.ArrayList;
108 import java.util.List;
109
110 /**
111  * This class services calls to the REST interface for VF Modules (http://host:port/ecomp/mso/catalog/v1)
112  * Both XML and JSON can be produced/consumed.  Set Accept: and Content-Type: headers appropriately.  XML is the default.
113  * Requests respond synchronously only
114  */
115 @Path("/{version: v[0-9]+}")
116 @Component
117 public class CatalogDbAdapterRest {
118     protected static Logger logger = LoggerFactory.getLogger(CatalogDbAdapterRest.class);
119     private static final boolean IS_ARRAY = true;
120     private static final String NETWORK_SERVICE = "network service";
121
122     @Autowired
123     private VnfCustomizationRepository vnfCustomizationRepo;
124
125     @Autowired
126     private ServiceRepository serviceRepo;
127
128     @Autowired
129     private NetworkResourceCustomizationRepository networkCustomizationRepo;
130
131     @Autowired
132     private NetworkResourceRepository networkResourceRepo;
133
134     @Autowired
135     private AllottedResourceCustomizationRepository allottedCustomizationRepo;
136
137     @Autowired
138     private ToscaCsarRepository toscaCsarRepo;
139
140     @Autowired
141     private VFModuleRepository vfModuleRepo;
142
143     @Autowired
144     private VnfRecipeRepository vnfRecipeRepo;
145
146     @Autowired
147     private NetworkRecipeRepository networkRecipeRepo;
148
149     @Autowired
150     private ArRecipeRepository arRecipeRepo;
151
152     @Autowired
153     private VnfResourceRepository vnfResourceRepo;
154
155     @Autowired
156     private AllottedResourceRepository arResourceRepo;
157
158     private static final String NO_MATCHING_PARAMETERS = "no matching parameters";
159
160     public Response respond(String version, int respStatus, boolean isArray, CatalogQuery qryResp) {
161         return Response
162                 .status(respStatus)
163                 //.entity(new GenericEntity<QueryServiceVnfs>(qryResp) {})
164                 .entity(qryResp.toJsonString(version, isArray))
165                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
166                 .build();
167     }
168
169     @GET
170     @Path("vnfResources/{vnfModelCustomizationUuid}")
171     @Transactional( readOnly = true)
172     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
173     public Response serviceVnfs (
174             @PathParam("version") String version,
175             @PathParam("vnfModelCustomizationUuid") String vnfUuid
176             ) {
177         return serviceVnfsImpl (version, !IS_ARRAY, vnfUuid, null, null, null, null);
178     }
179
180     @GET
181     @Path("serviceVnfs")
182     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
183     @Transactional( readOnly = true)
184     public Response serviceVnfs(
185             @PathParam("version") String version,
186             @QueryParam("vnfModelCustomizationUuid") String vnfUuid,
187             @QueryParam("serviceModelUuid") String smUuid,
188             @QueryParam("serviceModelInvariantUuid") String smiUuid,
189             @QueryParam("serviceModelVersion") String smVer,
190             @QueryParam("serviceModelName") String smName
191             ) {
192         return serviceVnfsImpl (version, IS_ARRAY, vnfUuid, smUuid, smiUuid, smVer, smName);
193     }
194
195     public Response serviceVnfsImpl(String version, boolean isArray, String vnfUuid, String serviceModelUUID, String smiUuid, String smVer, String smName) {
196         QueryServiceVnfs qryResp = null;
197         int respStatus = HttpStatus.SC_OK;              
198         List<VnfResourceCustomization> ret = new ArrayList<>();
199         Service service = null;
200         try {
201             if (vnfUuid != null && !"".equals(vnfUuid)) 
202                 ret = vnfCustomizationRepo.findByModelCustomizationUUID(vnfUuid);                       
203             else if (serviceModelUUID != null && !"".equals(serviceModelUUID))                          
204                 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(serviceModelUUID);
205             else if (smiUuid != null && !"".equals(smiUuid))                    
206                 if (smVer != null && !"".equals(smVer)) 
207                     service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer,smiUuid);                                  
208                 else                                    
209                     service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(smiUuid);
210             else if (smName != null && !"".equals(smName)) {
211                 if (smVer != null && !"".equals(smVer))                                         
212                     service = serviceRepo.findByModelNameAndModelVersion(smName, smVer);
213                 else 
214                     service = serviceRepo.findFirstByModelNameOrderByModelVersionDesc(smName);                  
215             }
216             else {
217                 throw(new Exception(NO_MATCHING_PARAMETERS));
218             }
219
220             if (service == null && ret.isEmpty()) {
221                 respStatus = HttpStatus.SC_NOT_FOUND;
222                 qryResp = new QueryServiceVnfs();
223             }else if( service == null && !ret.isEmpty()){
224                 qryResp = new QueryServiceVnfs(ret);                            
225             } else if (service != null) {
226                 qryResp = new QueryServiceVnfs(service.getVnfCustomizations());                         
227             }
228             logger.debug ("serviceVnfs qryResp= {}", qryResp);
229             return respond(version, respStatus, isArray, qryResp);
230         } catch (Exception e) {
231             logger.error("Exception - queryServiceVnfs", e);
232             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
233             return Response
234                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
235                     .entity(new GenericEntity<CatalogQueryException>(excResp) {})
236                     .build();
237         }
238     }
239
240     @GET
241     @Path("networkResources/{networkModelCustomizationUuid}")
242     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
243     @Transactional( readOnly = true)
244     public Response serviceNetworks (
245             @PathParam("version") String version,
246             @PathParam("networkModelCustomizationUuid") String nUuid
247             ) {
248         return serviceNetworksImpl (version, !IS_ARRAY, nUuid, null, null, null, null);
249     }
250
251     @GET
252     @Path("serviceNetworks")
253     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
254     @Transactional( readOnly = true)
255     public Response serviceNetworks (
256             @PathParam("version") String version,
257             @QueryParam("networkModelCustomizationUuid") String networkModelCustomizationUuid,
258             @QueryParam("networkType") String networkType,
259             @QueryParam("networkModelName") String networkModelName,
260             @QueryParam("serviceModelUuid") String serviceModelUuid,
261             @QueryParam("serviceModelInvariantUuid") String serviceModelInvariantUuid,
262             @QueryParam("serviceModelVersion") String serviceModelVersion,
263             @QueryParam("networkModelVersion") String networkModelVersion
264             ) {
265         if (networkModelName != null && !"".equals(networkModelName)) {
266             networkType = networkModelName;
267         }
268         return serviceNetworksImpl (version, IS_ARRAY,  networkModelCustomizationUuid, networkType, serviceModelUuid, serviceModelInvariantUuid, serviceModelVersion);
269     }
270
271     public Response serviceNetworksImpl (String version, boolean isArray, String  networkModelCustomizationUuid, String networkType, String serviceModelUuid, String serviceModelInvariantUuid, String serviceModelVersion) {
272         QueryServiceNetworks qryResp;
273         int respStatus = HttpStatus.SC_OK;
274         String uuid = "";
275         List<NetworkResourceCustomization> ret = new ArrayList<>();
276         Service service = null;
277
278         try{
279             if (networkModelCustomizationUuid != null && !"".equals(networkModelCustomizationUuid)) {
280                 uuid = networkModelCustomizationUuid;                           
281                 ret = networkCustomizationRepo.findByModelCustomizationUUID(networkModelCustomizationUuid);
282             }else if (networkType != null && !"".equals(networkType)) {
283                 uuid = networkType;                             
284                 NetworkResource networkResources = networkResourceRepo.findFirstByModelNameOrderByModelVersionDesc(networkType);
285                 if(networkResources != null)
286                     ret=networkResources.getNetworkResourceCustomization();
287             }
288             else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
289                 uuid = serviceModelInvariantUuid;
290                 if (serviceModelVersion != null && !"".equals(serviceModelVersion)) {                                   
291                     service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(serviceModelVersion, uuid);
292                 }
293                 else {                                  
294                     service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
295                 }
296             }else if (serviceModelUuid != null && !"".equals(serviceModelUuid)) {
297                 uuid = serviceModelUuid;                                
298                 service = serviceRepo.findOneByModelUUID(serviceModelUuid);
299             }
300             else {
301                 throw(new Exception(NO_MATCHING_PARAMETERS));
302             }
303
304             if(service != null)
305                 ret = service.getNetworkCustomizations();
306
307             if (ret == null || ret.isEmpty()) {
308                 logger.debug ("serviceNetworks not found");
309                 respStatus = HttpStatus.SC_NOT_FOUND;
310                 qryResp = new QueryServiceNetworks();
311             } else {                            
312                 qryResp = new QueryServiceNetworks(ret);
313                 logger.debug ("serviceNetworks found qryResp= {}", qryResp);
314             }
315             return respond(version, respStatus, isArray, qryResp);
316         } catch (Exception e) {
317             logger.error ("Exception - queryServiceNetworks", e);
318             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
319             return Response
320                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
321                     .entity(new GenericEntity<CatalogQueryException>(excResp) {})
322                     .build();
323         }
324     }
325
326     @GET
327     @Path("serviceResources")
328     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
329     @Transactional(readOnly = true)
330     public Response serviceResources(
331             @PathParam("version") String version,
332             @QueryParam("serviceModelUuid") String modelUUID,
333             @QueryParam("serviceModelInvariantUuid") String modelInvariantUUID,
334             @QueryParam("serviceModelVersion") String modelVersion) {
335         QueryServiceMacroHolder qryResp;
336         int respStatus = HttpStatus.SC_OK;
337         String uuid = "";
338         ServiceMacroHolder ret = new ServiceMacroHolder();
339
340         try{
341             if (modelUUID != null && !"".equals(modelUUID)) {
342                 uuid = modelUUID;
343                 logger.debug ("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: {}" , uuid);
344                 Service serv =serviceRepo.findOneByModelUUID(uuid);
345
346                 if (serv != null) {
347                     ret.setNetworkResourceCustomizations(new ArrayList(serv.getNetworkCustomizations()));
348                     ret.setVnfResourceCustomizations(new ArrayList(serv.getVnfCustomizations()));
349                     ret.setAllottedResourceCustomizations(new ArrayList(serv.getAllottedCustomizations()));
350                 }
351                 ret.setService(serv);
352             }
353             else if (modelInvariantUUID != null && !"".equals(modelInvariantUUID)) {
354                 uuid = modelInvariantUUID;
355                 if (modelVersion != null && !"".equals(modelVersion)) {
356                     logger.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: {}  serviceModelVersion: {}",uuid, modelVersion);
357                     Service serv = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(modelVersion, uuid);
358
359                     ret.setService(serv);
360                 }
361                 else {
362                     logger.debug ("Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: {}" , uuid);
363                     Service serv = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
364                     ret.setService(serv);
365                 }
366             }
367             else {
368                 throw(new Exception(NO_MATCHING_PARAMETERS));
369             }
370
371             if (ret.getService() == null) {
372                 logger.debug ("serviceMacroHolder not found");
373                 respStatus = HttpStatus.SC_NOT_FOUND;
374                 qryResp = new QueryServiceMacroHolder();
375             } else {
376                 qryResp = new QueryServiceMacroHolder(ret);
377                 logger.debug ("serviceMacroHolder qryResp= {}", qryResp);
378             }
379             return respond(version, respStatus, IS_ARRAY, qryResp);
380         } catch (Exception e) {
381             logger.error ("Exception - queryServiceMacroHolder", e);
382             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
383             return Response
384                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
385                     .entity(new GenericEntity<CatalogQueryException>(excResp){} )
386                     .build();
387         }
388     }
389
390
391     @GET
392     @Path("allottedResources/{arModelCustomizationUuid}")
393     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
394     @Transactional( readOnly = true)
395     public Response serviceAllottedResources (
396             @PathParam("version") String version,
397             @PathParam("arModelCustomizationUuid") String aUuid
398             ) {
399         return serviceAllottedResourcesImpl(version, !IS_ARRAY, aUuid, null, null, null);
400     }
401
402     @GET
403     @Path("serviceAllottedResources")
404     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
405     @Transactional( readOnly = true)
406     public Response serviceAllottedResources(
407             @PathParam("version") String version,
408             @QueryParam("serviceModelUuid") String smUuid,
409             @QueryParam("serviceModelInvariantUuid") String smiUuid,
410             @QueryParam("serviceModelVersion") String smVer,
411             @QueryParam("arModelCustomizationUuid") String aUuid
412             ) {
413         return serviceAllottedResourcesImpl(version, IS_ARRAY, aUuid, smUuid, smiUuid, smVer);
414     }
415
416     public Response serviceAllottedResourcesImpl(String version, boolean isArray, String aUuid, String smUuid, String serviceModelInvariantUuid, String smVer) {
417         QueryAllottedResourceCustomization qryResp;
418         int respStatus = HttpStatus.SC_OK;
419         String uuid = "";
420         List<AllottedResourceCustomization> ret = new ArrayList<>();
421         Service service = null;
422         try{
423             if (smUuid != null && !"".equals(smUuid)) {
424                 uuid = smUuid;                          
425                 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(uuid);                     
426             }
427             else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
428                 uuid = serviceModelInvariantUuid;
429                 if (smVer != null && !"".equals(smVer)) {                                       
430                     service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, uuid);
431                 }
432                 else {                          
433                     service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
434                 }
435             }
436             else if (aUuid != null && !"".equals(aUuid)) {
437                 uuid = aUuid;                           
438                 ret = allottedCustomizationRepo.findByModelCustomizationUUID(uuid);
439             }
440             else {
441                 throw(new Exception(NO_MATCHING_PARAMETERS));
442             }
443
444             if(service != null)
445                 ret=service.getAllottedCustomizations();
446
447             if (ret == null || ret.isEmpty()) {
448                 logger.debug ("AllottedResourceCustomization not found");
449                 respStatus = HttpStatus.SC_NOT_FOUND;
450                 qryResp = new QueryAllottedResourceCustomization();
451             } else {                            
452                 qryResp = new QueryAllottedResourceCustomization(ret);
453                 logger.debug ("AllottedResourceCustomization qryResp= {}", qryResp);
454             }                   
455             return respond(version, respStatus, isArray, qryResp);
456         } catch (Exception e) {
457             logger.error ("Exception - queryAllottedResourceCustomization", e);
458             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
459             return Response
460                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
461                     .entity(new GenericEntity<CatalogQueryException>(excResp) {})
462                     .build();
463         }
464     }
465
466     @GET
467     @Path("vfModules")
468     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
469     @Transactional( readOnly = true)
470     public Response vfModules(@QueryParam("vfModuleModelName") String vfModuleModelName) {
471         QueryVfModule qryResp;
472         int respStatus = HttpStatus.SC_OK;
473         List<VfModuleCustomization> ret = null; 
474         try{
475             if(vfModuleModelName != null && !"".equals(vfModuleModelName)){
476                 VfModule vfModule = vfModuleRepo.findFirstByModelNameOrderByModelVersionDesc(vfModuleModelName);
477                 if(vfModule != null)
478                     ret = vfModule.getVfModuleCustomization();                          
479             }else{
480                 throw(new Exception(NO_MATCHING_PARAMETERS));
481             }
482
483             if(ret == null || ret.isEmpty()){
484                 logger.debug ("vfModules not found");
485                 respStatus = HttpStatus.SC_NOT_FOUND;
486                 qryResp = new QueryVfModule();
487             }else{                      
488                 qryResp = new QueryVfModule(ret);                               
489                 if(logger.isDebugEnabled())
490                     logger.debug ("vfModules tojsonstring is: {}", qryResp.JSON2(false, false));
491             }                   
492             return Response
493                     .status(respStatus)
494                     .entity(qryResp.JSON2(false, false)) 
495                     .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
496                     .build();
497         }catch(Exception e){
498             logger.error ("Exception during query VfModules by vfModuleModuleName: ", e);
499             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(), CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
500             return Response
501                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
502                     .entity(new GenericEntity<CatalogQueryException>(excResp) {})
503                     .build();
504         }
505     }
506     /**
507      * Get the tosca csar info from catalog
508      * <br>
509      * 
510      * @param smUuid service model uuid
511      * @return the tosca csar information of the serivce.
512      * @since ONAP Beijing Release
513      */
514     @GET
515     @Path("serviceToscaCsar")
516     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
517     public Response serviceToscaCsar(@QueryParam("serviceModelUuid") String smUuid) {
518         int respStatus = HttpStatus.SC_OK;
519         String entity = "";
520         try {
521             if (smUuid != null && !"".equals(smUuid)) {
522                 logger.debug("Query Csar by service model uuid: {}",smUuid);
523                 ToscaCsar toscaCsar = toscaCsarRepo.findById(smUuid).orElseGet(() -> null);
524                 if (toscaCsar != null) {
525                     QueryServiceCsar serviceCsar = new QueryServiceCsar(toscaCsar);
526                     entity = serviceCsar.JSON2(false, false);
527                 } else {
528                     respStatus = HttpStatus.SC_NOT_FOUND;
529                 }
530             } else {
531                 throw (new Exception("Incoming parameter is null or blank"));
532             }
533             return Response
534                     .status(respStatus)
535                     .entity(entity)
536                     .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
537                     .build();
538         } catch (Exception e) {
539             logger.error("Exception during query csar by service model uuid: ", e);
540             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
541                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
542             return Response
543                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
544                     .entity(new GenericEntity<CatalogQueryException>(excResp) {
545                     })
546                     .build();
547         }
548     }
549
550     /**
551      * Get the resource recipe info from catalog
552      * <br>
553      * 
554      * @param rmUuid resource model uuid
555      * @return the recipe information of the resource.
556      * @since ONAP Beijing Release
557      */
558     @GET
559     @Path("resourceRecipe")
560     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
561     public Response resourceRecipe(@QueryParam("resourceModelUuid") String rmUuid, @QueryParam("action") String action) {
562         int respStatus = HttpStatus.SC_OK;
563         String entity = "";
564         try {
565             if (rmUuid != null && !"".equals(rmUuid)) {
566                 logger.debug("Query recipe by resource model uuid: {}", rmUuid);
567                 //check vnf and network and ar, the resource could be any resource.
568                 Recipe recipe = null;
569
570                 VnfResource vnf = vnfResourceRepo.findResourceByModelUUID(rmUuid);
571                 if (vnf != null) {
572                     recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndActionAndVersionStr(vnf.getModelName(), action, vnf.getModelVersion());
573
574                     // for network service fetch the default recipe
575                     if (recipe == null && vnf.getSubCategory().equalsIgnoreCase(NETWORK_SERVICE)) {
576                         recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndAction("NS_DEFAULT", action);
577                     }
578                 }
579
580
581                 if (null == recipe) {
582                     NetworkResource nResource = networkResourceRepo.findResourceByModelUUID(rmUuid);
583                     recipe = networkRecipeRepo.findFirstByModelNameAndActionAndVersionStr(nResource.getModelName(), action, vnf.getModelVersion());
584
585                     // for network fetch the default recipe
586                     if (recipe == null) {
587                         recipe = networkRecipeRepo.findFirstByModelNameAndAction("SDNC_DEFAULT", action);
588                     }
589                 }
590
591                 if (null == recipe) {
592                     AllottedResource arResource = arResourceRepo.findResourceByModelUUID(rmUuid);
593                     recipe = arRecipeRepo.findByModelNameAndActionAndVersion(arResource.getModelName(), action, arResource.getModelVersion());
594                 }
595                 if (recipe != null) {
596                     QueryResourceRecipe resourceRecipe = new QueryResourceRecipe(recipe);
597                     entity = resourceRecipe.JSON2(false, false);
598                 } else {
599                     respStatus = HttpStatus.SC_NOT_FOUND;
600                 }
601             } else {
602                 throw new Exception("Incoming parameter is null or blank");
603             }
604             return Response
605                     .status(respStatus)
606                     .entity(entity)
607                     .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
608                     .build();
609         } catch (Exception e) {
610             logger.error("Exception during query recipe by resource model uuid: ", e);
611             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
612                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
613             return Response
614                     .status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
615                     .entity(new GenericEntity<CatalogQueryException>(excResp) {
616                     })
617                     .build();
618         }
619     }
620 }