82b77da4795d496a96d0931f8c1346648be249da
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / primary / PrimaryDatabaseConfiguration.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.db.primary
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDataSourceProperties
20 import org.slf4j.LoggerFactory
21 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
22 import org.springframework.context.annotation.Bean
23 import org.springframework.context.annotation.ComponentScan
24 import org.springframework.context.annotation.Configuration
25 import org.springframework.data.jpa.repository.config.EnableJpaAuditing
26 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
27 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
28 import org.springframework.jdbc.datasource.DriverManagerDataSource
29 import org.springframework.orm.jpa.JpaTransactionManager
30 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
31 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
32 import org.springframework.transaction.PlatformTransactionManager
33 import java.util.*
34 import javax.sql.DataSource
35
36 @Configuration
37 @ConditionalOnProperty(name = ["blueprintsprocessor.db.primary.defaultConfig"], havingValue = "true",
38         matchIfMissing = true)
39 @ComponentScan
40 @EnableJpaRepositories(
41         basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor.*",
42             "org.onap.ccsdk.cds.controllerblueprints.*"],
43         entityManagerFactoryRef = "primaryEntityManager",
44         transactionManagerRef = "primaryTransactionManager"
45 )
46 @EnableJpaAuditing
47 open class PrimaryDatabaseConfiguration(private val primaryDataSourceProperties: PrimaryDataSourceProperties) {
48     private val log = LoggerFactory.getLogger(PrimaryDatabaseConfiguration::class.java)!!
49
50     @Bean("primaryEntityManager")
51     open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean {
52         val em = LocalContainerEntityManagerFactoryBean()
53         em.dataSource = primaryDataSource()
54         em.setPackagesToScan("org.onap.ccsdk.cds.blueprintsprocessor.*",
55                 "org.onap.ccsdk.cds.controllerblueprints.*")
56         em.jpaVendorAdapter = HibernateJpaVendorAdapter()
57         val properties = HashMap<String, Any>()
58         properties["hibernate.hbm2ddl.auto"] = primaryDataSourceProperties.hibernateHbm2ddlAuto
59         properties["hibernate.dialect"] = primaryDataSourceProperties.hibernateDialect
60         em.jpaPropertyMap = properties
61         return em
62     }
63
64     @Bean("primaryDataSource")
65     open fun primaryDataSource(): DataSource {
66         val dataSource = DriverManagerDataSource()
67         dataSource.setDriverClassName(primaryDataSourceProperties.driverClassName)
68         dataSource.url = primaryDataSourceProperties.url
69         dataSource.username = primaryDataSourceProperties.username
70         dataSource.password = primaryDataSourceProperties.password
71         return dataSource
72     }
73
74     @Bean("primaryTransactionManager")
75     open fun primaryTransactionManager(): PlatformTransactionManager {
76         val transactionManager = JpaTransactionManager()
77         transactionManager.entityManagerFactory = primaryEntityManager().getObject()
78         log.info("Initialised Primary Transaction Manager for url ${primaryDataSourceProperties.url}")
79         return transactionManager
80     }
81
82     @Bean("primaryNamedParameterJdbcTemplate")
83     open fun primaryNamedParameterJdbcTemplate(primaryDataSource: DataSource): NamedParameterJdbcTemplate {
84         return NamedParameterJdbcTemplate(primaryDataSource)
85     }
86
87 }