8f146e5378b6c00045dd3e4a045aea55bd572fed
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-database-service / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / database / config / NfvoDatabaseConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.etsi.nfvo.ns.lcm.database.config;
21
22 import javax.persistence.EntityManagerFactory;
23 import javax.sql.DataSource;
24 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Qualifier;
27 import org.springframework.boot.context.properties.ConfigurationProperties;
28 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
29 import org.springframework.context.annotation.Bean;
30 import org.springframework.context.annotation.Configuration;
31 import org.springframework.context.annotation.Primary;
32 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
33 import org.springframework.jmx.export.MBeanExporter;
34 import org.springframework.orm.jpa.JpaTransactionManager;
35 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
36 import org.springframework.transaction.PlatformTransactionManager;
37 import org.springframework.transaction.annotation.EnableTransactionManagement;
38 import com.zaxxer.hikari.HikariConfig;
39 import com.zaxxer.hikari.HikariDataSource;
40
41 /**
42  * @author Waqas Ikram (waqas.ikram@est.tech)
43  *
44  */
45 @Configuration
46 @EnableTransactionManagement
47 @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
48         basePackages = {"org.onap.so.etsi.nfvo.ns.lcm.database.repository"})
49 public class NfvoDatabaseConfiguration {
50
51     private static final String PERSISTENCE_UNIT = "nfvo";
52     private static final String NFVO_DATA_SOURCE_QUALIFIER = "nfvoDataSource";
53
54     @Autowired(required = false)
55     private MBeanExporter mBeanExporter;
56
57     @Bean
58     @ConfigurationProperties(prefix = "spring.datasource.hikari.nfvo")
59     public HikariConfig nfvoDbConfig() {
60         return new HikariConfig();
61     }
62
63     @Bean(name = NFVO_DATA_SOURCE_QUALIFIER)
64     public DataSource dataSource() {
65         if (mBeanExporter != null) {
66             mBeanExporter.addExcludedBean(NFVO_DATA_SOURCE_QUALIFIER);
67         }
68         final HikariConfig hikariConfig = this.nfvoDbConfig();
69         return new HikariDataSource(hikariConfig);
70     }
71
72     @Primary
73     @Bean(name = "entityManagerFactory")
74     public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder,
75             @Qualifier(NFVO_DATA_SOURCE_QUALIFIER) final DataSource dataSource) {
76         return builder.dataSource(dataSource).packages(NfvoJob.class.getPackage().getName())
77                 .persistenceUnit(PERSISTENCE_UNIT).build();
78     }
79
80     @Primary
81     @Bean(name = "transactionManager")
82     public PlatformTransactionManager transactionManager(
83             @Qualifier("entityManagerFactory") final EntityManagerFactory entityManagerFactory) {
84         return new JpaTransactionManager(entityManagerFactory);
85     }
86
87 }