92d63742eff28a662f8a7454d0c4db91f797872e
[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("/app/fusion/**").addResourceLocations("/app/fusion/");             
101                 registry.addResourceHandler("/static/**").addResourceLocations("/static/");
102                 registry.addResourceHandler("/images/**").addResourceLocations("/images/");
103                 registry.addResourceHandler("/**").addResourceLocations("/public/");
104         }
105
106         @PostConstruct
107         private void init() {
108                 try {
109                         // Loading defaults
110                         MDC.put(MDC_SERVICE_NAME, EPSystemProperties.ECOMP_PORTAL_BE);
111                         MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
112                         MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
113                         MDC.put(MDC_SERVICE_INSTANCE_ID, "");
114                         MDC.put(MDC_ALERT_SEVERITY, AlarmSeverityEnum.INFORMATIONAL.toString());
115                         MDC.put(MDC_INSTANCE_UUID, SystemProperties.getProperty(SystemProperties.INSTANCE_UUID));
116                 } catch (Exception e) {
117                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
118                 }
119         }
120
121         /*
122          * /
123          * 
124          * @PostConstruct // file://${catalina.home}/conf/log4j.properties public
125          * void initLog4j() throws FileNotFoundException { try { URL[] classpathurls
126          * = ((URLClassLoader)
127          * (Thread.currentThread().getContextClassLoader())).getURLs(); for (URL url
128          * : classpathurls) { System.out.println(url.getFile().toString()); }
129          * Log4jConfigurer.initLogging(
130          * "file://${catalina.home}/conf/log4j.properties"); } catch
131          * (FileNotFoundException e) { ((URLClassLoader)
132          * (Thread.currentThread().getContextClassLoader())).getURLs();
133          * Log4jConfigurer.initLogging("classpath:../conf/log4j.properties"); } } /
134          **/
135
136         public DataAccessService dataAccessService() {
137                 return super.dataAccessService();
138         }
139
140         public String[] tileDefinitions() {
141                 return super.tileDefinitions();
142         }
143
144         public List<String> addTileDefinitions() {
145                 List<String> definitions = new ArrayList<String>();
146                 definitions.add("/WEB-INF/defs/definitions.xml");
147                 return definitions;
148         }
149
150         @Bean
151         public AbstractCacheManager cacheManager() {
152                 return new CacheManager();
153         }
154
155         @Bean
156         public SessionTimeoutInterceptor sessionTimeoutInterceptor() {
157                 return new SessionTimeoutInterceptor();
158         }
159
160         @Bean
161         public PortalResourceInterceptor portalResourceInterceptor() {
162                 return new PortalResourceInterceptor();
163         }
164
165         @Bean
166         public EPLoginService eploginService() {
167                 return new EPLoginServiceImpl();
168         }
169
170         @Bean
171         public org.openecomp.portalsdk.core.auth.LoginStrategy coreLoginStrategy() {
172                 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC"))
173                         return new OpenIdConnectLoginStrategy();
174                 else
175                         return new SimpleLoginStrategy();
176         }
177
178         @Bean
179         public LoginStrategy loginStrategy() {
180
181                 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC"))
182                         return new OpenIdConnectLoginStrategy();
183                 else
184                         return new SimpleLoginStrategy();
185         }
186
187         public FnMenuService fnMenuService() {
188                 return new FnMenuServiceImpl();
189         }
190
191         @Override
192         public void addInterceptors(InterceptorRegistry registry) {
193                 // registry.addInterceptor(new
194                 // StaticResourcesInterceptor()).addPathPatterns("/index.htm",
195                 // "/applicationsHome", "/widgetsHome", "/admins", "/users",
196                 // "/applications", "/widgets");
197                 // Excludes login/logout pages and REST endpoints used by other
198                 // application servers.
199
200                 registry.addInterceptor(sessionTimeoutInterceptor()).excludePathPatterns("/oid-login", "/portalApi/healthCheck",
201                                 "/portalApi/healthCheck/", "/portalApi/healthCheckSuspend", "/portalApi/healthCheckSuspend/",
202                                 "/portalApi/healthCheckResume", "/portalApi/healthCheckResume/", "/login_external",
203                                 "/login_external.htm*", "login", "/login.htm*","/auxapi/*","/context/*", "/api*",
204                                 "/single_signon.htm", "/single_signon", "/dashboard", "/OpenSourceLogin.htm");
205
206                 registry.addInterceptor(portalResourceInterceptor());
207
208         }
209
210         /**
211          * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
212          * populates it with triggers.
213          *
214          * @return New instance of {@link SchedulerFactoryBean}
215          */
216
217         @Bean
218         public EPUebHelper epUebHelper() {
219                 return new EPUebHelper();
220         }
221
222         @Bean
223         public HealthMonitor healthMonitor() {
224                 return new HealthMonitor();
225         }
226
227         /**
228          * Creates and returns a new instance of a {@link MainUebHandler}.
229          * 
230          * @return New instance of {@link MainUebHandler}.
231          */
232         @Bean
233         public MainUebHandler mainUebHandler() {
234                 return new MainUebHandler();
235         }
236
237         /**
238          * Creates and returns a new instance of a {@link InitUebHandler}.
239          * 
240          * @return New instance of {@link InitUebHandler}.
241          */
242         @Bean
243         public InitUebHandler initUebHandler() {
244                 return new InitUebHandler();
245         }
246
247         /**
248          * Creates and returns a new instance of a {@link WidgetNotificationHandler}
249          * .
250          * 
251          * @return New instance of {@link WidgetNotificationHandler}.
252          */
253         @Bean
254         public WidgetNotificationHandler widgetNotificationHandler() {
255                 return new WidgetNotificationHandler();
256         }
257
258         /**
259          * Creates and returns a new instance of a {@link FunctionalMenuHandler} .
260          * 
261          * @return New instance of {@link FunctionalMenuHandler}.
262          */
263         @Bean
264         public FunctionalMenuHandler functionalMenuHandler() {
265                 return new FunctionalMenuHandler();
266         }
267
268         /**
269          * Creates and returns a new instance of a {@link SchedulerFactoryBean} and
270          * populates it with triggers.
271          *
272          * @return New instance of {@link SchedulerFactoryBean}
273          * @throws Exception if dataSource fails
274          */
275         // APPLICATIONS REQUIRING QUARTZ SHOULD RESTORE ANNOTATION
276         @Bean // ANNOTATION COMMENTED OUT
277         public SchedulerFactoryBean schedulerFactoryBean() throws Exception {
278                 SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
279                 scheduler.setConfigLocation(appApplicationContext.getResource("WEB-INF/conf/quartz.properties"));
280                 scheduler.setDataSource(dataSource());
281                 scheduler.setTriggers(schedulerRegistryAdapter.getTriggers());
282                 scheduler.setSchedulerName(getScheduleName());
283                 return scheduler;
284         }
285
286         protected String getScheduleName() {
287                 final String CRON_SITE_NAME = "cron_site_name";
288                 String cronSiteVal = "Default";
289                 try {
290                         cronSiteVal = SystemProperties.getProperty(CRON_SITE_NAME);
291                 } catch (Exception e) {
292                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
293                         logger.warn(EELFLoggerDelegate.errorLogger,
294                                         "Cron site name not added in property file, using Default value");
295                 }
296
297                 String cronSiteName = cronSiteVal != null ? cronSiteVal : "";
298
299                 SimpleDateFormat dateFormat = new SimpleDateFormat();
300                 dateFormat.applyPattern("YYYYMMdd");
301                 String currentDateStr = dateFormat.format(Calendar.getInstance().getTime());
302
303                 return "Scheduler" + "_" + currentDateStr + "_" + cronSiteName;
304         }
305
306         /**
307          * Sets the scheduler registry adapter.
308          *
309          * @param schedulerRegistryAdapter
310          *            Scheduler registry adapter
311          */
312         @Autowired
313         public void setSchedulerRegistryAdapter(final RegistryAdapter schedulerRegistryAdapter) {
314                 this.schedulerRegistryAdapter = schedulerRegistryAdapter;
315         }
316
317 }