Enhancement to use the common CryptoUtils
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / config / PDPRestConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017,2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pdp.rest.config;
22
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.util.Properties;
26 import javax.annotation.PostConstruct;
27 import javax.servlet.MultipartConfigElement;
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.eelf.PolicyLogger;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
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.web.servlet.config.annotation.EnableWebMvc;
42 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
43 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
44 import springfox.documentation.builders.ApiInfoBuilder;
45 import springfox.documentation.builders.PathSelectors;
46 import springfox.documentation.builders.RequestHandlerSelectors;
47 import springfox.documentation.service.ApiInfo;
48 import springfox.documentation.spi.DocumentationType;
49 import springfox.documentation.spring.web.plugins.Docket;
50 import springfox.documentation.swagger2.annotations.EnableSwagger2;
51
52 @Configuration
53 @EnableWebMvc
54 @EnableSwagger2
55 @ComponentScan(basePackages = {"org.onap.*", "com.*"})
56 public class PDPRestConfig extends WebMvcConfigurerAdapter {
57
58     private static final Logger LOGGER = FlexLogger.getLogger(PDPRestConfig.class);
59
60     private static String dbDriver = null;
61     private static String dbUrl = null;
62     private static String dbUserName = null;
63     private static String dbPassword = null;
64
65     @PostConstruct
66     public void init() {
67         Properties prop = new Properties();
68         try (InputStream input = new FileInputStream("xacml.pdp.properties")) {
69             // load a properties file
70             prop.load(input);
71             setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
72             setDbUrl(prop.getProperty("javax.persistence.jdbc.url"));
73             setDbUserName(prop.getProperty("javax.persistence.jdbc.user"));
74             PeCryptoUtils.initAesKey(prop.getProperty("org.onap.policy.encryption.aes.key"));
75             setDbPassword(PeCryptoUtils.decrypt(prop.getProperty("javax.persistence.jdbc.password")));
76         } catch (Exception e) {
77             LOGGER.error("Exception Occured while loading properties file" + e);
78         }
79     }
80
81     @Override
82     public void addResourceHandlers(ResourceHandlerRegistry registry) {
83         registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
84         registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
85     }
86
87     private ApiInfo apiInfo() {
88         return new ApiInfoBuilder().title("Policy Engine REST API")
89                 .description("This API helps to make queries against Policy Engine").version("3.0").build();
90     }
91
92     @Bean
93     public Docket policyAPI() {
94         PolicyLogger.info("Setting up Swagger... ");
95         return new Docket(DocumentationType.SWAGGER_2).select()
96                 .apis(RequestHandlerSelectors.basePackage("org.onap.policy.pdp.rest.api")).paths(PathSelectors.any())
97                 .build().apiInfo(apiInfo());
98     }
99
100     @Bean(name = "dataSource")
101     public DataSource getDataSource() {
102         BasicDataSource dataSource = new BasicDataSource();
103         dataSource.setDriverClassName(PDPRestConfig.getDbDriver());
104         dataSource.setUrl(PDPRestConfig.getDbUrl());
105         dataSource.setUsername(PDPRestConfig.getDbUserName());
106         dataSource.setPassword(PDPRestConfig.getDbPassword());
107         return dataSource;
108     }
109
110     @Autowired
111     @Bean(name = "sessionFactory")
112     public SessionFactory getSessionFactory(DataSource dataSource) {
113         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
114         sessionBuilder.scanPackages("org.onap.*", "com.*");
115         sessionBuilder.addProperties(getHibernateProperties());
116         return sessionBuilder.buildSessionFactory();
117     }
118
119     private Properties getHibernateProperties() {
120         Properties properties = new Properties();
121         properties.put("hibernate.show_sql", "true");
122         properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
123         return properties;
124     }
125
126     @Autowired
127     @Bean(name = "transactionManager")
128     public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
129         return new HibernateTransactionManager(sessionFactory);
130     }
131
132     @Bean
133     public MultipartConfigElement multipartConfigElement() {
134         String location = System.getProperty("java.io.tmpdir");
135         return new MultipartConfigElement(location);
136     }
137
138     public static String getDbDriver() {
139         return dbDriver;
140     }
141
142     public static void setDbDriver(String dbDriver) {
143         PDPRestConfig.dbDriver = dbDriver;
144     }
145
146     public static String getDbUrl() {
147         return dbUrl;
148     }
149
150     public static void setDbUrl(String dbUrl) {
151         PDPRestConfig.dbUrl = dbUrl;
152     }
153
154     public static String getDbUserName() {
155         return dbUserName;
156     }
157
158     public static void setDbUserName(String dbUserName) {
159         PDPRestConfig.dbUserName = dbUserName;
160     }
161
162     public static String getDbPassword() {
163         return dbPassword;
164     }
165
166     public static void setDbPassword(String dbPassword) {
167         PDPRestConfig.dbPassword = dbPassword;
168     }
169 }