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