2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.fe.impl;
22 import javax.servlet.http.HttpServletRequest;
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;
29 public class LogHandler {
31 public static final String UUID = "uuid";
32 public static final String TRANSACTION_START_TIME = "transactionStartTime";
34 public static void logFeRequest(HttpServletRequest httpRequest) {
35 Long transactionStartTime = System.currentTimeMillis();
36 String uuid = LogFieldsMdcHandler.getInstance().getKeyRequestId();
37 String serviceInstanceID = httpRequest.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER);
38 if (uuid != null && uuid.length() > 0) {
39 String userId = httpRequest.getHeader(Constants.USER_ID_HEADER);
40 String remoteAddr = httpRequest.getRemoteAddr();
41 String localAddr = httpRequest.getLocalAddr();
42 httpRequest.setAttribute(UUID, uuid);
43 httpRequest.setAttribute(TRANSACTION_START_TIME, transactionStartTime);
44 updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, null);
48 public static void logFeResponse(HttpServletRequest request) {
49 String uuid = (String) request.getAttribute(UUID);
50 String serviceInstanceID = request.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER);
51 String userId = request.getHeader(Constants.USER_ID_HEADER);
52 String remoteAddr = request.getRemoteAddr();
53 String localAddr = request.getLocalAddr();
54 String transactionRoundTime = null;
56 Long transactionStartTime = (Long) request.getAttribute(TRANSACTION_START_TIME);
57 if (transactionStartTime != null) {
58 transactionRoundTime = Long.toString(System.currentTimeMillis() - transactionStartTime);
60 updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, transactionRoundTime);
64 private static void updateMdc(String uuid, String serviceInstanceID, String userId, String remoteAddr, String localAddr,
65 String transactionElapsedTime) {
66 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, uuid);
67 MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceID);
68 MDC.put("userId", userId);
69 MDC.put("remoteAddr", remoteAddr);
70 MDC.put("localAddr", localAddr);
71 MDC.put(ILogConfiguration.MDC_ELAPSED_TIME, transactionElapsedTime);