f0a16561aa970f4220f7f320b377ddcd5b27b27b
[so.git] / common / src / main / java / org / onap / so / logging / jaxrs / filter / MDCSetup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.so.logging.jaxrs.filter;
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.util.Map;
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;
36 import org.slf4j.MDC;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class MDCSetup {
41     
42     protected static Logger logger = LoggerFactory.getLogger(MDCSetup.class); 
43     
44     private static final String INSTANCE_UUID = UUID.randomUUID().toString();
45     
46     public void setInstanceUUID(){
47         MDC.put(ONAPLogConstants.MDCs.INSTANCE_UUID, INSTANCE_UUID);
48     }
49
50     public void setServerFQDN(){
51         String serverFQDN = "";
52         InetAddress addr= null;
53         try {
54             addr = InetAddress.getLocalHost();
55             serverFQDN = addr.toString();
56         } catch (UnknownHostException e) {
57             logger.warn("Cannot Resolve Host Name");
58             serverFQDN = "";
59         }
60         MDC.put(ONAPLogConstants.MDCs.SERVER_FQDN, serverFQDN);
61     }
62
63     public void setClientIPAddress(HttpServletRequest httpServletRequest){
64         String remoteIpAddress = "";
65         if (httpServletRequest != null) {
66             remoteIpAddress = httpServletRequest.getRemoteAddr();
67         } 
68         MDC.put(ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, remoteIpAddress);
69     }
70
71     public void setEntryTimeStamp() {
72         MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP,ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
73     }
74     
75     public void setServiceName(HttpServletRequest request) {
76         MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, request.getRequestURI());
77     }
78
79     public void setRequestId(Map<String, String> headers) {
80         String requestId=headers.get(ONAPLogConstants.Headers.REQUEST_ID);
81         if(requestId == null || requestId.isEmpty())
82             requestId = UUID.randomUUID().toString();
83         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,requestId);
84     }
85
86     public void setInvocationId(Map<String, String> headers) {
87         String invocationId = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
88         if(invocationId == null || invocationId.isEmpty())
89             invocationId =UUID.randomUUID().toString();
90         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
91     }
92
93     public void setMDCPartnerName(Map<String, String> headers) {
94         String partnerName=headers.get(ONAPLogConstants.Headers.PARTNER_NAME);
95         if(partnerName == null || partnerName.isEmpty())
96             partnerName = "";
97         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME,partnerName);
98     }
99     
100
101     public void setResponseStatusCode(HttpServletResponse response) {
102         String statusCode;
103         if(Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){     
104             statusCode=ONAPLogConstants.ResponseStatus.COMPLETED.toString();
105         }else{                          
106             statusCode= ONAPLogConstants.ResponseStatus.ERROR.toString();               
107         }           
108         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
109     }
110 }