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