2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.logging.filter.base;
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.temporal.ChronoUnit;
29 import java.util.UUID;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.core.HttpHeaders;
32 import javax.ws.rs.core.Response;
33 import org.onap.logging.ref.slf4j.ONAPLogConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public abstract class AbstractFilter {
40 protected static Logger logger = LoggerFactory.getLogger(AbstractFilter.class);
42 private static final String INSTANCE_UUID = UUID.randomUUID().toString();
44 protected void setInstanceID() {
45 MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID);
48 protected void setServerFQDN() {
49 String serverFQDN = "";
50 InetAddress addr = null;
52 addr = InetAddress.getLocalHost();
53 serverFQDN = addr.getCanonicalHostName();
54 MDC.put(ONAPLogConstants.MDCs.SERVER_IP_ADDRESS, addr.getHostAddress());
55 } catch (UnknownHostException e) {
56 logger.warn("Cannot Resolve Host Name");
59 MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
62 protected void setClientIPAddress(HttpServletRequest httpServletRequest) {
63 String clientIpAddress = "";
64 if (httpServletRequest != null) {
65 // This logic is to avoid setting the client ip address to that of the load
66 // balancer in front of the application
67 String getForwadedFor = httpServletRequest.getHeader("X-Forwarded-For");
68 if (getForwadedFor != null) {
69 clientIpAddress = getForwadedFor;
71 clientIpAddress = httpServletRequest.getRemoteAddr();
74 MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, clientIpAddress);
77 protected void setEntryTimeStamp() {
78 MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
79 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
82 protected String getRequestId(SimpleMap headers) {
83 logger.trace("Checking X-ONAP-RequestID header for requestId.");
84 String requestId = headers.get(ONAPLogConstants.Headers.REQUEST_ID);
85 if (requestId != null && !requestId.isEmpty()) {
89 logger.trace("No valid X-ONAP-RequestID header value. Checking X-RequestID header for requestId.");
90 requestId = headers.get(Constants.HttpHeaders.HEADER_REQUEST_ID);
91 if (requestId != null && !requestId.isEmpty()) {
95 logger.trace("No valid X-RequestID header value. Checking X-TransactionID header for requestId.");
96 requestId = headers.get(Constants.HttpHeaders.TRANSACTION_ID);
97 if (requestId != null && !requestId.isEmpty()) {
101 logger.trace("No valid X-TransactionID header value. Checking X-ECOMP-RequestID header for requestId.");
102 requestId = headers.get(Constants.HttpHeaders.ECOMP_REQUEST_ID);
103 if (requestId != null && !requestId.isEmpty()) {
107 logger.trace("No valid requestId headers. Generating requestId: {}", requestId);
108 return UUID.randomUUID().toString();
111 protected void setInvocationId(SimpleMap headers) {
112 String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
113 if (invocationId == null || invocationId.isEmpty())
114 invocationId = UUID.randomUUID().toString();
115 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
118 protected void setInvocationIdFromMDC() {
119 String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID);
120 if (invocationId == null || invocationId.isEmpty())
121 invocationId = UUID.randomUUID().toString();
122 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
125 protected void setMDCPartnerName(SimpleMap headers) {
126 logger.trace("Checking X-ONAP-PartnerName header for partnerName.");
127 String partnerName = headers.get(ONAPLogConstants.Headers.PARTNER_NAME);
128 if (partnerName == null || partnerName.isEmpty()) {
129 logger.trace("No valid X-ONAP-PartnerName header value. Checking User-Agent header for partnerName.");
130 partnerName = headers.get(HttpHeaders.USER_AGENT);
131 if (partnerName == null || partnerName.isEmpty()) {
132 logger.trace("No valid User-Agent header value. Checking X-ClientID header for partnerName.");
133 partnerName = headers.get(Constants.HttpHeaders.CLIENT_ID);
134 if (partnerName == null || partnerName.isEmpty()) {
135 logger.trace("No valid partnerName headers. Defaulting partnerName to UNKNOWN.");
136 partnerName = Constants.DefaultValues.UNKNOWN;
140 MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
143 protected void setLogTimestamp() {
144 MDC.put(ONAPLogConstants.MDCs.LOG_TIMESTAMP,
145 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
148 protected void setElapsedTime() {
149 DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
150 ZonedDateTime entryTimestamp =
151 ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP), timeFormatter);
152 ZonedDateTime endTimestamp = ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
154 MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
155 Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
158 protected void setElapsedTimeInvokeTimestamp() {
159 DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
160 ZonedDateTime entryTimestamp =
161 ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP), timeFormatter);
162 ZonedDateTime endTimestamp = ZonedDateTime.parse(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP), timeFormatter);
164 MDC.put(ONAPLogConstants.MDCs.ELAPSED_TIME,
165 Long.toString(ChronoUnit.MILLIS.between(entryTimestamp, endTimestamp)));
168 protected void setResponseStatusCode(int code) {
170 if (Response.Status.Family.familyOf(code).equals(Response.Status.Family.SUCCESSFUL)) {
171 statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
173 statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
177 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
180 protected void setTargetEntity(ONAPComponents targetEntity) {
181 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity.toString());
184 protected void clearClientMDCs() {
185 MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID);
186 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION);
187 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE);
188 MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE);
189 MDC.remove(ONAPLogConstants.MDCs.TARGET_ENTITY);
190 MDC.remove(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
191 MDC.remove(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP);
192 MDC.remove(ONAPLogConstants.MDCs.ERROR_CODE);
193 MDC.remove(ONAPLogConstants.MDCs.ERROR_DESC);
196 protected void setResponseDescription(int statusCode) {
197 MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION, Response.Status.fromStatusCode(statusCode).toString());
200 protected void setErrorCode(int statusCode) {
201 MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, String.valueOf(statusCode));
204 protected void setErrorDesc(int statusCode) {
205 MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, Response.Status.fromStatusCode(statusCode).toString());
208 protected String getProperty(String property) {
209 logger.info("Checking for system property [{}]", property);
210 String propertyValue = System.getProperty(property);
211 if (propertyValue == null || propertyValue.isEmpty()) {
212 logger.info("System property was null or empty. Checking environment variable for: {}", property);
213 propertyValue = System.getenv(property);
214 if (propertyValue == null || propertyValue.isEmpty()) {
215 logger.info("Environment variable: {} was null or empty. Returning value: {}", property,
216 Constants.DefaultValues.UNKNOWN);
217 propertyValue = Constants.DefaultValues.UNKNOWN;
220 return propertyValue;