Merge "Enhancement to use the common CryptoUtils"
[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 import javax.annotation.PostConstruct;
28 import javax.sql.DataSource;
29 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
30 import org.hibernate.SessionFactory;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.rest.XACMLRestProperties;
34 import org.onap.policy.utils.PeCryptoUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.ComponentScan;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.orm.hibernate4.HibernateTransactionManager;
40 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
41 import org.springframework.transaction.annotation.EnableTransactionManagement;
42 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
43 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
44
45 @Configuration
46 @EnableWebMvc
47 @EnableTransactionManagement
48 @ComponentScan(basePackages = { "org.onap.*", "com.*" })
49 public class PAPRestConfig extends WebMvcConfigurerAdapter {
50     private static final Logger LOGGER  = FlexLogger.getLogger(PAPRestConfig.class);
51
52     private static String dbDriver = null;
53     private static String dbUrl = null;
54     private static String dbUserName = null;
55     private static String dbPassword = null;
56
57     @PostConstruct
58     public void init(){
59         Properties prop = new Properties();
60         try(InputStream input = new FileInputStream("xacml.pap.properties")) {
61             // load a properties file
62             prop.load(input);
63             setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
64             setDbUrl(prop.getProperty("javax.persistence.jdbc.url"));
65             setDbUserName(prop.getProperty("javax.persistence.jdbc.user"));
66             PeCryptoUtils.initAesKey(prop.getProperty(XACMLRestProperties.PROP_AES_KEY));
67             setDbPassword(PeCryptoUtils.decrypt(prop.getProperty("javax.persistence.jdbc.password")));
68         }catch(Exception e){
69             LOGGER.error("Exception Occured while loading properties file"+e);
70         }
71     }
72
73     @Bean(name = "dataSource")
74     public DataSource getDataSource() {
75         BasicDataSource dataSource = new BasicDataSource();
76         dataSource.setDriverClassName(PAPRestConfig.getDbDriver());
77         dataSource.setUrl(PAPRestConfig.getDbUrl());
78         dataSource.setUsername(PAPRestConfig.getDbUserName());
79         dataSource.setPassword(PAPRestConfig.getDbPassword());
80         return dataSource;
81     }
82
83     @Autowired
84     @Bean(name = "sessionFactory")
85     public SessionFactory getSessionFactory(DataSource dataSource) {
86         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
87         sessionBuilder.scanPackages("org.onap.*", "com.*");
88         sessionBuilder.addProperties(getHibernateProperties());
89         return sessionBuilder.buildSessionFactory();
90     }
91
92     private Properties getHibernateProperties() {
93         Properties properties = new Properties();
94         properties.put("hibernate.show_sql", "true");
95         properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
96         return properties;
97     }
98
99     @Autowired
100     @Bean(name = "transactionManager")
101     public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
102         return new HibernateTransactionManager(sessionFactory);
103     }
104
105     public static String getDbDriver() {
106         return dbDriver;
107     }
108
109     public static void setDbDriver(String dbDriver) {
110         PAPRestConfig.dbDriver = dbDriver;
111     }
112
113     public static String getDbUrl() {
114         return dbUrl;
115     }
116
117     public static void setDbUrl(String dbUrl) {
118         PAPRestConfig.dbUrl = dbUrl;
119     }
120
121     public static String getDbUserName() {
122         return dbUserName;
123     }
124
125     public static void setDbUserName(String dbUserName) {
126         PAPRestConfig.dbUserName = dbUserName;
127     }
128
129     public static String getDbPassword() {
130         return dbPassword;
131     }
132
133     public static void setDbPassword(String dbPassword) {
134         PAPRestConfig.dbPassword = dbPassword;
135     }
136
137 }