Enhancements for the aai-common library
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / aailog / filter / RestClientLoggingInterceptor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright Â© 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 package org.onap.aai.aailog.filter;
21
22 import org.onap.aai.aailog.logs.ServiceName;
23 import org.onap.logging.filter.base.AbstractMetricLogFilter;
24 import org.onap.logging.filter.base.Constants;
25 import org.onap.logging.filter.base.MDCSetup;
26 import org.onap.logging.ref.slf4j.ONAPLogConstants;
27 import org.slf4j.MDC;
28 import org.springframework.http.HttpRequest;
29 import org.springframework.http.client.ClientHttpRequestExecution;
30 import org.springframework.http.client.ClientHttpRequestInterceptor;
31 import org.springframework.http.client.ClientHttpResponse;
32 import org.springframework.http.HttpHeaders;
33
34 import javax.ws.rs.core.MultivaluedMap;
35 import java.io.IOException;
36 import java.util.List;
37 import java.util.UUID;
38
39 public class RestClientLoggingInterceptor extends AbstractMetricLogFilter<HttpRequest, ClientHttpResponse, HttpHeaders> implements ClientHttpRequestInterceptor {
40     @Override
41     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
42         throws IOException
43     {
44         this.setInvocationId(request.getHeaders());
45         pre(request, request.getHeaders());
46         ClientHttpResponse resp = execution.execute(request, body);
47         post(request, resp);
48         return resp;
49
50     }
51     protected void pre(HttpRequest request, HttpHeaders requestHeaders) {
52         try {
53             setupMDC(request);
54             setupHeaders(request, requestHeaders);
55             super.logInvoke();
56         } catch (Exception e) {
57             logger.warn("Error in RestClientLoggingInterceptor pre", e);
58         }
59     }
60     protected void setupHeaders(HttpRequest clientRequest, HttpHeaders requestHeaders) {
61         String requestId = extractRequestID(requestHeaders);
62         addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
63         addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
64         if (requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID) == null ||
65             requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID).isEmpty()) {
66             addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
67         }
68         addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
69         addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
70         String pName = getProperty(Constants.Property.PARTNER_NAME);
71         if (pName != null && (!pName.isEmpty())) {
72             addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, pName);
73         }
74     }
75     protected String extractRequestID(HttpHeaders requestHeaders) {
76         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
77         if (requestId == null || requestId.isEmpty()) {
78             requestId = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
79             if (requestId == null || requestId.isEmpty()) {
80                 requestId = UUID.randomUUID().toString();
81             }
82             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
83         }
84         return requestId;
85     }
86     public void setInvocationId(HttpHeaders headers) {
87         String invocationId = null;
88
89         List<String> headerList = headers.get(ONAPLogConstants.Headers.INVOCATION_ID);
90         if (headerList != null && (!headerList.isEmpty())) {
91             for (String h : headerList) {
92                 if ( h != null && (!h.isEmpty()) ) {
93                     invocationId = h;
94                     break;
95                 }
96             }
97             headers.remove(ONAPLogConstants.Headers.INVOCATION_ID);
98         }
99         if (invocationId == null) {
100             invocationId = UUID.randomUUID().toString();
101         }
102         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
103     }
104     @Override
105     protected void addHeader(HttpHeaders requestHeaders, String headerName, String headerValue) {
106         requestHeaders.add(headerName, headerValue);
107     }
108
109     protected String getTargetServiceName(HttpRequest request) {
110         return (getServiceName(request));
111     }
112     protected String getServiceName(HttpRequest request){
113         String path = request.getURI().getRawPath();
114         return(ServiceName.extractServiceName(path));
115     }
116
117     protected int getHttpStatusCode(ClientHttpResponse response) {
118         int result = 0;
119         if (response != null ) {
120             try {
121                 result = response.getStatusCode().value();
122             }
123             catch (IOException e) {
124                 logger.warn("Error in RestClientLoggingInterceptor getHttpStatusCode {}", e.getMessage());
125             }
126         }
127         return result;
128     }
129
130     protected String getResponseCode(ClientHttpResponse response) {
131         String result = "";
132         if (response != null ) {
133             try {
134                 result = response.getStatusCode().toString();
135             }
136             catch (IOException e) {
137                 logger.warn("Error in RestClientLoggingInterceptor getResponseCode {}", e.getMessage());
138             }
139         }
140         return result;
141     }
142
143     protected String getTargetEntity(HttpRequest request) {
144         //TODO where do we get this from?
145         return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
146     }
147 }