CHeckstyle and JUnit for base package in ONAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / PAPRestConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.xacml.rest;
23
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.util.Properties;
27
28 import javax.annotation.PostConstruct;
29 import javax.sql.DataSource;
30
31 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
32 import org.hibernate.SessionFactory;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.rest.XacmlRestProperties;
36 import org.onap.policy.utils.PeCryptoUtils;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.ComponentScan;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.orm.hibernate4.HibernateTransactionManager;
42 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
43 import org.springframework.transaction.annotation.EnableTransactionManagement;
44 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
45 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
46
47 @Configuration
48 @EnableWebMvc
49 @EnableTransactionManagement
50 @ComponentScan(basePackages = {"org.onap.*", "com.*"})
51 public class PAPRestConfig extends WebMvcConfigurerAdapter {
52     private static final Logger LOGGER = FlexLogger.getLogger(PAPRestConfig.class);
53
54     private static String dbDriver = null;
55     private static String dbUrl = null;
56     private static String dbUserName = null;
57     private static String dbPassword = null;
58
59     @PostConstruct
60     public void init() {
61         Properties prop = new Properties();
62         try (InputStream input = new FileInputStream("xacml.pap.properties")) {
63             // load a properties file
64             prop.load(input);
65             setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
66             setDbUrl(prop.getProperty("javax.persistence.jdbc.url"));
67             setDbUserName(prop.getProperty("javax.persistence.jdbc.user"));
68             PeCryptoUtils.initAesKey(prop.getProperty(XacmlRestProperties.PROP_AES_KEY));
69             setDbPassword(PeCryptoUtils.decrypt(prop.getProperty("javax.persistence.jdbc.password")));
70         } catch (Exception e) {
71             LOGGER.error("Exception Occured while loading properties file" + e);
72         }
73     }
74
75     @Bean(name = "dataSource")
76     public DataSource getDataSource() {
77         BasicDataSource dataSource = new BasicDataSource();
78         dataSource.setDriverClassName(PAPRestConfig.getDbDriver());
79         dataSource.setUrl(PAPRestConfig.getDbUrl());
80         dataSource.setUsername(PAPRestConfig.getDbUserName());
81         dataSource.setPassword(PAPRestConfig.getDbPassword());
82         return dataSource;
83     }
84
85     @Autowired
86     @Bean(name = "sessionFactory")
87     public SessionFactory getSessionFactory(DataSource dataSource) {
88         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
89         sessionBuilder.scanPackages("org.onap.*", "com.*");
90         sessionBuilder.addProperties(getHibernateProperties());
91         return sessionBuilder.buildSessionFactory();
92     }
93
94     private Properties getHibernateProperties() {
95         Properties properties = new Properties();
96         properties.put("hibernate.show_sql", "true");
97         properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
98         return properties;
99     }
100
101     @Autowired
102     @Bean(name = "transactionManager")
103     public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
104         return new HibernateTransactionManager(sessionFactory);
105     }
106
107     public static String getDbDriver() {
108         return dbDriver;
109     }
110
111     public static void setDbDriver(String dbDriver) {
112         PAPRestConfig.dbDriver = dbDriver;
113     }
114
115     public static String getDbUrl() {
116         return dbUrl;
117     }
118
119     public static void setDbUrl(String dbUrl) {
120         PAPRestConfig.dbUrl = dbUrl;
121     }
122
123     public static String getDbUserName() {
124         return dbUserName;
125     }
126
127     public static void setDbUserName(String dbUserName) {
128         PAPRestConfig.dbUserName = dbUserName;
129     }
130
131     public static String getDbPassword() {
132         return dbPassword;
133     }
134
135     public static void setDbPassword(String dbPassword) {
136         PAPRestConfig.dbPassword = dbPassword;
137     }
138
139 }