2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vfc.rest;
23 import ch.vorburger.exec.ManagedProcessException;
24 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
25 import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService;
26 import org.springframework.beans.factory.annotation.Qualifier;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.boot.jdbc.DataSourceBuilder;
29 import org.springframework.boot.context.properties.ConfigurationProperties;
30 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.context.annotation.Primary;
34 import org.springframework.context.annotation.Profile;
35 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
36 import org.springframework.orm.jpa.JpaTransactionManager;
37 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
38 import org.springframework.transaction.PlatformTransactionManager;
39 import org.springframework.transaction.annotation.EnableTransactionManagement;
40 import javax.persistence.EntityManagerFactory;
41 import javax.sql.DataSource;
45 @EnableTransactionManagement
46 @EnableJpaRepositories(entityManagerFactoryRef = "requestEntityManagerFactory",
47 transactionManagerRef = "requestTransactionManager", basePackages = {"org.onap.so.db.request.data.repository"})
48 public class EmbeddedMariaDbConfig {
51 MariaDB4jSpringService mariaDB4jSpringService() {
52 return new MariaDB4jSpringService();
56 @Bean(name = "requestDataSource")
57 @ConfigurationProperties(prefix = "spring.datasource")
58 DataSource dataSource(MariaDB4jSpringService mariaDB4jSpringService,
59 @Value("${mariaDB4j.databaseName}") String databaseName,
60 @Value("${spring.datasource.username}") String datasourceUsername,
61 @Value("${spring.datasource.password}") String datasourcePassword,
62 @Value("${spring.datasource.driver-class-name}") String datasourceDriver) throws ManagedProcessException {
63 // Create our database with default root user and no password
64 mariaDB4jSpringService.getDB().createDB(databaseName);
66 DBConfigurationBuilder config = mariaDB4jSpringService.getConfiguration();
68 return DataSourceBuilder.create().username(datasourceUsername).password(datasourcePassword)
69 .url(config.getURL(databaseName)).driverClassName(datasourceDriver).build();
73 @Bean(name = "requestEntityManagerFactory")
74 public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
75 @Qualifier("requestDataSource") DataSource dataSource) {
76 return builder.dataSource(dataSource).packages("org.onap.so.db.request.beans").persistenceUnit("requestDB")
81 @Bean(name = "requestTransactionManager")
82 public PlatformTransactionManager transactionManager(
83 @Qualifier("requestEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
84 return new JpaTransactionManager(entityManagerFactory);