2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.aailog.logs;
23 import org.onap.logging.filter.base.AbstractMetricLogFilter;
24 import org.onap.logging.filter.base.ONAPComponents;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.Response;
31 public class AaiDBMetricLog
32 extends AbstractMetricLogFilter<DBRequestWrapper, Response, MultivaluedMap<String, Object>> {
34 protected static final Logger logger = LoggerFactory.getLogger(AaiDBMetricLog.class);
35 private final String partnerName;
36 private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
37 private static final String TARGET_ENTITY = ONAPComponents.AAI.toString() + ".DB";
38 public AaiDBMetricLog(String subcomponent) {
39 partnerName = getPartnerName(subcomponent);
43 protected void addHeader(MultivaluedMap<String, Object> requestHeaders, String headerName, String headerValue) {
44 requestHeaders.add(headerName, headerValue);
48 protected String getTargetServiceName(DBRequestWrapper request) {
49 return (getServiceName(request));
53 protected String getServiceName(DBRequestWrapper request) {
54 String path = request.getUri().getRawPath();
55 return ServiceName.extractServiceName(path);
59 protected int getHttpStatusCode(Response response) {
60 int intCode = response.getStatus();
65 protected String getResponseCode(Response response) {
66 return String.valueOf(response.getStatus());
70 protected String getTargetEntity(DBRequestWrapper request) {
74 protected String getPartnerName(String subcomponent) {
75 StringBuilder sb = new StringBuilder(ONAPComponents.AAI.toString()).append(subcomponent);
76 return (sb.toString());
79 public void pre(DBRequestWrapper request) {
83 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
84 } catch (Exception e) {
85 logger.warn("Error in AaiDBMetricLog pre", e);
88 public void post(DBRequestWrapper request, Response response) {
91 setElapsedTimeInvokeTimestamp();
92 setResponseStatusCode(getHttpStatusCode(response));
93 setResponseDescription(getHttpStatusCode(response));
94 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
95 logger.info(INVOKE_RETURN, "InvokeReturn");
97 } catch (Exception e) {
98 logger.warn("Error in AaiDBMetricLog post", e);
102 public void setResponseStatusCode(int code) {
104 if (code / 100 == 2) {
105 statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
108 statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
112 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);