Catalog alignment
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / impl / LogHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.impl;
22
23 import org.onap.logging.ref.slf4j.ONAPLogConstants;
24 import org.openecomp.sdc.common.api.Constants;
25 import org.openecomp.sdc.common.log.api.ILogConfiguration;
26 import org.openecomp.sdc.common.log.elements.LogFieldsMdcHandler;
27 import org.slf4j.MDC;
28
29 import javax.servlet.http.HttpServletRequest;
30
31 public class LogHandler {
32     public static final String UUID = "uuid";
33     public static final String TRANSACTION_START_TIME = "transactionStartTime";
34
35     public static void logFeRequest(HttpServletRequest httpRequest) {
36         Long transactionStartTime = System.currentTimeMillis();
37         String uuid = LogFieldsMdcHandler.getInstance().getKeyRequestId();
38         String serviceInstanceID = httpRequest.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER);
39
40         if (uuid != null && uuid.length() > 0) {
41             String userId = httpRequest.getHeader(Constants.USER_ID_HEADER);
42             String remoteAddr = httpRequest.getRemoteAddr();
43             String localAddr = httpRequest.getLocalAddr();
44
45             httpRequest.setAttribute(UUID,uuid);
46             httpRequest.setAttribute(TRANSACTION_START_TIME,transactionStartTime);
47
48             updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, null);
49         }
50     }
51
52     public static void logFeResponse(HttpServletRequest request) {
53         String uuid = (String)request.getAttribute(UUID);
54         String serviceInstanceID = request.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER);
55         String userId = request.getHeader(Constants.USER_ID_HEADER);
56         String remoteAddr = request.getRemoteAddr();
57         String localAddr = request.getLocalAddr();
58         String transactionRoundTime = null;
59
60         if (uuid != null) {
61             Long transactionStartTime = (Long)request.getAttribute(TRANSACTION_START_TIME);
62             if(transactionStartTime != null){
63                 transactionRoundTime = Long.toString(System.currentTimeMillis() - transactionStartTime);
64             }
65             updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, transactionRoundTime);
66         }
67     }
68
69     private static void updateMdc(String uuid, String serviceInstanceID, String userId, String remoteAddr, String localAddr, String transactionElapsedTime) {
70         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, uuid);
71         MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceID);
72         MDC.put("userId", userId);
73         MDC.put("remoteAddr", remoteAddr);
74         MDC.put("localAddr", localAddr);
75         MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, transactionElapsedTime);
76     }
77 }