c4a92c0e80111772bd59570cc9c128757d58f5b5
[validation.git] / ExternalAppConfig.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.conf;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.sql.DataSource;
22
23 import org.akraino.validation.ui.login.LoginStrategyImpl;
24 import org.akraino.validation.ui.scheduler.RegistryAdapter;
25 import org.onap.portalapp.music.conf.MusicSessionConfig;
26 import org.onap.portalsdk.core.auth.LoginStrategy;
27 import org.onap.portalsdk.core.conf.AppConfig;
28 import org.onap.portalsdk.core.conf.Configurable;
29 import org.onap.portalsdk.core.logging.format.AlarmSeverityEnum;
30 import org.onap.portalsdk.core.logging.format.AppMessagesEnum;
31 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.onap.portalsdk.core.objectcache.AbstractCacheManager;
33 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
34 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
35 import org.onap.portalsdk.core.service.DataAccessService;
36 import org.onap.portalsdk.core.util.CacheManager;
37 import org.onap.portalsdk.core.util.SystemProperties;
38 import org.onap.portalsdk.core.web.support.UserUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.Bean;
41 import org.springframework.context.annotation.ComponentScan;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Import;
44 import org.springframework.context.annotation.Profile;
45 import org.springframework.context.annotation.PropertySource;
46 import org.springframework.scheduling.annotation.EnableAsync;
47 import org.springframework.scheduling.annotation.EnableScheduling;
48 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
49 import org.springframework.web.servlet.ViewResolver;
50 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
51 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
52 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
53
54 import com.mchange.v2.c3p0.ComboPooledDataSource;
55
56 @Configuration
57 @EnableWebMvc
58 @ComponentScan(basePackages = { "org.akraino", "org.onap" })
59 @PropertySource(value = { "${container.classpath:}/WEB-INF/conf/app/test.properties" }, ignoreResourceNotFound = true)
60 @Profile("src")
61 @EnableAsync
62 @EnableScheduling
63 @Import({ MusicSessionConfig.class })
64 public class ExternalAppConfig extends AppConfig implements Configurable {
65
66     private RegistryAdapter schedulerRegistryAdapter;
67     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ExternalAppConfig.class);
68
69     @Configuration
70     @Import(SystemProperties.class)
71     static class InnerConfiguration {
72     }
73
74     /**
75      * @see org.onap.portalsdk.core.conf.AppConfig#viewResolver()
76      */
77     @Override
78     public ViewResolver viewResolver() {
79         return super.viewResolver();
80     }
81
82     /**
83      * @see org.onap.portalsdk.core.conf.AppConfig#addResourceHandlers(ResourceHandlerRegistry)
84      *
85      * @param registry
86      */
87     @Override
88     public void addResourceHandlers(ResourceHandlerRegistry registry) {
89         super.addResourceHandlers(registry);
90     }
91
92     /**
93      * @see org.onap.portalsdk.core.conf.AppConfig#dataAccessService()
94      */
95     @Override
96     public DataAccessService dataAccessService() {
97         return super.dataAccessService();
98     }
99
100     /**
101      *
102      * Creates the Application Data Source.
103      *
104      * @return DataSource Object
105      * @throws Exception on failure to create data source object
106      */
107     @Override
108     @Bean
109     public DataSource dataSource() throws Exception {
110
111         systemProperties();
112
113         ComboPooledDataSource dataSource = new ComboPooledDataSource();
114         try {
115             dataSource.setDriverClass(SystemProperties.getProperty(SystemProperties.DB_DRIVER));
116             dataSource.setJdbcUrl("jdbc:mysql://" + System.getenv("DB_IP_PORT") + "/"
117                     + PortalApiProperties.getProperty("akraino_database_name"));
118             dataSource.setUser(System.getenv("MYSQL_USER"));
119             String password = System.getenv("MYSQL_PASSWORD");
120             if (SystemProperties.containsProperty(SystemProperties.DB_ENCRYPT_FLAG)) {
121                 String encryptFlag = SystemProperties.getProperty(SystemProperties.DB_ENCRYPT_FLAG);
122                 if (encryptFlag != null && encryptFlag.equalsIgnoreCase("true")) {
123                     password = CipherUtil.decrypt(password);
124                 }
125             }
126             dataSource.setPassword(password);
127             dataSource
128             .setMinPoolSize(Integer.parseInt(SystemProperties.getProperty(SystemProperties.DB_MIN_POOL_SIZE)));
129             dataSource
130             .setMaxPoolSize(Integer.parseInt(SystemProperties.getProperty(SystemProperties.DB_MAX_POOL_SIZE)));
131             dataSource.setIdleConnectionTestPeriod(
132                     Integer.parseInt(SystemProperties.getProperty(SystemProperties.IDLE_CONNECTION_TEST_PERIOD)));
133             dataSource.setTestConnectionOnCheckout(getConnectionOnCheckout());
134             dataSource.setPreferredTestQuery(getPreferredTestQuery());
135         } catch (Exception e) {
136             LOGGER.error(EELFLoggerDelegate.errorLogger,
137                     "Error initializing database, verify database settings in properties file: "
138                             + UserUtils.getStackTrace(e),
139                             AlarmSeverityEnum.CRITICAL);
140             LOGGER.error(EELFLoggerDelegate.debugLogger,
141                     "Error initializing database, verify database settings in properties file: "
142                             + UserUtils.getStackTrace(e),
143                             AlarmSeverityEnum.CRITICAL);
144             // Raise an alarm that opening a connection to the database failed.
145             LOGGER.logEcompError(AppMessagesEnum.BeDaoSystemError);
146             throw e;
147         }
148         return dataSource;
149     }
150
151     /**
152      * Creates a new list with a single entry that is the external app
153      * definitions.xml path.
154      *
155      * @return List of String, size 1
156      */
157     @Override
158     public List<String> addTileDefinitions() {
159         List<String> definitions = new ArrayList<>();
160         definitions.add("/WEB-INF/defs/definitions.xml");
161         return definitions;
162     }
163
164     /**
165      * Adds request interceptors to the specified registry by calling
166      * {@link AppConfig#addInterceptors(InterceptorRegistry)}, but excludes certain
167      * paths from the session timeout interceptor.
168      */
169     @Override
170     public void addInterceptors(InterceptorRegistry registry) {
171         super.setExcludeUrlPathsForSessionTimeout("/login_external", "*/login_external.htm", "login", "/login.htm",
172                 "/api*", "/single_signon.htm", "/single_signon", "logout", "/logout.htm", "/process_csp");
173         super.addInterceptors(registry);
174     }
175
176     /**
177      * Creates and returns a new instance of a {@link CacheManager} class.
178      *
179      * @return New instance of {@link CacheManager}
180      */
181     @Bean
182     public AbstractCacheManager cacheManager() {
183         return new CacheManager();
184     }
185
186     /**
187      * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
188      * populates it with triggers.
189      *
190      * @return New instance of {@link SchedulerFactoryBean}
191      * @throws Exception
192      */
193     // @Bean // ANNOTATION COMMENTED OUT
194     // APPLICATIONS REQUIRING QUARTZ SHOULD RESTORE ANNOTATION
195     public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
196         SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
197         scheduler.setTriggers(schedulerRegistryAdapter.getTriggers());
198         scheduler.setConfigLocation(appApplicationContext.getResource("WEB-INF/conf/quartz.properties"));
199         scheduler.setDataSource(dataSource());
200         return scheduler;
201     }
202
203     /**
204      * Sets the scheduler registry adapter.
205      *
206      * @param schedulerRegistryAdapter
207      */
208     @Autowired
209     public void setSchedulerRegistryAdapter(final RegistryAdapter schedulerRegistryAdapter) {
210         this.schedulerRegistryAdapter = schedulerRegistryAdapter;
211     }
212
213     @Bean
214     public LoginStrategy loginStrategy() {
215         return new LoginStrategyImpl();
216     }
217
218     /**
219      * Gets the value of the property {@link SystemProperties#PREFERRED_TEST_QUERY};
220      * defaults to "Select 1" if the property is not defined.
221      *
222      * @return String value that is a SQL query
223      */
224     private String getPreferredTestQuery() {
225         // Use simple default
226         String preferredTestQueryStr = "SELECT 1";
227         if (SystemProperties.containsProperty(SystemProperties.PREFERRED_TEST_QUERY)) {
228             preferredTestQueryStr = SystemProperties.getProperty(SystemProperties.PREFERRED_TEST_QUERY);
229             LOGGER.debug(EELFLoggerDelegate.debugLogger, "getPreferredTestQuery: property key {} value is {}",
230                     SystemProperties.PREFERRED_TEST_QUERY, preferredTestQueryStr);
231         } else {
232             LOGGER.info(EELFLoggerDelegate.errorLogger,
233                     "getPreferredTestQuery: property key {} not found, using default value {}",
234                     SystemProperties.PREFERRED_TEST_QUERY, preferredTestQueryStr);
235         }
236         return preferredTestQueryStr;
237     }
238
239     /**
240      * Gets the value of the property
241      * {@link SystemProperties#TEST_CONNECTION_ON_CHECKOUT}; defaults to true if the
242      * property is not defined.
243      *
244      * @return Boolean value
245      */
246     private Boolean getConnectionOnCheckout() {
247         // Default to true, always test connection
248         boolean testConnectionOnCheckout = true;
249         if (SystemProperties.containsProperty(SystemProperties.TEST_CONNECTION_ON_CHECKOUT)) {
250             testConnectionOnCheckout = Boolean
251                     .valueOf(SystemProperties.getProperty(SystemProperties.TEST_CONNECTION_ON_CHECKOUT));
252             LOGGER.debug(EELFLoggerDelegate.debugLogger, "getConnectionOnCheckout: property key {} value is {}",
253                     SystemProperties.TEST_CONNECTION_ON_CHECKOUT, testConnectionOnCheckout);
254         } else {
255             LOGGER.info(EELFLoggerDelegate.errorLogger,
256                     "getConnectionOnCheckout: property key {} not found, using default value {}",
257                     SystemProperties.TEST_CONNECTION_ON_CHECKOUT, testConnectionOnCheckout);
258         }
259         return testConnectionOnCheckout;
260     }
261 }