1 package org.openecomp.sdc.common.ecomplog;
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;
28 import java.net.InetAddress;
29 import java.util.ArrayList;
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;
38 public class EcompMDCWrapper implements IEcompMdcWrapper {
40 private static EcompMDCWrapper instanceMdcWrapper = new EcompMDCWrapper(new Stopwatch());
42 public static EcompMDCWrapper getInstance() {
43 return instanceMdcWrapper;
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;
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;
61 hostAddress = InetAddress.getLocalHost().getHostAddress();
62 fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
63 } catch (Exception ex) {
64 log.error("failed to get machine parameters", ex);
69 public EcompMDCWrapper startTimer() {
75 public EcompMDCWrapper stopTimer() {
78 } catch (Exception ex) {
79 log.error("StopWatch failed; probably start was not called before Stopwatch", ex);
85 public EcompMDCWrapper setClassName(String className) {
86 MDC.put(MDC_CLASS_NAME, className);
90 // automatic parameter this is optional
92 public EcompMDCWrapper setAutoServerFQDN(String serverFQDN) {
93 MDC.put(MDC_SERVER_FQDN, serverFQDN);
97 // automatic parameter this is optional
99 public EcompMDCWrapper setAutoServerIPAddress(String serverIPAddress) {
100 MDC.put(MDC_SERVER_IP_ADDRESS, serverIPAddress);
105 public EcompMDCWrapper setInstanceUUID(String instanceUUID) {
106 MDC.put(MDC_INSTANCE_UUID, instanceUUID);
111 public EcompMDCWrapper setProcessKey(String processKey) {
112 MDC.put(MDC_PROCESS_KEY, processKey);
117 public EcompMDCWrapper setAlertSeverity(Severity alertSeverity) {
118 MDC.put(MDC_ALERT_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
123 public EcompMDCWrapper setOptCustomField1(String customField1) {
124 MDC.put(MDC_OPT_FIELD1, customField1);
129 public EcompMDCWrapper setOptCustomField2(String customField2) {
130 MDC.put(MDC_OPT_FIELD2, customField2);
135 public EcompMDCWrapper setOptCustomField3(String customField3) {
136 MDC.put(MDC_OPT_FIELD3, customField3);
141 public EcompMDCWrapper setOptCustomField4(String customField4) {
142 MDC.put(MDC_OPT_FIELD4, customField4);
147 public EcompMDCWrapper setKeyRequestId(String keyRequestId) {
148 MDC.put(MDC_KEY_REQUEST_ID, keyRequestId); // eg. servletRequest.getSession().getId()
153 public EcompMDCWrapper setRemoteHost(String remoteHost) {
154 MDC.put(MDC_REMOTE_HOST, remoteHost);
159 public EcompMDCWrapper setServiceName(String serviceName) {
160 MDC.put(MDC_SERVICE_NAME, serviceName);
165 public EcompMDCWrapper setStatusCode(String statusCode) {
166 MDC.put(MDC_STATUS_CODE, statusCode);
171 public EcompMDCWrapper setPartnerName(String partnerName) {
172 MDC.put(MDC_PARTNER_NAME, partnerName);
177 public EcompMDCWrapper setResponseCode(int responseCode) {
178 MDC.put(MDC_RESPONSE_CODE, Integer.toString(responseCode));
183 public EcompMDCWrapper setResponseDesc(String responseDesc) {
184 MDC.put(MDC_RESPONSE_DESC, responseDesc);
189 public EcompMDCWrapper setServiceInstanceId(String serviceInstanceId) {
190 MDC.put(MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
195 public EcompMDCWrapper setTargetEntity(String targetEntity) {
196 MDC.put(MDC_TARGET_ENTITY, targetEntity);
201 public EcompMDCWrapper setTargetServiceName(String targetServiceName) {
202 MDC.put(MDC_TARGET_SERVICE_NAME, targetServiceName);
207 public EcompMDCWrapper setTargetVirtualEntity(String targetVirtualEntity) {
208 MDC.put(MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
213 public EcompMDCWrapper setErrorCode(int errorCode) {
214 MDC.put(MDC_ERROR_CODE, valueOf(errorCode).toString());
219 public EcompMDCWrapper setErrorDescription(String errorDescription) {
220 MDC.put(MDC_ERROR_DESC, errorDescription);
225 public void validateMandatoryFields() {
226 // this method only checks if the mandatory fields have been initialized
227 String filedNameThatHasNotBeenInitialized = checkMandatoryFieldsExistInMDC();
229 if (MDC.getCopyOfContextMap() == null || MDC.getCopyOfContextMap().isEmpty()) {
230 writeLogMDCEmptyError();
234 if (!"".equalsIgnoreCase(filedNameThatHasNotBeenInitialized)) {
235 writeLogMissingFieldsError(filedNameThatHasNotBeenInitialized);
239 protected void writeLogMissingFieldsError(String FiledNameThatHasNotBeenInitialized) {
240 log.error("mandatory parameters for EELF logging, missing fields: %s", FiledNameThatHasNotBeenInitialized);
243 protected void writeLogMDCEmptyError() {
244 log.error("write to log when MDC is empty error");
248 public EcompMDCWrapper clear() {
249 mandatoryFields.forEach(MDC::remove);
250 optionalFields.forEach(MDC::remove);
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(" ");
262 return missingFields.toString();
266 public void setMandatoryField(String parameterName) {
267 mandatoryFields.add(parameterName);
271 public void setOptionalField(String parameterName) {
272 optionalFields.add(parameterName);
276 public boolean isMDCParamEmpty(String mdcKeyName) {
277 String val = MDC.get(mdcKeyName);
278 return (val == null || val.trim().length() == 0);
282 public String getFqdn() {
287 public String getHostAddress() {