CSIT Fix for SDC-2585
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / log / elements / LoggerMetric.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.common.log.elements;
22
23 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
24 import org.openecomp.sdc.common.log.enums.LogLevel;
25 import org.openecomp.sdc.common.log.enums.LogMarkers;
26 import org.openecomp.sdc.common.log.enums.Severity;
27 import org.slf4j.Logger;
28 import org.slf4j.MarkerFactory;
29
30 import javax.ws.rs.core.Response;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_ALERT_SEVERITY;
36 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_CLASS_NAME;
37 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_ELAPSED_TIME;
38 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_END_TIMESTAMP;
39 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_INSTANCE_UUID;
40 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_KEY_REQUEST_ID;
41 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_METRIC_BEGIN_TIMESTAMP;
42 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_OPT_FIELD1;
43 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_OPT_FIELD2;
44 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_OPT_FIELD3;
45 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_OPT_FIELD4;
46 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_PARTNER_NAME;
47 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_PROCESS_KEY;
48 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_REMOTE_HOST;
49 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_RESPONSE_CODE;
50 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_RESPONSE_DESC;
51 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_SERVER_FQDN;
52 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_SERVER_IP_ADDRESS;
53 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_SERVICE_INSTANCE_ID;
54 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_SERVICE_NAME;
55 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_STATUS_CODE;
56 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_TARGET_ENTITY;
57 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_TARGET_SERVICE_NAME;
58 import static org.openecomp.sdc.common.log.api.ILogConfiguration.MDC_TARGET_VIRTUAL_ENTITY;
59
60
61 public class LoggerMetric extends LoggerBase {
62     private static ArrayList<String> mandatoryFields = new ArrayList<>(Arrays.asList(
63             MDC_METRIC_BEGIN_TIMESTAMP,
64             MDC_END_TIMESTAMP,
65             MDC_KEY_REQUEST_ID,
66             MDC_SERVICE_NAME,
67             MDC_PARTNER_NAME,
68             MDC_STATUS_CODE,
69             MDC_RESPONSE_CODE,
70             MDC_SERVICE_INSTANCE_ID,
71             MDC_RESPONSE_DESC,
72             MDC_ELAPSED_TIME,
73             MDC_TARGET_ENTITY,
74             MDC_TARGET_SERVICE_NAME,
75             MDC_TARGET_VIRTUAL_ENTITY,
76             MDC_SERVER_IP_ADDRESS,
77             MDC_SERVER_FQDN));
78
79     private static ArrayList<String> optionalFields = new ArrayList<>(Arrays.asList(
80             MDC_INSTANCE_UUID,
81             MDC_ALERT_SEVERITY,
82             MDC_REMOTE_HOST,
83             MDC_CLASS_NAME,
84             MDC_PROCESS_KEY,
85             MDC_OPT_FIELD1,
86             MDC_OPT_FIELD2,
87             MDC_OPT_FIELD3,
88             MDC_OPT_FIELD4));
89
90     LoggerMetric(ILogFieldsHandler ecompMdcWrapper, Logger logger) {
91         super(ecompMdcWrapper, MarkerFactory.getMarker(LogMarkers.METRIC_MARKER.text()), logger);
92         //put the remote host and FQDN values from another thread if they are set
93         ecompMdcWrapper.setServerIPAddressInternally();
94         ecompMdcWrapper.setServerFQDNInternally();
95     }
96
97     public void log(Response.StatusType statusInfo,
98                     String className,
99                     LogLevel logLevel,
100                     Severity securityLevel,
101                     String message) {
102         log(statusInfo, className, logLevel, securityLevel, message);
103     }
104
105     @Override
106     public void log(LogLevel logLevel, String message) {
107         setKeyRequestIdIfNotSetYet();
108         log(logLevel, message, (Object) null);
109     }
110
111     @Override
112     public LoggerMetric startTimer() {
113         clear();
114         ecompLogFieldsHandler.startMetricTimer();
115         return this;
116     }
117
118     public LoggerMetric stopTimer() {
119         ecompLogFieldsHandler.stopMetricTimer();
120         return this;
121     }
122
123     @Override
124     public LoggerMetric setKeyRequestId(String keyRequestId) {
125         return (LoggerMetric) super.setKeyRequestId(keyRequestId);
126     }
127
128     @Override
129     public List<String> getMandatoryFields() {
130         return mandatoryFields;
131     }
132
133     @Override
134     public LoggerMetric clear() {
135         ecompLogFieldsHandler.removeTargetEntity();
136         ecompLogFieldsHandler.removeTargetServiceName();
137         ecompLogFieldsHandler.removeResponseCode();
138         ecompLogFieldsHandler.removeResponseDesc();
139         ecompLogFieldsHandler.removeStatusCode();
140         return this;
141     }
142
143     // automatic parameter this is optional
144     public LoggerMetric setAutoServerFQDN(String serverFQDN) {
145         ecompLogFieldsHandler.setServerFQDN(serverFQDN);
146         return this;
147     }
148
149     // automatic parameter this is optional
150     public LoggerMetric setAutoServerIPAddress(String serverIPAddress) {
151         ecompLogFieldsHandler.setServerIPAddress(serverIPAddress);
152         return this;
153     }
154
155     public LoggerMetric setInstanceUUID(String instanceUUID) {
156         ecompLogFieldsHandler.setInstanceUUID(instanceUUID);
157         return this;
158     }
159
160     // log optional parameter
161     public LoggerMetric setOptProcessKey(String processKey) {
162         ecompLogFieldsHandler.setProcessKey(processKey);
163         return this;
164     }
165
166     // log optional parameter
167     public LoggerMetric setOptAlertSeverity(Severity alertSeverity) {
168         ecompLogFieldsHandler.setAlertSeverity(alertSeverity);
169         return this;
170     }
171
172     // log optional parameter
173     public LoggerMetric setOptCustomField1(String customField1) {
174         ecompLogFieldsHandler.setOptCustomField1(customField1);
175         return this;
176     }
177
178     // log optional parameter
179     public LoggerMetric setOptCustomField2(String customField2) {
180         ecompLogFieldsHandler.setOptCustomField2(customField2);
181         return this;
182     }
183
184     // log optional parameter
185     public LoggerMetric setOptCustomField3(String customField3) {
186         ecompLogFieldsHandler.setOptCustomField3(customField3);
187         return this;
188     }
189
190     // log optional parameter
191     public LoggerMetric setOptCustomField4(String customField4) {
192         ecompLogFieldsHandler.setOptCustomField4(customField4);
193         return this;
194     }
195
196     public LoggerMetric setRemoteHost(String remoteHost) {
197         ecompLogFieldsHandler.setRemoteHost(remoteHost);
198         return this;
199     }
200
201     public LoggerMetric setServiceName(String serviceName) {
202         ecompLogFieldsHandler.setServiceName(serviceName);
203         return this;
204     }
205
206     public LoggerMetric setStatusCode(String statusCode) {
207         ecompLogFieldsHandler.setStatusCode(statusCode);
208         return this;
209     }
210
211     public LoggerMetric setPartnerName(String partnerName) {
212         ecompLogFieldsHandler.setPartnerName(partnerName);
213         return this;
214     }
215
216     public LoggerMetric setResponseCode(int responseCode) {
217         ecompLogFieldsHandler.setResponseCode(responseCode);
218         return this;
219     }
220
221     public LoggerMetric setResponseDesc(String responseDesc) {
222         ecompLogFieldsHandler.setResponseDesc(responseDesc);
223         return this;
224     }
225
226     public LoggerMetric setOptServiceInstanceId(String serviceInstanceId) {
227         ecompLogFieldsHandler.setServiceInstanceId(serviceInstanceId);
228         return this;
229     }
230
231     public LoggerMetric setOptClassName(String className) {
232         ecompLogFieldsHandler.setClassName(className);
233         return this;
234     }
235
236     public LoggerMetric setTargetEntity(String targetEntity) {
237         ecompLogFieldsHandler.setTargetEntity(targetEntity);
238         return this;
239     }
240
241     public LoggerMetric setTargetServiceName(String targetServiceName) {
242         ecompLogFieldsHandler.setTargetServiceName(targetServiceName);
243         return this;
244     }
245
246     public LoggerMetric setTargetVirtualEntity(String targetVirtualEntity) {
247         ecompLogFieldsHandler.setTargetVirtualEntity(targetVirtualEntity);
248         return this;
249     }
250
251
252 }