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