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