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 javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.core.Response;
26 import org.onap.logging.filter.base.AbstractMetricLogFilter;
27 import org.onap.logging.filter.base.ONAPComponents;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
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";
39 public AaiDBMetricLog(String subcomponent) {
40 partnerName = getPartnerName(subcomponent);
44 protected void addHeader(MultivaluedMap<String, Object> requestHeaders, String headerName, String headerValue) {
45 requestHeaders.add(headerName, headerValue);
49 protected String getTargetServiceName(DBRequestWrapper request) {
50 return (getServiceName(request));
54 protected String getServiceName(DBRequestWrapper request) {
55 String path = request.getUri().getRawPath();
56 return ServiceName.extractServiceName(path);
60 protected int getHttpStatusCode(Response response) {
61 int intCode = response.getStatus();
66 protected String getResponseCode(Response response) {
67 return String.valueOf(response.getStatus());
71 protected String getTargetEntity(DBRequestWrapper request) {
75 protected String getPartnerName(String subcomponent) {
76 StringBuilder sb = new StringBuilder(ONAPComponents.AAI.toString()).append(subcomponent);
77 return (sb.toString());
80 public void pre(DBRequestWrapper request) {
84 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
85 } catch (Exception e) {
86 logger.warn("Error in AaiDBMetricLog pre", e);
90 public void post(DBRequestWrapper request, Response response) {
93 setElapsedTimeInvokeTimestamp();
94 setResponseStatusCode(getHttpStatusCode(response));
95 setResponseDescription(getHttpStatusCode(response));
96 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
97 logger.info(INVOKE_RETURN, "InvokeReturn");
99 } catch (Exception e) {
100 logger.warn("Error in AaiDBMetricLog post", e);
105 public void setResponseStatusCode(int code) {
107 if (code / 100 == 2) {
108 statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
110 statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
114 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);