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