2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.adapters.catalogdb.rest;
27 import java.util.ArrayList;
28 import java.util.List;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.QueryParam;
34 import javax.ws.rs.core.GenericEntity;
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import org.apache.http.HttpStatus;
39 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQuery;
40 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQueryException;
41 import org.onap.so.adapters.catalogdb.catalogrest.CatalogQueryExceptionCategory;
42 import org.onap.so.adapters.catalogdb.catalogrest.QueryAllottedResourceCustomization;
43 import org.onap.so.adapters.catalogdb.catalogrest.QueryResourceRecipe;
44 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceCsar;
45 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceMacroHolder;
46 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceNetworks;
47 import org.onap.so.adapters.catalogdb.catalogrest.QueryServiceVnfs;
48 import org.onap.so.adapters.catalogdb.catalogrest.QueryVfModule;
49 import org.onap.so.db.catalog.beans.*;
50 import org.onap.so.db.catalog.data.repository.*;
51 import org.onap.so.db.catalog.rest.beans.ServiceMacroHolder;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.stereotype.Component;
56 import org.springframework.transaction.annotation.Transactional;
59 @Path("/{version: v[0-9]+}")
61 public class CatalogDbAdapterRest {
62 protected static Logger logger = LoggerFactory.getLogger(CatalogDbAdapterRest.class);
63 private static final boolean IS_ARRAY = true;
64 private static final String NETWORK_SERVICE = "network service";
67 private VnfCustomizationRepository vnfCustomizationRepo;
70 private ServiceRepository serviceRepo;
73 private NetworkResourceCustomizationRepository networkCustomizationRepo;
76 private NetworkResourceRepository networkResourceRepo;
79 private AllottedResourceCustomizationRepository allottedCustomizationRepo;
82 private ToscaCsarRepository toscaCsarRepo;
85 private VFModuleRepository vfModuleRepo;
88 private VnfRecipeRepository vnfRecipeRepo;
91 private NetworkRecipeRepository networkRecipeRepo;
94 private ArRecipeRepository arRecipeRepo;
97 private VnfResourceRepository vnfResourceRepo;
100 private AllottedResourceRepository arResourceRepo;
103 private InstanceGroupRepository instanceGroupRepository;
105 private static final String NO_MATCHING_PARAMETERS = "no matching parameters";
107 public Response respond(String version, int respStatus, boolean isArray, CatalogQuery qryResp) {
108 return Response.status(respStatus).entity(qryResp.toJsonString(version, isArray))
109 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
113 @Path("vnfResources/{vnfModelCustomizationUuid}")
114 @Transactional(readOnly = true)
115 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
116 public Response serviceVnfs(@PathParam("version") String version,
117 @PathParam("vnfModelCustomizationUuid") String vnfUuid) {
118 return serviceVnfsImpl(version, !IS_ARRAY, vnfUuid, null, null, null, null);
123 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
124 @Transactional(readOnly = true)
125 public Response serviceVnfs(@PathParam("version") String version,
126 @QueryParam("vnfModelCustomizationUuid") String vnfUuid, @QueryParam("serviceModelUuid") String smUuid,
127 @QueryParam("serviceModelInvariantUuid") String smiUuid, @QueryParam("serviceModelVersion") String smVer,
128 @QueryParam("serviceModelName") String smName) {
129 return serviceVnfsImpl(version, IS_ARRAY, vnfUuid, smUuid, smiUuid, smVer, smName);
132 public Response serviceVnfsImpl(String version, boolean isArray, String vnfUuid, String serviceModelUUID,
133 String smiUuid, String smVer, String smName) {
134 QueryServiceVnfs qryResp = null;
135 int respStatus = HttpStatus.SC_OK;
136 List<VnfResourceCustomization> ret = new ArrayList<>();
137 Service service = null;
139 if (vnfUuid != null && !"".equals(vnfUuid))
140 ret = vnfCustomizationRepo.findByModelCustomizationUUID(vnfUuid);
141 else if (serviceModelUUID != null && !"".equals(serviceModelUUID))
142 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(serviceModelUUID);
143 else if (smiUuid != null && !"".equals(smiUuid))
144 if (smVer != null && !"".equals(smVer))
145 service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, smiUuid);
147 service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(smiUuid);
148 else if (smName != null && !"".equals(smName)) {
149 if (smVer != null && !"".equals(smVer))
150 service = serviceRepo.findByModelNameAndModelVersion(smName, smVer);
152 service = serviceRepo.findFirstByModelNameOrderByModelVersionDesc(smName);
154 throw (new Exception(NO_MATCHING_PARAMETERS));
157 if (service == null && ret.isEmpty()) {
158 respStatus = HttpStatus.SC_NOT_FOUND;
159 qryResp = new QueryServiceVnfs();
160 } else if (service == null && !ret.isEmpty()) {
161 qryResp = new QueryServiceVnfs(ret);
162 } else if (service != null) {
163 qryResp = new QueryServiceVnfs(service.getVnfCustomizations());
165 logger.debug("serviceVnfs qryResp= {}", qryResp);
166 return respond(version, respStatus, isArray, qryResp);
167 } catch (Exception e) {
168 logger.error("Exception - queryServiceVnfs", e);
169 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
170 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
171 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
172 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
177 @Path("networkResources/{networkModelCustomizationUuid}")
178 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
179 @Transactional(readOnly = true)
180 public Response serviceNetworks(@PathParam("version") String version,
181 @PathParam("networkModelCustomizationUuid") String nUuid) {
182 return serviceNetworksImpl(version, !IS_ARRAY, nUuid, null, null, null, null);
186 @Path("serviceNetworks")
187 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
188 @Transactional(readOnly = true)
189 public Response serviceNetworks(@PathParam("version") String version,
190 @QueryParam("networkModelCustomizationUuid") String networkModelCustomizationUuid,
191 @QueryParam("networkType") String networkType, @QueryParam("networkModelName") String networkModelName,
192 @QueryParam("serviceModelUuid") String serviceModelUuid,
193 @QueryParam("serviceModelInvariantUuid") String serviceModelInvariantUuid,
194 @QueryParam("serviceModelVersion") String serviceModelVersion,
195 @QueryParam("networkModelVersion") String networkModelVersion) {
196 if (networkModelName != null && !"".equals(networkModelName)) {
197 networkType = networkModelName;
199 return serviceNetworksImpl(version, IS_ARRAY, networkModelCustomizationUuid, networkType, serviceModelUuid,
200 serviceModelInvariantUuid, serviceModelVersion);
203 public Response serviceNetworksImpl(String version, boolean isArray, String networkModelCustomizationUuid,
204 String networkType, String serviceModelUuid, String serviceModelInvariantUuid, String serviceModelVersion) {
205 QueryServiceNetworks qryResp;
206 int respStatus = HttpStatus.SC_OK;
208 List<NetworkResourceCustomization> ret = new ArrayList<>();
209 Service service = null;
212 if (networkModelCustomizationUuid != null && !"".equals(networkModelCustomizationUuid)) {
213 uuid = networkModelCustomizationUuid;
214 ret = networkCustomizationRepo.findByModelCustomizationUUID(networkModelCustomizationUuid);
215 } else if (networkType != null && !"".equals(networkType)) {
217 NetworkResource networkResources =
218 networkResourceRepo.findFirstByModelNameOrderByModelVersionDesc(networkType);
219 if (networkResources != null)
220 ret = networkResources.getNetworkResourceCustomization();
221 } else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
222 uuid = serviceModelInvariantUuid;
223 if (serviceModelVersion != null && !"".equals(serviceModelVersion)) {
224 service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(serviceModelVersion, uuid);
226 service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
228 } else if (serviceModelUuid != null && !"".equals(serviceModelUuid)) {
229 uuid = serviceModelUuid;
230 service = serviceRepo.findOneByModelUUID(serviceModelUuid);
232 throw (new Exception(NO_MATCHING_PARAMETERS));
236 ret = service.getNetworkCustomizations();
238 if (ret == null || ret.isEmpty()) {
239 logger.debug("serviceNetworks not found");
240 respStatus = HttpStatus.SC_NOT_FOUND;
241 qryResp = new QueryServiceNetworks();
243 qryResp = new QueryServiceNetworks(ret);
244 logger.debug("serviceNetworks found qryResp= {}", qryResp);
246 return respond(version, respStatus, isArray, qryResp);
247 } catch (Exception e) {
248 logger.error("Exception - queryServiceNetworks", e);
249 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
250 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
251 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
252 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
257 @Path("serviceResources")
258 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
259 @Transactional(readOnly = true)
260 public Response serviceResources(@PathParam("version") String version,
261 @QueryParam("serviceModelUuid") String modelUUID,
262 @QueryParam("serviceModelInvariantUuid") String modelInvariantUUID,
263 @QueryParam("serviceModelVersion") String modelVersion) {
265 QueryServiceMacroHolder qryResp;
266 int respStatus = HttpStatus.SC_OK;
268 ServiceMacroHolder ret = new ServiceMacroHolder();
271 if (modelUUID != null && !"".equals(modelUUID)) {
273 logger.debug("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: {}", uuid);
274 Service serv = serviceRepo.findOneByModelUUID(uuid);
277 ret.setNetworkResourceCustomizations(new ArrayList(serv.getNetworkCustomizations()));
278 ret.setVnfResourceCustomizations(new ArrayList(serv.getVnfCustomizations()));
279 ret.setAllottedResourceCustomizations(new ArrayList(serv.getAllottedCustomizations()));
281 ret.setService(serv);
282 } else if (modelInvariantUUID != null && !"".equals(modelInvariantUUID)) {
283 uuid = modelInvariantUUID;
284 if (modelVersion != null && !"".equals(modelVersion)) {
286 "Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: {} serviceModelVersion: {}",
288 Service serv = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(modelVersion, uuid);
290 ret.setService(serv);
293 "Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: {}",
295 Service serv = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
296 ret.setService(serv);
299 throw (new Exception(NO_MATCHING_PARAMETERS));
302 if (ret.getService() == null) {
303 logger.debug("serviceMacroHolder not found");
304 respStatus = HttpStatus.SC_NOT_FOUND;
305 qryResp = new QueryServiceMacroHolder();
307 qryResp = new QueryServiceMacroHolder(ret);
308 logger.debug("serviceMacroHolder qryResp= {}", qryResp);
310 return respond(version, respStatus, IS_ARRAY, qryResp);
311 } catch (Exception e) {
312 logger.error("Exception - queryServiceMacroHolder", e);
313 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
314 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
315 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
316 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
322 @Path("allottedResources/{arModelCustomizationUuid}")
323 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
324 @Transactional(readOnly = true)
325 public Response serviceAllottedResources(@PathParam("version") String version,
326 @PathParam("arModelCustomizationUuid") String aUuid) {
327 return serviceAllottedResourcesImpl(version, !IS_ARRAY, aUuid, null, null, null);
331 @Path("serviceAllottedResources")
332 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
333 @Transactional(readOnly = true)
334 public Response serviceAllottedResources(@PathParam("version") String version,
335 @QueryParam("serviceModelUuid") String smUuid, @QueryParam("serviceModelInvariantUuid") String smiUuid,
336 @QueryParam("serviceModelVersion") String smVer, @QueryParam("arModelCustomizationUuid") String aUuid) {
337 return serviceAllottedResourcesImpl(version, IS_ARRAY, aUuid, smUuid, smiUuid, smVer);
340 public Response serviceAllottedResourcesImpl(String version, boolean isArray, String aUuid, String smUuid,
341 String serviceModelInvariantUuid, String smVer) {
342 QueryAllottedResourceCustomization qryResp;
343 int respStatus = HttpStatus.SC_OK;
345 List<AllottedResourceCustomization> ret = new ArrayList<>();
346 Service service = null;
348 if (smUuid != null && !"".equals(smUuid)) {
350 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(uuid);
351 } else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
352 uuid = serviceModelInvariantUuid;
353 if (smVer != null && !"".equals(smVer)) {
354 service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, uuid);
356 service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
358 } else if (aUuid != null && !"".equals(aUuid)) {
360 ret = allottedCustomizationRepo.findByModelCustomizationUUID(uuid);
362 throw (new Exception(NO_MATCHING_PARAMETERS));
366 ret = service.getAllottedCustomizations();
368 if (ret == null || ret.isEmpty()) {
369 logger.debug("AllottedResourceCustomization not found");
370 respStatus = HttpStatus.SC_NOT_FOUND;
371 qryResp = new QueryAllottedResourceCustomization();
373 qryResp = new QueryAllottedResourceCustomization(ret);
374 logger.debug("AllottedResourceCustomization qryResp= {}", qryResp);
376 return respond(version, respStatus, isArray, qryResp);
377 } catch (Exception e) {
378 logger.error("Exception - queryAllottedResourceCustomization", e);
379 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
380 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
381 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
382 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
388 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
389 @Transactional(readOnly = true)
390 public Response vfModules(@QueryParam("vfModuleModelName") String vfModuleModelName) {
391 QueryVfModule qryResp;
392 int respStatus = HttpStatus.SC_OK;
393 List<VfModuleCustomization> ret = null;
395 if (vfModuleModelName != null && !"".equals(vfModuleModelName)) {
396 VfModule vfModule = vfModuleRepo.findFirstByModelNameOrderByModelVersionDesc(vfModuleModelName);
397 if (vfModule != null)
398 ret = vfModule.getVfModuleCustomization();
400 throw (new Exception(NO_MATCHING_PARAMETERS));
403 if (ret == null || ret.isEmpty()) {
404 logger.debug("vfModules not found");
405 respStatus = HttpStatus.SC_NOT_FOUND;
406 qryResp = new QueryVfModule();
408 qryResp = new QueryVfModule(ret);
409 if (logger.isDebugEnabled())
410 logger.debug("vfModules tojsonstring is: {}", qryResp.JSON2(false, false));
412 return Response.status(respStatus).entity(qryResp.JSON2(false, false))
413 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
414 } catch (Exception e) {
415 logger.error("Exception during query VfModules by vfModuleModuleName: ", e);
416 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
417 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
418 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
419 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
424 * Get the tosca csar info from catalog <br>
426 * @param smUuid service model uuid
427 * @return the tosca csar information of the serivce.
428 * @since ONAP Beijing Release
431 @Path("serviceToscaCsar")
432 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
433 public Response serviceToscaCsar(@QueryParam("serviceModelUuid") String smUuid) {
434 int respStatus = HttpStatus.SC_OK;
437 if (smUuid != null && !"".equals(smUuid)) {
438 logger.debug("Query Csar by service model uuid: {}", smUuid);
440 Service service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(smUuid);
442 if (service != null) {
443 ToscaCsar toscaCsar = service.getCsar();
444 if (toscaCsar != null) {
445 QueryServiceCsar serviceCsar = new QueryServiceCsar(toscaCsar);
446 entity = serviceCsar.JSON2(false, false);
448 respStatus = HttpStatus.SC_NOT_FOUND;
451 respStatus = HttpStatus.SC_NOT_FOUND;
455 throw (new Exception("Incoming parameter is null or blank"));
457 return Response.status(respStatus).entity(entity)
458 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
459 } catch (Exception e) {
460 logger.error("Exception during query csar by service model uuid: ", e);
461 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
462 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
463 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
464 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
469 * Get the resource recipe info from catalog <br>
471 * @param rmUuid resource model uuid
472 * @return the recipe information of the resource.
473 * @since ONAP Beijing Release
476 @Path("resourceRecipe")
477 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
478 public Response resourceRecipe(@QueryParam("resourceModelUuid") String rmUuid,
479 @QueryParam("action") String action) {
480 int respStatus = HttpStatus.SC_OK;
483 if (rmUuid != null && !"".equals(rmUuid)) {
484 logger.debug("Query recipe by resource model uuid: {}", rmUuid);
485 // check vnf and network and ar, the resource could be any resource.
486 Recipe recipe = null;
488 VnfResource vnf = vnfResourceRepo.findResourceByModelUUID(rmUuid);
490 recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndActionAndVersionStr(vnf.getModelName(), action,
491 vnf.getModelVersion());
493 // for network service fetch the default recipe
494 if (recipe == null && vnf.getSubCategory().equalsIgnoreCase(NETWORK_SERVICE)) {
495 recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndAction("NS_DEFAULT", action);
500 if (null == recipe) {
501 NetworkResource nResource = networkResourceRepo.findResourceByModelUUID(rmUuid);
503 if (nResource != null) {
504 recipe = networkRecipeRepo.findFirstByModelNameAndActionAndVersionStr(nResource.getModelName(),
505 action, nResource.getModelVersion());
507 // for network fetch the default recipe
508 if (recipe == null) {
509 recipe = networkRecipeRepo.findFirstByModelNameAndAction("SDNC_DEFAULT", action);
514 if (null == recipe) {
515 AllottedResource arResource = arResourceRepo.findResourceByModelUUID(rmUuid);
516 if (arResource != null) {
517 recipe = arRecipeRepo.findByModelNameAndActionAndVersion(arResource.getModelName(), action,
518 arResource.getModelVersion());
522 if (null == recipe) {
523 InstanceGroup grpResource = instanceGroupRepository.findByModelUUID(rmUuid);
524 if (grpResource != null) {
525 recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndActionAndVersionStr(
526 grpResource.getModelName(), action, grpResource.getModelVersion());
531 if (recipe != null) {
532 QueryResourceRecipe resourceRecipe = new QueryResourceRecipe(recipe);
533 entity = resourceRecipe.JSON2(false, false);
535 respStatus = HttpStatus.SC_NOT_FOUND;
538 throw new Exception("Incoming parameter is null or blank");
540 return Response.status(respStatus).entity(entity)
541 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
542 } catch (Exception e) {
543 logger.error("Exception during query recipe by resource model uuid: ", e);
544 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
545 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
546 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
547 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();