Added oparent to sdc main
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / servlets / LoggingServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.openecomp.sdc.fe.servlets;
22
23 import com.google.common.cache.Cache;
24 import com.google.common.cache.CacheBuilder;
25 import org.openecomp.sdc.common.api.Constants;
26 import org.openecomp.sdc.common.servlets.BasicServlet;
27 import org.openecomp.sdc.fe.impl.MdcData;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.slf4j.MDC;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.core.Response;
34 import java.util.concurrent.TimeUnit;
35
36 public abstract class LoggingServlet extends BasicServlet {
37
38     private static final Logger log = LoggerFactory.getLogger(BasicServlet.class.getName());
39     private static final Cache<String, MdcData> mdcDataCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build();
40
41     /**
42      * log incoming requests
43      * @param httpRequest the http request
44      */
45     protected void logFeRequest(HttpServletRequest httpRequest) {
46
47         MDC.clear();
48
49         Long transactionStartTime = System.currentTimeMillis();
50         String uuid = httpRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER);
51         String serviceInstanceID = httpRequest.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER);
52
53         if (uuid != null && uuid.length() > 0) {
54             String userId = httpRequest.getHeader(Constants.USER_ID_HEADER);
55
56             String remoteAddr = httpRequest.getRemoteAddr();
57             String localAddr = httpRequest.getLocalAddr();
58
59             mdcDataCache.put(uuid, new MdcData(serviceInstanceID, userId, remoteAddr, localAddr, transactionStartTime));
60
61             updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, null);
62         }
63         inHttpRequest(httpRequest);
64     }
65
66     /**
67      * log response
68      * @param request orig request
69      * @param response returned response
70      */
71     protected void logFeResponse(HttpServletRequest request, Response response) {
72         String uuid = request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER);
73         String transactionRoundTime = null;
74
75         if (uuid != null) {
76             MdcData mdcData = mdcDataCache.getIfPresent(uuid);
77             if (mdcData != null) {
78                 Long transactionStartTime = mdcData.getTransactionStartTime();
79                 if (transactionStartTime != null) {// should'n ever be null, but
80                     // just to be defensive
81                     transactionRoundTime = Long.toString(System.currentTimeMillis() - transactionStartTime);
82                 }
83                 updateMdc(uuid, mdcData.getServiceInstanceID(), mdcData.getUserId(), mdcData.getRemoteAddr(), mdcData.getLocalAddr(), transactionRoundTime);
84             }
85         }
86         outHttpResponse(response);
87
88         MDC.clear();
89     }
90
91     /**
92      * Extracted for purpose of clear method name, for logback %M parameter
93      * @param httpRequest http request
94      */
95     protected abstract void inHttpRequest(HttpServletRequest httpRequest) ;
96
97
98     /**
99      * Extracted for purpose of clear method name, for logback %M parameter
100      * @param response http response
101      */
102     protected abstract void outHttpResponse(Response response);
103
104     /**
105      * update mdc with values from the request
106      * @param uuid service uuid
107      * @param serviceInstanceID serviceInstanceID
108      * @param userId userId
109      * @param remoteAddr remoteAddr
110      * @param localAddr localAddr
111      * @param transactionStartTime transactionStartTime
112      */
113     private void updateMdc(String uuid, String serviceInstanceID, String userId, String remoteAddr, String localAddr, String transactionStartTime) {
114         MDC.put("uuid", uuid);
115         MDC.put("serviceInstanceID", serviceInstanceID);
116         MDC.put("userId", userId);
117         MDC.put("remoteAddr", remoteAddr);
118         MDC.put("localAddr", localAddr);
119         MDC.put("timer", transactionStartTime);
120     }
121 }