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