b17dcf0fc386900c35e8a3e510fffcfdd89ea2d3
[ccsdk/cds.git] /
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.context.annotation.Primary
26 import org.springframework.data.jpa.repository.config.EnableJpaAuditing
27 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
28 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
29 import org.springframework.jdbc.datasource.DriverManagerDataSource
30 import org.springframework.orm.jpa.JpaTransactionManager
31 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
32 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
33 import org.springframework.transaction.PlatformTransactionManager
34 import java.util.*
35 import javax.sql.DataSource
36
37 @Configuration
38 @ConditionalOnProperty(name = ["blueprintsprocessor.db.primary.defaultConfig"], havingValue = "true",
39         matchIfMissing = true)
40 @ComponentScan
41 @EnableJpaRepositories(
42         basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor.*",
43             "org.onap.ccsdk.cds.controllerblueprints.*"],
44         entityManagerFactoryRef = "primaryEntityManager",
45         transactionManagerRef = "primaryTransactionManager"
46 )
47 @EnableJpaAuditing
48 open class PrimaryDatabaseConfiguration(private val primaryDataSourceProperties: PrimaryDataSourceProperties) {
49     private val log = LoggerFactory.getLogger(PrimaryDatabaseConfiguration::class.java)!!
50
51     @Primary
52     @Bean("primaryEntityManager")
53     open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean {
54         val em = LocalContainerEntityManagerFactoryBean()
55         em.dataSource = primaryDataSource()
56         em.setPackagesToScan("org.onap.ccsdk.cds.blueprintsprocessor.*",
57                 "org.onap.ccsdk.cds.controllerblueprints.*")
58         em.jpaVendorAdapter = HibernateJpaVendorAdapter()
59         val properties = HashMap<String, Any>()
60         properties["hibernate.hbm2ddl.auto"] = primaryDataSourceProperties.hibernateHbm2ddlAuto
61         properties["hibernate.dialect"] = primaryDataSourceProperties.hibernateDialect
62         em.jpaPropertyMap = properties
63         return em
64     }
65
66     @Primary
67     @Bean("primaryDataSource")
68     open fun primaryDataSource(): DataSource {
69         val dataSource = DriverManagerDataSource()
70         dataSource.setDriverClassName(primaryDataSourceProperties.driverClassName)
71         dataSource.url = primaryDataSourceProperties.url
72         dataSource.username = primaryDataSourceProperties.username
73         dataSource.password = primaryDataSourceProperties.password
74         return dataSource
75     }
76
77     @Primary
78     @Bean("primaryTransactionManager")
79     open fun primaryTransactionManager(): PlatformTransactionManager {
80         val transactionManager = JpaTransactionManager()
81         transactionManager.entityManagerFactory = primaryEntityManager().getObject()
82         log.info("Initialised Primary Transaction Manager for url ${primaryDataSourceProperties.url}")
83         return transactionManager
84     }
85
86     @Bean("primaryNamedParameterJdbcTemplate")
87     open fun primaryNamedParameterJdbcTemplate(primaryDataSource: DataSource): NamedParameterJdbcTemplate {
88         return NamedParameterJdbcTemplate(primaryDataSource)
89     }
90
91 }