From f758bb0ae798a53eee005b3e6e4a115f84b4ae19 Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Fri, 19 Jan 2018 10:43:43 -0500 Subject: Change package from openecomp to onap Change-Id: I2a79eb123e5fbb6a6c2f0e72030d6781fbbd7513 Issue-ID: DCAEGEN2-225 Signed-off-by: Michael Hwang --- pom.xml | 2 +- src/main/java/io/swagger/api/Bootstrap.java | 2 +- .../factories/DcaeServicesApiServiceFactory.java | 4 +- .../api/impl/DcaeServiceTypesApiServiceImpl.java | 12 +- .../api/impl/DcaeServicesApiServiceImpl.java | 22 +- .../impl/DcaeServicesGroupbyApiServiceImpl.java | 4 +- .../onap/dcae/inventory/InventoryApplication.java | 195 ++++++++++++++++++ .../dcae/inventory/InventoryConfiguration.java | 173 ++++++++++++++++ .../org/onap/dcae/inventory/LinkSerializer.java | 52 +++++ .../inventory/clients/DCAEControllerClient.java | 222 +++++++++++++++++++++ .../inventory/clients/DatabusControllerClient.java | 91 +++++++++ .../inventory/daos/DCAEServiceComponentsDAO.java | 62 ++++++ .../inventory/daos/DCAEServiceTransactionDAO.java | 158 +++++++++++++++ .../dcae/inventory/daos/DCAEServiceTypesDAO.java | 90 +++++++++ .../daos/DCAEServicesComponentsMapsDAO.java | 50 +++++ .../onap/dcae/inventory/daos/DCAEServicesDAO.java | 76 +++++++ .../org/onap/dcae/inventory/daos/InventoryDAO.java | 29 +++ .../dcae/inventory/daos/InventoryDAOManager.java | 181 +++++++++++++++++ .../inventory/dbthings/StringListArgument.java | 57 ++++++ .../mappers/DCAEServiceComponentObjectMapper.java | 48 +++++ .../dbthings/mappers/DCAEServiceObjectMapper.java | 51 +++++ .../mappers/DCAEServiceTypeObjectMapper.java | 73 +++++++ .../models/DCAEServiceComponentObject.java | 146 ++++++++++++++ .../dbthings/models/DCAEServiceObject.java | 168 ++++++++++++++++ .../dbthings/models/DCAEServiceTypeObject.java | 142 +++++++++++++ .../exceptions/DCAEControllerClientException.java | 36 ++++ .../DCAEControllerConnectionException.java | 36 ++++ .../exceptions/DCAEControllerTimeoutException.java | 36 ++++ .../DatabusControllerClientException.java | 36 ++++ .../exceptions/mappers/DBIExceptionMapper.java | 82 ++++++++ .../DCAEControllerConnectionExceptionMapper.java | 37 ++++ .../mappers/DCAEControllerExceptionMapper.java | 41 ++++ .../DCAEControllerTimeoutExceptionMapper.java | 37 ++++ .../providers/NotFoundExceptionMapper.java | 40 ++++ .../dcae/inventory/InventoryApplication.java | 197 ------------------ .../dcae/inventory/InventoryConfiguration.java | 173 ---------------- .../openecomp/dcae/inventory/LinkSerializer.java | 52 ----- .../inventory/clients/DCAEControllerClient.java | 222 --------------------- .../inventory/clients/DatabusControllerClient.java | 91 --------- .../inventory/daos/DCAEServiceComponentsDAO.java | 62 ------ .../inventory/daos/DCAEServiceTransactionDAO.java | 158 --------------- .../dcae/inventory/daos/DCAEServiceTypesDAO.java | 90 --------- .../daos/DCAEServicesComponentsMapsDAO.java | 50 ----- .../dcae/inventory/daos/DCAEServicesDAO.java | 76 ------- .../dcae/inventory/daos/InventoryDAO.java | 29 --- .../dcae/inventory/daos/InventoryDAOManager.java | 181 ----------------- .../inventory/dbthings/StringListArgument.java | 57 ------ .../mappers/DCAEServiceComponentObjectMapper.java | 48 ----- .../dbthings/mappers/DCAEServiceObjectMapper.java | 51 ----- .../mappers/DCAEServiceTypeObjectMapper.java | 73 ------- .../models/DCAEServiceComponentObject.java | 146 -------------- .../dbthings/models/DCAEServiceObject.java | 168 ---------------- .../dbthings/models/DCAEServiceTypeObject.java | 142 ------------- .../exceptions/DCAEControllerClientException.java | 36 ---- .../DCAEControllerConnectionException.java | 36 ---- .../exceptions/DCAEControllerTimeoutException.java | 36 ---- .../DatabusControllerClientException.java | 36 ---- .../exceptions/mappers/DBIExceptionMapper.java | 82 -------- .../DCAEControllerConnectionExceptionMapper.java | 37 ---- .../mappers/DCAEControllerExceptionMapper.java | 41 ---- .../DCAEControllerTimeoutExceptionMapper.java | 37 ---- .../providers/NotFoundExceptionMapper.java | 40 ---- .../java/DcaeServiceTypesApiServiceImplTests.java | 10 +- src/test/java/DcaeServicesApiServiceImplTests.java | 25 +-- .../DcaeServicesGroupbyApiServiceImplTests.java | 6 +- .../inventory/daos/InventoryDAOManagerTests.java | 44 ++++ .../daos/DCAEServiceTransactionDAOTests.java | 85 ++++++++ .../DCAEServiceComponentObjectMapperTests.java | 48 +++++ .../mappers/DCAEServiceObjectMapperTests.java | 50 +++++ .../mappers/DCAEServiceTypeObjectMapperTests.java | 110 ++++++++++ .../models/DCAEServiceComponentObjectTests.java | 71 +++++++ .../dbthings/models/DCAEServiceObjectTests.java | 72 +++++++ .../exception/mappers/DBIExceptionMapperTests.java | 117 +++++++++++ .../providers/NotFoundExceptionMapperTests.java | 42 ++++ .../inventory/daos/InventoryDAOManagerTests.java | 44 ---- .../daos/DCAEServiceTransactionDAOTests.java | 85 -------- .../DCAEServiceComponentObjectMapperTests.java | 48 ----- .../mappers/DCAEServiceObjectMapperTests.java | 50 ----- .../mappers/DCAEServiceTypeObjectMapperTests.java | 109 ---------- .../models/DCAEServiceComponentObjectTests.java | 71 ------- .../dbthings/models/DCAEServiceObjectTests.java | 72 ------- .../exception/mappers/DBIExceptionMapperTests.java | 117 ----------- .../providers/NotFoundExceptionMapperTests.java | 42 ---- 83 files changed, 3121 insertions(+), 3135 deletions(-) create mode 100644 src/main/java/org/onap/dcae/inventory/InventoryApplication.java create mode 100644 src/main/java/org/onap/dcae/inventory/InventoryConfiguration.java create mode 100644 src/main/java/org/onap/dcae/inventory/LinkSerializer.java create mode 100644 src/main/java/org/onap/dcae/inventory/clients/DCAEControllerClient.java create mode 100644 src/main/java/org/onap/dcae/inventory/clients/DatabusControllerClient.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/DCAEServiceComponentsDAO.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTransactionDAO.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTypesDAO.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/DCAEServicesDAO.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/InventoryDAO.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/StringListArgument.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObject.java create mode 100644 src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerClientException.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerConnectionException.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerTimeoutException.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientException.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java create mode 100644 src/main/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObject.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerClientException.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java delete mode 100644 src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java create mode 100644 src/test/java/org/onap/dcae/inventory/daos/InventoryDAOManagerTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapperTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java delete mode 100644 src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java diff --git a/pom.xml b/pom.xml index 074a5a2..1c43c5c 100644 --- a/pom.xml +++ b/pom.xml @@ -258,7 +258,7 @@ - org.openecomp.dcae.inventory.InventoryApplication + org.onap.dcae.inventory.InventoryApplication diff --git a/src/main/java/io/swagger/api/Bootstrap.java b/src/main/java/io/swagger/api/Bootstrap.java index 5050ed3..2d71ecb 100644 --- a/src/main/java/io/swagger/api/Bootstrap.java +++ b/src/main/java/io/swagger/api/Bootstrap.java @@ -38,7 +38,7 @@ public class Bootstrap extends HttpServlet { .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")) + .email("dcae@lists.onap.org")) .license(new License() .name("") .url("")); diff --git a/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java b/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java index 3c47b84..2fda7e7 100644 --- a/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java +++ b/src/main/java/io/swagger/api/factories/DcaeServicesApiServiceFactory.java @@ -20,8 +20,8 @@ package io.swagger.api.factories; -import org.openecomp.dcae.inventory.clients.DCAEControllerClient; -import org.openecomp.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.clients.DCAEControllerClient; +import org.onap.dcae.inventory.clients.DatabusControllerClient; import io.swagger.api.DcaeServicesApiService; import io.swagger.api.impl.DcaeServicesApiServiceImpl; diff --git a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java index e12b6fc..696cd87 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java @@ -20,12 +20,12 @@ 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; -import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; +import org.onap.dcae.inventory.daos.DCAEServiceTypesDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject; import io.swagger.api.*; import io.swagger.model.*; diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java index 4ef1a46..d709c5e 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServicesApiServiceImpl.java @@ -20,17 +20,17 @@ 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; -import org.openecomp.dcae.inventory.daos.DCAEServiceTransactionDAO; -import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; -import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; -import org.openecomp.dcae.inventory.exceptions.DCAEControllerClientException; -import org.openecomp.dcae.inventory.exceptions.DatabusControllerClientException; +import org.onap.dcae.inventory.clients.DCAEControllerClient; +import org.onap.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.daos.DCAEServiceComponentsDAO; +import org.onap.dcae.inventory.daos.DCAEServiceTransactionDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceComponentObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.exceptions.DCAEControllerClientException; +import org.onap.dcae.inventory.exceptions.DatabusControllerClientException; import io.swagger.api.*; import io.swagger.model.*; diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java index 09d3143..7c4e406 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java @@ -20,8 +20,8 @@ package io.swagger.api.impl; -import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; import io.swagger.api.*; import io.swagger.model.DCAEServiceGroupByResults; import io.swagger.model.DCAEServiceGroupByResultsPropertyValues; diff --git a/src/main/java/org/onap/dcae/inventory/InventoryApplication.java b/src/main/java/org/onap/dcae/inventory/InventoryApplication.java new file mode 100644 index 0000000..939cfdd --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/InventoryApplication.java @@ -0,0 +1,195 @@ +/*- + * ============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.onap.dcae.inventory; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dropwizard.configuration.ConfigurationFactory; +import io.dropwizard.configuration.ConfigurationFactoryFactory; +import io.dropwizard.configuration.JsonConfigurationFactory; +import io.dropwizard.configuration.UrlConfigurationSourceProvider; +import org.onap.dcae.inventory.clients.DCAEControllerClient; +import org.onap.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.exceptions.mappers.DBIExceptionMapper; +import org.onap.dcae.inventory.exceptions.mappers.DCAEControllerConnectionExceptionMapper; +import org.onap.dcae.inventory.exceptions.mappers.DCAEControllerTimeoutExceptionMapper; +import org.onap.dcae.inventory.providers.NotFoundExceptionMapper; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import com.fasterxml.jackson.databind.module.SimpleModule; +import io.dropwizard.Application; +import io.dropwizard.client.JerseyClientBuilder; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; +import io.swagger.api.DcaeServiceTypesApi; +import io.swagger.api.DcaeServicesApi; +import io.swagger.api.DcaeServicesGroupbyApi; +import io.swagger.api.factories.DcaeServicesApiServiceFactory; +import io.swagger.jaxrs.config.BeanConfig; +import io.swagger.jaxrs.listing.ApiListingResource; +import io.swagger.jaxrs.listing.SwaggerSerializers; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.DispatcherType; +import javax.servlet.FilterRegistration; +import javax.validation.Validator; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Link; +import java.util.EnumSet; +import java.util.Locale; + + +/** + * Created by mhwang on 4/11/16. + */ +public class InventoryApplication extends Application { + + static final Logger LOG = LoggerFactory.getLogger(InventoryApplication.class); + static boolean shouldRemoteFetchConfig = false; + + 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); + + if (args.length < 2 && "server".equals(args[0])) { + // When the start command is just "server", this will trigger inventory to look for its configuration + // from Consul's KV store. The url is hardcoded here which should be used as the "path" variable into + // the UrlConfigurationSourceProvider. + String[] customArgs = new String[args.length+1]; + System.arraycopy(args, 0, customArgs, 0, args.length); + customArgs[args.length] = "http://consul:8500/v1/kv/inventory?raw=true"; + shouldRemoteFetchConfig = true; + + new InventoryApplication().run(customArgs); + } else { + // You are here because you want to use the default way of configuring inventory - YAML file. + new InventoryApplication().run(args); + } + } + + @Override + public String getName() { + return "dcae-inventory"; + } + + private static class JsonConfigurationFactoryFactory implements ConfigurationFactoryFactory { + @Override + public ConfigurationFactory create(Class klass, Validator validator, ObjectMapper objectMapper, String propertyPrefix) { + return new JsonConfigurationFactory(klass, validator, objectMapper, propertyPrefix); + } + } + + @Override + public void initialize(Bootstrap bootstrap) { + // 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") + .contact(new Contact().email("dcae@lists.onap.org")); + // Swagger/servlet/jax-rs magic! + BeanConfig beanConfig = new BeanConfig(); + beanConfig.setInfo(info); + beanConfig.setResourcePackage("io.swagger.api"); + beanConfig.setScan(true); + + if (shouldRemoteFetchConfig) { + // You are here because the configuration is sitting on a remote server in json format + bootstrap.setConfigurationSourceProvider(new UrlConfigurationSourceProvider()); + bootstrap.setConfigurationFactoryFactory(new JsonConfigurationFactoryFactory<>()); + } + } + + @Override + 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())); + 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/ + // https://gist.github.com/yunspace/07d80a9ac32901f1e149#file-dropwizardjettycrossoriginintegrationtest-java-L11 + FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORSFilter", CrossOriginFilter.class); + filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); + filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*"); + filter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin"); + filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS"); + filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); + + // Want to serialize Link in a way we like + SimpleModule simpleModule = new SimpleModule(); + simpleModule.addSerializer(Link.class, new LinkSerializer()); + environment.getObjectMapper().registerModule(simpleModule); + + // Setup DCAE controller client + // Used by the dcae-services API + 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 + 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()); + environment.jersey().register(new DcaeServicesGroupbyApi()); + + // https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 + environment.jersey().register(new ApiListingResource()); + environment.jersey().register(new SwaggerSerializers()); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/InventoryConfiguration.java b/src/main/java/org/onap/dcae/inventory/InventoryConfiguration.java new file mode 100644 index 0000000..0dacb26 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/InventoryConfiguration.java @@ -0,0 +1,173 @@ +/*- + * ============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.onap.dcae.inventory; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.dropwizard.Configuration; +import io.dropwizard.client.JerseyClientConfiguration; +import io.dropwizard.db.DataSourceFactory; +import org.hibernate.validator.constraints.NotEmpty; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; + +/** + * Created by mhwang on 4/11/16. + */ +public class InventoryConfiguration extends Configuration { + + public static class DCAEControllerConnectionConfiguration { + @NotEmpty + @JsonProperty + private String host; + + @NotNull + @JsonProperty + private Integer port; + + @NotEmpty + @JsonProperty + private String basePath; + + @NotEmpty + @JsonProperty + private String user; + + @NotEmpty + @JsonProperty + private String password; + + @JsonProperty + private Boolean required = true; + + public String getHost() { + return this.host; + } + + public Integer getPort() { + return this.port; + } + + public String getBasePath() { + return this.basePath; + } + + public String getUser() { + return this.user; + } + + public String getPassword() { + return this.password; + } + + public Boolean getRequired() { + return this.required; + } + } + + public static class DatabusControllerConnectionConfiguration { + @NotEmpty + @JsonProperty + private String host; + + @NotNull + @JsonProperty + private Integer port; + + @NotNull + @JsonProperty + private String mechId; + + @NotNull + @JsonProperty + private String password; + + @JsonProperty + private Boolean required = true; + + public String getHost() { + return host; + } + + public Integer getPort() { + return port; + } + + public String getMechId() { + return mechId; + } + + public String getPassword() { + return password; + } + + public Boolean getRequired() { + return this.required; + } + } + + @NotEmpty + private String defaultName = "DCAEInventory"; + + @Valid + @NotNull + @JsonProperty + private DataSourceFactory database = new DataSourceFactory(); + + @NotNull + @JsonProperty + private DCAEControllerConnectionConfiguration dcaeControllerConnection = new DCAEControllerConnectionConfiguration(); + + @NotNull + @JsonProperty + private DatabusControllerConnectionConfiguration databusControllerConnection = new DatabusControllerConnectionConfiguration(); + + @NotNull + @JsonProperty + private JerseyClientConfiguration httpClient = new JerseyClientConfiguration(); + + @JsonProperty + public String getDefaultName() { + return defaultName; + } + + @JsonProperty + public void setDefaultName(String name) { + this.defaultName = name; + } + + public DataSourceFactory getDataSourceFactory() { + return this.database; + } + + public DCAEControllerConnectionConfiguration getDcaeControllerConnection() { + return this.dcaeControllerConnection; + } + + public DatabusControllerConnectionConfiguration getDatabusControllerConnection() { + return databusControllerConnection; + } + + public JerseyClientConfiguration getJerseyClientConfiguration() { + return httpClient; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/LinkSerializer.java b/src/main/java/org/onap/dcae/inventory/LinkSerializer.java new file mode 100644 index 0000000..ad5c6f3 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/LinkSerializer.java @@ -0,0 +1,52 @@ +/*- + * ============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.onap.dcae.inventory; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import javax.ws.rs.core.Link; +import java.io.IOException; + +/** + * Lifted from + * http://stackoverflow.com/questions/26989004/how-to-serialize-declarative-links-jersey-with-jackson + * + * Created by mhwang on 5/10/16. + */ +public class LinkSerializer extends JsonSerializer { + + @Override + public void serialize(Link link, JsonGenerator jg, SerializerProvider sp) throws IOException { + jg.writeStartObject(); + + if (link.getTitle() != null) { + jg.writeStringField("title", link.getTitle()); + } + + jg.writeStringField("rel", link.getRel()); + jg.writeStringField("href", link.getUri().toString()); + jg.writeEndObject(); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/clients/DCAEControllerClient.java b/src/main/java/org/onap/dcae/inventory/clients/DCAEControllerClient.java new file mode 100644 index 0000000..a97aa9e --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/clients/DCAEControllerClient.java @@ -0,0 +1,222 @@ +/*- + * ============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.onap.dcae.inventory.clients; + +import org.onap.dcae.inventory.InventoryConfiguration; +import org.onap.dcae.inventory.exceptions.DCAEControllerClientException; +import org.onap.dcae.inventory.exceptions.DCAEControllerConnectionException; +import org.onap.dcae.inventory.exceptions.DCAEControllerTimeoutException; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +import static org.glassfish.jersey.client.authentication.HttpAuthenticationFeature.*; + +/** + * Created by mhwang on 5/12/16. + */ +public class DCAEControllerClient { + + /** + * Used for JSON objects of the form: + * "hostService": {"$ref": "/services/vm-docker-host-2/instances/mtl2"} + */ + public static class Reference { + + @JsonProperty("$ref") + private String ref; + + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + + } + + /** + * Used for the JSON objects returned from /services/{service id}/instances/{instance id} + * calls. + */ + @JsonIgnoreProperties(ignoreUnknown = true) + public static class ServiceInstance { + + @JsonProperty("status") + private String status; + + /** + * Property points to the Docker host this Docker container runs on. + * This service instance is an application to be run as a Docker container if this value is not null. + */ + @JsonProperty("hostService") + @JsonInclude(JsonInclude.Include.NON_ABSENT) + private Reference hostService; + + /** + * Property points to the CDAP cluster this CDAP application is deployed on. + * This service instance is a CDAP application if this value is not null. + */ + @JsonProperty("clusterService") + @JsonInclude(JsonInclude.Include.NON_ABSENT) + private Reference clusterService; + + /** + * Property points to the location resource that this instance is associated with. + * This property is not null when the service instance is a "pure" VM. + */ + @JsonProperty("location") + @JsonInclude(JsonInclude.Include.NON_ABSENT) + private Reference location; + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public Reference getHostService() { + return hostService; + } + + public void setHostService(Reference hostService) { + this.hostService = hostService; + } + + public Reference getClusterService() { + return clusterService; + } + + public void setClusterService(Reference clusterService) { + this.clusterService = clusterService; + } + + public Reference getLocation() { + return location; + } + + public void setLocation(Reference location) { + this.location = location; + } + + } + + private final static Logger LOG = LoggerFactory.getLogger(DCAEControllerClient.class); + + private final Client client; + private final InventoryConfiguration.DCAEControllerConnectionConfiguration connectionConfiguration; + + public URI constructResourceURI(String resourcePath) { + // TODO: Better way to construct this? + + // Make sure that the resource path has a "/" because the UriBuilder sucks and doesn't do it for us. + if (resourcePath.charAt(0) != '/') { + resourcePath = (new StringBuilder("/")).append(resourcePath).toString(); + } + + StringBuilder actualPath = new StringBuilder("/"); + actualPath.append(this.connectionConfiguration.getBasePath()); + actualPath.append(resourcePath); + + return UriBuilder.fromPath(actualPath.toString()).scheme("http").host(this.connectionConfiguration.getHost()) + .port(this.connectionConfiguration.getPort()).build(); + } + + public ServiceInstance getServiceInstance(String componentId) throws DCAEControllerClientException { + URI uri = constructResourceURI(componentId); + Response response = null; + + try { + response = client.target(uri).request(MediaType.APPLICATION_JSON_TYPE) + .header("Content-Type", "application/json") + .property(HTTP_AUTHENTICATION_BASIC_USERNAME, connectionConfiguration.getUser()) + .property(HTTP_AUTHENTICATION_BASIC_PASSWORD, connectionConfiguration.getPassword()).get(); + } catch (ProcessingException e) { + // 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 :9998 + String message = "Connecting with DCAE controller probably timed out"; + LOG.error(message, e); + String exceptionMessage = String.format("%s: %s", message, e.getMessage()); + throw new DCAEControllerTimeoutException(exceptionMessage); + } catch (Exception e) { + String message = "Unexpected connection issue with DCAE controller"; + LOG.error(message, e); + String exceptionMessage = String.format("%s: %s", message, e.getMessage()); + throw new DCAEControllerConnectionException(exceptionMessage); + } + + if (LOG.isDebugEnabled()) { + LOG.debug(String.format("Received response from DCAE controller: %d", response.getStatus())); + } + + if (response.getStatus() == 200) { + ObjectMapper om = new ObjectMapper(); + + try { + return om.readValue((InputStream) response.getEntity(), ServiceInstance.class); + } catch (IOException e) { + throw new DCAEControllerClientException(e); + } + } + + throw new DCAEControllerClientException(String.format("Unexpected error from DCAE controller: %d", response.getStatus())); + } + + public String getLocation(ServiceInstance serviceInstance) { + if (serviceInstance.getLocation() != null) { + return serviceInstance.getLocation().getRef(); + } else if (serviceInstance.getClusterService() != null) { + // Drill down: Location is on the underlying CDAP cluster service instance + String cdapClusterRef = serviceInstance.getClusterService().getRef(); + return getLocation(getServiceInstance(cdapClusterRef)); + } else if (serviceInstance.getHostService() != null) { + // Drill down: Location is on the underlying Docker host service instance + String dockerHostRef = serviceInstance.getHostService().getRef(); + return getLocation(getServiceInstance(dockerHostRef)); + } else { + throw new DCAEControllerClientException("No valid location for service instance"); + } + } + + public DCAEControllerClient(Client client, + InventoryConfiguration.DCAEControllerConnectionConfiguration connectionConfiguration) { + this.client = client; + this.connectionConfiguration = connectionConfiguration; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/clients/DatabusControllerClient.java b/src/main/java/org/onap/dcae/inventory/clients/DatabusControllerClient.java new file mode 100644 index 0000000..29330b0 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/clients/DatabusControllerClient.java @@ -0,0 +1,91 @@ +/*- + * ============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.onap.dcae.inventory.clients; + +import org.onap.dcae.inventory.InventoryConfiguration; +import org.onap.dcae.inventory.exceptions.DatabusControllerClientException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import javax.ws.rs.client.Client; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.util.Map; + +/** + * Created by mhwang on 5/25/16. + */ +public class DatabusControllerClient { + + private final Client client; + private final InventoryConfiguration.DatabusControllerConnectionConfiguration connectionConfiguration; + + public URI constructResourceURI(String resourcePath) { + // Make sure that the resource path has a "/" because the UriBuilder sucks and doesn't do it for us. + if (resourcePath.charAt(0) != '/') { + resourcePath = (new StringBuilder("/")).append(resourcePath).toString(); + } + + return UriBuilder.fromPath(resourcePath.toString()).scheme("https").host(this.connectionConfiguration.getHost()) + .port(this.connectionConfiguration.getPort()).build(); + } + + // TODO: Actually model the JSON objects so that they can be returned to be used for providing useful information + // and thus change this to a "get". + public boolean isExists(String componentId) throws DatabusControllerClientException { + URI uri = constructResourceURI(componentId); + + Response response = client.target(uri).request(MediaType.APPLICATION_JSON_TYPE) + .header("Content-Type", "application/json").get(); + + if (response.getStatus() == 200) { + ObjectMapper om = new ObjectMapper(); + + try { + Map entity = om.readValue((InputStream) response.getEntity(), + new TypeReference>() {}); + + return (entity != null && entity.size() > 0) ? true : false; + } 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", + response.getStatus())); + } + + public DatabusControllerClient(Client client, + InventoryConfiguration.DatabusControllerConnectionConfiguration connectionConfiguration) { + this.client = client; + this.connectionConfiguration = connectionConfiguration; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceComponentsDAO.java b/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceComponentsDAO.java new file mode 100644 index 0000000..a2d70d0 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceComponentsDAO.java @@ -0,0 +1,62 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceComponentObjectMapper; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceComponentObject; +import org.skife.jdbi.v2.sqlobject.Bind; +import org.skife.jdbi.v2.sqlobject.BindBean; +import org.skife.jdbi.v2.sqlobject.SqlQuery; +import org.skife.jdbi.v2.sqlobject.SqlUpdate; +import org.skife.jdbi.v2.sqlobject.customizers.Mapper; + +import java.util.List; + +/** + * Created by mhwang on 4/19/16. + */ +public interface DCAEServiceComponentsDAO extends InventoryDAO { + + @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_service_components\')") + Boolean checkIfTableExists(); + + @SqlUpdate("create table dcae_service_components (component_id varchar not null primary key, component_type varchar not null, " + + "component_source varchar not null, shareable integer default 0, created timestamp not null, modified timestamp not null)") + void createTable(); + + @SqlUpdate("insert into dcae_service_components (component_id, component_type, component_source, shareable, created, modified) " + + "values (:componentId, :componentType, :componentSource, :shareable, :created, :modified)") + void insert(@BindBean DCAEServiceComponentObject componentObject); + + @SqlUpdate("update dcae_service_components set component_type = :componentType, component_source = :componentSource, " + + "shareable = :shareable, modified = :modified where component_id = :componentId") + void update(@BindBean DCAEServiceComponentObject componentObject); + + @Mapper(DCAEServiceComponentObjectMapper.class) + @SqlQuery("select c.* from dcae_services_components_maps m join dcae_service_components c " + + "on m.component_id = c.component_id where m.service_id = :it") + List getByServiceId(@Bind String serviceId); + + @Mapper(DCAEServiceComponentObjectMapper.class) + @SqlQuery("select c.* from dcae_service_components c where c.component_id = :it") + DCAEServiceComponentObject getByComponentId(@Bind String componentId); + +} diff --git a/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTransactionDAO.java b/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTransactionDAO.java new file mode 100644 index 0000000..cc7c88b --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTransactionDAO.java @@ -0,0 +1,158 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.onap.dcae.inventory.dbthings.models.DCAEServiceComponentObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.joda.time.DateTime; +import org.skife.jdbi.v2.sqlobject.CreateSqlObject; +import org.skife.jdbi.v2.sqlobject.Transaction; + +import java.util.ArrayList; +import java.util.List; + +/** + * This is based-off of this blog post. + * http://manikandan-k.github.io/2015/05/10/Transactions_in_jdbi.html + * + * Created by mhwang on 4/21/16. + */ +public abstract class DCAEServiceTransactionDAO { + + public static class DCAEServiceTransactionContext { + + private final String serviceId; + private final DateTime modified; + private DCAEServiceObject serviceObjectToInsert; + private DCAEServiceObject serviceObjectToUpdate; + private List componentObjectsToInsert; + private List componentObjectsToUpdate; + private List mappingsToInsert; + private List mappingsToDelete; + + public String getServiceId() { + return serviceId; + } + + public DateTime getModified() { + return modified; + } + + public DCAEServiceObject getServiceObjectToInsert() { + return serviceObjectToInsert; + } + + public void setServiceObjectToInsert(DCAEServiceObject serviceObjectToInsert) { + this.serviceObjectToInsert = serviceObjectToInsert; + } + + public DCAEServiceObject getServiceObjectToUpdate() { + return serviceObjectToUpdate; + } + + public void setServiceObjectToUpdate(DCAEServiceObject serviceObjectToUpdate) { + this.serviceObjectToUpdate = serviceObjectToUpdate; + } + + public List getComponentObjectsToInsert() { + return componentObjectsToInsert; + } + + public List addComponentObjectToInsert(DCAEServiceComponentObject componentObject) { + this.componentObjectsToInsert.add(componentObject); + return this.componentObjectsToInsert; + } + + public List getComponentObjectsToUpdate() { + return componentObjectsToUpdate; + } + + public List addComponentObjectToUpdate(DCAEServiceComponentObject componentObject) { + this.componentObjectsToUpdate.add(componentObject); + return this.componentObjectsToUpdate; + } + + public List getMappingsToInsert() { + return mappingsToInsert; + } + + public List addMappingsToInsert(String componentId) { + this.mappingsToInsert.add(componentId); + return this.mappingsToInsert; + } + + public List getMappingsToDelete() { + return mappingsToDelete; + } + + public List addMappingsToDelete(String componentId) { + this.mappingsToDelete.add(componentId); + return this.mappingsToDelete; + } + + public DCAEServiceTransactionContext(String serviceId, DateTime modified) { + this.serviceId = serviceId; + this.modified = modified; + this.componentObjectsToInsert = new ArrayList<>(); + this.componentObjectsToUpdate = new ArrayList<>(); + this.mappingsToInsert = new ArrayList<>(); + this.mappingsToDelete = new ArrayList<>(); + } + + } + + @CreateSqlObject + abstract public DCAEServicesDAO getServicesDAO(); + + @CreateSqlObject + abstract public DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO(); + + @CreateSqlObject + abstract public DCAEServiceComponentsDAO getComponentsDAO(); + + @Transaction + public void insert(DCAEServiceTransactionContext context) { + if (context.getServiceObjectToInsert() != null) { + this.getServicesDAO().insert(context.getServiceObjectToInsert()); + } + + if (context.getServiceObjectToUpdate() != null) { + this.getServicesDAO().update(context.getServiceObjectToUpdate()); + } + + for (DCAEServiceComponentObject sco : context.getComponentObjectsToInsert()) { + this.getComponentsDAO().insert(sco); + } + + for (DCAEServiceComponentObject sco : context.getComponentObjectsToUpdate()) { + this.getComponentsDAO().update(sco); + } + + for (String componentId : context.getMappingsToInsert()) { + this.getServicesComponentsMappingDAO().insert(context.getServiceId(), componentId, context.getModified()); + } + + for (String componentId : context.getMappingsToDelete()) { + this.getServicesComponentsMappingDAO().delete(context.serviceId, componentId); + } + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTypesDAO.java b/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTypesDAO.java new file mode 100644 index 0000000..bdb6298 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/DCAEServiceTypesDAO.java @@ -0,0 +1,90 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper; +import org.onap.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; +import org.skife.jdbi.v2.sqlobject.SqlUpdate; +import org.skife.jdbi.v2.sqlobject.customizers.Mapper; + +/** + * 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. + */ +public interface DCAEServiceTypesDAO extends InventoryDAO { + + @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_service_types\')") + Boolean checkIfTableExists(); + + /** + * 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_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(); + + // 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); + + @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 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/onap/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java b/src/main/java/org/onap/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java new file mode 100644 index 0000000..68ed59c --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java @@ -0,0 +1,50 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.joda.time.DateTime; +import org.skife.jdbi.v2.sqlobject.Bind; +import org.skife.jdbi.v2.sqlobject.SqlQuery; +import org.skife.jdbi.v2.sqlobject.SqlUpdate; + +/** + * This class manages the joining table called "dcae_services_components_maps" which maps the many-to-many relationship + * between DCAE services and DCAE service components. + * + * Created by mhwang on 4/19/16. + */ +public interface DCAEServicesComponentsMapsDAO extends InventoryDAO { + + @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_services_components_maps\')") + Boolean checkIfTableExists(); + + @SqlUpdate("create table dcae_services_components_maps (service_id varchar not null references dcae_services (service_id), " + + "component_id varchar not null references dcae_service_components (component_id), " + + "created timestamp not null, primary key (service_id, component_id))") + void createTable(); + + @SqlUpdate("insert into dcae_services_components_maps (service_id, component_id, created) values (:serviceId, :componentId, :created)") + void insert(@Bind("serviceId") String serviceId, @Bind("componentId") String componentId, @Bind("created") DateTime created); + + @SqlUpdate("delete from dcae_services_components_maps where service_id = :serviceId and component_id = :componentId") + void delete(@Bind("serviceId") String serviceId, @Bind("componentId") String componentId); + +} diff --git a/src/main/java/org/onap/dcae/inventory/daos/DCAEServicesDAO.java b/src/main/java/org/onap/dcae/inventory/daos/DCAEServicesDAO.java new file mode 100644 index 0000000..1398a26 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/DCAEServicesDAO.java @@ -0,0 +1,76 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.joda.time.DateTime; +import org.skife.jdbi.v2.sqlobject.Bind; +import org.skife.jdbi.v2.sqlobject.BindBean; +import org.skife.jdbi.v2.sqlobject.SqlQuery; +import org.skife.jdbi.v2.sqlobject.SqlUpdate; +import org.skife.jdbi.v2.sqlobject.customizers.Mapper; +import org.skife.jdbi.v2.util.IntegerMapper; + + +/** + * Created by mhwang on 4/19/16. + */ +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_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_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_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); + + @Mapper(DCAEServiceObjectMapper.class) + @SqlQuery("select * from dcae_services where status = :status and service_id = :serviceId") + DCAEServiceObject getByServiceId(@Bind("status") DCAEServiceObject.DCAEServiceStatus status, @Bind("serviceId") String serviceId); + + @Mapper(DCAEServiceObjectMapper.class) + @SqlQuery("select * from dcae_services where service_id = :serviceId") + DCAEServiceObject getByServiceId(@Bind("serviceId") String serviceId); + + @SqlUpdate("update dcae_services set modified = :modified, status = :status where service_id = :serviceId") + void updateStatusByServiceId(@Bind("modified") DateTime modified, + @Bind("status") DCAEServiceObject.DCAEServiceStatus status, + @Bind("serviceId") String serviceId); + + @Mapper(IntegerMapper.class) + @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/onap/dcae/inventory/daos/InventoryDAO.java b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAO.java new file mode 100644 index 0000000..a369cb1 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAO.java @@ -0,0 +1,29 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +/** + * Created by mhwang on 4/19/16. + */ +public interface InventoryDAO { + Boolean checkIfTableExists(); + void createTable(); +} diff --git a/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java new file mode 100644 index 0000000..b5f0b04 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java @@ -0,0 +1,181 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.onap.dcae.inventory.InventoryConfiguration; +import org.onap.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; + +import java.util.Arrays; +import java.util.List; + +/** + * Reluctantly made this into a singleton in order to have access to the DAOs in the request handling code. Didn't + * want to change the interface on the handlers because they are generated by Swagger and I wanted to flexibility + * to swap in changes easily. This meant sacrificing dependency injection which is preferred. + * + * Created by mhwang on 4/19/16. + */ +public final class InventoryDAOManager { + + private static InventoryDAOManager instance; + + public static InventoryDAOManager getInstance() { + if (instance == null) { + instance = new 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 DAO_CLASSES = Arrays.asList(DCAEServiceTypesDAO.class, DCAEServicesDAO.class, + DCAEServiceComponentsDAO.class, DCAEServicesComponentsMapsDAO.class); + + private DBI jdbi; + private Environment environment; + private InventoryConfiguration configuration; + + private InventoryDAOManager() { + } + + /** + * 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 daoClass : DAO_CLASSES) { + final InventoryDAO dao = jdbi.onDemand(daoClass); + + if (dao.checkIfTableExists()) { + LOG.info(String.format("Sql table exists: %s", daoClass.getSimpleName())); + } else { + dao.createTable(); + 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 klass) { + if (jdbi == null) { + throw new RuntimeException("InventoryDAOManager has not been initialized!"); + } + + // Using this approach to constructing the DAO, the client is not responsible for closing the handle. + // http://jdbi.org/sql_object_overview/ + // > In this case we do not need to (and in fact shouldn’t) ever take action to close the handle the sql object uses. + return jdbi.onDemand(klass); + } + + public DCAEServicesDAO getDCAEServicesDAO() { + return (DCAEServicesDAO) this.getDAO(DCAEServicesDAO.class); + } + + public DCAEServiceComponentsDAO getDCAEServiceComponentsDAO() { + return (DCAEServiceComponentsDAO) this.getDAO(DCAEServiceComponentsDAO.class); + } + + public DCAEServicesComponentsMapsDAO getDCAEServicesComponentsDAO() { + return (DCAEServicesComponentsMapsDAO) this.getDAO(DCAEServicesComponentsMapsDAO.class); + } + + public DCAEServiceTransactionDAO getDCAEServiceTransactionDAO() { + return jdbi.onDemand(DCAEServiceTransactionDAO.class); + } + + public DCAEServiceTypesDAO getDCAEServiceTypesDAO() { + return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class); + } + + /** + * Must close the handle that is returned here. It is AutoCloseable so just use it as a try-with-resource. + * + * @return + */ + public Handle getHandle() { + return this.jdbi.open(); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/dbthings/StringListArgument.java b/src/main/java/org/onap/dcae/inventory/dbthings/StringListArgument.java new file mode 100644 index 0000000..b08bec1 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/StringListArgument.java @@ -0,0 +1,57 @@ +/*- + * ============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.onap.dcae.inventory.dbthings; + +import org.skife.jdbi.v2.StatementContext; +import org.skife.jdbi.v2.tweak.Argument; +import org.skife.jdbi.v2.tweak.ArgumentFactory; + +import java.sql.Array; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +/** + * This class if needed for jdbi queries to be able to properly bind List objects. + * + * http://stackoverflow.com/questions/33062516/insert-2d-array-into-postresql-db-using-jdbi + * + * Created by mhwang on 5/4/16. + */ +public class StringListArgument implements ArgumentFactory> { + + @Override + public boolean accepts(Class expectedType, Object value, StatementContext statementContext) { + return value != null && List.class.isAssignableFrom(value.getClass()); + } + + @Override + public Argument build(Class expectedType, List value, StatementContext statementContext) { + return new Argument() { + @Override + public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { + Array values = statement.getConnection().createArrayOf("varchar", value.toArray()); + statement.setArray(position, values); + } + }; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java b/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java new file mode 100644 index 0000000..f214839 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java @@ -0,0 +1,48 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.mappers; + +import org.onap.dcae.inventory.dbthings.models.DCAEServiceComponentObject; +import org.joda.time.DateTime; +import org.skife.jdbi.v2.StatementContext; +import org.skife.jdbi.v2.tweak.ResultSetMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Created by mhwang on 4/19/16. + */ +public class DCAEServiceComponentObjectMapper implements ResultSetMapper { + + @Override + public DCAEServiceComponentObject map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { + DCAEServiceComponentObject object = new DCAEServiceComponentObject(); + object.setComponentId(resultSet.getString("component_id")); + object.setComponentType(resultSet.getString("component_type")); + object.setComponentSource(resultSet.getString("component_source")); + object.setShareable(resultSet.getInt("shareable")); + object.setCreated(new DateTime(resultSet.getTimestamp("created"))); + object.setModified(new DateTime(resultSet.getTimestamp("modified"))); + return object; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java b/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java new file mode 100644 index 0000000..f60be7b --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java @@ -0,0 +1,51 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.mappers; + +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.joda.time.DateTime; +import org.skife.jdbi.v2.StatementContext; +import org.skife.jdbi.v2.tweak.ResultSetMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Created by mhwang on 4/19/16. + */ +public class DCAEServiceObjectMapper implements ResultSetMapper { + + @Override + public DCAEServiceObject map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { + DCAEServiceObject object = new DCAEServiceObject(); + object.setServiceId(resultSet.getString("service_id")); + object.setTypeId(resultSet.getString("type_id")); + object.setVnfId(resultSet.getString("vnf_id")); + object.setVnfType(resultSet.getString("vnf_type")); + object.setVnfLocation(resultSet.getString("vnf_location")); + object.setDeploymentRef(resultSet.getString("deployment_ref")); + object.setCreated(new DateTime(resultSet.getTimestamp("created"))); + object.setModified(new DateTime(resultSet.getTimestamp("modified"))); + object.setStatus(DCAEServiceObject.DCAEServiceStatus.valueOf(resultSet.getString("status"))); + return object; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java b/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java new file mode 100644 index 0000000..bda4423 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java @@ -0,0 +1,73 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.mappers; + +import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject; +import org.joda.time.DateTime; +import org.skife.jdbi.v2.StatementContext; +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; + +/** + * Created by mhwang on 5/3/16. + */ +public class DCAEServiceTypeObjectMapper implements ResultSetMapper { + + @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)); + + Array serviceIdsArray = resultSet.getArray("service_ids"); + + if (serviceIdsArray != null) { + String[] serviceIds = (String[]) serviceIdsArray.getArray(); + object.setServiceIds(Arrays.asList(serviceIds)); + } + + Array serviceLocationsArray = resultSet.getArray("service_locations"); + + if (serviceLocationsArray != null) { + String[] serviceLocations = (String[]) serviceLocationsArray.getArray(); + object.setServiceLocations(Arrays.asList(serviceLocations)); + } + + 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/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java b/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java new file mode 100644 index 0000000..b262fc0 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java @@ -0,0 +1,146 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.models; + +import io.swagger.model.DCAEServiceComponentRequest; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; + +/** + * Created by mhwang on 4/19/16. + */ +public class DCAEServiceComponentObject { + + private String componentId = null; + private DateTime created = null; + private DateTime modified = null; + private String componentType = null; + private String componentSource = null; + private Integer shareable = null; + + public String getComponentId() { + return componentId; + } + + public void setComponentId(String componentId) { + this.componentId = componentId; + } + + public DateTime getCreated() { + return created; + } + + public void setCreated(DateTime created) { + this.created = created; + } + + public DateTime getModified() { + return modified; + } + + public void setModified(DateTime modified) { + this.modified = modified; + } + + public String getComponentType() { + return componentType; + } + + public void setComponentType(String componentType) { + this.componentType = componentType; + } + + public String getComponentSource() { + return componentSource; + } + + public void setComponentSource(String componentSource) { + this.componentSource = componentSource; + } + + public Integer getShareable() { + return shareable; + } + + public void setShareable(Integer shareable) { + this.shareable = shareable; + } + + public DCAEServiceComponentObject() { + } + + /** + * Intended to be used for inserts - new objects. + * + * @param request + */ + public DCAEServiceComponentObject(DCAEServiceComponentRequest request) { + DateTime now = DateTime.now(DateTimeZone.UTC); + this.setComponentId(request.getComponentId()); + this.setComponentType(request.getComponentType()); + this.setComponentSource(request.getComponentSource()); + this.setCreated(now); + this.setModified(now); + this.setShareable(request.getShareable()); + } + + /** + * Intended to be used for updates - some fields should not be updated. + * + * @param source + * @param updateRequest + */ + public DCAEServiceComponentObject(DCAEServiceComponentObject source, DCAEServiceComponentRequest updateRequest) { + // Immutable fields + this.setComponentId(source.getComponentId()); + this.setCreated(source.getCreated()); + + // Mutable fields + this.setComponentType(updateRequest.getComponentType()); + this.setComponentSource(updateRequest.getComponentSource()); + this.setShareable(updateRequest.getShareable()); + this.setModified(DateTime.now(DateTimeZone.UTC)); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class DCAEServiceComponentObject {\n"); + + sb.append(" componentId: ").append(toIndentedString(componentId)).append("\n"); + sb.append(" componentType: ").append(toIndentedString(componentType)).append("\n"); + sb.append(" componentSource: ").append(toIndentedString(componentSource)).append("\n"); + sb.append(" shareable: ").append(toIndentedString(shareable)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} diff --git a/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObject.java b/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObject.java new file mode 100644 index 0000000..83e2bfe --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObject.java @@ -0,0 +1,168 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.models; + +import io.swagger.model.DCAEServiceRequest; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; + +/** + * Created by mhwang on 4/19/16. + */ +public class DCAEServiceObject { + + public static enum DCAEServiceStatus { + RUNNING, + REMOVED + } + + private String serviceId = null; + private String typeId = null; + private DateTime created = null; + private DateTime modified = null; + private String vnfId = null; + private String vnfType = null; + private String vnfLocation = null; + private String deploymentRef = null; + + // These properties are meant to be used internally in the service only + private DCAEServiceStatus status = null; + + public String getServiceId() { + return serviceId; + } + + public void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + + public String getTypeId() { + return typeId; + } + + public void setTypeId(String typeId) { + this.typeId = typeId; + } + + public DateTime getCreated() { + return created; + } + + public void setCreated(DateTime created) { + this.created = created; + } + + public DateTime getModified() { + return modified; + } + + public void setModified(DateTime modified) { + this.modified = modified; + } + + public DCAEServiceStatus getStatus() { + return status; + } + + public void setStatus(DCAEServiceStatus status) { + this.status = status; + } + + public String getVnfId() { + return vnfId; + } + + public void setVnfId(String vnfId) { + this.vnfId = vnfId; + } + + public String getVnfType() { + return vnfType; + } + + public void setVnfType(String vnfType) { + this.vnfType = vnfType; + } + + public String getVnfLocation() { + return vnfLocation; + } + + public void setVnfLocation(String vnfLocation) { + this.vnfLocation = vnfLocation; + } + + public String getDeploymentRef() { + return deploymentRef; + } + + public void setDeploymentRef(String deploymentRef) { + this.deploymentRef = deploymentRef; + } + + public DCAEServiceObject() { + } + + // TODO: Move the constructors functionality below out into the actual handlers. + + /** + * Intended to be used for inserts - new objects. + * + * @param serviceId + * @param request + */ + public DCAEServiceObject(String serviceId, DCAEServiceRequest request) { + DateTime now = DateTime.now(DateTimeZone.UTC); + this.setServiceId(serviceId); + this.setTypeId(request.getTypeId()); + this.setVnfId(request.getVnfId()); + this.setVnfType(request.getVnfType()); + this.setVnfLocation(request.getVnfLocation()); + this.setDeploymentRef(request.getDeploymentRef()); + this.setCreated(now); + this.setModified(now); + // Assumption here is that you are here from the PUT which means that the service is RUNNING. + this.setStatus(DCAEServiceStatus.RUNNING); + } + + /** + * Intended to be used for updates - some fields should not be updated. + * + * @param source + * @param updateRequest + */ + public DCAEServiceObject(DCAEServiceObject source, DCAEServiceRequest updateRequest) { + // Immutable fields + this.setServiceId(source.getServiceId()); + this.setCreated(source.getCreated()); + + // Mutable fields + this.setTypeId(updateRequest.getTypeId()); + this.setVnfId(updateRequest.getVnfId()); + this.setVnfType(updateRequest.getVnfType()); + this.setVnfLocation(updateRequest.getVnfLocation()); + this.setDeploymentRef(updateRequest.getDeploymentRef()); + this.setModified(DateTime.now(DateTimeZone.UTC)); + // Assumption here is that you are here from the PUT which means that the service is RUNNING. + this.setStatus(DCAEServiceStatus.RUNNING); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java b/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java new file mode 100644 index 0000000..f264c99 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java @@ -0,0 +1,142 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.models; + +import org.joda.time.DateTime; + +import java.util.List; + +/** + * POJO representation of a record in dcae_service_types table. + * + * Created by mhwang on 5/3/16. + */ +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 vnfTypes = null; + private List serviceIds = null; + private List serviceLocations = 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; + } + + public void setTypeName(String typeName) { + 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; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public List getVnfTypes() { + return vnfTypes; + } + + public void setVnfTypes(List vnfTypes) { + this.vnfTypes = vnfTypes; + } + + public List getServiceIds() { + return serviceIds; + } + + public void setServiceIds(List serviceIds) { + this.serviceIds = serviceIds; + } + + public List getServiceLocations() { + return serviceLocations; + } + + public void setServiceLocations(List serviceLocations) { + this.serviceLocations = serviceLocations; + } + + public String getAsdcServiceId() { + return asdcServiceId; + } + + public void setAsdcServiceId(String asdcServiceId) { + this.asdcServiceId = asdcServiceId; + } + + public String getAsdcResourceId() { + return asdcResourceId; + } + + public void setAsdcResourceId(String asdcResourceId) { + this.asdcResourceId = asdcResourceId; + } + + public DateTime getCreated() { + return created; + } + + public void setCreated(DateTime created) { + this.created = created; + } + + public DateTime getDeactivated() { + return deactivated; + } + + public void setDeactivated(DateTime deactivated) { + this.deactivated = deactivated; + } +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerClientException.java b/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerClientException.java new file mode 100644 index 0000000..b549ee1 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerClientException.java @@ -0,0 +1,36 @@ +/*- + * ============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.onap.dcae.inventory.exceptions; + +/** + * Created by mhwang on 5/13/16. + */ +public class DCAEControllerClientException extends RuntimeException { + + public DCAEControllerClientException(String message) { + super(message); + } + + public DCAEControllerClientException(Throwable e) { + super(e); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerConnectionException.java b/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerConnectionException.java new file mode 100644 index 0000000..f96ffbe --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerConnectionException.java @@ -0,0 +1,36 @@ +/*- + * ============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.onap.dcae.inventory.exceptions; + +/** + * Created by mhwang on 8/23/16. + */ +public class DCAEControllerConnectionException extends RuntimeException { + + public DCAEControllerConnectionException(String message) { + super(message); + } + + public DCAEControllerConnectionException(Throwable e) { + super(e); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerTimeoutException.java b/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerTimeoutException.java new file mode 100644 index 0000000..48f5044 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/DCAEControllerTimeoutException.java @@ -0,0 +1,36 @@ +/*- + * ============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.onap.dcae.inventory.exceptions; + +/** + * Created by mhwang on 8/23/16. + */ +public class DCAEControllerTimeoutException extends RuntimeException { + + public DCAEControllerTimeoutException(String message) { + super(message); + } + + public DCAEControllerTimeoutException(Throwable e) { + super(e); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientException.java b/src/main/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientException.java new file mode 100644 index 0000000..14855a8 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientException.java @@ -0,0 +1,36 @@ +/*- + * ============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.onap.dcae.inventory.exceptions; + +/** + * Created by mhwang on 5/25/16. + */ +public class DatabusControllerClientException extends RuntimeException { + + public DatabusControllerClientException(String message) { + super(message); + } + + public DatabusControllerClientException(Throwable e) { + super(e); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java new file mode 100644 index 0000000..016ac27 --- /dev/null +++ b/src/main/java/org/onap/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.onap.dcae.inventory.exceptions.mappers; + +import org.onap.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 implements ExceptionMapper { + + 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 { + } + + public final static class UnableToCreateStatementExceptionMapper extends DBIExceptionMapper { + } + + public final static class UnableToExecuteStatementExceptionMapper extends DBIExceptionMapper { + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java new file mode 100644 index 0000000..e345c6e --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java @@ -0,0 +1,37 @@ +/*- + * ============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.onap.dcae.inventory.exceptions.mappers; + +import org.onap.dcae.inventory.exceptions.DCAEControllerConnectionException; + +import javax.ws.rs.core.Response; + +/** + * Created by mhwang on 8/23/16. + */ +public class DCAEControllerConnectionExceptionMapper extends DCAEControllerExceptionMapper { + + @Override + protected Response.Status getStatus() { + return Response.Status.BAD_GATEWAY; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java new file mode 100644 index 0000000..8e4eb3b --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java @@ -0,0 +1,41 @@ +/*- + * ============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.onap.dcae.inventory.exceptions.mappers; + +import io.swagger.api.ApiResponseMessage; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/** + * Created by mhwang on 8/23/16. + */ +public abstract class DCAEControllerExceptionMapper implements ExceptionMapper { + + abstract protected Response.Status getStatus(); + + @Override + public Response toResponse(T e) { + ApiResponseMessage response = new ApiResponseMessage(ApiResponseMessage.ERROR, e.getMessage()); + return Response.status(this.getStatus()).entity(response).build(); + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java new file mode 100644 index 0000000..67eb6ca --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java @@ -0,0 +1,37 @@ +/*- + * ============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.onap.dcae.inventory.exceptions.mappers; + +import org.onap.dcae.inventory.exceptions.DCAEControllerTimeoutException; + +import javax.ws.rs.core.Response; + +/** + * Created by mhwang on 8/23/16. + */ +public class DCAEControllerTimeoutExceptionMapper extends DCAEControllerExceptionMapper { + + @Override + protected Response.Status getStatus() { + return Response.Status.GATEWAY_TIMEOUT; + } + +} diff --git a/src/main/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapper.java b/src/main/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapper.java new file mode 100644 index 0000000..7967470 --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapper.java @@ -0,0 +1,40 @@ +/*- + * ============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.onap.dcae.inventory.providers; + +import io.swagger.api.ApiResponseMessage; +import io.swagger.api.NotFoundException; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/** + * Created by mhwang on 5/18/16. + */ +public class NotFoundExceptionMapper implements ExceptionMapper { + + @Override + public Response toResponse(NotFoundException e) { + ApiResponseMessage entity = new ApiResponseMessage(e.getCode(), e.getMessage()); + return Response.status(Response.Status.NOT_FOUND).entity(entity).build(); + } + +} diff --git a/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java b/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java deleted file mode 100644 index 9e78506..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/InventoryApplication.java +++ /dev/null @@ -1,197 +0,0 @@ -/*- - * ============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; - -import com.fasterxml.jackson.databind.ObjectMapper; -import io.dropwizard.cli.Cli; -import io.dropwizard.configuration.ConfigurationFactory; -import io.dropwizard.configuration.ConfigurationFactoryFactory; -import io.dropwizard.configuration.JsonConfigurationFactory; -import io.dropwizard.configuration.UrlConfigurationSourceProvider; -import io.dropwizard.util.JarLocation; -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.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.setup.Bootstrap; -import io.dropwizard.setup.Environment; -import io.swagger.api.DcaeServiceTypesApi; -import io.swagger.api.DcaeServicesApi; -import io.swagger.api.DcaeServicesGroupbyApi; -import io.swagger.api.factories.DcaeServicesApiServiceFactory; -import io.swagger.jaxrs.config.BeanConfig; -import io.swagger.jaxrs.listing.ApiListingResource; -import io.swagger.jaxrs.listing.SwaggerSerializers; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.servlet.DispatcherType; -import javax.servlet.FilterRegistration; -import javax.validation.Validator; -import javax.ws.rs.client.Client; -import javax.ws.rs.core.Link; -import java.util.EnumSet; -import java.util.Locale; - - -/** - * Created by mhwang on 4/11/16. - */ -public class InventoryApplication extends Application { - - static final Logger LOG = LoggerFactory.getLogger(InventoryApplication.class); - static boolean shouldRemoteFetchConfig = false; - - 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); - - if (args.length < 2 && "server".equals(args[0])) { - // When the start command is just "server", this will trigger inventory to look for its configuration - // from Consul's KV store. The url is hardcoded here which should be used as the "path" variable into - // the UrlConfigurationSourceProvider. - String[] customArgs = new String[args.length+1]; - System.arraycopy(args, 0, customArgs, 0, args.length); - customArgs[args.length] = "http://consul:8500/v1/kv/inventory?raw=true"; - shouldRemoteFetchConfig = true; - - new InventoryApplication().run(customArgs); - } else { - // You are here because you want to use the default way of configuring inventory - YAML file. - new InventoryApplication().run(args); - } - } - - @Override - public String getName() { - return "dcae-inventory"; - } - - private static class JsonConfigurationFactoryFactory implements ConfigurationFactoryFactory { - @Override - public ConfigurationFactory create(Class klass, Validator validator, ObjectMapper objectMapper, String propertyPrefix) { - return new JsonConfigurationFactory(klass, validator, objectMapper, propertyPrefix); - } - } - - @Override - public void initialize(Bootstrap bootstrap) { - // 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") - .contact(new Contact().email("dcae@lists.openecomp.org")); - // Swagger/servlet/jax-rs magic! - BeanConfig beanConfig = new BeanConfig(); - beanConfig.setInfo(info); - beanConfig.setResourcePackage("io.swagger.api"); - beanConfig.setScan(true); - - if (shouldRemoteFetchConfig) { - // You are here because the configuration is sitting on a remote server in json format - bootstrap.setConfigurationSourceProvider(new UrlConfigurationSourceProvider()); - bootstrap.setConfigurationFactoryFactory(new JsonConfigurationFactoryFactory<>()); - } - } - - @Override - 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())); - 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/ - // https://gist.github.com/yunspace/07d80a9ac32901f1e149#file-dropwizardjettycrossoriginintegrationtest-java-L11 - FilterRegistration.Dynamic filter = environment.servlets().addFilter("CORSFilter", CrossOriginFilter.class); - filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); - filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*"); - filter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin"); - filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS"); - filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); - - // Want to serialize Link in a way we like - SimpleModule simpleModule = new SimpleModule(); - simpleModule.addSerializer(Link.class, new LinkSerializer()); - environment.getObjectMapper().registerModule(simpleModule); - - // Setup DCAE controller client - // Used by the dcae-services API - 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 - 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()); - environment.jersey().register(new DcaeServicesGroupbyApi()); - - // https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 - environment.jersey().register(new ApiListingResource()); - environment.jersey().register(new SwaggerSerializers()); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java b/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java deleted file mode 100644 index 55bcca2..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/InventoryConfiguration.java +++ /dev/null @@ -1,173 +0,0 @@ -/*- - * ============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; - -import com.fasterxml.jackson.annotation.JsonProperty; -import io.dropwizard.Configuration; -import io.dropwizard.client.JerseyClientConfiguration; -import io.dropwizard.db.DataSourceFactory; -import org.hibernate.validator.constraints.NotEmpty; - -import javax.validation.Valid; -import javax.validation.constraints.NotNull; - -/** - * Created by mhwang on 4/11/16. - */ -public class InventoryConfiguration extends Configuration { - - public static class DCAEControllerConnectionConfiguration { - @NotEmpty - @JsonProperty - private String host; - - @NotNull - @JsonProperty - private Integer port; - - @NotEmpty - @JsonProperty - private String basePath; - - @NotEmpty - @JsonProperty - private String user; - - @NotEmpty - @JsonProperty - private String password; - - @JsonProperty - private Boolean required = true; - - public String getHost() { - return this.host; - } - - public Integer getPort() { - return this.port; - } - - public String getBasePath() { - return this.basePath; - } - - public String getUser() { - return this.user; - } - - public String getPassword() { - return this.password; - } - - public Boolean getRequired() { - return this.required; - } - } - - public static class DatabusControllerConnectionConfiguration { - @NotEmpty - @JsonProperty - private String host; - - @NotNull - @JsonProperty - private Integer port; - - @NotNull - @JsonProperty - private String mechId; - - @NotNull - @JsonProperty - private String password; - - @JsonProperty - private Boolean required = true; - - public String getHost() { - return host; - } - - public Integer getPort() { - return port; - } - - public String getMechId() { - return mechId; - } - - public String getPassword() { - return password; - } - - public Boolean getRequired() { - return this.required; - } - } - - @NotEmpty - private String defaultName = "DCAEInventory"; - - @Valid - @NotNull - @JsonProperty - private DataSourceFactory database = new DataSourceFactory(); - - @NotNull - @JsonProperty - private DCAEControllerConnectionConfiguration dcaeControllerConnection = new DCAEControllerConnectionConfiguration(); - - @NotNull - @JsonProperty - private DatabusControllerConnectionConfiguration databusControllerConnection = new DatabusControllerConnectionConfiguration(); - - @NotNull - @JsonProperty - private JerseyClientConfiguration httpClient = new JerseyClientConfiguration(); - - @JsonProperty - public String getDefaultName() { - return defaultName; - } - - @JsonProperty - public void setDefaultName(String name) { - this.defaultName = name; - } - - public DataSourceFactory getDataSourceFactory() { - return this.database; - } - - public DCAEControllerConnectionConfiguration getDcaeControllerConnection() { - return this.dcaeControllerConnection; - } - - public DatabusControllerConnectionConfiguration getDatabusControllerConnection() { - return databusControllerConnection; - } - - public JerseyClientConfiguration getJerseyClientConfiguration() { - return httpClient; - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java b/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java deleted file mode 100644 index b83697f..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/LinkSerializer.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============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; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.SerializerProvider; - -import javax.ws.rs.core.Link; -import java.io.IOException; - -/** - * Lifted from - * http://stackoverflow.com/questions/26989004/how-to-serialize-declarative-links-jersey-with-jackson - * - * Created by mhwang on 5/10/16. - */ -public class LinkSerializer extends JsonSerializer { - - @Override - public void serialize(Link link, JsonGenerator jg, SerializerProvider sp) throws IOException { - jg.writeStartObject(); - - if (link.getTitle() != null) { - jg.writeStringField("title", link.getTitle()); - } - - jg.writeStringField("rel", link.getRel()); - jg.writeStringField("href", link.getUri().toString()); - jg.writeEndObject(); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java b/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java deleted file mode 100644 index adbe8b7..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/clients/DCAEControllerClient.java +++ /dev/null @@ -1,222 +0,0 @@ -/*- - * ============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.clients; - -import org.openecomp.dcae.inventory.InventoryConfiguration; -import org.openecomp.dcae.inventory.exceptions.DCAEControllerClientException; -import org.openecomp.dcae.inventory.exceptions.DCAEControllerConnectionException; -import org.openecomp.dcae.inventory.exceptions.DCAEControllerTimeoutException; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.ws.rs.ProcessingException; -import javax.ws.rs.client.Client; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; - -import static org.glassfish.jersey.client.authentication.HttpAuthenticationFeature.*; - -/** - * Created by mhwang on 5/12/16. - */ -public class DCAEControllerClient { - - /** - * Used for JSON objects of the form: - * "hostService": {"$ref": "/services/vm-docker-host-2/instances/mtl2"} - */ - public static class Reference { - - @JsonProperty("$ref") - private String ref; - - public String getRef() { - return ref; - } - - public void setRef(String ref) { - this.ref = ref; - } - - } - - /** - * Used for the JSON objects returned from /services/{service id}/instances/{instance id} - * calls. - */ - @JsonIgnoreProperties(ignoreUnknown = true) - public static class ServiceInstance { - - @JsonProperty("status") - private String status; - - /** - * Property points to the Docker host this Docker container runs on. - * This service instance is an application to be run as a Docker container if this value is not null. - */ - @JsonProperty("hostService") - @JsonInclude(JsonInclude.Include.NON_ABSENT) - private Reference hostService; - - /** - * Property points to the CDAP cluster this CDAP application is deployed on. - * This service instance is a CDAP application if this value is not null. - */ - @JsonProperty("clusterService") - @JsonInclude(JsonInclude.Include.NON_ABSENT) - private Reference clusterService; - - /** - * Property points to the location resource that this instance is associated with. - * This property is not null when the service instance is a "pure" VM. - */ - @JsonProperty("location") - @JsonInclude(JsonInclude.Include.NON_ABSENT) - private Reference location; - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public Reference getHostService() { - return hostService; - } - - public void setHostService(Reference hostService) { - this.hostService = hostService; - } - - public Reference getClusterService() { - return clusterService; - } - - public void setClusterService(Reference clusterService) { - this.clusterService = clusterService; - } - - public Reference getLocation() { - return location; - } - - public void setLocation(Reference location) { - this.location = location; - } - - } - - private final static Logger LOG = LoggerFactory.getLogger(DCAEControllerClient.class); - - private final Client client; - private final InventoryConfiguration.DCAEControllerConnectionConfiguration connectionConfiguration; - - public URI constructResourceURI(String resourcePath) { - // TODO: Better way to construct this? - - // Make sure that the resource path has a "/" because the UriBuilder sucks and doesn't do it for us. - if (resourcePath.charAt(0) != '/') { - resourcePath = (new StringBuilder("/")).append(resourcePath).toString(); - } - - StringBuilder actualPath = new StringBuilder("/"); - actualPath.append(this.connectionConfiguration.getBasePath()); - actualPath.append(resourcePath); - - return UriBuilder.fromPath(actualPath.toString()).scheme("http").host(this.connectionConfiguration.getHost()) - .port(this.connectionConfiguration.getPort()).build(); - } - - public ServiceInstance getServiceInstance(String componentId) throws DCAEControllerClientException { - URI uri = constructResourceURI(componentId); - Response response = null; - - try { - response = client.target(uri).request(MediaType.APPLICATION_JSON_TYPE) - .header("Content-Type", "application/json") - .property(HTTP_AUTHENTICATION_BASIC_USERNAME, connectionConfiguration.getUser()) - .property(HTTP_AUTHENTICATION_BASIC_PASSWORD, connectionConfiguration.getPassword()).get(); - } catch (ProcessingException e) { - // 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 :9998 - String message = "Connecting with DCAE controller probably timed out"; - LOG.error(message, e); - String exceptionMessage = String.format("%s: %s", message, e.getMessage()); - throw new DCAEControllerTimeoutException(exceptionMessage); - } catch (Exception e) { - String message = "Unexpected connection issue with DCAE controller"; - LOG.error(message, e); - String exceptionMessage = String.format("%s: %s", message, e.getMessage()); - throw new DCAEControllerConnectionException(exceptionMessage); - } - - if (LOG.isDebugEnabled()) { - LOG.debug(String.format("Received response from DCAE controller: %d", response.getStatus())); - } - - if (response.getStatus() == 200) { - ObjectMapper om = new ObjectMapper(); - - try { - return om.readValue((InputStream) response.getEntity(), ServiceInstance.class); - } catch (IOException e) { - throw new DCAEControllerClientException(e); - } - } - - throw new DCAEControllerClientException(String.format("Unexpected error from DCAE controller: %d", response.getStatus())); - } - - public String getLocation(ServiceInstance serviceInstance) { - if (serviceInstance.getLocation() != null) { - return serviceInstance.getLocation().getRef(); - } else if (serviceInstance.getClusterService() != null) { - // Drill down: Location is on the underlying CDAP cluster service instance - String cdapClusterRef = serviceInstance.getClusterService().getRef(); - return getLocation(getServiceInstance(cdapClusterRef)); - } else if (serviceInstance.getHostService() != null) { - // Drill down: Location is on the underlying Docker host service instance - String dockerHostRef = serviceInstance.getHostService().getRef(); - return getLocation(getServiceInstance(dockerHostRef)); - } else { - throw new DCAEControllerClientException("No valid location for service instance"); - } - } - - public DCAEControllerClient(Client client, - InventoryConfiguration.DCAEControllerConnectionConfiguration connectionConfiguration) { - this.client = client; - this.connectionConfiguration = connectionConfiguration; - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java b/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java deleted file mode 100644 index 9df11d5..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/clients/DatabusControllerClient.java +++ /dev/null @@ -1,91 +0,0 @@ -/*- - * ============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.clients; - -import org.openecomp.dcae.inventory.InventoryConfiguration; -import org.openecomp.dcae.inventory.exceptions.DatabusControllerClientException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import javax.ws.rs.client.Client; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.util.Map; - -/** - * Created by mhwang on 5/25/16. - */ -public class DatabusControllerClient { - - private final Client client; - private final InventoryConfiguration.DatabusControllerConnectionConfiguration connectionConfiguration; - - public URI constructResourceURI(String resourcePath) { - // Make sure that the resource path has a "/" because the UriBuilder sucks and doesn't do it for us. - if (resourcePath.charAt(0) != '/') { - resourcePath = (new StringBuilder("/")).append(resourcePath).toString(); - } - - return UriBuilder.fromPath(resourcePath.toString()).scheme("https").host(this.connectionConfiguration.getHost()) - .port(this.connectionConfiguration.getPort()).build(); - } - - // TODO: Actually model the JSON objects so that they can be returned to be used for providing useful information - // and thus change this to a "get". - public boolean isExists(String componentId) throws DatabusControllerClientException { - URI uri = constructResourceURI(componentId); - - Response response = client.target(uri).request(MediaType.APPLICATION_JSON_TYPE) - .header("Content-Type", "application/json").get(); - - if (response.getStatus() == 200) { - ObjectMapper om = new ObjectMapper(); - - try { - Map entity = om.readValue((InputStream) response.getEntity(), - new TypeReference>() {}); - - return (entity != null && entity.size() > 0) ? true : false; - } 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", - response.getStatus())); - } - - public DatabusControllerClient(Client client, - InventoryConfiguration.DatabusControllerConnectionConfiguration connectionConfiguration) { - this.client = client; - this.connectionConfiguration = connectionConfiguration; - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java deleted file mode 100644 index 9a674c9..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceComponentsDAO.java +++ /dev/null @@ -1,62 +0,0 @@ -/*- - * ============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.daos; - -import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceComponentObjectMapper; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; -import org.skife.jdbi.v2.sqlobject.Bind; -import org.skife.jdbi.v2.sqlobject.BindBean; -import org.skife.jdbi.v2.sqlobject.SqlQuery; -import org.skife.jdbi.v2.sqlobject.SqlUpdate; -import org.skife.jdbi.v2.sqlobject.customizers.Mapper; - -import java.util.List; - -/** - * Created by mhwang on 4/19/16. - */ -public interface DCAEServiceComponentsDAO extends InventoryDAO { - - @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_service_components\')") - Boolean checkIfTableExists(); - - @SqlUpdate("create table dcae_service_components (component_id varchar not null primary key, component_type varchar not null, " + - "component_source varchar not null, shareable integer default 0, created timestamp not null, modified timestamp not null)") - void createTable(); - - @SqlUpdate("insert into dcae_service_components (component_id, component_type, component_source, shareable, created, modified) " + - "values (:componentId, :componentType, :componentSource, :shareable, :created, :modified)") - void insert(@BindBean DCAEServiceComponentObject componentObject); - - @SqlUpdate("update dcae_service_components set component_type = :componentType, component_source = :componentSource, " + - "shareable = :shareable, modified = :modified where component_id = :componentId") - void update(@BindBean DCAEServiceComponentObject componentObject); - - @Mapper(DCAEServiceComponentObjectMapper.class) - @SqlQuery("select c.* from dcae_services_components_maps m join dcae_service_components c " + - "on m.component_id = c.component_id where m.service_id = :it") - List getByServiceId(@Bind String serviceId); - - @Mapper(DCAEServiceComponentObjectMapper.class) - @SqlQuery("select c.* from dcae_service_components c where c.component_id = :it") - DCAEServiceComponentObject getByComponentId(@Bind String componentId); - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java deleted file mode 100644 index c9d6dc8..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java +++ /dev/null @@ -1,158 +0,0 @@ -/*- - * ============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.daos; - -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; -import org.joda.time.DateTime; -import org.skife.jdbi.v2.sqlobject.CreateSqlObject; -import org.skife.jdbi.v2.sqlobject.Transaction; - -import java.util.ArrayList; -import java.util.List; - -/** - * This is based-off of this blog post. - * http://manikandan-k.github.io/2015/05/10/Transactions_in_jdbi.html - * - * Created by mhwang on 4/21/16. - */ -public abstract class DCAEServiceTransactionDAO { - - public static class DCAEServiceTransactionContext { - - private final String serviceId; - private final DateTime modified; - private DCAEServiceObject serviceObjectToInsert; - private DCAEServiceObject serviceObjectToUpdate; - private List componentObjectsToInsert; - private List componentObjectsToUpdate; - private List mappingsToInsert; - private List mappingsToDelete; - - public String getServiceId() { - return serviceId; - } - - public DateTime getModified() { - return modified; - } - - public DCAEServiceObject getServiceObjectToInsert() { - return serviceObjectToInsert; - } - - public void setServiceObjectToInsert(DCAEServiceObject serviceObjectToInsert) { - this.serviceObjectToInsert = serviceObjectToInsert; - } - - public DCAEServiceObject getServiceObjectToUpdate() { - return serviceObjectToUpdate; - } - - public void setServiceObjectToUpdate(DCAEServiceObject serviceObjectToUpdate) { - this.serviceObjectToUpdate = serviceObjectToUpdate; - } - - public List getComponentObjectsToInsert() { - return componentObjectsToInsert; - } - - public List addComponentObjectToInsert(DCAEServiceComponentObject componentObject) { - this.componentObjectsToInsert.add(componentObject); - return this.componentObjectsToInsert; - } - - public List getComponentObjectsToUpdate() { - return componentObjectsToUpdate; - } - - public List addComponentObjectToUpdate(DCAEServiceComponentObject componentObject) { - this.componentObjectsToUpdate.add(componentObject); - return this.componentObjectsToUpdate; - } - - public List getMappingsToInsert() { - return mappingsToInsert; - } - - public List addMappingsToInsert(String componentId) { - this.mappingsToInsert.add(componentId); - return this.mappingsToInsert; - } - - public List getMappingsToDelete() { - return mappingsToDelete; - } - - public List addMappingsToDelete(String componentId) { - this.mappingsToDelete.add(componentId); - return this.mappingsToDelete; - } - - public DCAEServiceTransactionContext(String serviceId, DateTime modified) { - this.serviceId = serviceId; - this.modified = modified; - this.componentObjectsToInsert = new ArrayList<>(); - this.componentObjectsToUpdate = new ArrayList<>(); - this.mappingsToInsert = new ArrayList<>(); - this.mappingsToDelete = new ArrayList<>(); - } - - } - - @CreateSqlObject - abstract public DCAEServicesDAO getServicesDAO(); - - @CreateSqlObject - abstract public DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO(); - - @CreateSqlObject - abstract public DCAEServiceComponentsDAO getComponentsDAO(); - - @Transaction - public void insert(DCAEServiceTransactionContext context) { - if (context.getServiceObjectToInsert() != null) { - this.getServicesDAO().insert(context.getServiceObjectToInsert()); - } - - if (context.getServiceObjectToUpdate() != null) { - this.getServicesDAO().update(context.getServiceObjectToUpdate()); - } - - for (DCAEServiceComponentObject sco : context.getComponentObjectsToInsert()) { - this.getComponentsDAO().insert(sco); - } - - for (DCAEServiceComponentObject sco : context.getComponentObjectsToUpdate()) { - this.getComponentsDAO().update(sco); - } - - for (String componentId : context.getMappingsToInsert()) { - this.getServicesComponentsMappingDAO().insert(context.getServiceId(), componentId, context.getModified()); - } - - for (String componentId : context.getMappingsToDelete()) { - this.getServicesComponentsMappingDAO().delete(context.serviceId, componentId); - } - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java deleted file mode 100644 index 50b79b2..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTypesDAO.java +++ /dev/null @@ -1,90 +0,0 @@ -/*- - * ============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.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; -import org.skife.jdbi.v2.sqlobject.SqlUpdate; -import org.skife.jdbi.v2.sqlobject.customizers.Mapper; - -/** - * 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. - */ -public interface DCAEServiceTypesDAO extends InventoryDAO { - - @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_service_types\')") - Boolean checkIfTableExists(); - - /** - * 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_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(); - - // 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); - - @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 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 deleted file mode 100644 index 5bfd3cd..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesComponentsMapsDAO.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============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.daos; - -import org.joda.time.DateTime; -import org.skife.jdbi.v2.sqlobject.Bind; -import org.skife.jdbi.v2.sqlobject.SqlQuery; -import org.skife.jdbi.v2.sqlobject.SqlUpdate; - -/** - * This class manages the joining table called "dcae_services_components_maps" which maps the many-to-many relationship - * between DCAE services and DCAE service components. - * - * Created by mhwang on 4/19/16. - */ -public interface DCAEServicesComponentsMapsDAO extends InventoryDAO { - - @SqlQuery("select exists (select * from information_schema.tables where table_name = \'dcae_services_components_maps\')") - Boolean checkIfTableExists(); - - @SqlUpdate("create table dcae_services_components_maps (service_id varchar not null references dcae_services (service_id), " + - "component_id varchar not null references dcae_service_components (component_id), " + - "created timestamp not null, primary key (service_id, component_id))") - void createTable(); - - @SqlUpdate("insert into dcae_services_components_maps (service_id, component_id, created) values (:serviceId, :componentId, :created)") - void insert(@Bind("serviceId") String serviceId, @Bind("componentId") String componentId, @Bind("created") DateTime created); - - @SqlUpdate("delete from dcae_services_components_maps where service_id = :serviceId and component_id = :componentId") - void delete(@Bind("serviceId") String serviceId, @Bind("componentId") String componentId); - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java deleted file mode 100644 index 9200677..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServicesDAO.java +++ /dev/null @@ -1,76 +0,0 @@ -/*- - * ============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.daos; - -import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; -import org.joda.time.DateTime; -import org.skife.jdbi.v2.sqlobject.Bind; -import org.skife.jdbi.v2.sqlobject.BindBean; -import org.skife.jdbi.v2.sqlobject.SqlQuery; -import org.skife.jdbi.v2.sqlobject.SqlUpdate; -import org.skife.jdbi.v2.sqlobject.customizers.Mapper; -import org.skife.jdbi.v2.util.IntegerMapper; - - -/** - * Created by mhwang on 4/19/16. - */ -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_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_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_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); - - @Mapper(DCAEServiceObjectMapper.class) - @SqlQuery("select * from dcae_services where status = :status and service_id = :serviceId") - DCAEServiceObject getByServiceId(@Bind("status") DCAEServiceObject.DCAEServiceStatus status, @Bind("serviceId") String serviceId); - - @Mapper(DCAEServiceObjectMapper.class) - @SqlQuery("select * from dcae_services where service_id = :serviceId") - DCAEServiceObject getByServiceId(@Bind("serviceId") String serviceId); - - @SqlUpdate("update dcae_services set modified = :modified, status = :status where service_id = :serviceId") - void updateStatusByServiceId(@Bind("modified") DateTime modified, - @Bind("status") DCAEServiceObject.DCAEServiceStatus status, - @Bind("serviceId") String serviceId); - - @Mapper(IntegerMapper.class) - @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 deleted file mode 100644 index ef2fb8d..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAO.java +++ /dev/null @@ -1,29 +0,0 @@ -/*- - * ============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.daos; - -/** - * Created by mhwang on 4/19/16. - */ -public interface InventoryDAO { - Boolean checkIfTableExists(); - void createTable(); -} diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java deleted file mode 100644 index 77611b5..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java +++ /dev/null @@ -1,181 +0,0 @@ -/*- - * ============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.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; - -import java.util.Arrays; -import java.util.List; - -/** - * Reluctantly made this into a singleton in order to have access to the DAOs in the request handling code. Didn't - * want to change the interface on the handlers because they are generated by Swagger and I wanted to flexibility - * to swap in changes easily. This meant sacrificing dependency injection which is preferred. - * - * Created by mhwang on 4/19/16. - */ -public final class InventoryDAOManager { - - private static InventoryDAOManager instance; - - public static InventoryDAOManager getInstance() { - if (instance == null) { - instance = new 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 DAO_CLASSES = Arrays.asList(DCAEServiceTypesDAO.class, DCAEServicesDAO.class, - DCAEServiceComponentsDAO.class, DCAEServicesComponentsMapsDAO.class); - - private DBI jdbi; - private Environment environment; - private InventoryConfiguration configuration; - - private InventoryDAOManager() { - } - - /** - * 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 daoClass : DAO_CLASSES) { - final InventoryDAO dao = jdbi.onDemand(daoClass); - - if (dao.checkIfTableExists()) { - LOG.info(String.format("Sql table exists: %s", daoClass.getSimpleName())); - } else { - dao.createTable(); - 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 klass) { - if (jdbi == null) { - throw new RuntimeException("InventoryDAOManager has not been initialized!"); - } - - // Using this approach to constructing the DAO, the client is not responsible for closing the handle. - // http://jdbi.org/sql_object_overview/ - // > In this case we do not need to (and in fact shouldn’t) ever take action to close the handle the sql object uses. - return jdbi.onDemand(klass); - } - - public DCAEServicesDAO getDCAEServicesDAO() { - return (DCAEServicesDAO) this.getDAO(DCAEServicesDAO.class); - } - - public DCAEServiceComponentsDAO getDCAEServiceComponentsDAO() { - return (DCAEServiceComponentsDAO) this.getDAO(DCAEServiceComponentsDAO.class); - } - - public DCAEServicesComponentsMapsDAO getDCAEServicesComponentsDAO() { - return (DCAEServicesComponentsMapsDAO) this.getDAO(DCAEServicesComponentsMapsDAO.class); - } - - public DCAEServiceTransactionDAO getDCAEServiceTransactionDAO() { - return jdbi.onDemand(DCAEServiceTransactionDAO.class); - } - - public DCAEServiceTypesDAO getDCAEServiceTypesDAO() { - return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class); - } - - /** - * Must close the handle that is returned here. It is AutoCloseable so just use it as a try-with-resource. - * - * @return - */ - public Handle getHandle() { - return this.jdbi.open(); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java b/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java deleted file mode 100644 index afd0e78..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/StringListArgument.java +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * ============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.dbthings; - -import org.skife.jdbi.v2.StatementContext; -import org.skife.jdbi.v2.tweak.Argument; -import org.skife.jdbi.v2.tweak.ArgumentFactory; - -import java.sql.Array; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.List; - -/** - * This class if needed for jdbi queries to be able to properly bind List objects. - * - * http://stackoverflow.com/questions/33062516/insert-2d-array-into-postresql-db-using-jdbi - * - * Created by mhwang on 5/4/16. - */ -public class StringListArgument implements ArgumentFactory> { - - @Override - public boolean accepts(Class expectedType, Object value, StatementContext statementContext) { - return value != null && List.class.isAssignableFrom(value.getClass()); - } - - @Override - public Argument build(Class expectedType, List value, StatementContext statementContext) { - return new Argument() { - @Override - public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { - Array values = statement.getConnection().createArrayOf("varchar", value.toArray()); - statement.setArray(position, values); - } - }; - } - -} 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 deleted file mode 100644 index 23b42b1..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapper.java +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * ============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.dbthings.mappers; - -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; -import org.joda.time.DateTime; -import org.skife.jdbi.v2.StatementContext; -import org.skife.jdbi.v2.tweak.ResultSetMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * Created by mhwang on 4/19/16. - */ -public class DCAEServiceComponentObjectMapper implements ResultSetMapper { - - @Override - public DCAEServiceComponentObject map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { - DCAEServiceComponentObject object = new DCAEServiceComponentObject(); - object.setComponentId(resultSet.getString("component_id")); - object.setComponentType(resultSet.getString("component_type")); - object.setComponentSource(resultSet.getString("component_source")); - object.setShareable(resultSet.getInt("shareable")); - object.setCreated(new DateTime(resultSet.getTimestamp("created"))); - object.setModified(new DateTime(resultSet.getTimestamp("modified"))); - return object; - } - -} 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 deleted file mode 100644 index 27cb22f..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapper.java +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * ============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.dbthings.mappers; - -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; -import org.joda.time.DateTime; -import org.skife.jdbi.v2.StatementContext; -import org.skife.jdbi.v2.tweak.ResultSetMapper; - -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * Created by mhwang on 4/19/16. - */ -public class DCAEServiceObjectMapper implements ResultSetMapper { - - @Override - public DCAEServiceObject map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException { - DCAEServiceObject object = new DCAEServiceObject(); - object.setServiceId(resultSet.getString("service_id")); - object.setTypeId(resultSet.getString("type_id")); - object.setVnfId(resultSet.getString("vnf_id")); - object.setVnfType(resultSet.getString("vnf_type")); - object.setVnfLocation(resultSet.getString("vnf_location")); - object.setDeploymentRef(resultSet.getString("deployment_ref")); - object.setCreated(new DateTime(resultSet.getTimestamp("created"))); - object.setModified(new DateTime(resultSet.getTimestamp("modified"))); - object.setStatus(DCAEServiceObject.DCAEServiceStatus.valueOf(resultSet.getString("status"))); - return object; - } - -} 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 deleted file mode 100644 index dc64423..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapper.java +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * ============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.dbthings.mappers; - -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; -import org.joda.time.DateTime; -import org.skife.jdbi.v2.StatementContext; -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; - -/** - * Created by mhwang on 5/3/16. - */ -public class DCAEServiceTypeObjectMapper implements ResultSetMapper { - - @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)); - - Array serviceIdsArray = resultSet.getArray("service_ids"); - - if (serviceIdsArray != null) { - String[] serviceIds = (String[]) serviceIdsArray.getArray(); - object.setServiceIds(Arrays.asList(serviceIds)); - } - - Array serviceLocationsArray = resultSet.getArray("service_locations"); - - if (serviceLocationsArray != null) { - String[] serviceLocations = (String[]) serviceLocationsArray.getArray(); - object.setServiceLocations(Arrays.asList(serviceLocations)); - } - - 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 deleted file mode 100644 index 0ec9c19..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObject.java +++ /dev/null @@ -1,146 +0,0 @@ -/*- - * ============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.dbthings.models; - -import io.swagger.model.DCAEServiceComponentRequest; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; - -/** - * Created by mhwang on 4/19/16. - */ -public class DCAEServiceComponentObject { - - private String componentId = null; - private DateTime created = null; - private DateTime modified = null; - private String componentType = null; - private String componentSource = null; - private Integer shareable = null; - - public String getComponentId() { - return componentId; - } - - public void setComponentId(String componentId) { - this.componentId = componentId; - } - - public DateTime getCreated() { - return created; - } - - public void setCreated(DateTime created) { - this.created = created; - } - - public DateTime getModified() { - return modified; - } - - public void setModified(DateTime modified) { - this.modified = modified; - } - - public String getComponentType() { - return componentType; - } - - public void setComponentType(String componentType) { - this.componentType = componentType; - } - - public String getComponentSource() { - return componentSource; - } - - public void setComponentSource(String componentSource) { - this.componentSource = componentSource; - } - - public Integer getShareable() { - return shareable; - } - - public void setShareable(Integer shareable) { - this.shareable = shareable; - } - - public DCAEServiceComponentObject() { - } - - /** - * Intended to be used for inserts - new objects. - * - * @param request - */ - public DCAEServiceComponentObject(DCAEServiceComponentRequest request) { - DateTime now = DateTime.now(DateTimeZone.UTC); - this.setComponentId(request.getComponentId()); - this.setComponentType(request.getComponentType()); - this.setComponentSource(request.getComponentSource()); - this.setCreated(now); - this.setModified(now); - this.setShareable(request.getShareable()); - } - - /** - * Intended to be used for updates - some fields should not be updated. - * - * @param source - * @param updateRequest - */ - public DCAEServiceComponentObject(DCAEServiceComponentObject source, DCAEServiceComponentRequest updateRequest) { - // Immutable fields - this.setComponentId(source.getComponentId()); - this.setCreated(source.getCreated()); - - // Mutable fields - this.setComponentType(updateRequest.getComponentType()); - this.setComponentSource(updateRequest.getComponentSource()); - this.setShareable(updateRequest.getShareable()); - this.setModified(DateTime.now(DateTimeZone.UTC)); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class DCAEServiceComponentObject {\n"); - - sb.append(" componentId: ").append(toIndentedString(componentId)).append("\n"); - sb.append(" componentType: ").append(toIndentedString(componentType)).append("\n"); - sb.append(" componentSource: ").append(toIndentedString(componentSource)).append("\n"); - sb.append(" shareable: ").append(toIndentedString(shareable)).append("\n"); - sb.append("}"); - return sb.toString(); - } - - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ - private String toIndentedString(Object o) { - if (o == null) { - return "null"; - } - return o.toString().replace("\n", "\n "); - } -} 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 deleted file mode 100644 index 4374acd..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObject.java +++ /dev/null @@ -1,168 +0,0 @@ -/*- - * ============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.dbthings.models; - -import io.swagger.model.DCAEServiceRequest; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; - -/** - * Created by mhwang on 4/19/16. - */ -public class DCAEServiceObject { - - public static enum DCAEServiceStatus { - RUNNING, - REMOVED - } - - private String serviceId = null; - private String typeId = null; - private DateTime created = null; - private DateTime modified = null; - private String vnfId = null; - private String vnfType = null; - private String vnfLocation = null; - private String deploymentRef = null; - - // These properties are meant to be used internally in the service only - private DCAEServiceStatus status = null; - - public String getServiceId() { - return serviceId; - } - - public void setServiceId(String serviceId) { - this.serviceId = serviceId; - } - - public String getTypeId() { - return typeId; - } - - public void setTypeId(String typeId) { - this.typeId = typeId; - } - - public DateTime getCreated() { - return created; - } - - public void setCreated(DateTime created) { - this.created = created; - } - - public DateTime getModified() { - return modified; - } - - public void setModified(DateTime modified) { - this.modified = modified; - } - - public DCAEServiceStatus getStatus() { - return status; - } - - public void setStatus(DCAEServiceStatus status) { - this.status = status; - } - - public String getVnfId() { - return vnfId; - } - - public void setVnfId(String vnfId) { - this.vnfId = vnfId; - } - - public String getVnfType() { - return vnfType; - } - - public void setVnfType(String vnfType) { - this.vnfType = vnfType; - } - - public String getVnfLocation() { - return vnfLocation; - } - - public void setVnfLocation(String vnfLocation) { - this.vnfLocation = vnfLocation; - } - - public String getDeploymentRef() { - return deploymentRef; - } - - public void setDeploymentRef(String deploymentRef) { - this.deploymentRef = deploymentRef; - } - - public DCAEServiceObject() { - } - - // TODO: Move the constructors functionality below out into the actual handlers. - - /** - * Intended to be used for inserts - new objects. - * - * @param serviceId - * @param request - */ - public DCAEServiceObject(String serviceId, DCAEServiceRequest request) { - DateTime now = DateTime.now(DateTimeZone.UTC); - this.setServiceId(serviceId); - this.setTypeId(request.getTypeId()); - this.setVnfId(request.getVnfId()); - this.setVnfType(request.getVnfType()); - this.setVnfLocation(request.getVnfLocation()); - this.setDeploymentRef(request.getDeploymentRef()); - this.setCreated(now); - this.setModified(now); - // Assumption here is that you are here from the PUT which means that the service is RUNNING. - this.setStatus(DCAEServiceStatus.RUNNING); - } - - /** - * Intended to be used for updates - some fields should not be updated. - * - * @param source - * @param updateRequest - */ - public DCAEServiceObject(DCAEServiceObject source, DCAEServiceRequest updateRequest) { - // Immutable fields - this.setServiceId(source.getServiceId()); - this.setCreated(source.getCreated()); - - // Mutable fields - this.setTypeId(updateRequest.getTypeId()); - this.setVnfId(updateRequest.getVnfId()); - this.setVnfType(updateRequest.getVnfType()); - this.setVnfLocation(updateRequest.getVnfLocation()); - this.setDeploymentRef(updateRequest.getDeploymentRef()); - this.setModified(DateTime.now(DateTimeZone.UTC)); - // Assumption here is that you are here from the PUT which means that the service is RUNNING. - this.setStatus(DCAEServiceStatus.RUNNING); - } - -} 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 deleted file mode 100644 index b57a28f..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceTypeObject.java +++ /dev/null @@ -1,142 +0,0 @@ -/*- - * ============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.dbthings.models; - -import org.joda.time.DateTime; - -import java.util.List; - -/** - * POJO representation of a record in dcae_service_types table. - * - * Created by mhwang on 5/3/16. - */ -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 vnfTypes = null; - private List serviceIds = null; - private List serviceLocations = 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; - } - - public void setTypeName(String typeName) { - 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; - } - - public void setOwner(String owner) { - this.owner = owner; - } - - public List getVnfTypes() { - return vnfTypes; - } - - public void setVnfTypes(List vnfTypes) { - this.vnfTypes = vnfTypes; - } - - public List getServiceIds() { - return serviceIds; - } - - public void setServiceIds(List serviceIds) { - this.serviceIds = serviceIds; - } - - public List getServiceLocations() { - return serviceLocations; - } - - public void setServiceLocations(List serviceLocations) { - this.serviceLocations = serviceLocations; - } - - public String getAsdcServiceId() { - return asdcServiceId; - } - - public void setAsdcServiceId(String asdcServiceId) { - this.asdcServiceId = asdcServiceId; - } - - public String getAsdcResourceId() { - return asdcResourceId; - } - - public void setAsdcResourceId(String asdcResourceId) { - this.asdcResourceId = asdcResourceId; - } - - public DateTime getCreated() { - return created; - } - - public void setCreated(DateTime created) { - 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 deleted file mode 100644 index eb30ae7..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerClientException.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============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; - -/** - * Created by mhwang on 5/13/16. - */ -public class DCAEControllerClientException extends RuntimeException { - - public DCAEControllerClientException(String message) { - super(message); - } - - public DCAEControllerClientException(Throwable e) { - super(e); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java deleted file mode 100644 index 2b9e6e7..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerConnectionException.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============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; - -/** - * Created by mhwang on 8/23/16. - */ -public class DCAEControllerConnectionException extends RuntimeException { - - public DCAEControllerConnectionException(String message) { - super(message); - } - - public DCAEControllerConnectionException(Throwable e) { - super(e); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java deleted file mode 100644 index 8aa9255..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DCAEControllerTimeoutException.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============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; - -/** - * Created by mhwang on 8/23/16. - */ -public class DCAEControllerTimeoutException extends RuntimeException { - - public DCAEControllerTimeoutException(String message) { - super(message); - } - - public DCAEControllerTimeoutException(Throwable e) { - super(e); - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java b/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java deleted file mode 100644 index 6209521..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/DatabusControllerClientException.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============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; - -/** - * Created by mhwang on 5/25/16. - */ -public class DatabusControllerClientException extends RuntimeException { - - public DatabusControllerClientException(String message) { - super(message); - } - - public DatabusControllerClientException(Throwable e) { - super(e); - } - -} 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 deleted file mode 100644 index 5dce2dd..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DBIExceptionMapper.java +++ /dev/null @@ -1,82 +0,0 @@ -/*- - * ============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 implements ExceptionMapper { - - 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 { - } - - public final static class UnableToCreateStatementExceptionMapper extends DBIExceptionMapper { - } - - public final static class UnableToExecuteStatementExceptionMapper extends DBIExceptionMapper { - } - -} 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 deleted file mode 100644 index 76936e0..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerConnectionExceptionMapper.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============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.exceptions.DCAEControllerConnectionException; - -import javax.ws.rs.core.Response; - -/** - * Created by mhwang on 8/23/16. - */ -public class DCAEControllerConnectionExceptionMapper extends DCAEControllerExceptionMapper { - - @Override - protected Response.Status getStatus() { - return Response.Status.BAD_GATEWAY; - } - -} 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 deleted file mode 100644 index b9ffda0..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerExceptionMapper.java +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * ============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 io.swagger.api.ApiResponseMessage; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; - -/** - * Created by mhwang on 8/23/16. - */ -public abstract class DCAEControllerExceptionMapper implements ExceptionMapper { - - abstract protected Response.Status getStatus(); - - @Override - public Response toResponse(T e) { - ApiResponseMessage response = new ApiResponseMessage(ApiResponseMessage.ERROR, e.getMessage()); - return Response.status(this.getStatus()).entity(response).build(); - } - -} 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 deleted file mode 100644 index 9939aee..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/exceptions/mappers/DCAEControllerTimeoutExceptionMapper.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============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.exceptions.DCAEControllerTimeoutException; - -import javax.ws.rs.core.Response; - -/** - * Created by mhwang on 8/23/16. - */ -public class DCAEControllerTimeoutExceptionMapper extends DCAEControllerExceptionMapper { - - @Override - protected Response.Status getStatus() { - return Response.Status.GATEWAY_TIMEOUT; - } - -} diff --git a/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java b/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java deleted file mode 100644 index 0f54b46..0000000 --- a/src/main/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapper.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============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.providers; - -import io.swagger.api.ApiResponseMessage; -import io.swagger.api.NotFoundException; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; - -/** - * Created by mhwang on 5/18/16. - */ -public class NotFoundExceptionMapper implements ExceptionMapper { - - @Override - public Response toResponse(NotFoundException e) { - ApiResponseMessage entity = new ApiResponseMessage(e.getCode(), e.getMessage()); - return Response.status(Response.Status.NOT_FOUND).entity(entity).build(); - } - -} diff --git a/src/test/java/DcaeServiceTypesApiServiceImplTests.java b/src/test/java/DcaeServiceTypesApiServiceImplTests.java index c4ada1f..3882c53 100644 --- a/src/test/java/DcaeServiceTypesApiServiceImplTests.java +++ b/src/test/java/DcaeServiceTypesApiServiceImplTests.java @@ -18,11 +18,11 @@ * ============LICENSE_END========================================================= */ -import org.openecomp.dcae.inventory.daos.DCAEServiceTypesDAO; -import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; -import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; +import org.onap.dcae.inventory.daos.DCAEServiceTypesDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject; import io.swagger.api.impl.DcaeServiceTypesApiServiceImpl; import io.swagger.model.DCAEServiceType; import io.swagger.model.DCAEServiceTypeRequest; diff --git a/src/test/java/DcaeServicesApiServiceImplTests.java b/src/test/java/DcaeServicesApiServiceImplTests.java index 50d6e5b..75e3e00 100644 --- a/src/test/java/DcaeServicesApiServiceImplTests.java +++ b/src/test/java/DcaeServicesApiServiceImplTests.java @@ -21,22 +21,18 @@ import io.swagger.api.NotFoundException; import io.swagger.api.impl.DcaeServicesApiServiceImpl; import io.swagger.model.DCAEService; -import io.swagger.model.DCAEServiceComponent; import io.swagger.model.DCAEServiceRequest; -import org.joda.time.DateTime; import org.junit.Before; import org.junit.Test; -import org.junit.internal.runners.statements.Fail; import org.junit.runner.RunWith; -import org.openecomp.dcae.inventory.clients.DCAEControllerClient; -import org.openecomp.dcae.inventory.clients.DatabusControllerClient; -import org.openecomp.dcae.inventory.daos.DCAEServiceComponentsDAO; -import org.openecomp.dcae.inventory.daos.DCAEServiceTypesDAO; -import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; -import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.dbthings.mappers.DCAEServiceObjectMapper; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceComponentObject; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.clients.DCAEControllerClient; +import org.onap.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.daos.DCAEServiceComponentsDAO; +import org.onap.dcae.inventory.daos.DCAEServiceTypesDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceComponentObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -44,15 +40,10 @@ import org.powermock.modules.junit4.PowerMockRunner; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; -import org.skife.jdbi.v2.Foreman; -import org.skife.jdbi.v2.Handle; -import org.skife.jdbi.v2.Query; -import org.skife.jdbi.v2.tweak.Argument; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; import java.lang.reflect.Method; import java.util.ArrayList; diff --git a/src/test/java/DcaeServicesGroupbyApiServiceImplTests.java b/src/test/java/DcaeServicesGroupbyApiServiceImplTests.java index ffe7cb2..7ca8200 100644 --- a/src/test/java/DcaeServicesGroupbyApiServiceImplTests.java +++ b/src/test/java/DcaeServicesGroupbyApiServiceImplTests.java @@ -22,14 +22,10 @@ import io.swagger.api.impl.DcaeServicesGroupbyApiServiceImpl; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.stubbing.OngoingStubbing; -import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.daos.InventoryDAOManager; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.skife.jdbi.v2.Handle; -import org.skife.jdbi.v2.Query; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; diff --git a/src/test/java/org/onap/dcae/inventory/daos/InventoryDAOManagerTests.java b/src/test/java/org/onap/dcae/inventory/daos/InventoryDAOManagerTests.java new file mode 100644 index 0000000..8346372 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/daos/InventoryDAOManagerTests.java @@ -0,0 +1,44 @@ +/*- + * ============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.onap.dcae.inventory.daos; + +import org.onap.dcae.inventory.InventoryConfiguration; +import io.dropwizard.setup.Environment; +import org.junit.Test; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 3/8/17. + */ +public class InventoryDAOManagerTests { + + @Test(expected=InventoryDAOManager.InventoryDAOManagerSetupException.class) + public void testInitializeStrictness() { + InventoryDAOManager daoManager = InventoryDAOManager.getInstance(); + Environment environment = mock(Environment.class); + InventoryConfiguration configuration = mock(InventoryConfiguration.class); + // This should be ok + daoManager.setup(environment, configuration); + // Cannot do another call + daoManager.setup(environment, configuration); + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java b/src/test/java/org/onap/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java new file mode 100644 index 0000000..c0ad9b8 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java @@ -0,0 +1,85 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.daos; + +import org.joda.time.DateTime; +import org.junit.Test; +import org.onap.dcae.inventory.daos.DCAEServiceComponentsDAO; +import org.onap.dcae.inventory.daos.DCAEServiceTransactionDAO; +import org.onap.dcae.inventory.daos.DCAEServicesComponentsMapsDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceTransactionDAOTests { + + @Test + public void testCreateDCAEServiceTransactionContext() { + String serviceId = "service-foo"; + DateTime modifiedTime = new DateTime(); + DCAEServiceTransactionDAO.DCAEServiceTransactionContext context + = new DCAEServiceTransactionDAO.DCAEServiceTransactionContext(serviceId, modifiedTime); + + assertEquals(context.getServiceId(), serviceId); + assertEquals(context.getModified(), modifiedTime); + } + + @Test + public void testInsert() { + String serviceId = "service-foo"; + DateTime modifiedTime = new DateTime(); + DCAEServiceTransactionDAO.DCAEServiceTransactionContext context + = new DCAEServiceTransactionDAO.DCAEServiceTransactionContext(serviceId, modifiedTime); + + DCAEServicesDAO servicesDAO = mock(DCAEServicesDAO.class); + DCAEServicesComponentsMapsDAO componentsMapsDAO = mock(DCAEServicesComponentsMapsDAO.class); + DCAEServiceComponentsDAO componentsDAO = mock(DCAEServiceComponentsDAO.class); + + DCAEServiceTransactionDAO transactionDAO = new DCAEServiceTransactionDAO() { + + public DCAEServicesDAO getServicesDAO() { + return servicesDAO; + } + + public DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO() { + return componentsMapsDAO; + } + + public DCAEServiceComponentsDAO getComponentsDAO() { + return componentsDAO; + } + }; + + try { + transactionDAO.insert(context); + assertTrue(true); + } catch(Exception e) { + fail("Unexpected error"); + } + + } +} diff --git a/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java b/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java new file mode 100644 index 0000000..bb511e2 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java @@ -0,0 +1,48 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.mappers; + +import org.junit.Test; + +import java.sql.ResultSet; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceComponentObjectMapperTests { + + @Test + public void testMap() { + ResultSet resultSet = mock(ResultSet.class); + DCAEServiceComponentObjectMapper mapper = new DCAEServiceComponentObjectMapper(); + + try { + assertNotNull(mapper.map(0, resultSet, null)); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java b/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java new file mode 100644 index 0000000..ab150bb --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java @@ -0,0 +1,50 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.mappers; + +import org.junit.Test; + +import java.sql.ResultSet; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceObjectMapperTests { + + @Test + public void testMap() { + ResultSet resultSet = mock(ResultSet.class); + DCAEServiceObjectMapper mapper = new DCAEServiceObjectMapper(); + + try { + when(resultSet.getString("status")).thenReturn("RUNNING"); + assertNotNull(mapper.map(0, resultSet, null)); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java b/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java new file mode 100644 index 0000000..c1caa29 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java @@ -0,0 +1,110 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.mappers; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.sql.Array; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +import static junit.framework.TestCase.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceTypeObjectMapperTests { + + @Test + public void testMap() { + ResultSet resultSet = mock(ResultSet.class); + DCAEServiceTypeObjectMapper mapper = new DCAEServiceTypeObjectMapper(); + + try { + when(resultSet.getArray(anyString())).thenReturn(new Array() { + @Override + public String getBaseTypeName() throws SQLException { + return null; + } + + @Override + public int getBaseType() throws SQLException { + return 0; + } + + @Override + public Object getArray() throws SQLException { + return new String[10]; + } + + @Override + public Object getArray(Map> map) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count, Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { + return null; + } + + @Override + public void free() throws SQLException { + + } + }); + TestCase.assertNotNull(mapper.map(0, resultSet, null)); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java b/src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java new file mode 100644 index 0000000..cb81131 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java @@ -0,0 +1,71 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.models; + +import io.swagger.model.DCAEServiceComponentRequest; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; + +/** + * Created by mhwang on 9/27/17. + */ +public class DCAEServiceComponentObjectTests { + + @Test + public void testConstructionByRequest() { + DCAEServiceComponentRequest request = (new DCAEServiceComponentRequest()).componentId("some-component-id") + .componentType("scary-component-type"); + request.setComponentSource("controller"); + request.setShareable(0); + + DCAEServiceComponentObject object = new DCAEServiceComponentObject(request); + assertEquals(object.getComponentId(), request.getComponentId()); + assertNotNull(object.getCreated()); + assertNotNull(object.getModified()); + } + + @Test + public void testConstructionForUpdating() { + DCAEServiceComponentRequest requestFirst = (new DCAEServiceComponentRequest()).componentId("some-component-id") + .componentType("scary-component-type"); + requestFirst.setComponentSource("controller"); + requestFirst.setShareable(0); + + DCAEServiceComponentObject objectFirst = new DCAEServiceComponentObject(requestFirst); + + DCAEServiceComponentRequest requestSecond = (new DCAEServiceComponentRequest()).componentId("some-other-component-id") + .componentType("happy-component-type"); + requestFirst.setComponentSource("controllerless"); + requestFirst.setShareable(1); + + DCAEServiceComponentObject objectUpdated = new DCAEServiceComponentObject(objectFirst, requestSecond); + assertEquals(objectUpdated.getComponentId(), requestFirst.getComponentId()); + assertEquals(objectUpdated.getCreated(), objectFirst.getCreated()); + assertEquals(objectUpdated.getComponentType(), requestSecond.getComponentType()); + assertEquals(objectUpdated.getComponentSource(), requestSecond.getComponentSource()); + assertEquals(objectUpdated.getShareable(), requestSecond.getShareable()); + assertNotSame(objectUpdated.getModified(), objectFirst.getModified()); + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java b/src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java new file mode 100644 index 0000000..7a39883 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java @@ -0,0 +1,72 @@ +/*- + * ============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.onap.dcae.inventory.dbthings.models; + +import io.swagger.model.DCAEServiceRequest; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Created by mhwang on 9/27/17. + */ +public class DCAEServiceObjectTests { + + @Test + public void testConstructionByRequest() { + DCAEServiceRequest request = new DCAEServiceRequest(); + request.setTypeId("some-type-id"); + request.setVnfType("foo-vnf-type"); + request.setVnfLocation("san-janero"); + + String serviceId = "some-service-id"; + + DCAEServiceObject object = new DCAEServiceObject(serviceId, request); + assertEquals(object.getServiceId(), serviceId); + assertNotNull(object.getCreated()); + assertNotNull(object.getModified()); + } + + @Test + public void testConstructionByUpdating() { + DCAEServiceRequest requestFirst = new DCAEServiceRequest(); + requestFirst.setTypeId("some-type-id"); + requestFirst.setVnfType("foo-vnf-type"); + requestFirst.setVnfLocation("san-janero"); + + String serviceId = "some-service-id"; + + DCAEServiceObject objectFirst = new DCAEServiceObject(serviceId, requestFirst); + + DCAEServiceRequest requestSecond = new DCAEServiceRequest(); + requestFirst.setTypeId("other-type-id"); + requestFirst.setVnfType("bar-vnf-type"); + requestFirst.setVnfLocation("san-junipero"); + + DCAEServiceObject objectUpdated = new DCAEServiceObject(objectFirst, requestSecond); + assertEquals(objectUpdated.getServiceId(), objectFirst.getServiceId()); + assertEquals(objectUpdated.getTypeId(), requestSecond.getTypeId()); + assertEquals(objectUpdated.getVnfType(), requestSecond.getVnfType()); + assertEquals(objectUpdated.getVnfLocation(), requestSecond.getVnfLocation()); + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java b/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java new file mode 100644 index 0000000..7c04295 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java @@ -0,0 +1,117 @@ +/*- + * ============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.onap.dcae.inventory.exception.mappers; + +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.exceptions.mappers.DBIExceptionMapper; +import io.swagger.api.ApiResponseMessage; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.skife.jdbi.v2.exceptions.UnableToCreateStatementException; +import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException; +import org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException; + +import javax.ws.rs.core.Response; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mhwang on 3/8/17. + */ +@PrepareForTest({InventoryDAOManager.class}) +@RunWith(PowerMockRunner.class) +public class DBIExceptionMapperTests { + + @Test + public void testReinitializeSuccess() { + // PowerMockito does bytecode magic to mock static methods and use final classes + PowerMockito.mockStatic(InventoryDAOManager.class); + InventoryDAOManager mockDAOManager = mock(InventoryDAOManager.class); + when(InventoryDAOManager.getInstance()).thenReturn(mockDAOManager); + + RuntimeException fakeException = new RuntimeException("Spoofing database failure"); + + // Test UnableToObtainConnectionExceptionMapper + + DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne = new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper(); + Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException)); + assert responseOne.getStatus() == 502; + String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage(); + assert messageOne.contains("successfully reset"); + + // Test UnableToCreateStatementExceptionMapper + + DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo = new DBIExceptionMapper.UnableToCreateStatementExceptionMapper(); + Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException)); + assert responseTwo.getStatus() == 502; + String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage(); + assert messageTwo.contains("successfully reset"); + + // Test UnableToExecuteStatementExceptionMapper + + DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree = new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper(); + Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException)); + assert responseThree.getStatus() == 502; + String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage(); + assert messageThree.contains("successfully reset"); + } + + @Test + public void testReinitializeFailed() { + // PowerMockito does bytecode magic to mock static methods and use final classes + PowerMockito.mockStatic(InventoryDAOManager.class); + InventoryDAOManager mockDAOManager = mock(InventoryDAOManager.class); + when(InventoryDAOManager.getInstance()).thenReturn(mockDAOManager); + Mockito.doThrow(new RuntimeException("Spoof initialization failure")).when(mockDAOManager).initialize(); + + RuntimeException fakeException = new RuntimeException("Spoofing database failure"); + + // Test UnableToObtainConnectionExceptionMapper + + DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne = new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper(); + Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException)); + assert responseOne.getStatus() == 502; + String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage(); + assert !messageOne.contains("successfully reset"); + + // Test UnableToCreateStatementExceptionMapper + + DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo = new DBIExceptionMapper.UnableToCreateStatementExceptionMapper(); + Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException)); + assert responseTwo.getStatus() == 502; + String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage(); + assert !messageTwo.contains("successfully reset"); + + // Test UnableToExecuteStatementExceptionMapper + + DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree = new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper(); + Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException)); + assert responseThree.getStatus() == 502; + String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage(); + assert !messageThree.contains("successfully reset"); + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapperTests.java b/src/test/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapperTests.java new file mode 100644 index 0000000..8e2b767 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/providers/NotFoundExceptionMapperTests.java @@ -0,0 +1,42 @@ +/*- + * ============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.onap.dcae.inventory.providers; + +import io.swagger.api.NotFoundException; +import org.junit.Test; + +import javax.ws.rs.core.Response; + +import static org.junit.Assert.assertEquals; + +/** + * Created by mhwang on 10/2/17. + */ +public class NotFoundExceptionMapperTests { + + @Test + public void testNotFoundExceptionMapper() { + NotFoundExceptionMapper mapper = new NotFoundExceptionMapper(); + Response response = mapper.toResponse(new NotFoundException(100, "Some error message")); + assertEquals(response.getStatus(), 404); + } + +} diff --git a/src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java b/src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java deleted file mode 100644 index 16e3688..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java +++ /dev/null @@ -1,44 +0,0 @@ -/*- - * ============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.daos; - -import org.openecomp.dcae.inventory.InventoryConfiguration; -import io.dropwizard.setup.Environment; -import org.junit.Test; -import static org.mockito.Mockito.mock; - -/** - * Created by mhwang on 3/8/17. - */ -public class InventoryDAOManagerTests { - - @Test(expected=InventoryDAOManager.InventoryDAOManagerSetupException.class) - public void testInitializeStrictness() { - InventoryDAOManager daoManager = InventoryDAOManager.getInstance(); - Environment environment = mock(Environment.class); - InventoryConfiguration configuration = mock(InventoryConfiguration.class); - // This should be ok - daoManager.setup(environment, configuration); - // Cannot do another call - daoManager.setup(environment, configuration); - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java deleted file mode 100644 index 2a909f4..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java +++ /dev/null @@ -1,85 +0,0 @@ -/*- - * ============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.dbthings.daos; - -import org.joda.time.DateTime; -import org.junit.Test; -import org.openecomp.dcae.inventory.daos.DCAEServiceComponentsDAO; -import org.openecomp.dcae.inventory.daos.DCAEServiceTransactionDAO; -import org.openecomp.dcae.inventory.daos.DCAEServicesComponentsMapsDAO; -import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; - -/** - * Created by mhwang on 10/2/17. - */ -public class DCAEServiceTransactionDAOTests { - - @Test - public void testCreateDCAEServiceTransactionContext() { - String serviceId = "service-foo"; - DateTime modifiedTime = new DateTime(); - DCAEServiceTransactionDAO.DCAEServiceTransactionContext context - = new DCAEServiceTransactionDAO.DCAEServiceTransactionContext(serviceId, modifiedTime); - - assertEquals(context.getServiceId(), serviceId); - assertEquals(context.getModified(), modifiedTime); - } - - @Test - public void testInsert() { - String serviceId = "service-foo"; - DateTime modifiedTime = new DateTime(); - DCAEServiceTransactionDAO.DCAEServiceTransactionContext context - = new DCAEServiceTransactionDAO.DCAEServiceTransactionContext(serviceId, modifiedTime); - - DCAEServicesDAO servicesDAO = mock(DCAEServicesDAO.class); - DCAEServicesComponentsMapsDAO componentsMapsDAO = mock(DCAEServicesComponentsMapsDAO.class); - DCAEServiceComponentsDAO componentsDAO = mock(DCAEServiceComponentsDAO.class); - - DCAEServiceTransactionDAO transactionDAO = new DCAEServiceTransactionDAO() { - - public DCAEServicesDAO getServicesDAO() { - return servicesDAO; - } - - public DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO() { - return componentsMapsDAO; - } - - public DCAEServiceComponentsDAO getComponentsDAO() { - return componentsDAO; - } - }; - - try { - transactionDAO.insert(context); - assertTrue(true); - } catch(Exception e) { - fail("Unexpected error"); - } - - } -} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java deleted file mode 100644 index cb3c7f1..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * ============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.dbthings.mappers; - -import org.junit.Test; - -import java.sql.ResultSet; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; - -/** - * Created by mhwang on 10/2/17. - */ -public class DCAEServiceComponentObjectMapperTests { - - @Test - public void testMap() { - ResultSet resultSet = mock(ResultSet.class); - DCAEServiceComponentObjectMapper mapper = new DCAEServiceComponentObjectMapper(); - - try { - assertNotNull(mapper.map(0, resultSet, null)); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java deleted file mode 100644 index 1946343..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============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.dbthings.mappers; - -import org.junit.Test; - -import java.sql.ResultSet; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * Created by mhwang on 10/2/17. - */ -public class DCAEServiceObjectMapperTests { - - @Test - public void testMap() { - ResultSet resultSet = mock(ResultSet.class); - DCAEServiceObjectMapper mapper = new DCAEServiceObjectMapper(); - - try { - when(resultSet.getString("status")).thenReturn("RUNNING"); - assertNotNull(mapper.map(0, resultSet, null)); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java deleted file mode 100644 index f8c2428..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java +++ /dev/null @@ -1,109 +0,0 @@ -/*- - * ============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.dbthings.mappers; - -import org.junit.Test; - -import java.sql.Array; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Map; - -import static junit.framework.TestCase.assertNotNull; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; - -/** - * Created by mhwang on 10/2/17. - */ -public class DCAEServiceTypeObjectMapperTests { - - @Test - public void testMap() { - ResultSet resultSet = mock(ResultSet.class); - DCAEServiceTypeObjectMapper mapper = new DCAEServiceTypeObjectMapper(); - - try { - when(resultSet.getArray(anyString())).thenReturn(new Array() { - @Override - public String getBaseTypeName() throws SQLException { - return null; - } - - @Override - public int getBaseType() throws SQLException { - return 0; - } - - @Override - public Object getArray() throws SQLException { - return new String[10]; - } - - @Override - public Object getArray(Map> map) throws SQLException { - return null; - } - - @Override - public Object getArray(long index, int count) throws SQLException { - return null; - } - - @Override - public Object getArray(long index, int count, Map> map) throws SQLException { - return null; - } - - @Override - public ResultSet getResultSet() throws SQLException { - return null; - } - - @Override - public ResultSet getResultSet(Map> map) throws SQLException { - return null; - } - - @Override - public ResultSet getResultSet(long index, int count) throws SQLException { - return null; - } - - @Override - public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { - return null; - } - - @Override - public void free() throws SQLException { - - } - }); - assertNotNull(mapper.map(0, resultSet, null)); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java deleted file mode 100644 index 44f4f73..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceComponentObjectTests.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * ============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.dbthings.models; - -import io.swagger.model.DCAEServiceComponentRequest; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; - -/** - * Created by mhwang on 9/27/17. - */ -public class DCAEServiceComponentObjectTests { - - @Test - public void testConstructionByRequest() { - DCAEServiceComponentRequest request = (new DCAEServiceComponentRequest()).componentId("some-component-id") - .componentType("scary-component-type"); - request.setComponentSource("controller"); - request.setShareable(0); - - DCAEServiceComponentObject object = new DCAEServiceComponentObject(request); - assertEquals(object.getComponentId(), request.getComponentId()); - assertNotNull(object.getCreated()); - assertNotNull(object.getModified()); - } - - @Test - public void testConstructionForUpdating() { - DCAEServiceComponentRequest requestFirst = (new DCAEServiceComponentRequest()).componentId("some-component-id") - .componentType("scary-component-type"); - requestFirst.setComponentSource("controller"); - requestFirst.setShareable(0); - - DCAEServiceComponentObject objectFirst = new DCAEServiceComponentObject(requestFirst); - - DCAEServiceComponentRequest requestSecond = (new DCAEServiceComponentRequest()).componentId("some-other-component-id") - .componentType("happy-component-type"); - requestFirst.setComponentSource("controllerless"); - requestFirst.setShareable(1); - - DCAEServiceComponentObject objectUpdated = new DCAEServiceComponentObject(objectFirst, requestSecond); - assertEquals(objectUpdated.getComponentId(), requestFirst.getComponentId()); - assertEquals(objectUpdated.getCreated(), objectFirst.getCreated()); - assertEquals(objectUpdated.getComponentType(), requestSecond.getComponentType()); - assertEquals(objectUpdated.getComponentSource(), requestSecond.getComponentSource()); - assertEquals(objectUpdated.getShareable(), requestSecond.getShareable()); - assertNotSame(objectUpdated.getModified(), objectFirst.getModified()); - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java deleted file mode 100644 index 9f7b6f8..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/dbthings/models/DCAEServiceObjectTests.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============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.dbthings.models; - -import io.swagger.model.DCAEServiceRequest; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** - * Created by mhwang on 9/27/17. - */ -public class DCAEServiceObjectTests { - - @Test - public void testConstructionByRequest() { - DCAEServiceRequest request = new DCAEServiceRequest(); - request.setTypeId("some-type-id"); - request.setVnfType("foo-vnf-type"); - request.setVnfLocation("san-janero"); - - String serviceId = "some-service-id"; - - DCAEServiceObject object = new DCAEServiceObject(serviceId, request); - assertEquals(object.getServiceId(), serviceId); - assertNotNull(object.getCreated()); - assertNotNull(object.getModified()); - } - - @Test - public void testConstructionByUpdating() { - DCAEServiceRequest requestFirst = new DCAEServiceRequest(); - requestFirst.setTypeId("some-type-id"); - requestFirst.setVnfType("foo-vnf-type"); - requestFirst.setVnfLocation("san-janero"); - - String serviceId = "some-service-id"; - - DCAEServiceObject objectFirst = new DCAEServiceObject(serviceId, requestFirst); - - DCAEServiceRequest requestSecond = new DCAEServiceRequest(); - requestFirst.setTypeId("other-type-id"); - requestFirst.setVnfType("bar-vnf-type"); - requestFirst.setVnfLocation("san-junipero"); - - DCAEServiceObject objectUpdated = new DCAEServiceObject(objectFirst, requestSecond); - assertEquals(objectUpdated.getServiceId(), objectFirst.getServiceId()); - assertEquals(objectUpdated.getTypeId(), requestSecond.getTypeId()); - assertEquals(objectUpdated.getVnfType(), requestSecond.getVnfType()); - assertEquals(objectUpdated.getVnfLocation(), requestSecond.getVnfLocation()); - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java deleted file mode 100644 index 6e72182..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java +++ /dev/null @@ -1,117 +0,0 @@ -/*- - * ============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.exception.mappers; - -import org.openecomp.dcae.inventory.daos.InventoryDAOManager; -import org.openecomp.dcae.inventory.exceptions.mappers.DBIExceptionMapper; -import io.swagger.api.ApiResponseMessage; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.skife.jdbi.v2.exceptions.UnableToCreateStatementException; -import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException; -import org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException; - -import javax.ws.rs.core.Response; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * Created by mhwang on 3/8/17. - */ -@PrepareForTest({InventoryDAOManager.class}) -@RunWith(PowerMockRunner.class) -public class DBIExceptionMapperTests { - - @Test - public void testReinitializeSuccess() { - // PowerMockito does bytecode magic to mock static methods and use final classes - PowerMockito.mockStatic(InventoryDAOManager.class); - InventoryDAOManager mockDAOManager = mock(InventoryDAOManager.class); - when(InventoryDAOManager.getInstance()).thenReturn(mockDAOManager); - - RuntimeException fakeException = new RuntimeException("Spoofing database failure"); - - // Test UnableToObtainConnectionExceptionMapper - - DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne = new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper(); - Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException)); - assert responseOne.getStatus() == 502; - String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage(); - assert messageOne.contains("successfully reset"); - - // Test UnableToCreateStatementExceptionMapper - - DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo = new DBIExceptionMapper.UnableToCreateStatementExceptionMapper(); - Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException)); - assert responseTwo.getStatus() == 502; - String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage(); - assert messageTwo.contains("successfully reset"); - - // Test UnableToExecuteStatementExceptionMapper - - DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree = new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper(); - Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException)); - assert responseThree.getStatus() == 502; - String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage(); - assert messageThree.contains("successfully reset"); - } - - @Test - public void testReinitializeFailed() { - // PowerMockito does bytecode magic to mock static methods and use final classes - PowerMockito.mockStatic(InventoryDAOManager.class); - InventoryDAOManager mockDAOManager = mock(InventoryDAOManager.class); - when(InventoryDAOManager.getInstance()).thenReturn(mockDAOManager); - Mockito.doThrow(new RuntimeException("Spoof initialization failure")).when(mockDAOManager).initialize(); - - RuntimeException fakeException = new RuntimeException("Spoofing database failure"); - - // Test UnableToObtainConnectionExceptionMapper - - DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne = new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper(); - Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException)); - assert responseOne.getStatus() == 502; - String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage(); - assert !messageOne.contains("successfully reset"); - - // Test UnableToCreateStatementExceptionMapper - - DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo = new DBIExceptionMapper.UnableToCreateStatementExceptionMapper(); - Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException)); - assert responseTwo.getStatus() == 502; - String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage(); - assert !messageTwo.contains("successfully reset"); - - // Test UnableToExecuteStatementExceptionMapper - - DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree = new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper(); - Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException)); - assert responseThree.getStatus() == 502; - String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage(); - assert !messageThree.contains("successfully reset"); - } - -} diff --git a/src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java deleted file mode 100644 index 083b237..0000000 --- a/src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java +++ /dev/null @@ -1,42 +0,0 @@ -/*- - * ============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.providers; - -import io.swagger.api.NotFoundException; -import org.junit.Test; - -import javax.ws.rs.core.Response; - -import static org.junit.Assert.assertEquals; - -/** - * Created by mhwang on 10/2/17. - */ -public class NotFoundExceptionMapperTests { - - @Test - public void testNotFoundExceptionMapper() { - NotFoundExceptionMapper mapper = new NotFoundExceptionMapper(); - Response response = mapper.toResponse(new NotFoundException(100, "Some error message")); - assertEquals(response.getStatus(), 404); - } - -} -- cgit 1.2.3-korg