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;
106 private ServiceInfoRepository serviceInfoRepository;
108 private static final String NO_MATCHING_PARAMETERS = "no matching parameters";
110 public Response respond(String version, int respStatus, boolean isArray, CatalogQuery qryResp) {
111 return Response.status(respStatus).entity(qryResp.toJsonString(version, isArray))
112 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
116 @Path("vnfResources/{vnfModelCustomizationUuid}")
117 @Transactional(readOnly = true)
118 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
119 public Response serviceVnfs(@PathParam("version") String version,
120 @PathParam("vnfModelCustomizationUuid") String vnfUuid) {
121 return serviceVnfsImpl(version, !IS_ARRAY, vnfUuid, null, null, null, null);
126 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
127 @Transactional(readOnly = true)
128 public Response serviceVnfs(@PathParam("version") String version,
129 @QueryParam("vnfModelCustomizationUuid") String vnfUuid, @QueryParam("serviceModelUuid") String smUuid,
130 @QueryParam("serviceModelInvariantUuid") String smiUuid, @QueryParam("serviceModelVersion") String smVer,
131 @QueryParam("serviceModelName") String smName) {
132 return serviceVnfsImpl(version, IS_ARRAY, vnfUuid, smUuid, smiUuid, smVer, smName);
135 public Response serviceVnfsImpl(String version, boolean isArray, String vnfUuid, String serviceModelUUID,
136 String smiUuid, String smVer, String smName) {
137 QueryServiceVnfs qryResp = null;
138 int respStatus = HttpStatus.SC_OK;
139 List<VnfResourceCustomization> ret = new ArrayList<>();
140 Service service = null;
142 if (vnfUuid != null && !"".equals(vnfUuid))
143 ret = vnfCustomizationRepo.findByModelCustomizationUUID(vnfUuid);
144 else if (serviceModelUUID != null && !"".equals(serviceModelUUID))
145 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(serviceModelUUID);
146 else if (smiUuid != null && !"".equals(smiUuid))
147 if (smVer != null && !"".equals(smVer))
148 service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, smiUuid);
150 service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(smiUuid);
151 else if (smName != null && !"".equals(smName)) {
152 if (smVer != null && !"".equals(smVer))
153 service = serviceRepo.findByModelNameAndModelVersion(smName, smVer);
155 service = serviceRepo.findFirstByModelNameOrderByModelVersionDesc(smName);
157 throw (new Exception(NO_MATCHING_PARAMETERS));
160 if (service == null && ret.isEmpty()) {
161 respStatus = HttpStatus.SC_NOT_FOUND;
162 qryResp = new QueryServiceVnfs();
163 } else if (service == null && !ret.isEmpty()) {
164 qryResp = new QueryServiceVnfs(ret);
165 } else if (service != null) {
166 qryResp = new QueryServiceVnfs(service.getVnfCustomizations());
168 logger.debug("serviceVnfs qryResp= {}", qryResp);
169 return respond(version, respStatus, isArray, qryResp);
170 } catch (Exception e) {
171 logger.error("Exception - queryServiceVnfs", e);
172 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
173 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
174 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
175 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
180 @Path("networkResources/{networkModelCustomizationUuid}")
181 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
182 @Transactional(readOnly = true)
183 public Response serviceNetworks(@PathParam("version") String version,
184 @PathParam("networkModelCustomizationUuid") String nUuid) {
185 return serviceNetworksImpl(version, !IS_ARRAY, nUuid, null, null, null, null);
189 @Path("serviceNetworks")
190 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
191 @Transactional(readOnly = true)
192 public Response serviceNetworks(@PathParam("version") String version,
193 @QueryParam("networkModelCustomizationUuid") String networkModelCustomizationUuid,
194 @QueryParam("networkType") String networkType, @QueryParam("networkModelName") String networkModelName,
195 @QueryParam("serviceModelUuid") String serviceModelUuid,
196 @QueryParam("serviceModelInvariantUuid") String serviceModelInvariantUuid,
197 @QueryParam("serviceModelVersion") String serviceModelVersion,
198 @QueryParam("networkModelVersion") String networkModelVersion) {
199 if (networkModelName != null && !"".equals(networkModelName)) {
200 networkType = networkModelName;
202 return serviceNetworksImpl(version, IS_ARRAY, networkModelCustomizationUuid, networkType, serviceModelUuid,
203 serviceModelInvariantUuid, serviceModelVersion);
206 public Response serviceNetworksImpl(String version, boolean isArray, String networkModelCustomizationUuid,
207 String networkType, String serviceModelUuid, String serviceModelInvariantUuid, String serviceModelVersion) {
208 QueryServiceNetworks qryResp;
209 int respStatus = HttpStatus.SC_OK;
211 List<NetworkResourceCustomization> ret = new ArrayList<>();
212 Service service = null;
215 if (networkModelCustomizationUuid != null && !"".equals(networkModelCustomizationUuid)) {
216 uuid = networkModelCustomizationUuid;
217 ret = networkCustomizationRepo.findByModelCustomizationUUID(networkModelCustomizationUuid);
218 } else if (networkType != null && !"".equals(networkType)) {
220 NetworkResource networkResources =
221 networkResourceRepo.findFirstByModelNameOrderByModelVersionDesc(networkType);
222 if (networkResources != null)
223 ret = networkResources.getNetworkResourceCustomization();
224 } else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
225 uuid = serviceModelInvariantUuid;
226 if (serviceModelVersion != null && !"".equals(serviceModelVersion)) {
227 service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(serviceModelVersion, uuid);
229 service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
231 } else if (serviceModelUuid != null && !"".equals(serviceModelUuid)) {
232 uuid = serviceModelUuid;
233 service = serviceRepo.findOneByModelUUID(serviceModelUuid);
235 throw (new Exception(NO_MATCHING_PARAMETERS));
239 ret = service.getNetworkCustomizations();
241 if (ret == null || ret.isEmpty()) {
242 logger.debug("serviceNetworks not found");
243 respStatus = HttpStatus.SC_NOT_FOUND;
244 qryResp = new QueryServiceNetworks();
246 qryResp = new QueryServiceNetworks(ret);
247 logger.debug("serviceNetworks found qryResp= {}", qryResp);
249 return respond(version, respStatus, isArray, qryResp);
250 } catch (Exception e) {
251 logger.error("Exception - queryServiceNetworks", e);
252 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
253 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
254 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
255 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
260 @Path("serviceResources")
261 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
262 @Transactional(readOnly = true)
263 public Response serviceResources(@PathParam("version") String version,
264 @QueryParam("serviceModelUuid") String modelUUID,
265 @QueryParam("serviceModelInvariantUuid") String modelInvariantUUID,
266 @QueryParam("serviceModelVersion") String modelVersion) {
268 QueryServiceMacroHolder qryResp;
269 int respStatus = HttpStatus.SC_OK;
271 ServiceMacroHolder ret = new ServiceMacroHolder();
274 if (modelUUID != null && !"".equals(modelUUID)) {
276 logger.debug("Query serviceMacroHolder getAllResourcesByServiceModelUuid serviceModelUuid: {}", uuid);
277 Service serv = serviceRepo.findOneByModelUUID(uuid);
280 ret.setNetworkResourceCustomizations(new ArrayList(serv.getNetworkCustomizations()));
281 ret.setVnfResourceCustomizations(new ArrayList(serv.getVnfCustomizations()));
282 ret.setAllottedResourceCustomizations(new ArrayList(serv.getAllottedCustomizations()));
284 ret.setService(serv);
285 } else if (modelInvariantUUID != null && !"".equals(modelInvariantUUID)) {
286 uuid = modelInvariantUUID;
287 if (modelVersion != null && !"".equals(modelVersion)) {
289 "Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelInvariantUuid: {} serviceModelVersion: {}",
291 Service serv = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(modelVersion, uuid);
293 ret.setService(serv);
296 "Query serviceMacroHolder getAllResourcesByServiceModelInvariantUuid serviceModelUuid: {}",
298 Service serv = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
299 ret.setService(serv);
302 throw (new Exception(NO_MATCHING_PARAMETERS));
305 if (ret.getService() == null) {
306 logger.debug("serviceMacroHolder not found");
307 respStatus = HttpStatus.SC_NOT_FOUND;
308 qryResp = new QueryServiceMacroHolder();
310 ServiceInfo serviceInfo = serviceInfoRepository.findByService(ret.getService());
311 ret.setServiceInfo(serviceInfo);
312 qryResp = new QueryServiceMacroHolder(ret);
313 logger.debug("serviceMacroHolder qryResp= {}", qryResp);
315 return respond(version, respStatus, IS_ARRAY, qryResp);
316 } catch (Exception e) {
317 logger.error("Exception - queryServiceMacroHolder", e);
318 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
319 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
320 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
321 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
327 @Path("allottedResources/{arModelCustomizationUuid}")
328 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
329 @Transactional(readOnly = true)
330 public Response serviceAllottedResources(@PathParam("version") String version,
331 @PathParam("arModelCustomizationUuid") String aUuid) {
332 return serviceAllottedResourcesImpl(version, !IS_ARRAY, aUuid, null, null, null);
336 @Path("serviceAllottedResources")
337 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
338 @Transactional(readOnly = true)
339 public Response serviceAllottedResources(@PathParam("version") String version,
340 @QueryParam("serviceModelUuid") String smUuid, @QueryParam("serviceModelInvariantUuid") String smiUuid,
341 @QueryParam("serviceModelVersion") String smVer, @QueryParam("arModelCustomizationUuid") String aUuid) {
342 return serviceAllottedResourcesImpl(version, IS_ARRAY, aUuid, smUuid, smiUuid, smVer);
345 public Response serviceAllottedResourcesImpl(String version, boolean isArray, String aUuid, String smUuid,
346 String serviceModelInvariantUuid, String smVer) {
347 QueryAllottedResourceCustomization qryResp;
348 int respStatus = HttpStatus.SC_OK;
350 List<AllottedResourceCustomization> ret = new ArrayList<>();
351 Service service = null;
353 if (smUuid != null && !"".equals(smUuid)) {
355 service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(uuid);
356 } else if (serviceModelInvariantUuid != null && !"".equals(serviceModelInvariantUuid)) {
357 uuid = serviceModelInvariantUuid;
358 if (smVer != null && !"".equals(smVer)) {
359 service = serviceRepo.findFirstByModelVersionAndModelInvariantUUID(smVer, uuid);
361 service = serviceRepo.findFirstByModelInvariantUUIDOrderByModelVersionDesc(uuid);
363 } else if (aUuid != null && !"".equals(aUuid)) {
365 ret = allottedCustomizationRepo.findByModelCustomizationUUID(uuid);
367 throw (new Exception(NO_MATCHING_PARAMETERS));
371 ret = service.getAllottedCustomizations();
373 if (ret == null || ret.isEmpty()) {
374 logger.debug("AllottedResourceCustomization not found");
375 respStatus = HttpStatus.SC_NOT_FOUND;
376 qryResp = new QueryAllottedResourceCustomization();
378 qryResp = new QueryAllottedResourceCustomization(ret);
379 logger.debug("AllottedResourceCustomization qryResp= {}", qryResp);
381 return respond(version, respStatus, isArray, qryResp);
382 } catch (Exception e) {
383 logger.error("Exception - queryAllottedResourceCustomization", e);
384 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
385 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
386 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
387 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
393 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
394 @Transactional(readOnly = true)
395 public Response vfModules(@QueryParam("vfModuleModelName") String vfModuleModelName) {
396 QueryVfModule qryResp;
397 int respStatus = HttpStatus.SC_OK;
398 List<VfModuleCustomization> ret = null;
400 if (vfModuleModelName != null && !"".equals(vfModuleModelName)) {
401 VfModule vfModule = vfModuleRepo.findFirstByModelNameOrderByModelVersionDesc(vfModuleModelName);
402 if (vfModule != null)
403 ret = vfModule.getVfModuleCustomization();
405 throw (new Exception(NO_MATCHING_PARAMETERS));
408 if (ret == null || ret.isEmpty()) {
409 logger.debug("vfModules not found");
410 respStatus = HttpStatus.SC_NOT_FOUND;
411 qryResp = new QueryVfModule();
413 qryResp = new QueryVfModule(ret);
414 if (logger.isDebugEnabled())
415 logger.debug("vfModules tojsonstring is: {}", qryResp.JSON2(false, false));
417 return Response.status(respStatus).entity(qryResp.JSON2(false, false))
418 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
419 } catch (Exception e) {
420 logger.error("Exception during query VfModules by vfModuleModuleName: ", 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();
429 * Get the tosca csar info from catalog <br>
431 * @param smUuid service model uuid
432 * @return the tosca csar information of the serivce.
433 * @since ONAP Beijing Release
436 @Path("serviceToscaCsar")
437 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
438 public Response serviceToscaCsar(@QueryParam("serviceModelUuid") String smUuid) {
439 int respStatus = HttpStatus.SC_OK;
442 if (smUuid != null && !"".equals(smUuid)) {
443 logger.debug("Query Csar by service model uuid: {}", smUuid);
445 Service service = serviceRepo.findFirstOneByModelUUIDOrderByModelVersionDesc(smUuid);
447 if (service != null) {
448 ToscaCsar toscaCsar = service.getCsar();
449 if (toscaCsar != null) {
450 QueryServiceCsar serviceCsar = new QueryServiceCsar(toscaCsar);
451 entity = serviceCsar.JSON2(false, false);
453 respStatus = HttpStatus.SC_NOT_FOUND;
456 respStatus = HttpStatus.SC_NOT_FOUND;
460 throw (new Exception("Incoming parameter is null or blank"));
462 return Response.status(respStatus).entity(entity)
463 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
464 } catch (Exception e) {
465 logger.error("Exception during query csar by service model uuid: ", e);
466 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
467 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
468 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
469 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();
474 * Get the resource recipe info from catalog <br>
476 * @param rmUuid resource model uuid
477 * @return the recipe information of the resource.
478 * @since ONAP Beijing Release
481 @Path("resourceRecipe")
482 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
483 public Response resourceRecipe(@QueryParam("resourceModelUuid") String rmUuid,
484 @QueryParam("action") String action) {
485 int respStatus = HttpStatus.SC_OK;
488 if (rmUuid != null && !"".equals(rmUuid)) {
489 logger.debug("Query recipe by resource model uuid: {}", rmUuid);
490 // check vnf and network and ar, the resource could be any resource.
491 Recipe recipe = null;
493 VnfResource vnf = vnfResourceRepo.findResourceByModelUUID(rmUuid);
495 recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndActionAndVersionStr(vnf.getModelName(), action,
496 vnf.getModelVersion());
498 // for network service fetch the default recipe
499 if (recipe == null && vnf.getSubCategory().equalsIgnoreCase(NETWORK_SERVICE)) {
500 recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndAction("NS_DEFAULT", action);
505 if (null == recipe) {
506 NetworkResource nResource = networkResourceRepo.findResourceByModelUUID(rmUuid);
508 if (nResource != null) {
509 recipe = networkRecipeRepo.findFirstByModelNameAndActionAndVersionStr(nResource.getModelName(),
510 action, nResource.getModelVersion());
512 // for network fetch the default recipe
513 if (recipe == null) {
514 recipe = networkRecipeRepo.findFirstByModelNameAndAction("SDNC_DEFAULT", action);
519 if (null == recipe) {
520 AllottedResource arResource = arResourceRepo.findResourceByModelUUID(rmUuid);
521 if (arResource != null) {
522 recipe = arRecipeRepo.findByModelNameAndActionAndVersion(arResource.getModelName(), action,
523 arResource.getModelVersion());
527 if (null == recipe) {
528 InstanceGroup grpResource = instanceGroupRepository.findByModelUUID(rmUuid);
529 if (grpResource != null) {
530 recipe = vnfRecipeRepo.findFirstVnfRecipeByNfRoleAndActionAndVersionStr(
531 grpResource.getModelName(), action, grpResource.getModelVersion());
536 if (recipe != null) {
537 QueryResourceRecipe resourceRecipe = new QueryResourceRecipe(recipe);
538 entity = resourceRecipe.JSON2(false, false);
540 respStatus = HttpStatus.SC_NOT_FOUND;
543 throw new Exception("Incoming parameter is null or blank");
545 return Response.status(respStatus).entity(entity)
546 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).build();
547 } catch (Exception e) {
548 logger.error("Exception during query recipe by resource model uuid: ", e);
549 CatalogQueryException excResp = new CatalogQueryException(e.getMessage(),
550 CatalogQueryExceptionCategory.INTERNAL, Boolean.FALSE, null);
551 return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
552 .entity(new GenericEntity<CatalogQueryException>(excResp) {}).build();