diff options
author | Michael Hwang <mhwang@research.att.com> | 2017-05-09 09:52:15 -0400 |
---|---|---|
committer | Vijay Venkatesh Kumar <vv770d@att.com> | 2017-05-10 20:23:54 +0000 |
commit | 03b4fd0a6e6bc0ce88b8904cfa01869b74a2f210 (patch) | |
tree | 8d76a527ed86807658a879df6153c6c6e73ba971 /src/main/java | |
parent | 473bcb6c7dea189d9481f894b82358f6ec93629b (diff) |
[DCAE-16] Merge changes from internal AT&T 1707
Non-functional changes like licensing, pom consolidation, filtering of
keywords.
Functional changes include support for integration with asdc which
caused an API breaking change.
Change-Id: I5a52d968c6403a7d35c4bbf49f7ddf7a5ba4428e
Signed-off-by: Michael Hwang <mhwang@research.att.com>
Diffstat (limited to 'src/main/java')
36 files changed, 917 insertions, 692 deletions
diff --git a/src/main/java/io/swagger/api/Bootstrap.java b/src/main/java/io/swagger/api/Bootstrap.java index 71be330..5050ed3 100644 --- a/src/main/java/io/swagger/api/Bootstrap.java +++ b/src/main/java/io/swagger/api/Bootstrap.java @@ -1,28 +1,25 @@ -package io.swagger.api; - -/* - * ============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; + import io.swagger.jaxrs.config.SwaggerContextService; import io.swagger.models.*; @@ -40,6 +37,8 @@ public class Bootstrap extends HttpServlet { .title("Swagger Server") .description("DCAE Inventory is a web service that provides the following:\n\n1. Real-time data on all DCAE services and their components\n2. Comprehensive details on available DCAE service types\n") .termsOfService("") + .contact(new Contact() + .email("dcae@lists.openecomp.org")) .license(new License() .name("") .url("")); diff --git a/src/main/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactory.java b/src/main/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactory.java index ba583b8..fecd8fe 100644 --- a/src/main/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactory.java +++ b/src/main/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactory.java @@ -1,28 +1,25 @@ -package io.swagger.api.factories; - -/* - * ============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.factories; + import io.swagger.api.DcaeServiceTypesApiService; import io.swagger.api.impl.DcaeServiceTypesApiServiceImpl; diff --git a/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java b/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java index 6bd06a0..47f213d 100644 --- a/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java +++ b/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java @@ -1,28 +1,25 @@ -package io.swagger.api.factories; - -/* - * ============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.factories; + import org.openecomp.dcae.inventory.clients.DCAEControllerClient; import org.openecomp.dcae.inventory.clients.DatabusControllerClient; import io.swagger.api.DcaeServicesApiService; @@ -44,13 +41,6 @@ public class DcaeServicesApiServiceFactory { } public static DcaeServicesApiService getDcaeServicesApi() { - // TODO: Hide these exceptions behind a custom exception. This should be fatal. - if (dcaeControllerClient == null) { - throw new RuntimeException("DCAE controller client not configured"); - } - if (databusControllerClient == null) { - throw new RuntimeException("Databus controller client not configured"); - } return new DcaeServicesApiServiceImpl(dcaeControllerClient, databusControllerClient); } diff --git a/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java b/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java index 43cdcb6..041fbaf 100644 --- a/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java +++ b/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java @@ -1,28 +1,25 @@ -package io.swagger.api.factories; - -/* - * ============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.factories; + import io.swagger.api.DcaeServicesGroupbyApiService; import io.swagger.api.impl.DcaeServicesGroupbyApiServiceImpl; 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(); } } diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java index 1861688..d1e790c 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.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.clients.DCAEControllerClient; import org.openecomp.dcae.inventory.clients.DatabusControllerClient; import org.openecomp.dcae.inventory.daos.DCAEServiceComponentsDAO; @@ -66,7 +63,7 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { DCAEService service = new DCAEService(); service.setServiceId(serviceObject.getServiceId()); service.setSelfLink(DcaeServicesApi.buildLinkForGet(uriInfo, "self", serviceObject.getServiceId())); - service.setTypeLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "type", serviceObject.getTypeName())); + service.setTypeLink(DcaeServiceTypesApi.buildLinkForGet(uriInfo, "type", serviceObject.getTypeId())); service.setCreated(serviceObject.getCreated().toDate()); service.setModified(serviceObject.getModified().toDate()); service.setVnfId(serviceObject.getVnfId()); @@ -89,30 +86,34 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { // TODO: When putting together the components fail. Should this be a 500 case? // For now, this is just logged as a warning. - if (COMPONENT_SOURCE_DCAE_CONTROLLER.equals(sco.getComponentSource().toUpperCase())) { - try { - DCAEControllerClient.ServiceInstance serviceInstance - = this.dcaeControllerClient.getServiceInstance(component.getComponentId()); - component.setStatus(serviceInstance.getStatus()); - // There's no specific location rather its inferred from the tenant - component.setLocation(this.dcaeControllerClient.getLocation(serviceInstance)); - Link componentLink = Link.fromUri(this.dcaeControllerClient.constructResourceURI(sco.getComponentId())) - .rel("component").title(component.getComponentId()).build(); - component.setComponentLink(componentLink); - } catch (DCAEControllerClientException e) { - LOG.warn(String.format("%s, %s", e.getMessage(), sco.toString())); - } - } else if (COMPONENT_SOURCE_DATA_BUS_CONTROLLER.equals(sco.getComponentSource().toUpperCase())) { - try { - if (this.databusControllerClient.isExists(sco.getComponentId())) { - Link componentLink = Link.fromUri(this.databusControllerClient.constructResourceURI(sco.getComponentId())) + if (COMPONENT_SOURCE_DCAE_CONTROLLER.equals(sco.getComponentSource().toUpperCase(Locale.ENGLISH))) { + if (this.dcaeControllerClient != null) { + try { + DCAEControllerClient.ServiceInstance serviceInstance + = this.dcaeControllerClient.getServiceInstance(component.getComponentId()); + component.setStatus(serviceInstance.getStatus()); + // There's no specific location rather its inferred from the AIC tenant + component.setLocation(this.dcaeControllerClient.getLocation(serviceInstance)); + Link componentLink = Link.fromUri(this.dcaeControllerClient.constructResourceURI(sco.getComponentId())) .rel("component").title(component.getComponentId()).build(); component.setComponentLink(componentLink); - } else { - LOG.warn(String.format("Feed/topic does not exist: %s", sco.getComponentId())); + } catch (DCAEControllerClientException e) { + LOG.warn(String.format("%s, %s", e.getMessage(), sco.toString())); + } + } + } else if (COMPONENT_SOURCE_DATA_BUS_CONTROLLER.equals(sco.getComponentSource().toUpperCase(Locale.ENGLISH))) { + if (this.databusControllerClient != null) { + try { + if (this.databusControllerClient.isExists(sco.getComponentId())) { + Link componentLink = Link.fromUri(this.databusControllerClient.constructResourceURI(sco.getComponentId())) + .rel("component").title(component.getComponentId()).build(); + component.setComponentLink(componentLink); + } else { + LOG.warn(String.format("Feed/topic does not exist: %s", sco.getComponentId())); + } + } catch (DatabusControllerClientException e) { + LOG.warn(String.format("%s, %s", e.getMessage(), sco.toString())); } - } catch (DatabusControllerClientException e) { - LOG.warn(String.format("%s, %s", e.getMessage(), sco.toString())); } } else { LOG.warn(String.format("Handling unknown component source: %s", sco.getComponentSource())); @@ -127,7 +128,7 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { } @Override - public Response dcaeServicesGet(String typeName, String vnfId, String vnfType, String vnfLocation, + public Response dcaeServicesGet(String typeId, String vnfId, String vnfType, String vnfLocation, String componentType, Boolean shareable, DateTime created, Integer offset, UriInfo uriInfo, SecurityContext securityContext) { List<DCAEServiceObject> serviceObjects = new ArrayList<>(); @@ -147,8 +148,8 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { List<String> whereClauses = new ArrayList<String>(); - if (typeName != null) { - whereClauses.add("ds.type_name = :typeName"); + if (typeId != null) { + whereClauses.add("ds.type_id = :typeId"); } if (vnfId != null) { @@ -156,7 +157,7 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { } if (vnfType != null) { - whereClauses.add("ds.vnf_type = :vnfType"); + whereClauses.add("lower(ds.vnf_type) = lower(:vnfType)"); } if (vnfLocation != null) { @@ -184,8 +185,8 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { Query<DCAEServiceObject> query = jdbiHandle.createQuery(sb.toString()).map(new DCAEServiceObjectMapper()); - if (typeName != null) { - query.bind("typeName", typeName); + if (typeId != null) { + query.bind("typeId", typeId); } if (vnfId != null) { @@ -240,14 +241,14 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { Integer offsetPrev = offset - PAGINATION_PAGE_SIZE; if (offsetPrev >= 0) { - navigationLinks.setPreviousLink(DcaeServicesApi.buildLinkForGet(uriInfo, "prev", typeName, vnfId, vnfType, + navigationLinks.setPreviousLink(DcaeServicesApi.buildLinkForGet(uriInfo, "prev", typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created, offsetPrev)); } Integer offsetNext = offset + PAGINATION_PAGE_SIZE; if (offsetNext < serviceObjects.size()) { - navigationLinks.setNextLink(DcaeServicesApi.buildLinkForGet(uriInfo, "next", typeName, vnfId, vnfType, + navigationLinks.setNextLink(DcaeServicesApi.buildLinkForGet(uriInfo, "next", typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created, offsetNext)); } @@ -278,8 +279,8 @@ public class DcaeServicesApiServiceImpl extends DcaeServicesApiService { public Response dcaeServicesServiceIdPut(String serviceId, DCAEServiceRequest request, UriInfo uriInfo, SecurityContext securityContext) { // Check to make sure that the DCAE service type exists - if (InventoryDAOManager.getInstance().getDCAEServiceTypesDAO().getByTypeName(request.getTypeName()) == null) { - String errorMessage = String.format("DCAE service type does not exist: %s", request.getTypeName()); + if (InventoryDAOManager.getInstance().getDCAEServiceTypesDAO().getByTypeIdActiveOnly(request.getTypeId()) == null) { + String errorMessage = String.format("DCAE service type does not exist: %s", request.getTypeId()); ApiResponseMessage message = new ApiResponseMessage(ApiResponseMessage.ERROR, errorMessage); return Response.status(422).entity(message).build(); } diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java index 10bd7fa..09d3143 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.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.InventoryDAOManager; import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; import io.swagger.api.*; @@ -46,7 +43,7 @@ public class DcaeServicesGroupbyApiServiceImpl extends DcaeServicesGroupbyApiSer switch (propertyName) { case "type": - columnName = "type_name"; + columnName = "type_id"; break; case "vnfLocation": columnName = "vnf_location"; @@ -83,7 +80,7 @@ public class DcaeServicesGroupbyApiServiceImpl extends DcaeServicesGroupbyApiSer switch (propertyName) { case "type": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByTypeName(uriInfo, "dcae_services", + value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByTypeId(uriInfo, "dcae_services", propertyValue)); break; case "vnfLocation": diff --git a/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java b/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java index 9a16331..b64a917 100644 --- a/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java +++ b/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java @@ -1,39 +1,35 @@ -package org.openecomp.dcae.inventory; - -/* - * ============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 org.openecomp.dcae.inventory; + import org.openecomp.dcae.inventory.clients.DCAEControllerClient; import org.openecomp.dcae.inventory.clients.DatabusControllerClient; import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.dbthings.StringListArgument; +import org.openecomp.dcae.inventory.exceptions.mappers.DBIExceptionMapper; import org.openecomp.dcae.inventory.exceptions.mappers.DCAEControllerConnectionExceptionMapper; import org.openecomp.dcae.inventory.exceptions.mappers.DCAEControllerTimeoutExceptionMapper; import org.openecomp.dcae.inventory.providers.NotFoundExceptionMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import io.dropwizard.Application; import io.dropwizard.client.JerseyClientBuilder; -import io.dropwizard.jdbi.DBIFactory; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import io.swagger.api.DcaeServiceTypesApi; @@ -47,7 +43,6 @@ import io.swagger.models.Contact; import io.swagger.models.Info; import org.eclipse.jetty.servlets.CrossOriginFilter; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.skife.jdbi.v2.DBI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,6 +51,7 @@ import javax.servlet.FilterRegistration; import javax.ws.rs.client.Client; import javax.ws.rs.core.Link; import java.util.EnumSet; +import java.util.Locale; /** @@ -66,6 +62,10 @@ public class InventoryApplication extends Application<InventoryConfiguration> { static final Logger LOG = LoggerFactory.getLogger(InventoryApplication.class); public static void main(String[] args) throws Exception { + // This is here to try to fix a "high" issue caught by Fortify. Did this **plus** setting locale for each of the + // string comparisons that use `toUpper` because of this StackOverflow post: + // http://stackoverflow.com/questions/38308777/fixed-fortify-scan-locale-changes-are-reappearing + Locale.setDefault(Locale.ENGLISH); new InventoryApplication().run(args); } @@ -79,7 +79,8 @@ public class InventoryApplication extends Application<InventoryConfiguration> { // This Info object was lifted from the Swagger generated io.swagger.api.Bootstrap file. Although it was not generated // correctly. Info info = new Info().title("DCAE Inventory API").version("0.8.0") - .description("DCAE Inventory is a web service that provides the following:\n\n1. Real-time data on all DCAE services and their components\n2. Comprehensive details on available DCAE service types\n"); + .description("DCAE Inventory is a web service that provides the following:\n\n1. Real-time data on all DCAE services and their components\n2. Comprehensive details on available DCAE service types\n") + .contact(new Contact().email("dcae@lists.openecomp.org")); // Swagger/servlet/jax-rs magic! BeanConfig beanConfig = new BeanConfig(); beanConfig.setInfo(info); @@ -91,10 +92,8 @@ public class InventoryApplication extends Application<InventoryConfiguration> { public void run(InventoryConfiguration configuration, Environment environment) { LOG.info("Starting DCAE inventory application"); LOG.info(String.format("DB driver properties: %s", configuration.getDataSourceFactory().getProperties().toString())); - final DBIFactory factory = new DBIFactory(); - final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "dcae-database"); - jdbi.registerArgumentFactory(new StringListArgument()); - InventoryDAOManager.getInstance().init(jdbi); + InventoryDAOManager.getInstance().setup(environment, configuration); + InventoryDAOManager.getInstance().initialize(); // Add filter for CORS support for DCAE dashboard // http://jitterted.com/tidbits/2014/09/12/cors-for-dropwizard-0-7-x/ @@ -113,24 +112,42 @@ public class InventoryApplication extends Application<InventoryConfiguration> { // Setup DCAE controller client // Used by the dcae-services API - final Client clientDCAEController = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()) - .build("DCAEControllerClient"); - HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().build(); - clientDCAEController.register(feature); - final DCAEControllerClient dcaeControllerClient = new DCAEControllerClient(clientDCAEController, configuration.getDcaeControllerConnection()); - DcaeServicesApiServiceFactory.setDcaeControllerClient(dcaeControllerClient); + if (configuration.getDcaeControllerConnection().getRequired()) { + final Client clientDCAEController = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()) + .build("DCAEControllerClient"); + HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().build(); + clientDCAEController.register(feature); + final DCAEControllerClient dcaeControllerClient = new DCAEControllerClient(clientDCAEController, configuration.getDcaeControllerConnection()); + DcaeServicesApiServiceFactory.setDcaeControllerClient(dcaeControllerClient); + + LOG.info("Use of DCAE controller client is required. Turned on."); + } else { + LOG.warn("Use of DCAE controller client is *not* required. Turned off."); + } // Setup Databus controller client // Used by the dcae-services API - final Client clientDatabusController = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()) - .build("DatabusControllerClient"); - final DatabusControllerClient databusControllerClient = new DatabusControllerClient(clientDatabusController, - configuration.getDatabusControllerConnection()); - DcaeServicesApiServiceFactory.setDatabusControllerClient(databusControllerClient); + if (configuration.getDatabusControllerConnection().getRequired()) { + final Client clientDatabusController = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()) + .build("DatabusControllerClient"); + clientDatabusController.register(HttpAuthenticationFeature.basicBuilder().credentials( + configuration.getDatabusControllerConnection().getMechId(), + configuration.getDatabusControllerConnection().getPassword()).build()); + final DatabusControllerClient databusControllerClient = new DatabusControllerClient(clientDatabusController, + configuration.getDatabusControllerConnection()); + DcaeServicesApiServiceFactory.setDatabusControllerClient(databusControllerClient); + + LOG.info("Use of databus controller client is required. Turned on."); + } else { + LOG.warn("Use of databus controller client is *not* required. Turned off."); + } environment.jersey().register(NotFoundExceptionMapper.class); environment.jersey().register(DCAEControllerConnectionExceptionMapper.class); environment.jersey().register(DCAEControllerTimeoutExceptionMapper.class); + environment.jersey().register(DBIExceptionMapper.UnableToObtainConnectionExceptionMapper.class); + environment.jersey().register(DBIExceptionMapper.UnableToExecuteStatementExceptionMapper.class); + environment.jersey().register(DBIExceptionMapper.UnableToCreateStatementExceptionMapper.class); environment.jersey().register(new DcaeServicesApi()); environment.jersey().register(new DcaeServiceTypesApi()); diff --git a/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java b/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java index 54370ee..55bcca2 100644 --- a/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java +++ b/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory; - -/* - * ============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 org.openecomp.dcae.inventory; + import com.fasterxml.jackson.annotation.JsonProperty; import io.dropwizard.Configuration; import io.dropwizard.client.JerseyClientConfiguration; @@ -58,6 +55,9 @@ public class InventoryConfiguration extends Configuration { @JsonProperty private String password; + @JsonProperty + private Boolean required = true; + public String getHost() { return this.host; } @@ -77,6 +77,10 @@ public class InventoryConfiguration extends Configuration { public String getPassword() { return this.password; } + + public Boolean getRequired() { + return this.required; + } } public static class DatabusControllerConnectionConfiguration { @@ -88,6 +92,17 @@ public class InventoryConfiguration extends Configuration { @JsonProperty private Integer port; + @NotNull + @JsonProperty + private String mechId; + + @NotNull + @JsonProperty + private String password; + + @JsonProperty + private Boolean required = true; + public String getHost() { return host; } @@ -95,6 +110,18 @@ public class InventoryConfiguration extends Configuration { public Integer getPort() { return port; } + + public String getMechId() { + return mechId; + } + + public String getPassword() { + return password; + } + + public Boolean getRequired() { + return this.required; + } } @NotEmpty diff --git a/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java b/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java index 8cf07db..b83697f 100644 --- a/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java +++ b/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory; - -/* - * ============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 org.openecomp.dcae.inventory; + import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; diff --git a/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java b/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java index 23e05fd..adbe8b7 100644 --- a/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java +++ b/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.clients; - -/* - * ============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 org.openecomp.dcae.inventory.clients; + import org.openecomp.dcae.inventory.InventoryConfiguration; import org.openecomp.dcae.inventory.exceptions.DCAEControllerClientException; import org.openecomp.dcae.inventory.exceptions.DCAEControllerConnectionException; @@ -171,7 +168,7 @@ public class DCAEControllerClient { // Apparently the exceptions are wrapped which is not ideal because many different types of errors are embedded // in single exception. TODO: May want to come back to split up the errors. // Example: - // javax.ws.rs.ProcessingException: org.apache.http.conn.ConnectTimeoutException: Connect to aaa.bbb.ccc:9998 + // javax.ws.rs.ProcessingException: org.apache.http.conn.ConnectTimeoutException: Connect to <dcae controller domain name>:9998 String message = "Connecting with DCAE controller probably timed out"; LOG.error(message, e); String exceptionMessage = String.format("%s: %s", message, e.getMessage()); diff --git a/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java b/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java index ac3bbf1..9df11d5 100644 --- a/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java +++ b/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.clients; - -/* - * ============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 org.openecomp.dcae.inventory.clients; + import org.openecomp.dcae.inventory.InventoryConfiguration; import org.openecomp.dcae.inventory.exceptions.DatabusControllerClientException; import com.fasterxml.jackson.core.type.TypeReference; @@ -51,7 +48,7 @@ public class DatabusControllerClient { resourcePath = (new StringBuilder("/")).append(resourcePath).toString(); } - return UriBuilder.fromPath(resourcePath.toString()).scheme("http").host(this.connectionConfiguration.getHost()) + return UriBuilder.fromPath(resourcePath.toString()).scheme("https").host(this.connectionConfiguration.getHost()) .port(this.connectionConfiguration.getPort()).build(); } @@ -74,6 +71,11 @@ public class DatabusControllerClient { } catch (IOException e) { throw new DatabusControllerClientException(e); } + } else if (response.getStatus() == 401) { + // You probably got this because your mech id/password is not authorized + throw new DatabusControllerClientException(String.format("Check the mech id/password: %d", response.getStatus())); + } else if (response.getStatus() == 403) { + throw new DatabusControllerClientException(String.format("Credentials not authorized: %d", response.getStatus())); } throw new DatabusControllerClientException(String.format("Unexpected error from databus controller: %d", diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java index d1b5ec0..9a674c9 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceComponentObjectMapper; import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; import org.skife.jdbi.v2.sqlobject.Bind; diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java index dc3d6b3..f0fca86 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; import org.joda.time.DateTime; diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypeTransactionDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypeTransactionDAO.java deleted file mode 100644 index c3c65e2..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypeTransactionDAO.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============LICENSE_START========================================== - * =================================================================== - * 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 - * - * 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. - * - */ - -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; -import org.skife.jdbi.v2.sqlobject.CreateSqlObject; - -/** - * Created by mhwang on 5/19/16. - */ -public abstract class DCAEServiceTypeTransactionDAO { - - @CreateSqlObject - abstract DCAEServiceTypesDAO getServiceTypesDAO(); - - public void upsert(DCAEServiceTypeObject newerVersion) { - // Order of operation is important here! - this.getServiceTypesDAO().deactivateExisting(newerVersion.getTypeName()); - this.getServiceTypesDAO().insertNewerVersion(newerVersion); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java index 5c961f9..50b79b2 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java @@ -1,30 +1,29 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper; + import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; +import io.swagger.model.DCAEServiceTypeRequest; import org.skife.jdbi.v2.sqlobject.Bind; import org.skife.jdbi.v2.sqlobject.BindBean; import org.skife.jdbi.v2.sqlobject.SqlQuery; @@ -32,10 +31,9 @@ import org.skife.jdbi.v2.sqlobject.SqlUpdate; import org.skife.jdbi.v2.sqlobject.customizers.Mapper; /** - * DCAE service type records are treated immutable. Newer versions are added with the flag "is_active" set to TRUE. - * The responsibility of managing the active version on the caller of this DAO. - * - * Currently the "is_active" flag is hidden to the application except in this DAO. + * DCAE service type records are no longer treated immutable because versioning is handled by clients + * to DCAE inventory i.e. ASDC. There is field "deactivated" that determines if a service type is active. + * Inserts and updates automatically activates a service type while deleting deactivates a service type. * * Created by mhwang on 4/19/16. */ @@ -48,21 +46,45 @@ public interface DCAEServiceTypesDAO extends InventoryDAO { * Note that service_ids and service_locations are nullable fields. This might not be the right decision but because * the resource model allows for nulls, thought it should consistent. */ - @SqlUpdate("create table dcae_service_types (type_name varchar not null, owner varchar not null," + - "vnf_types varchar[] not null, service_ids varchar[], service_locations varchar[], blueprint_template text not null, " + - "created timestamp not null, is_active boolean not null, constraint pk_type_created primary key (type_name, created))") + @SqlUpdate("create table dcae_service_types (type_id varchar not null, type_version integer not null, " + + "type_name varchar not null, owner varchar not null, blueprint_template text not null, " + + "vnf_types varchar[] not null, service_ids varchar[], service_locations varchar[], " + + "asdc_service_id varchar, asdc_resource_id varchar, " + + "created timestamp not null, deactivated timestamp, constraint pk_type_created primary key (type_id))") void createTable(); - @SqlUpdate("insert into dcae_service_types(type_name, owner, vnf_types, service_ids, service_locations, " + - "blueprint_template, created, is_active) values (:typeName, :owner, :vnfTypes, :serviceIds, :serviceLocations, " + - ":blueprintTemplate, :created, TRUE)") - void insertNewerVersion(@BindBean DCAEServiceTypeObject serviceObject); + // REVIEW: asdcServiceId and asdcResourceId is implicitly part of the unique key and thus shouldn't be updated. + @SqlUpdate("insert into dcae_service_types(type_id, type_version, type_name, owner, blueprint_template, vnf_types, " + + "service_ids, service_locations, asdc_service_id, asdc_resource_id, created, deactivated) " + + "values (:typeId, :typeVersion, :typeName, :owner, :blueprintTemplate, :vnfTypes, :serviceIds, " + + ":serviceLocations, :asdcServiceId, :asdcResourceId, :created, null)") + void insert(@BindBean DCAEServiceTypeObject serviceObject); + + @SqlUpdate("update dcae_service_types set " + + "owner = :owner, blueprint_template = :blueprintTemplate, vnf_types = :vnfTypes, " + + "service_ids = :serviceIds, service_locations = :serviceLocations, created = :created, " + + "deactivated = null where type_id = :typeId") + void update(@BindBean DCAEServiceTypeObject serviceObject); + + @SqlUpdate("update dcae_service_types set deactivated = (now() at time zone 'utc') where type_id = :typeId") + void deactivateExisting(@Bind("typeId") String typeId); + + @Mapper(DCAEServiceTypeObjectMapper.class) + @SqlQuery("select * from dcae_service_types where type_id = :it") + DCAEServiceTypeObject getByTypeId(@Bind String typeId); + + @Mapper(DCAEServiceTypeObjectMapper.class) + @SqlQuery("select * from dcae_service_types where deactivated is null and type_id = :it") + DCAEServiceTypeObject getByTypeIdActiveOnly(@Bind String typeId); - @SqlUpdate("update dcae_service_types set is_active = FALSE where type_name = :typeName and is_active = TRUE") - void deactivateExisting(@Bind("typeName") String typeName); + @Mapper(DCAEServiceTypeObjectMapper.class) + @SqlQuery("select * from dcae_service_types where type_name = :typeName and type_version = :typeVersion " + + "and asdc_service_id is null and asdc_resource_id is null") + DCAEServiceTypeObject getByRequestFromNotASDC(@BindBean DCAEServiceTypeRequest serviceTypeObject); @Mapper(DCAEServiceTypeObjectMapper.class) - @SqlQuery("select * from dcae_service_types where is_active = TRUE and type_name = :it") - DCAEServiceTypeObject getByTypeName(@Bind String typeName); + @SqlQuery("select * from dcae_service_types where type_name = :typeName and type_version = :typeVersion " + + "and asdc_service_id = :asdcServiceId and asdc_resource_id = :asdcResourceId") + DCAEServiceTypeObject getByRequestFromASDC(@BindBean DCAEServiceTypeRequest serviceTypeObject); } diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java index 405476d..5bfd3cd 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + import org.joda.time.DateTime; import org.skife.jdbi.v2.sqlobject.Bind; import org.skife.jdbi.v2.sqlobject.SqlQuery; diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java index e40b8b5..9200677 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper; import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; import org.joda.time.DateTime; @@ -42,17 +39,17 @@ public interface DCAEServicesDAO extends InventoryDAO { @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_services\')") Boolean checkIfTableExists(); - @SqlUpdate("create table dcae_services (service_id varchar not null primary key, type_name varchar not null, " + + @SqlUpdate("create table dcae_services (service_id varchar not null primary key, type_id varchar not null, " + "vnf_id varchar not null, vnf_type varchar not null, vnf_location varchar not null, deployment_ref varchar, " + "created timestamp not null, modified timestamp not null, status varchar not null)") void createTable(); - @SqlUpdate("insert into dcae_services(service_id, type_name, vnf_id, vnf_type, vnf_location, deployment_ref, " + - "created, modified, status) values (:serviceId, :typeName, :vnfId, :vnfType, :vnfLocation, :deploymentRef, " + + @SqlUpdate("insert into dcae_services(service_id, type_id, vnf_id, vnf_type, vnf_location, deployment_ref, " + + "created, modified, status) values (:serviceId, :typeId, :vnfId, :vnfType, :vnfLocation, :deploymentRef, " + ":created, :modified, :status)") void insert(@BindBean DCAEServiceObject serviceObject); - @SqlUpdate("update dcae_services set type_name = :typeName, vnf_id = :vnfId, vnf_type = :vnfType, " + + @SqlUpdate("update dcae_services set type_id = :typeId, vnf_id = :vnfId, vnf_type = :vnfType, " + "vnf_location = :vnfLocation, deployment_ref = :deploymentRef, modified = :modified, status = :status " + "where service_id = :serviceId") void update(@BindBean DCAEServiceObject serviceObject); @@ -71,7 +68,9 @@ public interface DCAEServicesDAO extends InventoryDAO { @Bind("serviceId") String serviceId); @Mapper(IntegerMapper.class) - @SqlQuery("select count(1) from dcae_services where status = :status and type_name = :typeName") - Integer countByType(@Bind("status") DCAEServiceObject.DCAEServiceStatus status, @Bind("typeName") String typeName); + @SqlQuery("select count(1) from dcae_services where status = :status and type_id = :typeId") + Integer countByType(@Bind("status") DCAEServiceObject.DCAEServiceStatus status, @Bind("typeId") String typeId); } + + diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java index 6205d2c..ef2fb8d 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + /** * Created by mhwang on 4/19/16. */ diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java index fd4d2eb..77611b5 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java @@ -1,30 +1,32 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============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 org.openecomp.dcae.inventory.daos; + +import org.openecomp.dcae.inventory.InventoryConfiguration; +import org.openecomp.dcae.inventory.dbthings.StringListArgument; +import io.dropwizard.jdbi.DBIFactory; +import io.dropwizard.setup.Environment; import org.skife.jdbi.v2.DBI; import org.skife.jdbi.v2.Handle; +import org.skife.jdbi.v2.util.BooleanMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,18 +52,54 @@ public final class InventoryDAOManager { return instance; } + public static class InventoryDAOManagerSetupException extends RuntimeException { + + public InventoryDAOManagerSetupException(String message) { + super(message); + } + + } + private final static Logger LOG = LoggerFactory.getLogger(InventoryDAOManager.class); // WATCH! Table creation order matters where mapping tables refer to other tables for foreign keys. private final static List<Class> DAO_CLASSES = Arrays.asList(DCAEServiceTypesDAO.class, DCAEServicesDAO.class, DCAEServiceComponentsDAO.class, DCAEServicesComponentsMapsDAO.class); private DBI jdbi; + private Environment environment; + private InventoryConfiguration configuration; private InventoryDAOManager() { } - public void init(DBI jdbi) { - this.jdbi = jdbi; + /** + * Setup the manager + * + * Saving the Dropwizard environment and configuration which are used to construct the DBI object in a later + * initialize call. This method can only be called once to be safe and to avoid runtime problems that could be + * caused if the global instance of this class gets into a weird state (Couldn't use Java's `final` qualifier). + * + * @param environment + * @param inventoryConfiguration + */ + public void setup(Environment environment, InventoryConfiguration inventoryConfiguration) { + if (this.environment == null && this.configuration == null) { + this.environment = environment; + this.configuration = inventoryConfiguration; + } else { + throw new InventoryDAOManagerSetupException("InventoryDAOManager setup can only be called once."); + } + } + + /** + * Initialize the manager + * + * Create the underlying validated DBI object that is used to manage database connections + */ + public void initialize() { + final DBIFactory factory = new DBIFactory(); + final DBI jdbi = factory.build(this.environment, this.configuration.getDataSourceFactory(), "dcae-database"); + jdbi.registerArgumentFactory(new StringListArgument()); for (Class<? extends InventoryDAO> daoClass : DAO_CLASSES) { final InventoryDAO dao = jdbi.onDemand(daoClass); @@ -73,6 +111,31 @@ public final class InventoryDAOManager { LOG.info(String.format("Sql table created: %s", daoClass.getSimpleName())); } } + + // CREATE VIEWS + // TODO: This doesn't belong here and is not consistent with the above approach. Make it better. + try (Handle jdbiHandle = jdbi.open()) { + String viewName = "dcae_service_types_latest"; + String checkQuery = String.format("select exists (select * from information_schema.tables where table_name = '%s')", + viewName); + + if (jdbiHandle.createQuery(checkQuery).map(BooleanMapper.FIRST).first()) { + LOG.info(String.format("Sql view exists: %s", viewName)); + } else { + StringBuilder sb = new StringBuilder(String.format("create view %s as ", viewName)); + sb.append("select s.* from dcae_service_types s "); + sb.append("join (select type_name, max(type_version) as max_version from dcae_service_types group by type_name) as f "); + sb.append("on s.type_name = f.type_name and s.type_version = f.max_version"); + + jdbiHandle.execute(sb.toString()); + LOG.info(String.format("Sql view created: %s", viewName)); + } + } catch (Exception e) { + throw new RuntimeException("", e); + } + + // Do this assignment at the end after performing table checks to ensure that connection is good + this.jdbi = jdbi; } private InventoryDAO getDAO(Class<? extends InventoryDAO> klass) { @@ -102,10 +165,6 @@ public final class InventoryDAOManager { return jdbi.onDemand(DCAEServiceTransactionDAO.class); } - public DCAEServiceTypeTransactionDAO getDCAEServiceTypeTransactionDAO() { - return jdbi.onDemand(DCAEServiceTypeTransactionDAO.class); - } - public DCAEServiceTypesDAO getDCAEServiceTypesDAO() { return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class); } diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java index 11c77fd..afd0e78 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings; + import org.skife.jdbi.v2.StatementContext; import org.skife.jdbi.v2.tweak.Argument; import org.skife.jdbi.v2.tweak.ArgumentFactory; diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java index ee0602f..23b42b1 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings.mappers; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings.mappers; + import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; import org.joda.time.DateTime; import org.skife.jdbi.v2.StatementContext; diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java index 7cff043..27cb22f 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings.mappers; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings.mappers; + import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; import org.joda.time.DateTime; import org.skife.jdbi.v2.StatementContext; @@ -40,7 +37,7 @@ public class DCAEServiceObjectMapper implements ResultSetMapper<DCAEServiceObjec public DCAEServiceObject map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { DCAEServiceObject object = new DCAEServiceObject(); object.setServiceId(resultSet.getString("service_id")); - object.setTypeName(resultSet.getString("type_name")); + object.setTypeId(resultSet.getString("type_id")); object.setVnfId(resultSet.getString("vnf_id")); object.setVnfType(resultSet.getString("vnf_type")); object.setVnfLocation(resultSet.getString("vnf_location")); diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java index bbd9903..dc64423 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings.mappers; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings.mappers; + import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; import org.joda.time.DateTime; import org.skife.jdbi.v2.StatementContext; @@ -31,6 +28,7 @@ import org.skife.jdbi.v2.tweak.ResultSetMapper; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.Arrays; /** @@ -41,7 +39,9 @@ public class DCAEServiceTypeObjectMapper implements ResultSetMapper<DCAEServiceT @Override public DCAEServiceTypeObject map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { DCAEServiceTypeObject object = new DCAEServiceTypeObject(); + object.setTypeId(resultSet.getString("type_id")); object.setTypeName(resultSet.getString("type_name")); + object.setTypeVersion(resultSet.getInt("type_version")); object.setOwner(resultSet.getString("owner")); String[] vnfTypes = (String[]) resultSet.getArray("vnf_types").getArray(); object.setVnfTypes(Arrays.asList(vnfTypes)); @@ -61,7 +61,12 @@ public class DCAEServiceTypeObjectMapper implements ResultSetMapper<DCAEServiceT } object.setBlueprintTemplate(resultSet.getString("blueprint_template")); + object.setAsdcServiceId(resultSet.getString("asdc_service_id")); + object.setAsdcResourceId(resultSet.getString("asdc_resource_id")); object.setCreated(new DateTime(resultSet.getTimestamp("created"))); + + Timestamp deactivated = resultSet.getTimestamp("deactivated"); + object.setDeactivated(deactivated == null ? null : new DateTime(deactivated)); return object; } diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java index f7b783e..0ec9c19 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings.models; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings.models; + import io.swagger.model.DCAEServiceComponentRequest; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObject.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObject.java index 5defd46..4374acd 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObject.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObject.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings.models; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings.models; + import io.swagger.model.DCAEServiceRequest; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -38,7 +35,7 @@ public class DCAEServiceObject { } private String serviceId = null; - private String typeName = null; + private String typeId = null; private DateTime created = null; private DateTime modified = null; private String vnfId = null; @@ -57,12 +54,12 @@ public class DCAEServiceObject { this.serviceId = serviceId; } - public String getTypeName() { - return typeName; + public String getTypeId() { + return typeId; } - public void setTypeName(String typeName) { - this.typeName = typeName; + public void setTypeId(String typeId) { + this.typeId = typeId; } public DateTime getCreated() { @@ -135,7 +132,7 @@ public class DCAEServiceObject { public DCAEServiceObject(String serviceId, DCAEServiceRequest request) { DateTime now = DateTime.now(DateTimeZone.UTC); this.setServiceId(serviceId); - this.setTypeName(request.getTypeName()); + this.setTypeId(request.getTypeId()); this.setVnfId(request.getVnfId()); this.setVnfType(request.getVnfType()); this.setVnfLocation(request.getVnfLocation()); @@ -158,7 +155,7 @@ public class DCAEServiceObject { this.setCreated(source.getCreated()); // Mutable fields - this.setTypeName(updateRequest.getTypeName()); + this.setTypeId(updateRequest.getTypeId()); this.setVnfId(updateRequest.getVnfId()); this.setVnfType(updateRequest.getVnfType()); this.setVnfLocation(updateRequest.getVnfLocation()); diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java index 1213207..b57a28f 100644 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java +++ b/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.dbthings.models; - -/* - * ============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 org.openecomp.dcae.inventory.dbthings.models; + import org.joda.time.DateTime; import java.util.List; @@ -34,13 +31,26 @@ import java.util.List; */ public class DCAEServiceTypeObject { + private String typeId = null; private String typeName = null; + private Integer typeVersion = null; + private String blueprintTemplate = null; private String owner = null; private List<String> vnfTypes = null; private List<String> serviceIds = null; private List<String> serviceLocations = null; - private String blueprintTemplate = null; + private String asdcServiceId = null; + private String asdcResourceId = null; private DateTime created = null; + private DateTime deactivated = null; + + public String getTypeId() { + return typeId; + } + + public void setTypeId(String typeId) { + this.typeId = typeId; + } public String getTypeName() { return typeName; @@ -50,6 +60,22 @@ public class DCAEServiceTypeObject { this.typeName = typeName; } + public Integer getTypeVersion() { + return typeVersion; + } + + public void setTypeVersion(Integer typeVersion) { + this.typeVersion = typeVersion; + } + + public String getBlueprintTemplate() { + return blueprintTemplate; + } + + public void setBlueprintTemplate(String blueprintTemplate) { + this.blueprintTemplate = blueprintTemplate; + } + public String getOwner() { return owner; } @@ -82,12 +108,20 @@ public class DCAEServiceTypeObject { this.serviceLocations = serviceLocations; } - public String getBlueprintTemplate() { - return blueprintTemplate; + public String getAsdcServiceId() { + return asdcServiceId; } - public void setBlueprintTemplate(String blueprintTemplate) { - this.blueprintTemplate = blueprintTemplate; + public void setAsdcServiceId(String asdcServiceId) { + this.asdcServiceId = asdcServiceId; + } + + public String getAsdcResourceId() { + return asdcResourceId; + } + + public void setAsdcResourceId(String asdcResourceId) { + this.asdcResourceId = asdcResourceId; } public DateTime getCreated() { @@ -98,4 +132,11 @@ public class DCAEServiceTypeObject { this.created = created; } + public DateTime getDeactivated() { + return deactivated; + } + + public void setDeactivated(DateTime deactivated) { + this.deactivated = deactivated; + } } diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerClientException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerClientException.java index 5fde36b..eb30ae7 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerClientException.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerClientException.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions; + /** * Created by mhwang on 5/13/16. */ diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java index b901915..2b9e6e7 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions; + /** * Created by mhwang on 8/23/16. */ diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java index 5d76de6..8aa9255 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions; + /** * Created by mhwang on 8/23/16. */ diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java index dbc3e3b..6209521 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions; + /** * Created by mhwang on 5/25/16. */ diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java new file mode 100644 index 0000000..5dce2dd --- /dev/null +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java @@ -0,0 +1,82 @@ +/*- + * ============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 + * + * 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========================================================= + */ + +package org.openecomp.dcae.inventory.exceptions.mappers; + +import org.openecomp.dcae.inventory.daos.InventoryDAOManager; +import io.swagger.api.ApiResponseMessage; +import org.skife.jdbi.v2.exceptions.DBIException; +import org.skife.jdbi.v2.exceptions.UnableToCreateStatementException; +import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException; +import org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/** + * Created by mhwang on 3/6/17. + * + * Responsible for handling DBIExceptions for Inventory which are raised by database client calls + * http://jdbi.org/apidocs/org/skife/jdbi/v2/exceptions/DBIException.html + * + * This class is intended to be extended to match on specific exceptions that are derived from DBIException + */ +public class DBIExceptionMapper<T extends DBIException> implements ExceptionMapper<T> { + + private final static Logger LOG = LoggerFactory.getLogger(DBIExceptionMapper.class); + + /** + * Upon a DBIException, this handler will attempt to re-initialize the Inventory's database connection + * and craft a specific message telling the client what to do. + * + * @param exception Derived class of DBIException + * @return Returns a Response with a status code of 502 with an ApiResponseMessage object + */ + @Override + public Response toResponse(T exception) { + LOG.error("", exception); + StringBuilder clientMessage = new StringBuilder("There is a database issue."); + + try { + InventoryDAOManager.getInstance().initialize(); + clientMessage.append(" Connection has been successfully reset. Please try again."); + } catch(Exception e) { + LOG.error(String.format("Failed to re-initialize database connection: %s", e.getMessage())); + clientMessage.append(" Connection reset attempt has failed. Please try again soon."); + } + + ApiResponseMessage response = new ApiResponseMessage(ApiResponseMessage.ERROR, clientMessage.toString()); + return Response.status(Response.Status.BAD_GATEWAY).entity(response).build(); + } + + // Here are the handlers for specific derived DBIException + + public final static class UnableToObtainConnectionExceptionMapper extends DBIExceptionMapper<UnableToObtainConnectionException> { + } + + public final static class UnableToCreateStatementExceptionMapper extends DBIExceptionMapper<UnableToCreateStatementException> { + } + + public final static class UnableToExecuteStatementExceptionMapper extends DBIExceptionMapper<UnableToExecuteStatementException> { + } + +} diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java index f5efd77..76936e0 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions.mappers; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions.mappers; + import org.openecomp.dcae.inventory.exceptions.DCAEControllerConnectionException; import javax.ws.rs.core.Response; diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java index 5647b75..b9ffda0 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions.mappers; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions.mappers; + import io.swagger.api.ApiResponseMessage; import javax.ws.rs.core.Response; diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java index 2b850ff..9939aee 100644 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.exceptions.mappers; - -/* - * ============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 org.openecomp.dcae.inventory.exceptions.mappers; + import org.openecomp.dcae.inventory.exceptions.DCAEControllerTimeoutException; import javax.ws.rs.core.Response; diff --git a/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java b/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java index ce5a55a..0f54b46 100644 --- a/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java +++ b/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java @@ -1,28 +1,25 @@ -package org.openecomp.dcae.inventory.providers; - -/* - * ============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 org.openecomp.dcae.inventory.providers; + import io.swagger.api.ApiResponseMessage; import io.swagger.api.NotFoundException; |