summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/common/onaplog/OnapMDCWrapper.java
blob: 2249f14b04a4780ef666ab51acb0942b9a0cf4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package org.onap.sdc.common.onaplog;

import org.onap.sdc.common.onaplog.Enums.Severity;
import org.onap.sdc.common.onaplog.interfaces.IOnapMdcWrapper;
import org.onap.sdc.common.onaplog.interfaces.IStopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.net.InetAddress;
import java.util.ArrayList;

public class OnapMDCWrapper implements IOnapMdcWrapper {
    private IStopWatch stopWatch;
    private ArrayList<String> mandatoryFields = new ArrayList<>();
    private ArrayList<String> optionalFields = new ArrayList<>();
    private static String hostAddress;
    private static String fqdn;

    private static OnapMDCWrapper instanceMdcWrapper = new OnapMDCWrapper(new Stopwatch());
    private static final Logger log = LoggerFactory.getLogger(OnapMDCWrapper.class.getName());


    // in package classes can instantiate this class
    // to use directly from outside the package usr the getInstance() Method
    OnapMDCWrapper(IStopWatch stopwatch) {
        this.stopWatch = stopwatch;
    }


    public static OnapMDCWrapper getInstance() {
        return instanceMdcWrapper;
    }

    static {
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
            fqdn = InetAddress.getByName(hostAddress).getCanonicalHostName();
        } catch (Exception ex) {
            log.error("failed to get machine parameters", ex);
        }
    }

    @Override
    public OnapMDCWrapper startTimer() {
        stopWatch.start();
        return this;
    }

    @Override
    public OnapMDCWrapper stopTimer() {
        try {
            stopWatch.stop();
        } catch (Exception ex) {
            log.error("StopWatch failed; probably start was not called before Stopwatch", ex);
        }
        return this;
    }

    @Override
    public OnapMDCWrapper setClassName(String className) {
        MDC.put(OnapLogConfiguration.MDC_CLASS_NAME, className);
        return this;
    }

    // automatic parameter this is optional
    @Override
    public OnapMDCWrapper setAutoServerFQDN(String serverFQDN) {
        MDC.put(OnapLogConfiguration.MDC_SERVER_FQDN, serverFQDN);
        return this;
    }

    // automatic parameter this is optional
    @Override
    public OnapMDCWrapper setAutoServerIPAddress(String serverIPAddress) {
        MDC.put(OnapLogConfiguration.MDC_SERVER_IP_ADDRESS, serverIPAddress);
        return this;
    }

    @Override
    public OnapMDCWrapper setInstanceUUID(String instanceUUID) {
        MDC.put(OnapLogConfiguration.MDC_INSTANCE_UUID, instanceUUID);
        return this;
    }

    @Override
    public OnapMDCWrapper setProcessKey(String processKey) {
        MDC.put(OnapLogConfiguration.MDC_PROCESS_KEY, processKey);
        return this;
    }

    @Override
    public OnapMDCWrapper setAlertSeverity(Severity alertSeverity) {
        MDC.put(OnapLogConfiguration.MDC_ALERT_SEVERITY, String.valueOf(alertSeverity.getSeverityType()));
        return this;
    }

    @Override
    public OnapMDCWrapper setOptCustomField1(String customField1) {
        MDC.put(OnapLogConfiguration.MDC_OPT_FIELD1, customField1);
        return this;
    }

    @Override
    public OnapMDCWrapper setOptCustomField2(String customField2) {
        MDC.put(OnapLogConfiguration.MDC_OPT_FIELD2, customField2);
        return this;
    }

    @Override
    public OnapMDCWrapper setOptCustomField3(String customField3) {
        MDC.put(OnapLogConfiguration.MDC_OPT_FIELD3, customField3);
        return this;
    }

    @Override
    public OnapMDCWrapper setOptCustomField4(String customField4) {
        MDC.put(OnapLogConfiguration.MDC_OPT_FIELD4, customField4);
        return this;
    }

    @Override
    public OnapMDCWrapper setKeyRequestId(String keyRequestId) {
        MDC.put(OnapLogConfiguration.MDC_KEY_REQUEST_ID, keyRequestId); // eg. servletRequest.getSession().getId()
        return this;
    }

    @Override
    public OnapMDCWrapper setRemoteHost(String remoteHost) {
        MDC.put(OnapLogConfiguration.MDC_REMOTE_HOST, remoteHost);
        return this;
    }

    @Override
    public OnapMDCWrapper setServiceName(String serviceName) {
        MDC.put(OnapLogConfiguration.MDC_SERVICE_NAME, serviceName);
        return this;
    }

    @Override
    public OnapMDCWrapper setStatusCode(String statusCode) {
        MDC.put(OnapLogConfiguration.MDC_STATUS_CODE, statusCode);
        return this;
    }

    @Override
    public OnapMDCWrapper setPartnerName(String partnerName) {
        MDC.put(OnapLogConfiguration.MDC_PARTNER_NAME, partnerName);
        return this;
    }

    @Override
    public OnapMDCWrapper setResponseCode(int responseCode) {
        MDC.put(OnapLogConfiguration.MDC_RESPONSE_CODE, Integer.toString(responseCode));
        return this;
    }

    @Override
    public OnapMDCWrapper setResponseDesc(String responseDesc) {
        MDC.put(OnapLogConfiguration.MDC_RESPONSE_DESC, responseDesc);
        return this;
    }

    @Override
    public OnapMDCWrapper setServiceInstanceId(String serviceInstanceId) {
        MDC.put(OnapLogConfiguration.MDC_SERVICE_INSTANCE_ID, serviceInstanceId);
        return this;
    }

    @Override
    public OnapMDCWrapper setTargetEntity(String targetEntity) {
        MDC.put(OnapLogConfiguration.MDC_TARGET_ENTITY, targetEntity);
        return this;
    }

    @Override
    public OnapMDCWrapper setTargetServiceName(String targetServiceName) {
        MDC.put(OnapLogConfiguration.MDC_TARGET_SERVICE_NAME, targetServiceName);
        return this;
    }

    @Override
    public OnapMDCWrapper setTargetVirtualEntity(String targetVirtualEntity) {
        MDC.put(OnapLogConfiguration.MDC_TARGET_VIRTUAL_ENTITY, targetVirtualEntity);
        return this;
    }

    @Override
    public OnapMDCWrapper setErrorCode(int errorCode) {
        MDC.put(OnapLogConfiguration.MDC_ERROR_CODE, Integer.toString(errorCode));
        return this;
    }

    @Override
    public OnapMDCWrapper setErrorDescription(String errorDescription) {
        MDC.put(OnapLogConfiguration.MDC_ERROR_DESC, errorDescription);
        return this;
    }

    @Override
    public void validateMandatoryFields() {
        // this method only checks if the mandatory fields have been initialized
        String missingFieldNames = checkMandatoryFieldsExistInMDC();

        if (MDC.getCopyOfContextMap() == null || MDC.getCopyOfContextMap().isEmpty()) {
            writeLogMDCEmptyError();
            return;
        }

        if (!"".equalsIgnoreCase(missingFieldNames)) {
            writeLogMissingFieldsError(missingFieldNames);
        }
    }

    private void writeLogMissingFieldsError(String filedNameThatHasNotBeenInitialized) {
        log.warn("mandatory parameters for EELF logging, missing fields: {}", filedNameThatHasNotBeenInitialized);
    }

    private void writeLogMDCEmptyError() {
        log.error("write to log when MDC is empty error");
    }

    @Override
    public OnapMDCWrapper clear() {
        mandatoryFields.forEach(MDC::remove);
        optionalFields.forEach(MDC::remove);
        return this;
    }

    private String checkMandatoryFieldsExistInMDC() {
        // this method returns a String of uninitialised fields
        StringBuilder missingFields = new StringBuilder();
        mandatoryFields.forEach(field -> {
            if (isMDCParamEmpty(field)) {
                missingFields.append(field).append(" ");
            }
        });
        return missingFields.toString();
    }

    @Override
    public void setMandatoryField(String parameterName) {
        mandatoryFields.add(parameterName);
    }

    @Override
    public void setOptionalField(String parameterName) {
        optionalFields.add(parameterName);
    }

    @Override
    public boolean isMDCParamEmpty(String mdcKeyName) {
        String val = MDC.get(mdcKeyName);
        return val == null || val.trim().length() == 0;
    }

    @Override
    public String getFqdn() {
        return fqdn;
    }

    @Override
    public String getHostAddress() {
        return hostAddress;
    }
}