7b44add924dfff8b85a44fa69fa4b92bf333b384
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / conf / ExternalAppConfig.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.conf;
39
40 import static com.att.eelf.configuration.Configuration.MDC_ALERT_SEVERITY;
41 import static com.att.eelf.configuration.Configuration.MDC_INSTANCE_UUID;
42 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
43 import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
44 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID;
45 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
46
47 import java.net.InetAddress;
48 import java.text.SimpleDateFormat;
49 import java.util.ArrayList;
50 import java.util.Calendar;
51 import java.util.List;
52
53 import javax.annotation.PostConstruct;
54
55 import org.openecomp.portalapp.authentication.LoginStrategy;
56 import org.openecomp.portalapp.authentication.OpenIdConnectLoginStrategy;
57 import org.openecomp.portalapp.authentication.SimpleLoginStrategy;
58 import org.openecomp.portalapp.controller.core.LogoutController;
59 import org.openecomp.portalapp.controller.core.SDKLoginController;
60 import org.openecomp.portalapp.portal.interceptor.PortalResourceInterceptor;
61 import org.openecomp.portalapp.portal.interceptor.SessionTimeoutInterceptor;
62 import org.openecomp.portalapp.portal.listener.HealthMonitor;
63 import org.openecomp.portalapp.portal.service.EPLoginService;
64 import org.openecomp.portalapp.portal.service.EPLoginServiceImpl;
65 import org.openecomp.portalapp.portal.ueb.EPUebHelper;
66 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
67 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
68 import org.openecomp.portalapp.scheduler.RegistryAdapter;
69 import org.openecomp.portalapp.uebhandler.FunctionalMenuHandler;
70 import org.openecomp.portalapp.uebhandler.InitUebHandler;
71 import org.openecomp.portalapp.uebhandler.MainUebHandler;
72 import org.openecomp.portalapp.uebhandler.WidgetNotificationHandler;
73 import org.openecomp.portalsdk.core.conf.AppConfig;
74 import org.openecomp.portalsdk.core.conf.Configurable;
75 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
76 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
77 import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager;
78 import org.openecomp.portalsdk.core.service.DataAccessService;
79 import org.openecomp.portalsdk.core.service.FnMenuService;
80 import org.openecomp.portalsdk.core.service.FnMenuServiceImpl;
81 import org.openecomp.portalsdk.core.util.CacheManager;
82 import org.openecomp.portalsdk.core.util.SystemProperties;
83 import org.slf4j.MDC;
84 import org.springframework.beans.factory.annotation.Autowired;
85 import org.springframework.context.annotation.Bean;
86 import org.springframework.context.annotation.ComponentScan;
87 import org.springframework.context.annotation.ComponentScan.Filter;
88 import org.springframework.context.annotation.Configuration;
89 import org.springframework.context.annotation.FilterType;
90 import org.springframework.context.annotation.Profile;
91 import org.springframework.scheduling.annotation.EnableAsync;
92 import org.springframework.scheduling.annotation.EnableScheduling;
93 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
94 import org.springframework.web.servlet.ViewResolver;
95 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
96 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
97 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
98
99 @Configuration
100 @EnableWebMvc
101 @ComponentScan(basePackages = { "org.openecomp" }, excludeFilters = {
102                 @Filter(value = { LogoutController.class, SDKLoginController.class }, type = FilterType.ASSIGNABLE_TYPE) })
103 @Profile("src")
104 @EnableAsync
105 @EnableScheduling
106 public class ExternalAppConfig extends AppConfig implements Configurable {
107
108         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppConfig.class);
109
110         private RegistryAdapter schedulerRegistryAdapter;
111
112         public ViewResolver viewResolver() {
113                 return super.viewResolver();
114         }
115
116         @Override
117         public void addResourceHandlers(ResourceHandlerRegistry registry) {
118                 registry.addResourceHandler("/app/fusion/**").addResourceLocations("/app/fusion/");             
119                 registry.addResourceHandler("/static/**").addResourceLocations("/static/");
120                 registry.addResourceHandler("/images/**").addResourceLocations("/images/");
121                 registry.addResourceHandler("/**").addResourceLocations("/public/");
122         }
123
124         @PostConstruct
125         private void init() {
126                 try {
127                         // Loading defaults
128                         MDC.put(MDC_SERVICE_NAME, EPSystemProperties.ECOMP_PORTAL_BE);
129                         MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
130                         MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
131                         MDC.put(MDC_SERVICE_INSTANCE_ID, "");
132                         MDC.put(MDC_ALERT_SEVERITY, AlarmSeverityEnum.INFORMATIONAL.toString());
133                         MDC.put(MDC_INSTANCE_UUID, SystemProperties.getProperty(SystemProperties.INSTANCE_UUID));
134                 } catch (Exception e) {
135                         logger.error(EELFLoggerDelegate.errorLogger, "init failed", e);
136                 }
137         }
138
139         /*
140          * /
141          * 
142          * @PostConstruct // file://${catalina.home}/conf/log4j.properties public
143          * void initLog4j() throws FileNotFoundException { try { URL[] classpathurls
144          * = ((URLClassLoader)
145          * (Thread.currentThread().getContextClassLoader())).getURLs(); for (URL url
146          * : classpathurls) { System.out.println(url.getFile().toString()); }
147          * Log4jConfigurer.initLogging(
148          * "file://${catalina.home}/conf/log4j.properties"); } catch
149          * (FileNotFoundException e) { ((URLClassLoader)
150          * (Thread.currentThread().getContextClassLoader())).getURLs();
151          * Log4jConfigurer.initLogging("classpath:../conf/log4j.properties"); } } /
152          **/
153
154         public DataAccessService dataAccessService() {
155                 return super.dataAccessService();
156         }
157
158         public String[] tileDefinitions() {
159                 return super.tileDefinitions();
160         }
161
162         public List<String> addTileDefinitions() {
163                 List<String> definitions = new ArrayList<String>();
164                 definitions.add("/WEB-INF/defs/definitions.xml");
165                 return definitions;
166         }
167
168         @Bean
169         public AbstractCacheManager cacheManager() {
170                 return new CacheManager();
171         }
172
173         @Bean
174         public SessionTimeoutInterceptor sessionTimeoutInterceptor() {
175                 return new SessionTimeoutInterceptor();
176         }
177
178         @Bean
179         public PortalResourceInterceptor portalResourceInterceptor() {
180                 return new PortalResourceInterceptor();
181         }
182
183         @Bean
184         public EPLoginService eploginService() {
185                 return new EPLoginServiceImpl();
186         }
187
188         @Bean
189         public org.openecomp.portalsdk.core.auth.LoginStrategy coreLoginStrategy() {
190                 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC"))
191                         return new OpenIdConnectLoginStrategy();
192                 else
193                         return new SimpleLoginStrategy();
194         }
195
196         @Bean
197         public LoginStrategy loginStrategy() {
198
199                 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC"))
200                         return new OpenIdConnectLoginStrategy();
201                 else
202                         return new SimpleLoginStrategy();
203         }
204
205         public FnMenuService fnMenuService() {
206                 return new FnMenuServiceImpl();
207         }
208
209         @Override
210         public void addInterceptors(InterceptorRegistry registry) {
211                 // registry.addInterceptor(new
212                 // StaticResourcesInterceptor()).addPathPatterns("/index.htm",
213                 // "/applicationsHome", "/widgetsHome", "/admins", "/users",
214                 // "/applications", "/widgets");
215                 // Excludes login/logout pages and REST endpoints used by other
216                 // application servers.
217
218                 registry.addInterceptor(sessionTimeoutInterceptor()).excludePathPatterns("/oid-login", "/portalApi/healthCheck",
219                                 "/portalApi/healthCheck/", "/portalApi/healthCheckSuspend", "/portalApi/healthCheckSuspend/",
220                                 "/portalApi/healthCheckResume", "/portalApi/healthCheckResume/", "/login_external",
221                                 "/login_external.htm*", "login", "/login.htm*","/auxapi/*","/context/*", "/api*",
222                                 "/single_signon.htm", "/single_signon", "/dashboard", "/OpenSourceLogin.htm");
223
224                 registry.addInterceptor(portalResourceInterceptor());
225
226         }
227
228         /**
229          * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
230          * populates it with triggers.
231          *
232          * @return New instance of {@link SchedulerFactoryBean}
233          */
234
235         @Bean
236         public EPUebHelper epUebHelper() {
237                 return new EPUebHelper();
238         }
239
240         @Bean
241         public HealthMonitor healthMonitor() {
242                 return new HealthMonitor();
243         }
244
245         /**
246          * Creates and returns a new instance of a {@link MainUebHandler}.
247          * 
248          * @return New instance of {@link MainUebHandler}.
249          */
250         @Bean
251         public MainUebHandler mainUebHandler() {
252                 return new MainUebHandler();
253         }
254
255         /**
256          * Creates and returns a new instance of a {@link InitUebHandler}.
257          * 
258          * @return New instance of {@link InitUebHandler}.
259          */
260         @Bean
261         public InitUebHandler initUebHandler() {
262                 return new InitUebHandler();
263         }
264
265         /**
266          * Creates and returns a new instance of a {@link WidgetNotificationHandler}
267          * .
268          * 
269          * @return New instance of {@link WidgetNotificationHandler}.
270          */
271         @Bean
272         public WidgetNotificationHandler widgetNotificationHandler() {
273                 return new WidgetNotificationHandler();
274         }
275
276         /**
277          * Creates and returns a new instance of a {@link FunctionalMenuHandler} .
278          * 
279          * @return New instance of {@link FunctionalMenuHandler}.
280          */
281         @Bean
282         public FunctionalMenuHandler functionalMenuHandler() {
283                 return new FunctionalMenuHandler();
284         }
285
286         /**
287          * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
288          * populates it with triggers.
289          *
290          * @return New instance of {@link SchedulerFactoryBean}
291          * @throws Exception if dataSource fails
292          */
293         // APPLICATIONS REQUIRING QUARTZ SHOULD RESTORE ANNOTATION
294         @Bean // ANNOTATION COMMENTED OUT
295         public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
296                 SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
297                 scheduler.setConfigLocation(appApplicationContext.getResource("WEB-INF/conf/quartz.properties"));
298                 scheduler.setDataSource(dataSource());
299                 scheduler.setTriggers(schedulerRegistryAdapter.getTriggers());
300                 scheduler.setSchedulerName(getScheduleName());
301                 return scheduler;
302         }
303
304         protected String getScheduleName() {
305                 final String CRON_SITE_NAME = "cron_site_name";
306                 String cronSiteVal = "Default";
307                 try {
308                         cronSiteVal = SystemProperties.getProperty(CRON_SITE_NAME);
309                 } catch (Exception e) {
310                         logger.error(EELFLoggerDelegate.errorLogger, "getScheduleName failed", e);
311                         logger.warn(EELFLoggerDelegate.errorLogger,
312                                         "Cron site name not added in property file, using Default value");
313                 }
314
315                 String cronSiteName = cronSiteVal != null ? cronSiteVal : "";
316
317                 SimpleDateFormat dateFormat = new SimpleDateFormat();
318                 dateFormat.applyPattern("YYYYMMdd");
319                 String currentDateStr = dateFormat.format(Calendar.getInstance().getTime());
320
321                 return "Scheduler" + "_" + currentDateStr + "_" + cronSiteName;
322         }
323
324         /**
325          * Sets the scheduler registry adapter.
326          *
327          * @param schedulerRegistryAdapter
328          *            Scheduler registry adapter
329          */
330         @Autowired
331         public void setSchedulerRegistryAdapter(final RegistryAdapter schedulerRegistryAdapter) {
332                 this.schedulerRegistryAdapter = schedulerRegistryAdapter;
333         }
334
335 }