Sync Integ to Master
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / ecomplog / EcompMDCWrapper.java
1 package org.openecomp.sdc.common.ecomplog;
2
3 import static java.lang.Integer.valueOf;
4 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_ALERT_SEVERITY;
5 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_CLASS_NAME;
6 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_ERROR_CODE;
7 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_ERROR_DESC;
8 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_INSTANCE_UUID;
9 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_KEY_REQUEST_ID;
10 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_OPT_FIELD1;
11 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_OPT_FIELD2;
12 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_OPT_FIELD3;
13 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_OPT_FIELD4;
14 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_PARTNER_NAME;
15 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_PROCESS_KEY;
16 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_REMOTE_HOST;
17 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_RESPONSE_CODE;
18 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_RESPONSE_DESC;
19 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_SERVER_FQDN;
20 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_SERVER_IP_ADDRESS;
21 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_SERVICE_INSTANCE_ID;
22 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_SERVICE_NAME;
23 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_STATUS_CODE;
24 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_TARGET_ENTITY;
25 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_TARGET_SERVICE_NAME;
26 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_TARGET_VIRTUAL_ENTITY;
27
28 import java.net.InetAddress;
29 import java.util.ArrayList;
30
31 import org.openecomp.sdc.common.ecomplog.Enums.Severity;
32 import org.openecomp.sdc.common.ecomplog.api.IEcompMdcWrapper;
33 import org.openecomp.sdc.common.ecomplog.api.IStopWatch;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37
38 public class EcompMDCWrapper implements IEcompMdcWrapper {
39
40     private static EcompMDCWrapper instanceMdcWrapper = new EcompMDCWrapper(new Stopwatch());
41
42     public static EcompMDCWrapper getInstance() {
43         return instanceMdcWrapper;
44     }
45
46     private IStopWatch stopWatch;
47     protected static Logger log = LoggerFactory.getLogger(EcompMDCWrapper.class.getName());
48     protected ArrayList<String> mandatoryFields = new ArrayList<>();
49     protected ArrayList<String> optionalFields = new ArrayList<>();
50     protected static String hostAddress;
51     protected static String fqdn;
52
53     // in package classes can instantiate this class
54     // to use directly from outside the package usr the getInstance() Method
55     EcompMDCWrapper(IStopWatch stopwatch) {
56         this.stopWatch = stopwatch;
57     }
58
59     static {
60         try {
61             hostAddress = InetAddress.getLocalHost().getHostAddress();
62             fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
63         } catch (Exception ex) {
64             log.error("failed to get machine parameters", ex);
65         }
66     }
67
68     @Override
69     public EcompMDCWrapper startTimer() {
70         stopWatch.start();
71         return this;
72     }
73
74     @Override
75     public EcompMDCWrapper stopTimer() {
76         try {
77             stopWatch.stop();
78         } catch (Exception ex) {
79             log.error("StopWatch failed; probably start was not called before Stopwatch", ex);
80         }
81         return this;
82     }
83
84     @Override
85     public EcompMDCWrapper setClassName(String className) {
86         MDC.put(MDC_CLASS_NAME, className);
87         return this;
88     }
89
90     // automatic parameter this is optional
91     @Override
92     public EcompMDCWrapper setAutoServerFQDN(String serverFQDN) {
93         MDC.put(MDC_SERVER_FQDN, serverFQDN);
94         return this;
95     }
96
97     // automatic parameter this is optional
98     @Override
99     public EcompMDCWrapper setAutoServerIPAddress(String serverIPAddress) {
100         MDC.put(MDC_SERVER_IP_ADDRESS, serverIPAddress);
101         return this;
102     }
103
104     @Override
105     public EcompMDCWrapper setInstanceUUID(String instanceUUID) {
106         MDC.put(MDC_INSTANCE_UUID, instanceUUID);
107         return this;
108     }
109
110     @Override
111     public EcompMDCWrapper setProcessKey(String processKey) {
112         MDC.put(MDC_PROCESS_KEY, processKey);
113         return this;
114     }
115
116     @Override
117     public EcompMDCWrapper setAlertSeverity(Severity alertSeverity) {
118         MDC.put(MDC_ALERT_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
119         return this;
120     }
121
122     @Override
123     public EcompMDCWrapper setOptCustomField1(String customField1) {
124         MDC.put(MDC_OPT_FIELD1, customField1);
125         return this;
126     }
127
128     @Override
129     public EcompMDCWrapper setOptCustomField2(String customField2) {
130         MDC.put(MDC_OPT_FIELD2, customField2);
131         return this;
132     }
133
134     @Override
135     public EcompMDCWrapper setOptCustomField3(String customField3) {
136         MDC.put(MDC_OPT_FIELD3, customField3);
137         return this;
138     }
139
140     @Override
141     public EcompMDCWrapper setOptCustomField4(String customField4) {
142         MDC.put(MDC_OPT_FIELD4, customField4);
143         return this;
144     }
145
146     @Override
147     public EcompMDCWrapper setKeyRequestId(String keyRequestId) {
148         MDC.put(MDC_KEY_REQUEST_ID, keyRequestId); // eg. servletRequest.getSession().getId()
149         return this;
150     }
151
152     @Override
153     public EcompMDCWrapper setRemoteHost(String remoteHost) {
154         MDC.put(MDC_REMOTE_HOST, remoteHost);
155         return this;
156     }
157
158     @Override
159     public EcompMDCWrapper setServiceName(String serviceName) {
160         MDC.put(MDC_SERVICE_NAME, serviceName);
161         return this;
162     }
163
164     @Override
165     public EcompMDCWrapper setStatusCode(String statusCode) {
166         MDC.put(MDC_STATUS_CODE, statusCode);
167         return this;
168     }
169
170     @Override
171     public EcompMDCWrapper setPartnerName(String partnerName) {
172         MDC.put(MDC_PARTNER_NAME, partnerName);
173         return this;
174     }
175
176     @Override
177     public EcompMDCWrapper setResponseCode(int responseCode) {
178         MDC.put(MDC_RESPONSE_CODE, Integer.toString(responseCode));
179         return this;
180     }
181
182     @Override
183     public EcompMDCWrapper setResponseDesc(String responseDesc) {
184         MDC.put(MDC_RESPONSE_DESC, responseDesc);
185         return this;
186     }
187
188     @Override
189     public EcompMDCWrapper setServiceInstanceId(String serviceInstanceId) {
190         MDC.put(MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
191         return this;
192     }
193
194     @Override
195     public EcompMDCWrapper setTargetEntity(String targetEntity) {
196         MDC.put(MDC_TARGET_ENTITY, targetEntity);
197         return this;
198     }
199
200     @Override
201     public EcompMDCWrapper setTargetServiceName(String targetServiceName) {
202         MDC.put(MDC_TARGET_SERVICE_NAME, targetServiceName);
203         return this;
204     }
205
206     @Override
207     public EcompMDCWrapper setTargetVirtualEntity(String targetVirtualEntity) {
208         MDC.put(MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
209         return this;
210     }
211
212     @Override
213     public EcompMDCWrapper setErrorCode(int errorCode) {
214         MDC.put(MDC_ERROR_CODE, valueOf(errorCode).toString());
215         return this;
216     }
217
218     @Override
219     public EcompMDCWrapper setErrorDescription(String errorDescription) {
220         MDC.put(MDC_ERROR_DESC, errorDescription);
221         return this;
222     }
223
224     @Override
225     public void validateMandatoryFields() {
226         // this method only checks if the mandatory fields have been initialized
227         String filedNameThatHasNotBeenInitialized = checkMandatoryFieldsExistInMDC();
228
229         if (MDC.getCopyOfContextMap() == null || MDC.getCopyOfContextMap().isEmpty()) {
230             writeLogMDCEmptyError();
231             return;
232         }
233
234         if (!"".equalsIgnoreCase(filedNameThatHasNotBeenInitialized)) {
235             writeLogMissingFieldsError(filedNameThatHasNotBeenInitialized);
236         }
237     }
238
239     protected void writeLogMissingFieldsError(String FiledNameThatHasNotBeenInitialized) {
240         log.error("mandatory parameters for EELF logging, missing fields: %s", FiledNameThatHasNotBeenInitialized);
241     }
242
243     protected void writeLogMDCEmptyError() {
244         log.error("write to log when MDC is empty error");
245     }
246
247     @Override
248     public EcompMDCWrapper clear() {
249         mandatoryFields.forEach(MDC::remove);
250         optionalFields.forEach(MDC::remove);
251         return this;
252     }
253
254     protected String checkMandatoryFieldsExistInMDC() {
255         // this method returns a String of uninitialised fields
256         StringBuilder missingFields = new StringBuilder();
257         mandatoryFields.forEach(field -> {
258             if (isMDCParamEmpty(field)) {
259                 missingFields.append(field).append(" ");
260             }
261         });
262         return missingFields.toString();
263     }
264
265     @Override
266     public void setMandatoryField(String parameterName) {
267         mandatoryFields.add(parameterName);
268     }
269
270     @Override
271     public void setOptionalField(String parameterName) {
272         optionalFields.add(parameterName);
273     }
274
275     @Override
276     public boolean isMDCParamEmpty(String mdcKeyName) {
277         String val = MDC.get(mdcKeyName);
278         return (val == null || val.trim().length() == 0);
279     }
280
281     @Override
282     public String getFqdn() {
283         return fqdn;
284     }
285
286     @Override
287     public String getHostAddress() {
288         return hostAddress;
289     }
290 }