Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / filter / ElasticSearchSynchronizerFilter.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file 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  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.synchronizer.filter;
27
28 import java.io.IOException;
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
31
32 import javax.servlet.Filter;
33 import javax.servlet.FilterChain;
34 import javax.servlet.FilterConfig;
35 import javax.servlet.ServletException;
36 import javax.servlet.ServletRequest;
37 import javax.servlet.ServletResponse;
38
39 import org.openecomp.cl.api.Logger;
40 import org.openecomp.cl.eelf.LoggerFactory;
41 import org.openecomp.sparky.config.oxm.OxmModelLoader;
42 import org.openecomp.sparky.logging.AaiUiMsgs;
43 import org.openecomp.sparky.synchronizer.SyncHelper;
44 import org.openecomp.sparky.util.NodeUtils;
45
46 import org.openecomp.cl.mdc.MdcContext;
47
48 /*
49  * This is a wire-frame for an experiment to get the jetty filter-lifecyle initialization method to
50  * setup a scheduled thread executor with an ElasticSearchSynchronization task, which (I'm hoping)
51  * will allow us to do periodic ES <=> AAI synchronization.
52  * 
53  * Alternatively, if the embedded java approach doesn't work we could try instead to do a
54  * System.exec( "perl refreshElasticSearchInstance.pl"). We have two options, I'm hoping the
55  * embedded options will work for us.
56  */
57
58 /**
59  * The Class ElasticSearchSynchronizerFilter.
60  */
61 public class ElasticSearchSynchronizerFilter implements Filter {
62
63   private static final Logger LOG = LoggerFactory.getInstance().getLogger(ElasticSearchSynchronizerFilter.class);
64
65   private SyncHelper syncHelper;
66
67   /* (non-Javadoc)
68    * @see javax.servlet.Filter#destroy()
69    */
70   @Override
71   public void destroy() {
72
73     if (syncHelper != null) {
74       syncHelper.shutdown();
75     }
76   }
77
78   /* (non-Javadoc)
79    * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
80    */
81   @Override
82   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
83       throws IOException, ServletException {
84
85     /*
86      * However, we will setup the filtermap with a url that should never get it, so we shouldn't
87      * ever be in here.
88      */
89
90     chain.doFilter(request, response);
91   }
92
93   /* (non-Javadoc)
94    * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
95    */
96   @Override
97   public void init(FilterConfig filterConfig) throws ServletException {
98         String txnID = NodeUtils.getRandomTxnId();
99         MdcContext.initialize(txnID, "ElasticSearchSynchronizerFilter", "", "Init", "");
100             
101         LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "init()");
102
103     try {
104       new SyncHelper(OxmModelLoader.getInstance());
105     } catch (Exception exc) {
106       throw new ServletException("Caught an exception while initializing filter", exc);
107     }
108
109   }
110
111 }