2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.so.logging.jaxrs.filter;
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;
29 import java.util.UUID;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
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;
37 import org.springframework.stereotype.Component;
40 public class MDCSetup {
42 protected static Logger logger = LoggerFactory.getLogger(MDCSetup.class);
44 private static final String INSTANCE_UUID = UUID.randomUUID().toString();
46 public void setInstanceUUID() {
47 MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID);
50 public void setServerFQDN() {
51 String serverFQDN = "";
52 InetAddress addr = null;
54 addr = InetAddress.getLocalHost();
55 serverFQDN = addr.toString();
56 } catch (UnknownHostException e) {
57 logger.warn("Cannot Resolve Host Name");
60 MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
63 public void setClientIPAddress(HttpServletRequest httpServletRequest) {
64 String remoteIpAddress = "";
65 if (httpServletRequest != null) {
66 remoteIpAddress = httpServletRequest.getRemoteAddr();
68 MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, remoteIpAddress);
71 public void setEntryTimeStamp() {
72 MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,
73 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
76 public void setServiceName(HttpServletRequest request) {
77 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI());
80 public void setRequestId(Map<String, String> headers) {
81 String requestId = headers.get(ONAPLogConstants.Headers.REQUEST_ID);
82 if (requestId == null || requestId.isEmpty())
83 requestId = UUID.randomUUID().toString();
84 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
87 public void setInvocationId(Map<String, String> headers) {
88 String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
89 if (invocationId == null || invocationId.isEmpty())
90 invocationId = UUID.randomUUID().toString();
91 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
94 public void setMDCPartnerName(Map<String, String> headers) {
95 String partnerName = headers.get(ONAPLogConstants.Headers.PARTNER_NAME);
96 if (partnerName == null || partnerName.isEmpty())
98 MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
102 public void setResponseStatusCode(HttpServletResponse response) {
104 if (Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)) {
105 statusCode = ONAPLogConstants.ResponseStatus.COMPLETED.toString();
107 statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
109 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);