2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.policy.pdp.rest.config;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Properties;
27 import javax.annotation.PostConstruct;
28 import javax.servlet.MultipartConfigElement;
29 import javax.sql.DataSource;
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;
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;
58 @ComponentScan(basePackages = { "org.onap.*", "com.*" })
59 public class PDPRestConfig extends WebMvcConfigurerAdapter{
61 private static final Logger LOGGER = FlexLogger.getLogger(PDPRestConfig.class);
63 private static String dbDriver = null;
64 private static String dbUrl = null;
65 private static String dbUserName = null;
66 private static String dbPassword = null;
70 Properties prop = new Properties();
71 try (InputStream input = new FileInputStream("xacml.pdp.properties")){
72 // load a properties file
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"));
79 LOGGER.error("Exception Occured while loading properties file"+e);
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/");
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")
98 public Docket policyAPI(){
99 PolicyLogger.info("Setting up Swagger... ");
100 return new Docket(DocumentationType.SWAGGER_2)
102 .apis(RequestHandlerSelectors.basePackage("org.onap.policy.pdp.rest.api"))
103 .paths(PathSelectors.any())
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());
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();
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");
135 @Bean(name = "transactionManager")
136 public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
137 return new HibernateTransactionManager(sessionFactory);
141 public MultipartConfigElement multipartConfigElement(){
142 String location = System.getProperty("java.io.tmpdir");
143 MultipartConfigElement mp = new MultipartConfigElement(location);
147 public static String getDbDriver() {
151 public static void setDbDriver(String dbDriver) {
152 PDPRestConfig.dbDriver = dbDriver;
155 public static String getDbUrl() {
159 public static void setDbUrl(String dbUrl) {
160 PDPRestConfig.dbUrl = dbUrl;
163 public static String getDbUserName() {
167 public static void setDbUserName(String dbUserName) {
168 PDPRestConfig.dbUserName = dbUserName;
171 public static String getDbPassword() {
175 public static void setDbPassword(String dbPassword) {
176 PDPRestConfig.dbPassword = dbPassword;