From 8923639a534b79dddfb9e4006acbee31bc2649c3 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Thu, 6 Dec 2018 16:46:19 -0500 Subject: Add Primary Data Source Configuration Change-Id: I338f8564c20ebc2c139c80806468be8f8d3bb7e4 Issue-ID: CCSDK-698 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../modules/commons/db-lib/pom.xml | 7 +- .../db/BluePrintDBLibConfiguration.kt | 41 +++++++++++ .../blueprintsprocessor/db/BluePrintDBLibData.kt | 29 ++++++++ .../db/BluePrintDBLibGenericService.kt | 28 +++++++ .../db/primary/PrimaryDatabaseConfiguration.kt | 85 ++++++++++++++++++++++ .../primary/service/PrimaryDBLibGenericService.kt | 38 ++++++++++ .../db/primary/PrimaryDatabaseConfigurationTest.kt | 44 +++++++++++ .../src/test/resources/application-test.properties | 24 ++++++ .../db-lib/src/test/resources/logback-test.xml | 35 +++++++++ 9 files changed, 328 insertions(+), 3 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibData.kt create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibGenericService.kt create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfiguration.kt create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/service/PrimaryDBLibGenericService.kt create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfigurationTest.kt create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/application-test.properties create mode 100644 ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/logback-test.xml (limited to 'ms/blueprintsprocessor/modules/commons') diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/pom.xml b/ms/blueprintsprocessor/modules/commons/db-lib/pom.xml index 5f5b89de..4b11f83f 100644 --- a/ms/blueprintsprocessor/modules/commons/db-lib/pom.xml +++ b/ms/blueprintsprocessor/modules/commons/db-lib/pom.xml @@ -33,7 +33,6 @@ org.onap.ccsdk.apps.blueprintsprocessor core - org.springframework.boot spring-boot-starter-data-jpa @@ -42,17 +41,19 @@ javax.validation validation-api - com.h2database h2 + + org.mariadb.jdbc + mariadb-java-client + org.hibernate hibernate-testing test - diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt new file mode 100644 index 00000000..0bdee9be --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt @@ -0,0 +1,41 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.db + +import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.ComponentScan +import org.springframework.context.annotation.Configuration + +@Configuration +@ComponentScan +@EnableConfigurationProperties +open class BluePrintDBLibConfiguration(private var bluePrintProperties: BluePrintProperties) { + + @Bean("primary-database-properties") + open fun getPrimaryProperties(): PrimaryDataSourceProperties { + return bluePrintProperties.propertyBeanType(DBLibConstants.PREFIX_DB_PRIMARY, + PrimaryDataSourceProperties::class.java) + } +} + +class DBLibConstants { + companion object { + const val PREFIX_DB_PRIMARY: String = "blueprintsprocessor.db.primary" + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibData.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibData.kt new file mode 100644 index 00000000..a16e5565 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibData.kt @@ -0,0 +1,29 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.db + + +open class PrimaryDataSourceProperties { + lateinit var url: String + lateinit var username: String + lateinit var password: String + lateinit var driverClassName: String + lateinit var hibernateHbm2ddlAuto: String + lateinit var hibernateDDLAuto: String + lateinit var hibernateNamingStrategy: String + lateinit var hibernateDialect: String +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibGenericService.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibGenericService.kt new file mode 100644 index 00000000..86420f9a --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/BluePrintDBLibGenericService.kt @@ -0,0 +1,28 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.db + +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate + +interface BluePrintDBLibGenericService { + + fun namedParameterJdbcTemplate(): NamedParameterJdbcTemplate + + fun query(sql: String, params: Map): List> + + fun update(sql: String, params: Map): Int +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfiguration.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfiguration.kt new file mode 100644 index 00000000..bf76e213 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfiguration.kt @@ -0,0 +1,85 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.db.primary + +import org.onap.ccsdk.apps.blueprintsprocessor.db.PrimaryDataSourceProperties +import org.slf4j.LoggerFactory +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Primary +import org.springframework.data.jpa.repository.config.EnableJpaRepositories +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate +import org.springframework.jdbc.datasource.DriverManagerDataSource +import org.springframework.orm.jpa.JpaTransactionManager +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter +import org.springframework.transaction.PlatformTransactionManager +import java.util.* +import javax.sql.DataSource + +@Configuration +@EnableJpaRepositories( + basePackages = ["org.onap.ccsdk.apps.blueprintsprocessor.db.primary"], + entityManagerFactoryRef = "primaryEntityManager", + transactionManagerRef = "primaryTransactionManager" +) +open class PrimaryDatabaseConfiguration(private val primaryDataSourceProperties: PrimaryDataSourceProperties) { + val log = LoggerFactory.getLogger(PrimaryDatabaseConfiguration::class.java)!! + + @Primary + @Bean("primaryEntityManager") + open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean { + val em = LocalContainerEntityManagerFactoryBean() + em.dataSource = primaryDataSource() + em.setPackagesToScan( + "org.onap.ccsdk.apps.blueprintsprocessor.db.primary") + + val vendorAdapter = HibernateJpaVendorAdapter() + em.jpaVendorAdapter = vendorAdapter + val properties = HashMap() + properties["hibernate.hbm2ddl.auto"] = primaryDataSourceProperties.hibernateHbm2ddlAuto + properties["hibernate.dialect"] = primaryDataSourceProperties.hibernateDialect + em.jpaPropertyMap = properties + return em + } + + @Primary + @Bean("primaryDataSource") + open fun primaryDataSource(): DataSource { + val dataSource = DriverManagerDataSource() + dataSource.setDriverClassName(primaryDataSourceProperties.driverClassName) + dataSource.url = primaryDataSourceProperties.url + dataSource.username = primaryDataSourceProperties.username + dataSource.password = primaryDataSourceProperties.password + return dataSource + } + + @Primary + @Bean("primaryTransactionManager") + open fun primaryTransactionManager(): PlatformTransactionManager { + val transactionManager = JpaTransactionManager() + transactionManager.entityManagerFactory = primaryEntityManager().getObject() + log.info("Initialised Primary Transaction Manager for url ${primaryDataSourceProperties.url}") + return transactionManager + } + + @Bean("primaryNamedParameterJdbcTemplate") + open fun primaryNamedParameterJdbcTemplate(primaryDataSource: DataSource): NamedParameterJdbcTemplate { + return NamedParameterJdbcTemplate(primaryDataSource) + } + +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/service/PrimaryDBLibGenericService.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/service/PrimaryDBLibGenericService.kt new file mode 100644 index 00000000..87f32ef2 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/service/PrimaryDBLibGenericService.kt @@ -0,0 +1,38 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.db.primary.service + +import org.onap.ccsdk.apps.blueprintsprocessor.db.BluePrintDBLibGenericService +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate +import org.springframework.stereotype.Service + +@Service +class PrimaryDBLibGenericService(private val primaryNamedParameterJdbcTemplate: NamedParameterJdbcTemplate) + : BluePrintDBLibGenericService { + + override fun namedParameterJdbcTemplate(): NamedParameterJdbcTemplate { + return primaryNamedParameterJdbcTemplate + } + + override fun query(sql: String, params: Map): List> { + return primaryNamedParameterJdbcTemplate.queryForList(sql, params) + } + + override fun update(sql: String, params: Map): Int { + return primaryNamedParameterJdbcTemplate.update(sql, params) + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfigurationTest.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfigurationTest.kt new file mode 100644 index 00000000..aa48c8e3 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/db/primary/PrimaryDatabaseConfigurationTest.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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. + */ + +package org.onap.ccsdk.apps.blueprintsprocessor.db.primary + +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintProperties +import org.onap.ccsdk.apps.blueprintsprocessor.core.BlueprintPropertyConfiguration +import org.onap.ccsdk.apps.blueprintsprocessor.db.BluePrintDBLibConfiguration +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner +import javax.sql.DataSource +import kotlin.test.assertNotNull + +@RunWith(SpringRunner::class) +@ContextConfiguration(classes = [BlueprintPropertyConfiguration::class, BluePrintProperties::class, + BluePrintDBLibConfiguration::class]) +@TestPropertySource(locations = ["classpath:application-test.properties"]) +class PrimaryDatabaseConfigurationTest { + + @Autowired + lateinit var primaryDataSource: DataSource + + @Test + fun testPrimaryDatabaseConfiguration() { + assertNotNull(primaryDataSource, " failed to create primary data source") + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/application-test.properties b/ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/application-test.properties new file mode 100644 index 00000000..672ff80f --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/application-test.properties @@ -0,0 +1,24 @@ +# suppress inspection "UnusedProperty" for whole file +# +# Copyright © 2017-2018 AT&T Intellectual Property. +# +# 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. +# +blueprintsprocessor.db.primary.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE +blueprintsprocessor.db.primary.username=sa +blueprintsprocessor.db.primary.password= +blueprintsprocessor.db.primary.driverClassName=org.h2.Driver +blueprintsprocessor.db.primary.hibernateHbm2ddlAuto=create-drop +blueprintsprocessor.db.primary.hibernateDDLAuto=update +blueprintsprocessor.db.primary.hibernateNamingStrategy=org.hibernate.cfg.ImprovedNamingStrategy +blueprintsprocessor.db.primary.hibernateDialect=org.hibernate.dialect.H2Dialect \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/logback-test.xml b/ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/logback-test.xml new file mode 100644 index 00000000..f9ec9fe5 --- /dev/null +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/test/resources/logback-test.xml @@ -0,0 +1,35 @@ + + + + + + + %d{HH:mm:ss.SSS} %-5level %logger{100} - %msg%n + + + + + + + + + + + + + -- cgit 1.2.3-korg