watch tls files
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / listen / BEAppContextListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.listen;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.jar.Attributes;
28 import java.util.jar.Manifest;
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32
33 import org.apache.commons.io.filefilter.FileFilterUtils;
34 import org.apache.commons.io.filefilter.IOFileFilter;
35 import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
36 import org.apache.commons.io.monitor.FileAlterationMonitor;
37 import org.apache.commons.io.monitor.FileAlterationObserver;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
40 import org.openecomp.sdc.be.monitoring.BeMonitoringService;
41 import org.openecomp.sdc.common.api.Constants;
42 import org.openecomp.sdc.common.impl.ExternalConfiguration;
43 import org.openecomp.sdc.common.listener.AppContextListener;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45
46 public class BEAppContextListener extends AppContextListener implements ServletContextListener {
47
48     private static final String MANIFEST_FILE_NAME = "/META-INF/MANIFEST.MF";
49     private static final Logger log = Logger.getLogger(BEAppContextListener.class);
50
51     public void contextInitialized(ServletContextEvent context) {
52         super.contextInitialized(context);
53         ConfigurationManager configurationManager = new ConfigurationManager(ExternalConfiguration.getConfigurationSource());
54         log.debug("loading configuration from configDir: {} appName: {}", ExternalConfiguration.getConfigDir(), ExternalConfiguration.getAppName());
55         context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
56         WebAppContextWrapper webAppContextWrapper = new WebAppContextWrapper();
57         context.getServletContext().setAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR, webAppContextWrapper);
58         context.getServletContext().setAttribute(Constants.ASDC_RELEASE_VERSION_ATTR, getVersionFromManifest(context));
59         // Monitoring service
60         BeMonitoringService bms = new BeMonitoringService(context.getServletContext());
61         bms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(15));
62         initTlsFileMonitoring();
63         log.debug("After executing {}", this.getClass());
64     }
65
66     private String getVersionFromManifest(ServletContextEvent context) {
67         ServletContext servletContext = context.getServletContext();
68         InputStream inputStream = servletContext.getResourceAsStream(MANIFEST_FILE_NAME);
69         String version = null;
70         try {
71             Manifest mf = new Manifest(inputStream);
72             Attributes atts = mf.getMainAttributes();
73             version = atts.getValue(Constants.ASDC_RELEASE_VERSION_ATTR);
74             if (version == null || version.isEmpty()) {
75                 log.warn("failed to read ASDC version from MANIFEST.");
76             } else {
77                 log.info("ASDC version from MANIFEST is {}", version);
78             }
79         } catch (IOException e) {
80             log.warn("failed to read ASDC version from MANIFEST", e);
81         }
82         return version;
83     }
84     
85     private void initTlsFileMonitoring() {
86         final Map<String, IOFileFilter> tlsFileFilters = createTlsFileFilters();
87         if (!tlsFileFilters.isEmpty()) {
88             final TlsFileChangeHandler tlsFileChangeHandler = new TlsFileChangeHandler();
89             tlsFileFilters.entrySet().stream().forEach(entry -> listenForChanges(entry.getKey(), tlsFileChangeHandler, entry.getValue()));
90         }
91     }
92     
93     private Map<String, IOFileFilter> createTlsFileFilters() {        
94         final Map<String, IOFileFilter> filters = new HashMap<>();
95         addFilter(filters, ConfigurationManager.getConfigurationManager().getConfiguration().getTlsCert());
96         addFilter(filters, ConfigurationManager.getConfigurationManager().getConfiguration().getTlsKey());
97         addFilter(filters, ConfigurationManager.getConfigurationManager().getConfiguration().getCaCert());
98         return filters;
99     }
100     
101     private void addFilter(final Map<String, IOFileFilter> filters, final String path) {
102         if (path != null) {
103             final File file = new File(path);
104             final IOFileFilter caCertFileFilter =
105                     FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.nameFileFilter(file.getName()));
106             
107             if (filters.containsKey(file.getParent())) {
108                 filters.put(file.getParent(), FileFilterUtils.or(filters.get(file.getParent()), caCertFileFilter));
109             } else {
110                 filters.put(file.getParent(), caCertFileFilter);  
111             }
112         }
113     }
114     
115     private void listenForChanges(String path, FileAlterationListenerAdaptor changeListener, IOFileFilter ioFileFilter) {
116         FileAlterationMonitor monitor = new FileAlterationMonitor();
117         final FileAlterationObserver observer = new FileAlterationObserver(path, ioFileFilter);
118         observer.addListener(changeListener);
119         monitor.addObserver(observer);
120         try {
121             monitor.start();
122         } catch (final Exception exception) {
123             log.error("Error starting monitoring of TLS files", exception);
124         }
125     }
126 }