31b89464c938445a0241a2f325a8645c68b69ce5
[sdc.git] /
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
21 package org.openecomp.sdc.logging.servlet;
22
23 import org.omg.CORBA.Request;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.slf4j.MDC;
27
28 import javax.servlet.*;
29 import javax.servlet.http.HttpServletRequest;
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.util.UUID;
34 import java.util.concurrent.atomic.AtomicLong;
35
36
37 /**
38  *
39  * <p>Pushes information required by EELF onto MDC (Mapped Diagnostic Context).</p>
40  *
41  * <p>This is servlet filter that should be configured in <i>web.xml</i> to be used. Example:</p>
42  *
43  * <pre>
44  *
45  *  &lt;filter&gt;
46  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
47  *      &lt;filter-class&gt;org.openecomp.sdc.logging.servlet.LoggingFilter&lt;/filter-class&gt;
48  *  &lt;/filter&gt;
49  *
50  *  &lt;filter-mapping&gt;
51  *      &lt;filter-name&gt;LoggingServletFilter&lt;/filter-name&gt;
52  *      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
53  *  &lt;/filter-mapping&gt;
54  *
55  * </pre>
56  *
57  * @author evitaliy
58  * @since 25/07/2016.
59  */
60 public class LoggingFilter implements Filter {
61
62     // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
63     private static final HostAddressCache HOST_ADDRESS = new HostAddressCache();
64     private static final String UNKNOWN = "UNKNOWN";
65
66     private final static Logger log = (Logger) LoggerFactory.getLogger(LoggingFilter.class.getName());
67
68     public void destroy() {
69     }
70
71     public void doFilter(ServletRequest request, ServletResponse response,
72                          FilterChain chain) throws IOException, ServletException {
73
74         try {
75
76             MDC.clear();
77
78             MDC.put("RequestId", UUID.randomUUID().toString());
79             MDC.put("ServiceInstanceId", "N/A"); // not applicable
80             MDC.put("ServiceName", "ASDC");
81             MDC.put("InstanceUUID", "N/A");
82
83             // For some reason chooses IPv4 or IPv6 in a random way
84             MDC.put("RemoteHost", request.getRemoteHost());
85
86             InetAddress host = HOST_ADDRESS.get();
87
88             String ipAddress, hostName;
89             if (host == null) {
90                 ipAddress = UNKNOWN;
91                 hostName = UNKNOWN;
92             } else {
93                 ipAddress = host.getHostAddress();
94                 hostName = host.getHostName();
95             }
96
97             MDC.put("ServerIPAddress", ipAddress);
98             MDC.put("ServerFQDN", hostName);
99
100             if(request instanceof HttpServletRequest) {
101                 String userName = ((HttpServletRequest) request).getHeader("USER_ID");
102                 MDC.put("PartnerName", userName);
103             }
104             // TODO: Clarify what these stand for
105     //        MDC.put("AlertSeverity", );
106     //        MDC.put("Timer", );
107
108             chain.doFilter(request, response);
109
110         } finally {
111             MDC.clear();
112         }
113     }
114
115     public void init(FilterConfig config) throws ServletException { }
116
117     private static class HostAddressCache {
118
119         private static final long REFRESH_TIME = 1000L;
120
121         private AtomicLong lastUpdated = new AtomicLong(0L);
122         private InetAddress hostAddress;
123
124         public InetAddress get() {
125
126             long current = System.currentTimeMillis();
127             if (current - lastUpdated.get() > REFRESH_TIME) {
128
129                 synchronized (this) {
130
131                     try {
132                         lastUpdated.set(current); // set now to register the attempt even if failed
133                         hostAddress = InetAddress.getLocalHost();
134                     } catch (UnknownHostException e) {
135                         log.debug("",e);
136                         hostAddress = null;
137                     }
138                 }
139             }
140
141             return hostAddress;
142         }
143     }
144 }