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