Fix NPE under MDCSetup for response code 422
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / MDCSetup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2019 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.onap.logging.filter.base;
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.time.ZoneOffset;
26 import java.time.ZonedDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.time.format.DateTimeFormatterBuilder;
29 import java.time.temporal.ChronoUnit;
30 import java.util.Base64;
31 import java.util.UUID;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.Response;
35 import org.onap.logging.ref.slf4j.ONAPLogConstants;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.slf4j.MDC;
39
40 public class MDCSetup {
41
42     protected static Logger logger = LoggerFactory.getLogger(MDCSetup.class);
43     private static final String INSTANCE_UUID = UUID.randomUUID().toString();
44     protected static final String serverIpAddressOverride = "SERVER_IP_ADDRESS_OVERRIDE";
45     protected static final String serverFqdnOverride = "SERVER_FQDN_OVERRIDE";
46     protected static final String INSTANT_PRECISION_OVERRIDE = "INSTANT_PRECISION_OVERRIDE";
47     protected static final String checkHeaderLogPattern = "Checking {} header to determine the value of {}";
48     protected String serverFqdn;
49     protected String serverIpAddress;
50     protected String[] prioritizedIdHeadersNames;
51     protected String[] prioritizedPartnerHeadersNames;
52     protected DateTimeFormatter iso8601Formatter;
53
54     public MDCSetup() {
55         this.prioritizedIdHeadersNames =
56                 new String[] {ONAPLogConstants.Headers.REQUEST_ID, Constants.HttpHeaders.HEADER_REQUEST_ID,
57                         Constants.HttpHeaders.TRANSACTION_ID, Constants.HttpHeaders.ECOMP_REQUEST_ID};
58         this.prioritizedPartnerHeadersNames =
59                 new String[] {HttpHeaders.AUTHORIZATION, ONAPLogConstants.Headers.PARTNER_NAME, HttpHeaders.USER_AGENT};
60         initServerFqdnandIp();
61         this.iso8601Formatter = createFormatter();
62     }
63
64     protected String getCurrentTimeStamp() {
65         return ZonedDateTime.now(ZoneOffset.UTC).format(iso8601Formatter);
66     }
67
68     protected DateTimeFormatter createFormatter() {
69         DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
70         try {
71             Integer instantPrecision = Integer.valueOf(System.getProperty(INSTANT_PRECISION_OVERRIDE, "3"));
72             builder.appendInstant(instantPrecision);
73         } catch (NumberFormatException nfe) {
74             logger.warn("instant precision could not be read and thus won't be set, the default will be used instead."
75                     + nfe.getMessage());
76         }
77         return builder.toFormatter();
78     }
79
80     public void setInstanceID() {
81         MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID);
82     }
83
84     protected void initServerFqdnandIp() {
85         serverFqdn = getProperty(serverFqdnOverride);
86         serverIpAddress = getProperty(serverIpAddressOverride);
87
88         if (serverIpAddress.equals(Constants.DefaultValues.UNKNOWN)
89                 || serverFqdn.equals(Constants.DefaultValues.UNKNOWN)) {
90             try {
91                 InetAddress addr = InetAddress.getLocalHost();
92                 if (serverFqdn.equals(Constants.DefaultValues.UNKNOWN)) {
93                     serverFqdn = addr.getCanonicalHostName();
94                 }
95                 if (serverIpAddress.equals(Constants.DefaultValues.UNKNOWN)) {
96                     serverIpAddress = addr.getHostAddress();
97                 }
98             } catch (UnknownHostException e) {
99                 logger.trace("Cannot Resolve Host Name." + e.getMessage());
100             }
101         }
102     }
103
104     public void setServerFQDN() {
105         MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFqdn);
106         MDC.put(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS, serverIpAddress);
107     }
108
109     public void setClientIPAddress(HttpServletRequest httpServletRequest) {
110         String clientIpAddress = "";
111         if (httpServletRequest != null) {
112             // This logic is to avoid setting the client ip address to that of the load
113             // balancer in front of the application
114             String getForwadedFor = httpServletRequest.getHeader("X-Forwarded-For");
115             if (getForwadedFor != null) {
116                 clientIpAddress = getForwadedFor;
117             } else {
118                 clientIpAddress = httpServletRequest.getRemoteAddr();
119             }
120         }
121         MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, clientIpAddress);
122     }
123
124     public void setEntryTimeStamp() {
125         MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, getCurrentTimeStamp());
126     }
127
128     public String getRequestId(SimpleMap headers) {
129         String requestId = null;
130         for (String headerName : this.prioritizedIdHeadersNames) {
131             logger.trace(checkHeaderLogPattern, headerName, ONAPLogConstants.Headers.REQUEST_ID);
132             requestId = headers.get(headerName);
133             if (requestId != null && !requestId.isEmpty()) {
134                 return requestId;
135             }
136         }
137         logger.trace("No valid requestId headers. Generating requestId: {}", requestId);
138         return UUID.randomUUID().toString();
139     }
140
141     public void setInvocationId(SimpleMap headers) {
142         String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
143         if (invocationId == null || invocationId.isEmpty())
144             invocationId = UUID.randomUUID().toString();
145         MDC.put(ONAPLogConstants.MDCs.SERVER_INVOCATION_ID, invocationId);
146         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
147     }
148
149     public void setMDCPartnerName(SimpleMap headers) {
150         String partnerName = getMDCPartnerName(headers);
151         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
152     }
153
154     protected String getMDCPartnerName(SimpleMap headers) {
155         String partnerName = null;
156         for (String headerName : prioritizedPartnerHeadersNames) {
157             logger.trace(checkHeaderLogPattern, headerName, ONAPLogConstants.MDCs.PARTNER_NAME);
158             if (headerName.equals(HttpHeaders.AUTHORIZATION)) {
159                 partnerName = getBasicAuthUserName(headers);
160             } else {
161                 partnerName = headers.get(headerName);
162             }
163             if (partnerName != null && !partnerName.isEmpty()) {
164                 return partnerName;
165             }
166
167         }
168         logger.trace("{} value could not be determined, defaulting partnerName to {}.",
169                 ONAPLogConstants.MDCs.PARTNER_NAME, Constants.DefaultValues.UNKNOWN);
170         return Constants.DefaultValues.UNKNOWN;
171     }
172
173     public void setLogTimestamp() {
174         MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP, getCurrentTimeStamp());
175     }
176
177     public void setElapsedTime() {
178         try {
179             DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
180             ZonedDateTime entryTimestamp =
181                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), timeFormatter);
182             ZonedDateTime endTimestamp =
183                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
184
185             MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
186                     Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
187         } catch (Exception e) {
188             logger.trace("Unable to calculate elapsed time due to error: {}", e.getMessage());
189         }
190     }
191
192     public void setElapsedTimeInvokeTimestamp() {
193         try {
194             DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
195             ZonedDateTime entryTimestamp =
196                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), timeFormatter);
197             ZonedDateTime endTimestamp =
198                     ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
199
200             MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
201                     Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
202         } catch (Exception e) {
203             logger.trace("Unable to calculate elapsed time due to error: {}", e.getMessage());
204         }
205     }
206
207     public void setResponseStatusCode(int code) {
208         String statusCode;
209         if (Response.Status.Family.familyOf(code).equals(Response.Status.Family.SUCCESSFUL)) {
210             statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
211         } else {
212             statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
213             setErrorCode(code);
214             setErrorDescription(code);
215         }
216         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
217     }
218
219     public void setTargetEntity(ONAPComponentsList targetEntity) {
220         MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity.toString());
221     }
222
223     public void clearClientMDCs() {
224         MDC.remove(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID);
225         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
226         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
227         MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
228         MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
229         MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
230         MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);
231         MDC.remove(ONAPLogConstants.MDCs.ERROR_CODE);
232         MDC.remove(ONAPLogConstants.MDCs.ERROR_DESC);
233     }
234
235     public void setResponseDescription(int statusCode) {
236         MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, extractDescription(statusCode));
237     }
238
239     private String extractDescription(int statusCode) {
240         Response.Status responseStatus = Response.Status.fromStatusCode(statusCode);
241         if (responseStatus != null) {
242             return responseStatus.toString();
243         }
244         CustomResponseStatus customResponseStatus = CustomResponseStatus.fromStatusCode(statusCode);
245         if (customResponseStatus != null) {
246             return customResponseStatus.toString();
247         }
248         return String.format("Unknown description for response code %d.", statusCode);
249     }
250
251     public void setErrorCode(int statusCode) {
252         MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(statusCode));
253     }
254
255     public void setErrorDescription(int statusCode) {
256         MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, extractDescription(statusCode));
257     }
258
259     public String getProperty(String property) {
260         String propertyValue = System.getProperty(property);
261         if (propertyValue == null || propertyValue.isEmpty()) {
262             propertyValue = System.getenv(property);
263             if (propertyValue == null || propertyValue.isEmpty()) {
264                 propertyValue = Constants.DefaultValues.UNKNOWN;
265             }
266         }
267         return propertyValue;
268     }
269
270     protected String getBasicAuthUserName(SimpleMap headers) {
271         String encodedAuthorizationValue = headers.get(HttpHeaders.AUTHORIZATION);
272         if (encodedAuthorizationValue != null && encodedAuthorizationValue.startsWith("Basic")) {
273             try {
274                 // This will strip the word Basic and single space
275                 encodedAuthorizationValue = encodedAuthorizationValue.substring(6);
276                 byte[] decodedBytes = Base64.getDecoder().decode(encodedAuthorizationValue);
277                 String decodedString = new String(decodedBytes);
278                 int idx = decodedString.indexOf(':');
279                 return decodedString.substring(0, idx);
280             } catch (IllegalArgumentException e) {
281                 logger.error("could not decode basic auth value " + encodedAuthorizationValue, e);
282             }
283         }
284         return null;
285     }
286 }