diff options
Diffstat (limited to 'src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java')
-rw-r--r-- | src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java | 194 |
1 files changed, 142 insertions, 52 deletions
diff --git a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java index 261f52c..34d8a4b 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java @@ -1,28 +1,25 @@ -package io.swagger.api.impl; - -/* - * ============LICENSE_START========================================== - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * ============LICENSE_END============================================ - * - * ECOMP and OpenECOMP are trademarks - * and service marks of AT&T Intellectual Property. - * + * ============LICENSE_END========================================================= */ +package io.swagger.api.impl; + import org.openecomp.dcae.inventory.daos.DCAEServiceTypesDAO; import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; import org.openecomp.dcae.inventory.daos.InventoryDAOManager; @@ -45,6 +42,8 @@ import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; import java.util.ArrayList; import java.util.List; +import java.util.Locale; +import java.util.UUID; @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { @@ -54,21 +53,30 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { private DCAEServiceType createDCAEServiceType(DCAEServiceTypeObject serviceTypeObject, UriInfo uriInfo) { DCAEServiceType serviceType = new DCAEServiceType(); - serviceType.setSelfLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "self", serviceTypeObject.getTypeName())); + serviceType.setSelfLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "self", serviceTypeObject.getTypeId())); + serviceType.setTypeId(serviceTypeObject.getTypeId()); serviceType.setTypeName(serviceTypeObject.getTypeName()); + serviceType.setTypeVersion(serviceTypeObject.getTypeVersion()); serviceType.setOwner(serviceTypeObject.getOwner()); serviceType.setVnfTypes(serviceTypeObject.getVnfTypes()); serviceType.setServiceIds(serviceTypeObject.getServiceIds()); serviceType.setServiceLocations(serviceTypeObject.getServiceLocations()); serviceType.setBlueprintTemplate(serviceTypeObject.getBlueprintTemplate()); - serviceType.created(serviceTypeObject.getCreated().toDate()); + serviceType.setAsdcServiceId(serviceTypeObject.getAsdcServiceId()); + serviceType.setAsdcResourceId(serviceTypeObject.getAsdcResourceId()); + // TODO: Construct ASDC service URL somehow + serviceType.setCreated(serviceTypeObject.getCreated().toDate()); + + DateTime deactivated = serviceTypeObject.getDeactivated(); + serviceType.setDeactivated(deactivated == null ? null : deactivated.toDate()); return serviceType; } @Override - public Response dcaeServiceTypesGet(String vnfType, String serviceId, String serviceLocation, Integer offset, UriInfo uriInfo, - SecurityContext securityContext) + public Response dcaeServiceTypesGet(String typeName, Boolean onlyLatest, Boolean onlyActive, String vnfType, + String serviceId, String serviceLocation, String asdcServiceId, + String asdcResourceId, Integer offset, UriInfo uriInfo, SecurityContext securityContext) throws NotFoundException { List<DCAEServiceTypeObject> serviceTypeObjects = new ArrayList<>(); @@ -76,12 +84,23 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { DateTime createdCutoff = DateTime.now(DateTimeZone.UTC); try (Handle jdbiHandle = InventoryDAOManager.getInstance().getHandle()) { - StringBuilder sb = new StringBuilder("select * from dcae_service_types"); + StringBuilder sb = new StringBuilder("select * from "); + + if (onlyLatest) { + // Use the view which filters types that are of only the latest versions + sb.append("dcae_service_types_latest"); + } else { + sb.append("dcae_service_types"); + } List<String> whereClauses = new ArrayList<String>(); + if (typeName != null) { + whereClauses.add(":typeName = type_name"); + } + if (vnfType != null) { - whereClauses.add(":vnfType = any(vnf_types)"); + whereClauses.add("lower(:vnfType) = any(lower(vnf_types\\:\\:text)\\:\\:text[])"); } if (serviceId != null) { @@ -92,9 +111,27 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { whereClauses.add("(:serviceLocation = any(service_locations) or service_locations = \'{}\' or service_locations is null)"); } + if (asdcServiceId != null) { + if ("NONE".equals(asdcServiceId.toUpperCase(Locale.ENGLISH))) { + whereClauses.add("asdc_service_id is null"); + } else { + whereClauses.add(":asdcServiceId = asdc_service_id"); + } + } + + if (asdcResourceId != null) { + if ("NONE".equals(asdcResourceId.toUpperCase(Locale.ENGLISH))) { + whereClauses.add("asdc_resource_id is null"); + } else { + whereClauses.add(":asdcResourceId = asdc_resource_id"); + } + } + whereClauses.add("created < :createdCutoff"); - // We only want the active service types - whereClauses.add("is_active = TRUE"); + + if (onlyActive) { + whereClauses.add("deactivated is null"); + } if (!whereClauses.isEmpty()) { sb.append(" where "); @@ -106,6 +143,10 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { Query<DCAEServiceTypeObject> query = jdbiHandle.createQuery(sb.toString()).map(new DCAEServiceTypeObjectMapper()); + if (typeName != null) { + query.bind("typeName", typeName); + } + if (vnfType != null) { query.bind("vnfType", vnfType); } @@ -118,6 +159,14 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { query.bind("serviceLocation", serviceLocation); } + if (asdcServiceId != null && !"NONE".equals(asdcServiceId.toUpperCase(Locale.ENGLISH))) { + query.bind("asdcServiceId", asdcServiceId); + } + + if (asdcResourceId != null && !"NONE".equals(asdcResourceId.toUpperCase(Locale.ENGLISH))) { + query.bind("asdcResourceId", asdcResourceId); + } + query.bind("createdCutoff", createdCutoff); serviceTypeObjects = query.list(); @@ -144,14 +193,18 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { InlineResponse200Links navigationLinks = new InlineResponse200Links(); Integer offsetPrev = offset - PAGINATION_PAGE_SIZE; + // TODO: MUST UPDATE THIS LINK NAV CODE + if (offsetPrev >= 0) { - navigationLinks.setPreviousLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "prev", vnfType, offsetPrev)); + navigationLinks.setPreviousLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "prev", typeName, onlyLatest, + onlyActive, vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, offsetPrev)); } Integer offsetNext = offset + PAGINATION_PAGE_SIZE; if (offsetNext < totalCount) { - navigationLinks.setNextLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "next", vnfType, offsetNext)); + navigationLinks.setNextLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "next", typeName, onlyLatest, + onlyActive, vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, offsetNext)); } response.setLinks(navigationLinks); @@ -160,10 +213,10 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { } @Override - public Response dcaeServiceTypesTypeNameGet(String typeName, UriInfo uriInfo, SecurityContext securityContext) + public Response dcaeServiceTypesTypeIdGet(String typeId, UriInfo uriInfo, SecurityContext securityContext) throws NotFoundException { DCAEServiceTypesDAO serviceTypesDAO = InventoryDAOManager.getInstance().getDCAEServiceTypesDAO(); - DCAEServiceTypeObject serviceTypeObject = serviceTypesDAO.getByTypeName(typeName); + DCAEServiceTypeObject serviceTypeObject = serviceTypesDAO.getByTypeId(typeId); if (serviceTypeObject == null) { return Response.status(Response.Status.NOT_FOUND).build(); @@ -172,51 +225,88 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { return Response.ok().entity(createDCAEServiceType(serviceTypeObject, uriInfo)).build(); } + /** + * Create a DCAE service type database object + * + * Utility method that takes a DCAE service type request body and creates a database object representation + * to insert. + * + * @param typeId + * @param request + * @return + */ + private static DCAEServiceTypeObject createDCAEServiceTypeDBO(String typeId, DCAEServiceTypeRequest request) { + DCAEServiceTypeObject serviceTypeObject = new DCAEServiceTypeObject(); + serviceTypeObject.setTypeId(typeId); + serviceTypeObject.setTypeName(request.getTypeName()); + serviceTypeObject.setTypeVersion(request.getTypeVersion()); + serviceTypeObject.setOwner(request.getOwner()); + serviceTypeObject.setBlueprintTemplate(request.getBlueprintTemplate()); + serviceTypeObject.setVnfTypes(request.getVnfTypes()); + serviceTypeObject.setServiceIds(request.getServiceIds()); + serviceTypeObject.setServiceLocations(request.getServiceLocations()); + serviceTypeObject.setAsdcServiceId(request.getAsdcServiceId()); + serviceTypeObject.setAsdcResourceId(request.getAsdcResourceId()); + serviceTypeObject.setCreated(DateTime.now(DateTimeZone.UTC)); + + return serviceTypeObject; + } + @Override - public Response dcaeServiceTypesTypeNamePut(String typeName, DCAEServiceTypeRequest request, UriInfo uriInfo, + public Response dcaeServiceTypesTypeIdPost(DCAEServiceTypeRequest request, UriInfo uriInfo, SecurityContext securityContext) { DCAEServiceTypesDAO serviceTypesDAO = InventoryDAOManager.getInstance().getDCAEServiceTypesDAO(); - DCAEServiceTypeObject serviceTypeObject = serviceTypesDAO.getByTypeName(typeName); + // Must query by the implicit composite key: type name, type version, asdc service id, asdc resource id + // Had to split up the queries into two because in SQL selecting by null has to be `some_field is null` + // + // FIXME: There is a race condition here where there could be multiple records with different ids + // for the same implicit composite key. Maybe the answer is to simply add back in a PUT. + DCAEServiceTypeObject serviceTypeObject + = (request.getAsdcServiceId() == null || request.getAsdcResourceId() == null) + ? serviceTypesDAO.getByRequestFromNotASDC(request) : serviceTypesDAO.getByRequestFromASDC(request); if (serviceTypeObject == null) { - // Create object from request - serviceTypeObject = new DCAEServiceTypeObject(); - serviceTypeObject.setTypeName(typeName); - serviceTypeObject.setOwner(request.getOwner()); - serviceTypeObject.setVnfTypes(request.getVnfTypes()); - serviceTypeObject.setServiceIds(request.getServiceIds()); - serviceTypeObject.setServiceLocations(request.getServiceLocations()); - serviceTypeObject.setBlueprintTemplate(request.getBlueprintTemplate()); - serviceTypeObject.setCreated(DateTime.now(DateTimeZone.UTC)); - - serviceTypesDAO.insertNewerVersion(serviceTypeObject); - + // Generate a new type id + String typeId = UUID.randomUUID().toString(); + serviceTypeObject = createDCAEServiceTypeDBO(typeId, request); + serviceTypesDAO.insert(serviceTypeObject); return Response.ok().entity(createDCAEServiceType(serviceTypeObject, uriInfo)).build(); } + // Service type with same composite key already exists so try to update + + String typeId = serviceTypeObject.getTypeId(); DCAEServicesDAO servicesDAO = InventoryDAOManager.getInstance().getDCAEServicesDAO(); - Integer count = servicesDAO.countByType(DCAEServiceObject.DCAEServiceStatus.RUNNING, typeName); + Integer count = servicesDAO.countByType(DCAEServiceObject.DCAEServiceStatus.RUNNING, typeId); - LOG.info(String.format("Checked num DCAE services running: %s, %d", typeName, count)); + LOG.info(String.format("Checked num DCAE services running: %s, %d", typeId, count)); // Allow the updating of an existing DCAE service type IFF there are no running DCAE services for this type if (count > 0) { - String message = String.format("DCAE services of type %s are still running: %d", typeName, count); + String message = String.format("DCAE services of type %s are still running: %d", typeId, count); ApiResponseMessage entity = new ApiResponseMessage(ApiResponseMessage.ERROR, message); return Response.status(Response.Status.CONFLICT).entity(entity).build(); } else { - serviceTypeObject.setOwner(request.getOwner()); - serviceTypeObject.setVnfTypes(request.getVnfTypes()); - serviceTypeObject.setServiceIds(request.getServiceIds()); - serviceTypeObject.setServiceLocations(request.getServiceLocations()); - serviceTypeObject.setBlueprintTemplate(request.getBlueprintTemplate()); - serviceTypeObject.setCreated(DateTime.now(DateTimeZone.UTC)); + serviceTypeObject = createDCAEServiceTypeDBO(typeId, request); + serviceTypesDAO.update(serviceTypeObject); + return Response.ok().entity(serviceTypeObject).build(); + } + } - InventoryDAOManager.getInstance().getDCAEServiceTypeTransactionDAO().upsert(serviceTypeObject); + @Override + public Response dcaeServiceTypesTypeIdDelete(String typeId, UriInfo uriInfo, SecurityContext securityContext) + throws NotFoundException { + DCAEServiceTypesDAO serviceTypesDAO = InventoryDAOManager.getInstance().getDCAEServiceTypesDAO(); - return Response.ok().entity(serviceTypeObject).build(); + if (serviceTypesDAO.getByTypeId(typeId) == null) { + throw new NotFoundException(1, String.format("DCAE service type not found: %s", typeId)); + } else if (serviceTypesDAO.getByTypeIdActiveOnly(typeId) == null) { + return Response.status(Response.Status.GONE).build(); } + + serviceTypesDAO.deactivateExisting(typeId); + return Response.ok().build(); } } |