aboutsummaryrefslogtreecommitdiffstats
path: root/aai-els-onap-logging/src/main/java/org/onap/aai/logging/ErrorLogHelper.java
blob: 1bc33f492a74cbae6be2158ff7940dde2797ec50 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.logging;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.apache.commons.lang3.StringUtils;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.util.AAIConstants;
import org.onap.aai.util.MapperUtil;
import org.onap.logging.filter.base.Constants;
import org.onap.logging.filter.base.MDCSetup;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
 *
 * This classes loads the application error properties file
 * and provides a method that returns an ErrorObject
 *
 */

public class ErrorLogHelper {
    private static final Logger LOGGER = LoggerFactory.getLogger(ErrorLogHelper.class);
    private static final HashMap<String, ErrorObject> ERROR_OBJECTS = new HashMap<String, ErrorObject>();

    static {
        try {
            loadProperties();
        } catch (IOException e) {
            throw new RuntimeException("Failed to load error.properties file", e);
        } catch (ErrorObjectFormatException e) {
            throw new RuntimeException("Failed to parse error.properties file", e);
        }
    }

    /**
     * Load properties.
     * @throws IOException the exception
     * @throws ErrorObjectFormatException
     */
    public static void loadProperties() throws IOException, ErrorObjectFormatException {
        final String filePath = AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "error.properties";
        final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("error.properties");
        final Properties properties = new Properties();

        try (final FileInputStream fis = new FileInputStream(filePath)) {
            LOGGER.info("Found the error.properties in the following location: {}", AAIConstants.AAI_HOME_ETC_APP_PROPERTIES);
            properties.load(fis);
        } catch(Exception ex){
            LOGGER.info("Unable to find the error.properties from filesystem so using file in jar");
            if (is != null) {
                properties.load(is);
            } else {
                LOGGER.error("Expected to find the error.properties in the jar but unable to find it");
            }
        }

        for (Entry<Object, Object> entry : properties.entrySet()) {
            final String key = (String) entry.getKey();
            final String value = (String) entry.getValue();
            final String[] errorProperties = value.split(":");

            if (errorProperties.length < 7)
                throw new ErrorObjectFormatException();

            final ErrorObject errorObject = new ErrorObject();

            errorObject.setDisposition(errorProperties[0].trim());
            errorObject.setCategory(errorProperties[1].trim());
            errorObject.setSeverity(errorProperties[2].trim());
            errorObject.setErrorCode(errorProperties[3].trim());
            errorObject.setHTTPResponseCode(errorProperties[4].trim());
            errorObject.setRESTErrorCode(errorProperties[5].trim());
            errorObject.setErrorText(errorProperties[6].trim());
            if (errorProperties.length > 7) {
                errorObject.setAaiElsErrorCode(errorProperties[7].trim());
            }

            ERROR_OBJECTS.put(key, errorObject);
        }
    }

    /**
     * Logs a known A&AI exception (i.e. one that can be found in error.properties)
     *
     * @param code for the error in the error.properties file
     * @throws IOException
     * @throws ErrorObjectNotFoundException
     */
    public static ErrorObject getErrorObject(String code) throws ErrorObjectNotFoundException {

        if (code == null)
            throw new IllegalArgumentException("Key cannot be null");

        final ErrorObject errorObject = ERROR_OBJECTS.get(code);

        if (errorObject == null) {
            LOGGER.warn("Unknown AAIException with code=" + code + ".  Using default AAIException");
            return ERROR_OBJECTS.get(AAIException.DEFAULT_EXCEPTION_CODE);
        }

        return errorObject;
    }

    /**
     * Determines whether category is policy or not. If policy (1), this is a POL error, else it's a SVC error.
     * The AAIRESTException may contain a different ErrorObject than that created with the REST error key.
     * This allows lower level exception detail to be returned to the client to help troubleshoot the problem.
     * If no error object is embedded in the AAIException, one will be created using the error object from the
     * AAIException.
     *
     * @param are must have a restError value whose numeric value must match what should be returned in the REST API
     * @param variables optional list of variables to flesh out text in error string
     * @return appropriately formatted JSON response per the REST API spec.
     * @throws IOException
     * @deprecated
     */
    public static String getRESTAPIErrorResponse(AAIException are, ArrayList<String> variables) {
        List<MediaType> acceptHeaders = new ArrayList<MediaType>();
        acceptHeaders.add(MediaType.APPLICATION_JSON_TYPE);

        return getRESTAPIErrorResponse(acceptHeaders, are, variables);
    }

    /**
     * Determines whether category is policy or not. If policy (1), this is a POL error, else it's a SVC error.
     * The AAIRESTException may contain a different ErrorObject than that created with the REST error key.
     * This allows lower level exception detail to be returned to the client to help troubleshoot the problem.
     * If no error object is embedded in the AAIException, one will be created using the error object from the
     * AAIException.
     *
     * @param acceptHeadersOrig the accept headers orig
     * @param are must have a restError value whose numeric value must match what should be returned in the REST API
     * @param variables optional list of variables to flesh out text in error string
     * @return appropriately formatted JSON response per the REST API spec.
     */
    public static String getRESTAPIErrorResponse(List<MediaType> acceptHeadersOrig, AAIException are,
            ArrayList<String> variables) {

        StringBuilder text = new StringBuilder();
        String response = null;

        List<MediaType> acceptHeaders = new ArrayList<MediaType>();
        // we might have an exception but no accept header, so we'll set default to JSON
        boolean foundValidAcceptHeader = false;
        for (MediaType mt : acceptHeadersOrig) {
            if (MediaType.APPLICATION_XML_TYPE.isCompatible(mt) || MediaType.APPLICATION_JSON_TYPE.isCompatible(mt)) {
                acceptHeaders.add(mt);
                foundValidAcceptHeader = true;
            }
        }
        if (foundValidAcceptHeader == false) {
            // override the exception, client needs to set an appropriate Accept header
            are = new AAIException("AAI_4014");
            acceptHeaders.add(MediaType.APPLICATION_JSON_TYPE);
        }

        final ErrorObject eo = are.getErrorObject();

        int restErrorCode = Integer.parseInt(eo.getRESTErrorCode());

        ErrorObject restErrorObject;

        try {
            restErrorObject = ErrorLogHelper.getErrorObject("AAI_" + restErrorCode);
        } catch (ErrorObjectNotFoundException e) {
            LOGGER.warn("Failed to find related error object AAI_" + restErrorCode + " for error object "
                    + eo.getErrorCode() + "; using AAI_" + restErrorCode);
            restErrorObject = eo;
        }

        text.append(restErrorObject.getErrorText());

        // We want to always append the (msg=%n) (ec=%n+1) to the text, but have to find value of n
        // This assumes that the variables in the ArrayList, which might be more than are needed to flesh out the
        // error, are ordered based on the error string.
        int localDataIndex = StringUtils.countMatches(restErrorObject.getErrorText(), "%");
        text.append(" (msg=%").append(localDataIndex + 1).append(") (ec=%").append(localDataIndex + 2).append(")");

        if (variables == null) {
            variables = new ArrayList<String>();
        }

        if (variables.size() < localDataIndex) {
            ErrorLogHelper.logError("AAI_4011", "data missing for rest error");
            while (variables.size() < localDataIndex) {
                variables.add("null");
            }
        }

        // This will put the error code and error text into the right positions
        if (are.getMessage() == null || are.getMessage().length() == 0) {
            variables.add(localDataIndex++, eo.getErrorText());
        } else {
            variables.add(localDataIndex++, eo.getErrorText() + ":" + are.getMessage());
        }
        variables.add(localDataIndex, eo.getErrorCodeString());

        for (MediaType mediaType : acceptHeaders) {
            if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) {
                JAXBContext context = null;
                try {
                    if (eo.getCategory().equals("1")) {

                        context = JAXBContext.newInstance(org.onap.aai.domain.restPolicyException.Fault.class);
                        Marshaller m = context.createMarshaller();
                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

                        org.onap.aai.domain.restPolicyException.ObjectFactory factory =
                                new org.onap.aai.domain.restPolicyException.ObjectFactory();
                        org.onap.aai.domain.restPolicyException.Fault fault = factory.createFault();
                        org.onap.aai.domain.restPolicyException.Fault.RequestError requestError =
                                factory.createFaultRequestError();
                        org.onap.aai.domain.restPolicyException.Fault.RequestError.PolicyException policyException =
                                factory.createFaultRequestErrorPolicyException();
                        org.onap.aai.domain.restPolicyException.Fault.RequestError.PolicyException.Variables polvariables =
                                factory.createFaultRequestErrorPolicyExceptionVariables();

                        policyException.setMessageId("POL" + eo.getRESTErrorCode());
                        policyException.setText(text.toString());
                        for (int i = 0; i < variables.size(); i++) {
                            polvariables.getVariable().add(variables.get(i));
                        }
                        policyException.setVariables(polvariables);
                        requestError.setPolicyException(policyException);
                        fault.setRequestError(requestError);

                        StringWriter sw = new StringWriter();
                        m.marshal(fault, sw);

                        response = sw.toString();

                    } else {

                        context = JAXBContext.newInstance(org.onap.aai.domain.restServiceException.Fault.class);
                        Marshaller m = context.createMarshaller();
                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

                        org.onap.aai.domain.restServiceException.ObjectFactory factory =
                                new org.onap.aai.domain.restServiceException.ObjectFactory();
                        org.onap.aai.domain.restServiceException.Fault fault = factory.createFault();
                        org.onap.aai.domain.restServiceException.Fault.RequestError requestError =
                                factory.createFaultRequestError();
                        org.onap.aai.domain.restServiceException.Fault.RequestError.ServiceException serviceException =
                                factory.createFaultRequestErrorServiceException();
                        org.onap.aai.domain.restServiceException.Fault.RequestError.ServiceException.Variables svcvariables =
                                factory.createFaultRequestErrorServiceExceptionVariables();
                        serviceException.setMessageId("SVC" + eo.getRESTErrorCode());
                        serviceException.setText(text.toString());
                        for (int i = 0; i < variables.size(); i++) {
                            svcvariables.getVariable().add(variables.get(i));
                        }
                        serviceException.setVariables(svcvariables);
                        requestError.setServiceException(serviceException);
                        fault.setRequestError(requestError);

                        StringWriter sw = new StringWriter();
                        m.marshal(fault, sw);

                        response = sw.toString();

                    }
                } catch (Exception ex) {
                    LOGGER.error(
                            "We were unable to create a rest exception to return on an API because of a parsing error "
                                    + ex.getMessage());
                }
            } else {
                try {
                    if (eo.getCategory().equals("1")) {
                        org.onap.aai.domain.restPolicyException.RESTResponse restresp =
                                new org.onap.aai.domain.restPolicyException.RESTResponse();
                        org.onap.aai.domain.restPolicyException.RequestError reqerr =
                                new org.onap.aai.domain.restPolicyException.RequestError();
                        org.onap.aai.domain.restPolicyException.PolicyException polexc =
                                new org.onap.aai.domain.restPolicyException.PolicyException();
                        polexc.setMessageId("POL" + eo.getRESTErrorCode());
                        polexc.setText(text.toString());
                        polexc.setVariables(variables);
                        reqerr.setPolicyException(polexc);
                        restresp.setRequestError(reqerr);
                        response = (MapperUtil.writeAsJSONString((Object) restresp));

                    } else {
                        org.onap.aai.domain.restServiceException.RESTResponse restresp =
                                new org.onap.aai.domain.restServiceException.RESTResponse();
                        org.onap.aai.domain.restServiceException.RequestError reqerr =
                                new org.onap.aai.domain.restServiceException.RequestError();
                        org.onap.aai.domain.restServiceException.ServiceException svcexc =
                                new org.onap.aai.domain.restServiceException.ServiceException();
                        svcexc.setMessageId("SVC" + eo.getRESTErrorCode());
                        svcexc.setText(text.toString());
                        svcexc.setVariables(variables);
                        reqerr.setServiceException(svcexc);
                        restresp.setRequestError(reqerr);
                        response = (MapperUtil.writeAsJSONString((Object) restresp));
                    }
                } catch (Exception ex) {
                    LOGGER.error(
                            "We were unable to create a rest exception to return on an API because of a parsing error "
                                    + ex.getMessage());
                }
            }
        }

        return response;
    }

    /**
     * Gets the RESTAPI error response with logging.
     *
     * @param acceptHeadersOrig the accept headers orig
     * @param are the are
     * @param variables the variables
     */
    public static String getRESTAPIErrorResponseWithLogging(List<MediaType> acceptHeadersOrig, AAIException are,
            ArrayList<String> variables) {
        String response = ErrorLogHelper.getRESTAPIErrorResponse(acceptHeadersOrig, are, variables);
        logException(are);
        return response;
    }

    /**
     * Gets the RESTAPI info response.
     *
     * @param acceptHeaders the accept headers
     * @param areList the are list
     * @return the RESTAPI info response
     */
    public static Object getRESTAPIInfoResponse(List<MediaType> acceptHeaders,
            HashMap<AAIException, ArrayList<String>> areList) {

        Object respObj = null;

        org.onap.aai.domain.restResponseInfo.ObjectFactory factory =
                new org.onap.aai.domain.restResponseInfo.ObjectFactory();
        org.onap.aai.domain.restResponseInfo.Info info = factory.createInfo();
        org.onap.aai.domain.restResponseInfo.Info.ResponseMessages responseMessages =
                factory.createInfoResponseMessages();
        Iterator<Entry<AAIException, ArrayList<String>>> it = areList.entrySet().iterator();

        while (it.hasNext()) {
            Entry<AAIException, ArrayList<String>> pair = (Entry<AAIException, ArrayList<String>>) it.next();
            AAIException are = pair.getKey();
            ArrayList<String> variables = pair.getValue();

            StringBuilder text = new StringBuilder();

            ErrorObject eo = are.getErrorObject();

            int restErrorCode = Integer.parseInt(eo.getRESTErrorCode());
            ErrorObject restErrorObject;
            try {
                restErrorObject = ErrorLogHelper.getErrorObject("AAI_" + String.format("%04d", restErrorCode));
            } catch (ErrorObjectNotFoundException e) {
                restErrorObject = eo;
            }
            text.append(restErrorObject.getErrorText());

            // We want to always append the (msg=%n) (ec=%n+1) to the text, but have to find value of n
            // This assumes that the variables in the ArrayList, which might be more than are needed to flesh out the
            // error, are ordered based on the error string.
            int localDataIndex = StringUtils.countMatches(restErrorObject.getErrorText(), "%");
            text.append(" (msg=%").append(localDataIndex + 1).append(") (rc=%").append(localDataIndex + 2).append(")");

            if (variables == null) {
                variables = new ArrayList<String>();
            }

            if (variables.size() < localDataIndex) {
                ErrorLogHelper.logError("AAI_4011", "data missing for rest error");
                while (variables.size() < localDataIndex) {
                    variables.add("null");
                }
            }

            // This will put the error code and error text into the right positions
            if (are.getMessage() == null) {
                variables.add(localDataIndex++, eo.getErrorText());
            } else {
                variables.add(localDataIndex++, eo.getErrorText() + ":" + are.getMessage());
            }
            variables.add(localDataIndex, eo.getErrorCodeString());

            try {
                org.onap.aai.domain.restResponseInfo.Info.ResponseMessages.ResponseMessage responseMessage =
                        factory.createInfoResponseMessagesResponseMessage();
                org.onap.aai.domain.restResponseInfo.Info.ResponseMessages.ResponseMessage.Variables infovariables =
                        factory.createInfoResponseMessagesResponseMessageVariables();

                responseMessage.setMessageId("INF" + eo.getRESTErrorCode());
                responseMessage.setText(text.toString());
                for (int i = 0; i < variables.size(); i++) {
                    infovariables.getVariable().add(variables.get(i));
                }

                responseMessage.setVariables(infovariables);
                responseMessages.getResponseMessage().add(responseMessage);

            } catch (Exception ex) {
                LOGGER.error("We were unable to create a rest exception to return on an API because of a parsing error "
                        + ex.getMessage());
            }
        }

        info.setResponseMessages(responseMessages);
        respObj = (Object) info;

        return respObj;
    }

    /**
     * Determines whether category is policy or not. If policy (1), this is a POL error, else it's a SVC error.
     * The AAIRESTException may contain a different ErrorObject than that created with the REST error key.
     * This allows lower level exception detail to be returned to the client to help troubleshoot the problem.
     * If no error object is embedded in the AAIException, one will be created using the error object from the
     * AAIException.
     *
     * @param are must have a restError value whose numeric value must match what should be returned in the REST API
     * @param variables optional list of variables to flesh out text in error string
     * @return appropriately formatted JSON response per the REST API spec.
     */
    public static String getRESTAPIPolicyErrorResponseXML(AAIException are, ArrayList<String> variables) {

        StringBuilder text = new StringBuilder();
        String response = null;
        JAXBContext context = null;

        ErrorObject eo = are.getErrorObject();

        int restErrorCode = Integer.parseInt(eo.getRESTErrorCode());
        ErrorObject restErrorObject;
        try {
            restErrorObject = ErrorLogHelper.getErrorObject("AAI_" + restErrorCode);
        } catch (ErrorObjectNotFoundException e) {
            restErrorObject = eo;
        }

        text.append(restErrorObject.getErrorText());

        // We want to always append the (msg=%n) (ec=%n+1) to the text, but have to find value of n
        // This assumes that the variables in the ArrayList, which might be more than are needed to flesh out the
        // error, are ordered based on the error string.
        int localDataIndex = StringUtils.countMatches(restErrorObject.getErrorText(), "%");
        text.append(" (msg=%").append(localDataIndex + 1).append(") (ec=%").append(localDataIndex + 2).append(")");

        if (variables == null) {
            variables = new ArrayList<String>();
        }

        if (variables.size() < localDataIndex) {
            ErrorLogHelper.logError("AAI_4011", "data missing for rest error");
            while (variables.size() < localDataIndex) {
                variables.add("null");
            }
        }

        // This will put the error code and error text into the right positions
        if (are.getMessage() == null) {
            variables.add(localDataIndex++, eo.getErrorText());
        } else {
            variables.add(localDataIndex++, eo.getErrorText() + ":" + are.getMessage());
        }
        variables.add(localDataIndex, eo.getErrorCodeString());

        try {
            if (eo.getCategory().equals("1")) {

                context = JAXBContext.newInstance(org.onap.aai.domain.restPolicyException.Fault.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

                org.onap.aai.domain.restPolicyException.ObjectFactory factory =
                        new org.onap.aai.domain.restPolicyException.ObjectFactory();
                org.onap.aai.domain.restPolicyException.Fault fault = factory.createFault();
                org.onap.aai.domain.restPolicyException.Fault.RequestError requestError =
                        factory.createFaultRequestError();
                org.onap.aai.domain.restPolicyException.Fault.RequestError.PolicyException policyException =
                        factory.createFaultRequestErrorPolicyException();
                org.onap.aai.domain.restPolicyException.Fault.RequestError.PolicyException.Variables polvariables =
                        factory.createFaultRequestErrorPolicyExceptionVariables();

                policyException.setMessageId("POL" + eo.getRESTErrorCode());
                policyException.setText(text.toString());
                for (int i = 0; i < variables.size(); i++) {
                    polvariables.getVariable().add(variables.get(i));
                }
                policyException.setVariables(polvariables);
                requestError.setPolicyException(policyException);
                fault.setRequestError(requestError);

                StringWriter sw = new StringWriter();
                m.marshal(fault, sw);

                response = sw.toString();

            } else {

                context = JAXBContext.newInstance(org.onap.aai.domain.restServiceException.Fault.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

                org.onap.aai.domain.restServiceException.ObjectFactory factory =
                        new org.onap.aai.domain.restServiceException.ObjectFactory();
                org.onap.aai.domain.restServiceException.Fault fault = factory.createFault();
                org.onap.aai.domain.restServiceException.Fault.RequestError requestError =
                        factory.createFaultRequestError();
                org.onap.aai.domain.restServiceException.Fault.RequestError.ServiceException serviceException =
                        factory.createFaultRequestErrorServiceException();
                org.onap.aai.domain.restServiceException.Fault.RequestError.ServiceException.Variables svcvariables =
                        factory.createFaultRequestErrorServiceExceptionVariables();
                serviceException.setMessageId("POL" + eo.getRESTErrorCode());
                serviceException.setText(text.toString());
                for (int i = 0; i < variables.size(); i++) {
                    svcvariables.getVariable().add(variables.get(i));
                }
                serviceException.setVariables(svcvariables);
                requestError.setServiceException(serviceException);
                fault.setRequestError(requestError);

                StringWriter sw = new StringWriter();
                m.marshal(fault, sw);

                response = sw.toString();

            }
        } catch (Exception ex) {
            LOGGER.error("We were unable to create a rest exception to return on an API because of a parsing error "
                    + ex.getMessage());
        }
        return response;
    }

    public static void logException(AAIException e) {
        final ErrorObject errorObject = e.getErrorObject();
        /*
        String severityCode = errorObject.getSeverityCode(errorObject.getSeverity());

        Severify should be left empty per Logging Specification 2019.11
        if (!StringUtils.isEmpty(severityCode)) {
            int sevCode = Integer.parseInt(severityCode);
            if (sevCode > 0 && sevCode <= 3) {
                LoggingContext.severity(sevCode);
            }
        }
        */
        String stackTrace = "";
        try {
            stackTrace = LogFormatTools.getStackTop(e);
        } catch (Exception a) {
            // ignore
        }
        final String errorMessage = new StringBuilder().append(errorObject.getErrorText()).append(":")
                .append(errorObject.getRESTErrorCode()).append(":").append(errorObject.getHTTPResponseCode())
                .append(":").append(e.getMessage()).toString().replaceAll("\\n", "^");

        MDCSetup mdcSetup = new MDCSetup();
        mdcSetup.setResponseStatusCode(errorObject.getHTTPResponseCode().getStatusCode());
        mdcSetup.setErrorCode(Integer.parseInt(errorObject.getAaiElsErrorCode()));
        String serviceName = MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME);
        if (serviceName == null || serviceName.isEmpty()) {
            MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, Constants.DefaultValues.UNKNOWN);
        }
        MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorMessage);
        final String details =
                new StringBuilder().append(errorObject.getErrorCodeString()).append(" ").append(stackTrace).toString();

        if (errorObject.getSeverity().equalsIgnoreCase("WARN"))
            LOGGER.warn(details);
        else if (errorObject.getSeverity().equalsIgnoreCase("ERROR"))
            LOGGER.error(details);
        else if (errorObject.getSeverity().equalsIgnoreCase("FATAL"))
            LOGGER.error(details);
        else if (errorObject.getSeverity().equals("INFO"))
            LOGGER.info(details);
    }

    public static void logError(String code) {
        logError(code, "");
    }

    public static void logError(String code, String message) {
        logException(new AAIException(code, message));
    }
}