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