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