Replaced all tabs with spaces in java and pom.xml
[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
27 import java.util.ArrayList;
28 import java.util.List;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.QueryParam;
34 import javax.ws.rs.core.GenericEntity;
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import org.apache.http.HttpStatus;
39 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQuery;
40 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQueryException;
41 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQueryExceptionCategory;
42 import org.onap.so.adapters.catalogdb.catalogrest.QueryAllottedResourceCustomization;
43 import org.onap.so.adapters.catalogdb.catalogrest.QueryResourceRecipe;
44 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceCsar;
45 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceMacroHolder;
46 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceNetworks;
47 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceVnfs;
48 import org.onap.so.adapters.catalogdb.catalogrest.QueryVfModule;
49 import org.onap.so.db.catalog.beans.AllottedResource;
50 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
51 import org.onap.so.db.catalog.beans.NetworkResource;
52 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
53 import org.onap.so.db.catalog.beans.Recipe;
54 import org.onap.so.db.catalog.beans.Service;
55 import org.onap.so.db.catalog.beans.ToscaCsar;
56 import org.onap.so.db.catalog.beans.VfModule;
57 import org.onap.so.db.catalog.beans.VfModuleCustomization;
58 import org.onap.so.db.catalog.beans.VnfResource;
59 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
60 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
61 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
62 import org.onap.so.db.catalog.data.repository.ArRecipeRepository;
63 import org.onap.so.db.catalog.data.repository.NetworkRecipeRepository;
64 import org.onap.so.db.catalog.data.repository.NetworkResourceCustomizationRepository;
65 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
66 import org.onap.so.db.catalog.data.repository.ServiceRepository;
67 import org.onap.so.db.catalog.data.repository.ToscaCsarRepository;
68 import org.onap.so.db.catalog.data.repository.VFModuleRepository;
69 import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
70 import org.onap.so.db.catalog.data.repository.VnfRecipeRepository;
71 import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
72 import org.onap.so.db.catalog.rest.beans.ServiceMacroHolder;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.stereotype.Component;
77 import org.springframework.transaction.annotation.Transactional;
78
79
80 @Path("/{version: v[0-9]+}")
81 @Component
82 public class CatalogDbAdapterRest {
83     protected static Logger logger = LoggerFactory.getLogger(CatalogDbAdapterRest.class);
84     private static final boolean IS_ARRAY = true;
85     private static final String NETWORK_SERVICE = "network service";
86
87     @Autowired
88     private VnfCustomizationRepository vnfCustomizationRepo;
89
90     @Autowired
91     private ServiceRepository serviceRepo;
92
93     @Autowired
94     private NetworkResourceCustomizationRepository networkCustomizationRepo;
95
96     @Autowired
97     private NetworkResourceRepository networkResourceRepo;
98
99     @Autowired
100     private AllottedResourceCustomizationRepository allottedCustomizationRepo;
101
102     @Autowired
103     private ToscaCsarRepository toscaCsarRepo;
104
105     @Autowired
106     private VFModuleRepository vfModuleRepo;
107
108     @Autowired
109     private VnfRecipeRepository vnfRecipeRepo;
110
111     @Autowired
112     private NetworkRecipeRepository networkRecipeRepo;
113
114     @Autowired
115     private ArRecipeRepository arRecipeRepo;
116
117     @Autowired
118     private VnfResourceRepository vnfResourceRepo;
119
120     @Autowired
121     private AllottedResourceRepository arResourceRepo;
122
123     private static final String NO_MATCHING_PARAMETERS = "no matching parameters";
124
125     public Response respond(String version, int respStatus, boolean isArray, CatalogQuery qryResp) {
126         return Response.status(respStatus).entity(qryResp.toJsonString(version, isArray))
127                 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
128     }
129
130     @GET
131     @Path("vnfResources/{vnfModelCustomizationUuid}")
132     @Transactional(readOnly = true)
133     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
134     public Response serviceVnfs(@PathParam("version") String version,
135             @PathParam("vnfModelCustomizationUuid") String vnfUuid) {
136         return serviceVnfsImpl(version, !IS_ARRAY, vnfUuid, null, null, null, null);
137     }
138
139     @GET
140     @Path("serviceVnfs")
141     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
142     @Transactional(readOnly = true)
143     public Response serviceVnfs(@PathParam("version") String version,
144             @QueryParam("vnfModelCustomizationUuid") String vnfUuid, @QueryParam("serviceModelUuid") String smUuid,
145             @QueryParam("serviceModelInvariantUuid") String smiUuid, @QueryParam("serviceModelVersion") String smVer,
146             @QueryParam("serviceModelName") String smName) {
147         return serviceVnfsImpl(version, IS_ARRAY, vnfUuid, smUuid, smiUuid, smVer, smName);
148     }
149
150     public Response serviceVnfsImpl(String version, boolean isArray, String vnfUuid, String serviceModelUUID,
151             String smiUuid, String smVer, String smName) {
152         QueryServiceVnfs qryResp = null;
153         int respStatus = HttpStatus.SC_OK;
154         List<VnfResourceCustomization> ret = new ArrayList<>();
155         Service service = null;
156         try {
157             if (vnfUuid != null && !"".equals(vnfUuid))
158                 ret = vnfCustomizationRepo.findByModelCustomizationUUID(vnfUuid);
159             else if (serviceModelUUID != null && !"".equals(serviceModelUUID))
160                 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(serviceModelUUID);
161             else if (smiUuid != null && !"".equals(smiUuid))
162                 if (smVer != null && !"".equals(smVer))
163                     service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, smiUuid);
164                 else
165                     service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(smiUuid);
166             else if (smName != null && !"".equals(smName)) {
167                 if (smVer != null && !"".equals(smVer))
168                     service = serviceRepo.findByModelNameAndModelVersion(smName, smVer);
169                 else
170                     service = serviceRepo.findFirstByModelNameOrderByModelVersionDesc(smName);
171             } else {
172                 throw (new Exception(NO_MATCHING_PARAMETERS));
173             }
174
175             if (service == null && ret.isEmpty()) {
176                 respStatus = HttpStatus.SC_NOT_FOUND;
177                 qryResp = new QueryServiceVnfs();
178             } else if (service == null && !ret.isEmpty()) {
179                 qryResp = new QueryServiceVnfs(ret);
180             } else if (service != null) {
181                 qryResp = new QueryServiceVnfs(service.getVnfCustomizations());
182             }
183             logger.debug("serviceVnfs qryResp= {}", qryResp);
184             return respond(version, respStatus, isArray, qryResp);
185         } catch (Exception e) {
186             logger.error("Exception - queryServiceVnfs", e);
187             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
188                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
189             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
190                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
191         }
192     }
193
194     @GET
195     @Path("networkResources/{networkModelCustomizationUuid}")
196     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
197     @Transactional(readOnly = true)
198     public Response serviceNetworks(@PathParam("version") String version,
199             @PathParam("networkModelCustomizationUuid") String nUuid) {
200         return serviceNetworksImpl(version, !IS_ARRAY, nUuid, null, null, null, null);
201     }
202
203     @GET
204     @Path("serviceNetworks")
205     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
206     @Transactional(readOnly = true)
207     public Response serviceNetworks(@PathParam("version") String version,
208             @QueryParam("networkModelCustomizationUuid") String networkModelCustomizationUuid,
209             @QueryParam("networkType") String networkType, @QueryParam("networkModelName") String networkModelName,
210             @QueryParam("serviceModelUuid") String serviceModelUuid,
211             @QueryParam("serviceModelInvariantUuid") String serviceModelInvariantUuid,
212             @QueryParam("serviceModelVersion") String serviceModelVersion,
213             @QueryParam("networkModelVersion") String networkModelVersion) {
214         if (networkModelName != null && !"".equals(networkModelName)) {
215             networkType = networkModelName;
216         }
217         return serviceNetworksImpl(version, IS_ARRAY, networkModelCustomizationUuid, networkType, serviceModelUuid,
218                 serviceModelInvariantUuid, serviceModelVersion);
219     }
220
221     public Response serviceNetworksImpl(String version, boolean isArray, String networkModelCustomizationUuid,
222             String networkType, String serviceModelUuid, String serviceModelInvariantUuid, String serviceModelVersion) {
223         QueryServiceNetworks qryResp;
224         int respStatus = HttpStatus.SC_OK;
225         String uuid = "";
226         List<NetworkResourceCustomization> ret = new ArrayList<>();
227         Service service = null;
228
229         try {
230             if (networkModelCustomizationUuid != null && !"".equals(networkModelCustomizationUuid)) {
231                 uuid = networkModelCustomizationUuid;
232                 ret = networkCustomizationRepo.findByModelCustomizationUUID(networkModelCustomizationUuid);
233             } else if (networkType != null && !"".equals(networkType)) {
234                 uuid = networkType;
235                 NetworkResource networkResources =
236                         networkResourceRepo.findFirstByModelNameOrderByModelVersionDesc(networkType);
237                 if (networkResources != null)
238                     ret = networkResources.getNetworkResourceCustomization();
239             } else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
240                 uuid = serviceModelInvariantUuid;
241                 if (serviceModelVersion != null && !"".equals(serviceModelVersion)) {
242                     service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(serviceModelVersion, uuid);
243                 } else {
244                     service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
245                 }
246             } else if (serviceModelUuid != null && !"".equals(serviceModelUuid)) {
247                 uuid = serviceModelUuid;
248                 service = serviceRepo.findOneByModelUUID(serviceModelUuid);
249             } else {
250                 throw (new Exception(NO_MATCHING_PARAMETERS));
251             }
252
253             if (service != null)
254                 ret = service.getNetworkCustomizations();
255
256             if (ret == null || ret.isEmpty()) {
257                 logger.debug("serviceNetworks not found");
258                 respStatus = HttpStatus.SC_NOT_FOUND;
259                 qryResp = new QueryServiceNetworks();
260             } else {
261                 qryResp = new QueryServiceNetworks(ret);
262                 logger.debug("serviceNetworks found qryResp= {}", qryResp);
263             }
264             return respond(version, respStatus, isArray, qryResp);
265         } catch (Exception e) {
266             logger.error("Exception - queryServiceNetworks", e);
267             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
268                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
269             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
270                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
271         }
272     }
273
274     @GET
275     @Path("serviceResources")
276     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
277     @Transactional(readOnly = true)
278     public Response serviceResources(@PathParam("version") String version,
279             @QueryParam("serviceModelUuid") String modelUUID,
280             @QueryParam("serviceModelInvariantUuid") String modelInvariantUUID,
281             @QueryParam("serviceModelVersion") String modelVersion) {
282         QueryServiceMacroHolder qryResp;
283         int respStatus = HttpStatus.SC_OK;
284         String uuid = "";
285         ServiceMacroHolder ret = new ServiceMacroHolder();
286
287         try {
288             if (modelUUID != null && !"".equals(modelUUID)) {
289                 uuid = modelUUID;
290                 logger.debug("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: {}", uuid);
291                 Service serv = serviceRepo.findOneByModelUUID(uuid);
292
293                 if (serv != null) {
294                     ret.setNetworkResourceCustomizations(new ArrayList(serv.getNetworkCustomizations()));
295                     ret.setVnfResourceCustomizations(new ArrayList(serv.getVnfCustomizations()));
296                     ret.setAllottedResourceCustomizations(new ArrayList(serv.getAllottedCustomizations()));
297                 }
298                 ret.setService(serv);
299             } else if (modelInvariantUUID != null && !"".equals(modelInvariantUUID)) {
300                 uuid = modelInvariantUUID;
301                 if (modelVersion != null && !"".equals(modelVersion)) {
302                     logger.debug(
303                             "Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: {}  serviceModelVersion: {}",
304                             uuid, modelVersion);
305                     Service serv = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(modelVersion, uuid);
306
307                     ret.setService(serv);
308                 } else {
309                     logger.debug(
310                             "Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: {}",
311                             uuid);
312                     Service serv = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
313                     ret.setService(serv);
314                 }
315             } else {
316                 throw (new Exception(NO_MATCHING_PARAMETERS));
317             }
318
319             if (ret.getService() == null) {
320                 logger.debug("serviceMacroHolder not found");
321                 respStatus = HttpStatus.SC_NOT_FOUND;
322                 qryResp = new QueryServiceMacroHolder();
323             } else {
324                 qryResp = new QueryServiceMacroHolder(ret);
325                 logger.debug("serviceMacroHolder qryResp= {}", qryResp);
326             }
327             return respond(version, respStatus, IS_ARRAY, qryResp);
328         } catch (Exception e) {
329             logger.error("Exception - queryServiceMacroHolder", e);
330             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
331                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
332             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
333                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
334         }
335     }
336
337
338     @GET
339     @Path("allottedResources/{arModelCustomizationUuid}")
340     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
341     @Transactional(readOnly = true)
342     public Response serviceAllottedResources(@PathParam("version") String version,
343             @PathParam("arModelCustomizationUuid") String aUuid) {
344         return serviceAllottedResourcesImpl(version, !IS_ARRAY, aUuid, null, null, null);
345     }
346
347     @GET
348     @Path("serviceAllottedResources")
349     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
350     @Transactional(readOnly = true)
351     public Response serviceAllottedResources(@PathParam("version") String version,
352             @QueryParam("serviceModelUuid") String smUuid, @QueryParam("serviceModelInvariantUuid") String smiUuid,
353             @QueryParam("serviceModelVersion") String smVer, @QueryParam("arModelCustomizationUuid") String aUuid) {
354         return serviceAllottedResourcesImpl(version, IS_ARRAY, aUuid, smUuid, smiUuid, smVer);
355     }
356
357     public Response serviceAllottedResourcesImpl(String version, boolean isArray, String aUuid, String smUuid,
358             String serviceModelInvariantUuid, String smVer) {
359         QueryAllottedResourceCustomization qryResp;
360         int respStatus = HttpStatus.SC_OK;
361         String uuid = "";
362         List<AllottedResourceCustomization> ret = new ArrayList<>();
363         Service service = null;
364         try {
365             if (smUuid != null && !"".equals(smUuid)) {
366                 uuid = smUuid;
367                 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(uuid);
368             } else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
369                 uuid = serviceModelInvariantUuid;
370                 if (smVer != null && !"".equals(smVer)) {
371                     service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, uuid);
372                 } else {
373                     service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
374                 }
375             } else if (aUuid != null && !"".equals(aUuid)) {
376                 uuid = aUuid;
377                 ret = allottedCustomizationRepo.findByModelCustomizationUUID(uuid);
378             } else {
379                 throw (new Exception(NO_MATCHING_PARAMETERS));
380             }
381
382             if (service != null)
383                 ret = service.getAllottedCustomizations();
384
385             if (ret == null || ret.isEmpty()) {
386                 logger.debug("AllottedResourceCustomization not found");
387                 respStatus = HttpStatus.SC_NOT_FOUND;
388                 qryResp = new QueryAllottedResourceCustomization();
389             } else {
390                 qryResp = new QueryAllottedResourceCustomization(ret);
391                 logger.debug("AllottedResourceCustomization qryResp= {}", qryResp);
392             }
393             return respond(version, respStatus, isArray, qryResp);
394         } catch (Exception e) {
395             logger.error("Exception - queryAllottedResourceCustomization", e);
396             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
397                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
398             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
399                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
400         }
401     }
402
403     @GET
404     @Path("vfModules")
405     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
406     @Transactional(readOnly = true)
407     public Response vfModules(@QueryParam("vfModuleModelName") String vfModuleModelName) {
408         QueryVfModule qryResp;
409         int respStatus = HttpStatus.SC_OK;
410         List<VfModuleCustomization> ret = null;
411         try {
412             if (vfModuleModelName != null && !"".equals(vfModuleModelName)) {
413                 VfModule vfModule = vfModuleRepo.findFirstByModelNameOrderByModelVersionDesc(vfModuleModelName);
414                 if (vfModule != null)
415                     ret = vfModule.getVfModuleCustomization();
416             } else {
417                 throw (new Exception(NO_MATCHING_PARAMETERS));
418             }
419
420             if (ret == null || ret.isEmpty()) {
421                 logger.debug("vfModules not found");
422                 respStatus = HttpStatus.SC_NOT_FOUND;
423                 qryResp = new QueryVfModule();
424             } else {
425                 qryResp = new QueryVfModule(ret);
426                 if (logger.isDebugEnabled())
427                     logger.debug("vfModules tojsonstring is: {}", qryResp.JSON2(false, false));
428             }
429             return Response.status(respStatus).entity(qryResp.JSON2(false, false))
430                     .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
431         } catch (Exception e) {
432             logger.error("Exception during query VfModules by vfModuleModuleName: ", e);
433             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
434                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
435             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
436                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
437         }
438     }
439
440     /**
441      * Get the tosca csar info from catalog <br>
442      * 
443      * @param smUuid service model uuid
444      * @return the tosca csar information of the serivce.
445      * @since ONAP Beijing Release
446      */
447     @GET
448     @Path("serviceToscaCsar")
449     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
450     public Response serviceToscaCsar(@QueryParam("serviceModelUuid") String smUuid) {
451         int respStatus = HttpStatus.SC_OK;
452         String entity = "";
453         try {
454             if (smUuid != null && !"".equals(smUuid)) {
455                 logger.debug("Query Csar by service model uuid: {}", smUuid);
456
457                 Service service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(smUuid);
458
459                 if (service != null) {
460                     ToscaCsar toscaCsar = service.getCsar();
461                     if (toscaCsar != null) {
462                         QueryServiceCsar serviceCsar = new QueryServiceCsar(toscaCsar);
463                         entity = serviceCsar.JSON2(false, false);
464                     } else {
465                         respStatus = HttpStatus.SC_NOT_FOUND;
466                     }
467                 } else {
468                     respStatus = HttpStatus.SC_NOT_FOUND;
469                 }
470
471             } else {
472                 throw (new Exception("Incoming parameter is null or blank"));
473             }
474             return Response.status(respStatus).entity(entity)
475                     .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
476         } catch (Exception e) {
477             logger.error("Exception during query csar by service model uuid: ", e);
478             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
479                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
480             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
481                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
482         }
483     }
484
485     /**
486      * Get the resource recipe info from catalog <br>
487      * 
488      * @param rmUuid resource model uuid
489      * @return the recipe information of the resource.
490      * @since ONAP Beijing Release
491      */
492     @GET
493     @Path("resourceRecipe")
494     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
495     public Response resourceRecipe(@QueryParam("resourceModelUuid") String rmUuid,
496             @QueryParam("action") String action) {
497         int respStatus = HttpStatus.SC_OK;
498         String entity = "";
499         try {
500             if (rmUuid != null && !"".equals(rmUuid)) {
501                 logger.debug("Query recipe by resource model uuid: {}", rmUuid);
502                 // check vnf and network and ar, the resource could be any resource.
503                 Recipe recipe = null;
504
505                 VnfResource vnf = vnfResourceRepo.findResourceByModelUUID(rmUuid);
506                 if (vnf != null) {
507                     recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndActionAndVersionStr(vnf.getModelName(), action,
508                             vnf.getModelVersion());
509
510                     // for network service fetch the default recipe
511                     if (recipe == null && vnf.getSubCategory().equalsIgnoreCase(NETWORK_SERVICE)) {
512                         recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndAction("NS_DEFAULT", action);
513                     }
514                 }
515
516
517                 if (null == recipe) {
518                     NetworkResource nResource = networkResourceRepo.findResourceByModelUUID(rmUuid);
519
520                     if (nResource != null) {
521                         recipe = networkRecipeRepo.findFirstByModelNameAndActionAndVersionStr(nResource.getModelName(),
522                                 action, nResource.getModelVersion());
523
524                         // for network fetch the default recipe
525                         if (recipe == null) {
526                             recipe = networkRecipeRepo.findFirstByModelNameAndAction("SDNC_DEFAULT", action);
527                         }
528                     }
529                 }
530
531                 if (null == recipe) {
532                     AllottedResource arResource = arResourceRepo.findResourceByModelUUID(rmUuid);
533                     if (arResource != null) {
534                         recipe = arRecipeRepo.findByModelNameAndActionAndVersion(arResource.getModelName(), action,
535                                 arResource.getModelVersion());
536                     }
537                 }
538                 if (recipe != null) {
539                     QueryResourceRecipe resourceRecipe = new QueryResourceRecipe(recipe);
540                     entity = resourceRecipe.JSON2(false, false);
541                 } else {
542                     respStatus = HttpStatus.SC_NOT_FOUND;
543                 }
544             } else {
545                 throw new Exception("Incoming parameter is null or blank");
546             }
547             return Response.status(respStatus).entity(entity)
548                     .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
549         } catch (Exception e) {
550             logger.error("Exception during query recipe by resource model uuid: ", e);
551             CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
552                     CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
553             return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
554                     .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
555         }
556     }
557 }