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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  20 package org.onap.so.etsi.nfvo.ns.lcm.database.config;
 
  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;
 
  42  * @author Waqas Ikram (waqas.ikram@est.tech)
 
  46 @EnableTransactionManagement
 
  47 @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
 
  48         basePackages = {"org.onap.so.etsi.nfvo.ns.lcm.database.repository"})
 
  49 public class NfvoDatabaseConfiguration {
 
  51     @Autowired(required = false)
 
  52     private MBeanExporter mBeanExporter;
 
  54     private static final String NFVO_DATA_SOURCE_QUALIFIER = "nfvoDataSource";
 
  57     @ConfigurationProperties(prefix = "spring.datasource.hikari.nfvo")
 
  58     public HikariConfig nfvoDbConfig() {
 
  59         return new HikariConfig();
 
  62     @Bean(name = NFVO_DATA_SOURCE_QUALIFIER)
 
  63     public DataSource dataSource() {
 
  64         if (mBeanExporter != null) {
 
  65             mBeanExporter.addExcludedBean(NFVO_DATA_SOURCE_QUALIFIER);
 
  67         final HikariConfig hikariConfig = this.nfvoDbConfig();
 
  68         return new HikariDataSource(hikariConfig);
 
  72     @Bean(name = "entityManagerFactory")
 
  73     public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder,
 
  74             @Qualifier(NFVO_DATA_SOURCE_QUALIFIER) final DataSource dataSource) {
 
  75         return builder.dataSource(dataSource).packages(NfvoJob.class.getPackage().getName()).persistenceUnit("nfvo")
 
  80     @Bean(name = "transactionManager")
 
  81     public PlatformTransactionManager transactionManager(
 
  82             @Qualifier("entityManagerFactory") final EntityManagerFactory entityManagerFactory) {
 
  83         return new JpaTransactionManager(entityManagerFactory);