Merge "Removing so-monitoring module"
[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 static org.slf4j.LoggerFactory.getLogger;
23 import javax.persistence.EntityManagerFactory;
24 import javax.sql.DataSource;
25 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
26 import org.slf4j.Logger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Qualifier;
29 import org.springframework.boot.autoconfigure.flyway.FlywayDataSource;
30 import org.springframework.boot.context.properties.ConfigurationProperties;
31 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
32 import org.springframework.context.annotation.Bean;
33 import org.springframework.context.annotation.Configuration;
34 import org.springframework.context.annotation.Primary;
35 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
36 import org.springframework.jmx.export.MBeanExporter;
37 import org.springframework.orm.jpa.JpaTransactionManager;
38 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
39 import org.springframework.transaction.PlatformTransactionManager;
40 import org.springframework.transaction.annotation.EnableTransactionManagement;
41 import com.zaxxer.hikari.HikariConfig;
42 import com.zaxxer.hikari.HikariDataSource;
43
44 /**
45  * @author Waqas Ikram (waqas.ikram@est.tech)
46  *
47  */
48 @Configuration
49 @EnableTransactionManagement
50 @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
51         basePackages = {"org.onap.so.etsi.nfvo.ns.lcm.database.repository"})
52 public class NfvoDatabaseConfiguration {
53     private static final Logger logger = getLogger(NfvoDatabaseConfiguration.class);
54
55     private static final String PERSISTENCE_UNIT = "nfvo";
56     private static final String NFVO_DATA_SOURCE_QUALIFIER = "nfvoDataSource";
57
58     @Autowired(required = false)
59     private MBeanExporter mBeanExporter;
60
61     @Bean
62     @ConfigurationProperties(prefix = "spring.datasource.hikari.nfvo")
63     public HikariConfig nfvoDbConfig() {
64         logger.debug("Creating NFVO HikariConfig bean ... ");
65         return new HikariConfig();
66     }
67
68     @Primary
69     @FlywayDataSource
70     @Bean(name = NFVO_DATA_SOURCE_QUALIFIER)
71     public DataSource dataSource() {
72         if (mBeanExporter != null) {
73             mBeanExporter.addExcludedBean(NFVO_DATA_SOURCE_QUALIFIER);
74         }
75         logger.debug("Creating NFVO HikariDataSource bean ... ");
76         final HikariConfig hikariConfig = this.nfvoDbConfig();
77         return new HikariDataSource(hikariConfig);
78     }
79
80     @Primary
81     @Bean(name = "entityManagerFactory")
82     public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder,
83             @Qualifier(NFVO_DATA_SOURCE_QUALIFIER) final DataSource dataSource) {
84         return builder.dataSource(dataSource).packages(NfvoJob.class.getPackage().getName())
85                 .persistenceUnit(PERSISTENCE_UNIT).build();
86     }
87
88     @Primary
89     @Bean(name = "transactionManager")
90     public PlatformTransactionManager transactionManager(
91             @Qualifier("entityManagerFactory") final EntityManagerFactory entityManagerFactory) {
92         return new JpaTransactionManager(entityManagerFactory);
93     }
94
95 }