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