2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.policy.pdp.rest.config;
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;
55 @ComponentScan(basePackages = {"org.onap.*", "com.*"})
56 public class PDPRestConfig extends WebMvcConfigurerAdapter {
58 private static final Logger LOGGER = FlexLogger.getLogger(PDPRestConfig.class);
60 private static String dbDriver = null;
61 private static String dbUrl = null;
62 private static String dbUserName = null;
63 private static String dbPassword = null;
67 Properties prop = new Properties();
68 try (InputStream input = new FileInputStream("xacml.pdp.properties")) {
69 // load a properties file
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);
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/");
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();
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());
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());
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();
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");
127 @Bean(name = "transactionManager")
128 public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
129 return new HibernateTransactionManager(sessionFactory);
133 public MultipartConfigElement multipartConfigElement() {
134 String location = System.getProperty("java.io.tmpdir");
135 return new MultipartConfigElement(location);
138 public static String getDbDriver() {
142 public static void setDbDriver(String dbDriver) {
143 PDPRestConfig.dbDriver = dbDriver;
146 public static String getDbUrl() {
150 public static void setDbUrl(String dbUrl) {
151 PDPRestConfig.dbUrl = dbUrl;
154 public static String getDbUserName() {
158 public static void setDbUserName(String dbUserName) {
159 PDPRestConfig.dbUserName = dbUserName;
162 public static String getDbPassword() {
166 public static void setDbPassword(String dbPassword) {
167 PDPRestConfig.dbPassword = dbPassword;