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