fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / log / elements / LoggerAudit.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
24 import org.openecomp.sdc.common.log.api.ILogConfiguration;
25 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
26 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
27 import org.openecomp.sdc.common.log.enums.LogMarkers;
28 import org.openecomp.sdc.common.log.enums.Severity;
29 import org.openecomp.sdc.common.log.enums.StatusCode;
30 import org.slf4j.Logger;
31 import org.slf4j.MDC;
32 import org.slf4j.MarkerFactory;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38
39 public class LoggerAudit extends LoggerBase {
40     private static final int STATUS_MAJOR_PART_MASK = 100;
41     private static ArrayList<String> mandatoryFields = new ArrayList<>(Arrays.asList(
42             ILogConfiguration.MDC_AUDIT_BEGIN_TIMESTAMP,
43             ILogConfiguration.MDC_END_TIMESTAMP,
44             ILogConfiguration.MDC_KEY_REQUEST_ID,
45             ILogConfiguration.MDC_SERVICE_NAME,
46             ILogConfiguration.MDC_PARTNER_NAME,
47             ILogConfiguration.MDC_STATUS_CODE,
48             ILogConfiguration.MDC_RESPONSE_CODE,
49             ILogConfiguration.MDC_SERVICE_INSTANCE_ID,
50             ILogConfiguration.MDC_RESPONSE_DESC,
51             ILogConfiguration.MDC_ELAPSED_TIME,
52             ILogConfiguration.MDC_SERVER_IP_ADDRESS,
53             ILogConfiguration.MDC_SERVER_FQDN));
54
55     private static ArrayList<String> optionalFields = new ArrayList<>(Arrays.asList(
56             ILogConfiguration.MDC_INSTANCE_UUID,
57             ILogConfiguration.MDC_ALERT_SEVERITY,
58             ILogConfiguration.MDC_REMOTE_HOST,
59             ILogConfiguration.MDC_CLASS_NAME,
60             ILogConfiguration.MDC_PROCESS_KEY,
61             ILogConfiguration.MDC_OPT_FIELD1,
62             ILogConfiguration.MDC_OPT_FIELD2,
63             ILogConfiguration.MDC_OPT_FIELD3,
64             ILogConfiguration.MDC_OPT_FIELD4));
65
66     LoggerAudit(ILogFieldsHandler ecompMdcWrapper, Logger logger) {
67         super(ecompMdcWrapper, MarkerFactory.getMarker(LogMarkers.AUDIT_MARKER.text()), logger);
68         //put the remote host and FQDN values from another thread if they are set
69         ecompMdcWrapper.setServerIPAddressInternally();
70         ecompMdcWrapper.setServerFQDNInternally();
71     }
72
73     @Override
74     public LoggerAudit startTimer() {
75         ecompLogFieldsHandler.startAuditTimer();
76         return this;
77     }
78
79     public LoggerAudit stopTimer() {
80         ecompLogFieldsHandler.stopAuditTimer();
81         return this;
82     }
83
84     public LoggerAudit setInstanceUUID(String instanceUUID) {
85         ecompLogFieldsHandler.setInstanceUUID(instanceUUID);
86         return this;
87     }
88
89     public LoggerAudit setOptClassName(String className) {
90         MDC.put("ClassName", className);
91         return this;
92     }
93
94     public LoggerAudit setOptProcessKey(String processKey) {
95         ecompLogFieldsHandler.setProcessKey(processKey);
96         return this;
97     }
98
99     public LoggerAudit setOptAlertSeverity(Severity alertSeverity) {
100         ecompLogFieldsHandler.setAlertSeverity(alertSeverity);
101         return this;
102     }
103
104     // log optional parameter
105     public LoggerAudit setOptCustomField1(String customField1) {
106         ecompLogFieldsHandler.setOptCustomField1(customField1);
107         return this;
108     }
109
110     // log optional parameter
111     public LoggerAudit setOptCustomField2(String customField2) {
112         ecompLogFieldsHandler.setOptCustomField2(customField2);
113         return this;
114     }
115
116     // log optional parameter
117     public LoggerAudit setOptCustomField3(String customField3) {
118         ecompLogFieldsHandler.setOptCustomField3(customField3);
119         return this;
120     }
121
122     public LoggerAudit setOptCustomField4(String customField4) {
123         ecompLogFieldsHandler.setOptCustomField4(customField4);
124         return this;
125     }
126
127     @Override
128     public LoggerAudit setKeyRequestId(String keyRequestId) {
129         return (LoggerAudit) super.setKeyRequestId(keyRequestId);
130     }
131
132     public LoggerAudit setRemoteHost(String remoteHost) {
133         ecompLogFieldsHandler.setRemoteHost(remoteHost);
134         return this;
135     }
136
137     public LoggerAudit setServiceName(String serviceName) {
138         ecompLogFieldsHandler.setServiceName(serviceName);
139         return this;
140     }
141
142     public LoggerAudit setStatusCode(String statusCode) {
143         // status code is either success (COMPLETE) or failure (ERROR) of the request.
144         String respStatus = Integer.parseInt(statusCode) / STATUS_MAJOR_PART_MASK == 2 ? StatusCode.COMPLETE.getStatusCodeEnum() : StatusCode.ERROR.getStatusCodeEnum();
145         ecompLogFieldsHandler.setStatusCode(respStatus);
146         return this;
147     }
148
149     public LoggerAudit setPartnerName(String partnerName) {
150         ecompLogFieldsHandler.setPartnerName(partnerName);
151         return this;
152     }
153
154     public LoggerAudit setResponseCode(EcompLoggerErrorCode responseCode) {
155         ecompLogFieldsHandler.setResponseCode(responseCode.getErrorCode());
156         return this;
157     }
158
159     public LoggerAudit setResponseDesc(String responseDesc) {
160         ecompLogFieldsHandler.setResponseDesc(responseDesc);
161         return this;
162     }
163
164     public LoggerAudit setOptServiceInstanceId(String serviceInstanceId) {
165         ecompLogFieldsHandler.setServiceInstanceId(serviceInstanceId);
166         return this;
167     }
168
169     public String getAuditMessage() {
170         return ecompLogFieldsHandler.getAuditMessage();
171     }
172
173
174     @Override
175     public List<String> getMandatoryFields() {
176         return Collections.unmodifiableList(mandatoryFields);
177     }
178
179     @Override
180     public LoggerAudit clear() {
181         super.clear();
182         ecompLogFieldsHandler.setServerFQDNInternally();
183         ecompLogFieldsHandler.setServerIPAddressInternally();
184         return this;
185     }
186
187 }