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