ef199f7d05ed1ea35c647137a2ccc74af4a93c28
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / synchronizer / filter / ElasticSearchSynchronizerFilter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.synchronizer.filter;
24
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28
29 import javax.servlet.Filter;
30 import javax.servlet.FilterChain;
31 import javax.servlet.FilterConfig;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletRequest;
34 import javax.servlet.ServletResponse;
35
36 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
37 import org.onap.aai.sparky.logging.AaiUiMsgs;
38 import org.onap.aai.sparky.synchronizer.SyncHelper;
39 import org.onap.aai.sparky.util.NodeUtils;
40 import org.onap.aai.cl.api.Logger;
41 import org.onap.aai.cl.eelf.LoggerFactory;
42 import org.onap.aai.cl.mdc.MdcContext;
43
44 /*
45  * This is a wire-frame for an experiment to get the jetty filter-lifecyle initialization method to
46  * setup a scheduled thread executor with an ElasticSearchSynchronization task, which (I'm hoping)
47  * will allow us to do periodic ES <=> AAI synchronization.
48  * 
49  * Alternatively, if the embedded java approach doesn't work we could try instead to do a
50  * System.exec( "perl refreshElasticSearchInstance.pl"). We have two options, I'm hoping the
51  * embedded options will work for us.
52  */
53
54 /**
55  * The Class ElasticSearchSynchronizerFilter.
56  */
57 public class ElasticSearchSynchronizerFilter implements Filter {
58
59   private static final Logger LOG =
60       LoggerFactory.getInstance().getLogger(ElasticSearchSynchronizerFilter.class);
61
62   private SyncHelper syncHelper;
63
64   /*
65    * (non-Javadoc)
66    * 
67    * @see javax.servlet.Filter#destroy()
68    */
69   @Override
70   public void destroy() {
71
72     if (syncHelper != null) {
73       syncHelper.shutdown();
74     }
75   }
76
77   /*
78    * (non-Javadoc)
79    * 
80    * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
81    * javax.servlet.FilterChain)
82    */
83   @Override
84   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
85       throws IOException, ServletException {
86
87     /*
88      * However, we will setup the filtermap with a url that should never get it, so we shouldn't
89      * ever be in here.
90      */
91
92     chain.doFilter(request, response);
93   }
94
95   /*
96    * (non-Javadoc)
97    * 
98    * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
99    */
100   @Override
101   public void init(FilterConfig filterConfig) throws ServletException {
102     String txnID = NodeUtils.getRandomTxnId();
103     MdcContext.initialize(txnID, "ElasticSearchSynchronizerFilter", "", "Init", "");
104
105     LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "init()");
106
107     try {
108       new SyncHelper(OxmModelLoader.getInstance());
109     } catch (Exception exc) {
110       throw new ServletException("Caught an exception while initializing filter", exc);
111     }
112
113   }
114
115   /**
116    * @return the syncHelper
117    */
118   public SyncHelper getSyncHelper() {
119     return syncHelper;
120   }
121
122   /**
123    * @param syncHelper the syncHelper to set
124    */
125   public void setSyncHelper(SyncHelper syncHelper) {
126     this.syncHelper = syncHelper;
127   }
128
129   /**
130    * @return the log
131    */
132   public static Logger getLog() {
133     return LOG;
134   }
135
136 }