summaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/config/EcompErrorLogUtil.java
blob: 7145a110054a46dcd72c0658235c000ff28a79b4 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 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.openecomp.sdc.common.config;

import fj.data.Either;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.common.log.enums.EcompErrorSeverity;
import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.slf4j.MDC;

import java.util.Formatter;
import java.util.IllegalFormatException;
import java.util.Locale;

public class EcompErrorLogUtil {

	private static String ECOMP_ERROR_TMPL = "ETYPE = \"%s\" ENAME = \"%s\" ECODE = \"%s\" ECONTEXT = \"%s\" EDESC = \"%s\"";
	private static Logger log = Logger.getLogger(EcompErrorLogUtil.class.getName());
	private static final String FATAL_ERROR_PREFIX = "FATAL ERROR!! ";

	public static void logEcompError(EcompErrorName ecompErrorName, EcompErrorInfo ecompErrorInfo,
			String ecompErrorContext, String... ecompDescriptionParams) {
		if (ecompErrorInfo != null) {
			StringBuilder sb = new StringBuilder();
			Formatter formatter = new Formatter(sb, Locale.US);
			try {
				String description = ecompErrorInfo.getDescription();
				String severityStr = ecompErrorInfo.getSeverity();
				EcompErrorSeverity severity = EcompErrorSeverity.ERROR;
				// Since there is no FATAL log level, this is how we distinguish
				// the FATAL errors
				if (severityStr.equals(EcompErrorSeverity.FATAL.name())) {
					description = FATAL_ERROR_PREFIX + description;
					severity = EcompErrorSeverity.FATAL;
				} else if (severityStr.equals(EcompErrorSeverity.WARN.name())) {
					severity = EcompErrorSeverity.WARN;
				} else if (severityStr.equals(EcompErrorSeverity.INFO.name())) {
					severity = EcompErrorSeverity.INFO;
				}

				MDC.put("alarmSeverity", ecompErrorInfo.getAlarmSeverity());
				// else it stays ERROR
				formatter.format(ECOMP_ERROR_TMPL, ecompErrorInfo.getType(), ecompErrorName.name(),
						ecompErrorInfo.getCode(), ecompErrorContext, description);
                log.error(severity, EcompLoggerErrorCode.getByValue(ecompErrorInfo.getCode()),
                        ecompErrorContext, ecompErrorContext, description);
            } finally {
				formatter.close();
				MDC.remove("alarmSeverity");
			}
		}
	}

	public static void logEcompError(String ecompErrorContext, EcompErrorEnum ecompErrorEnum,
			String... ecompDescriptionParams) {
		logEcompError(ecompErrorContext, ecompErrorEnum, true, ecompDescriptionParams);
	}

	public static void logEcompError(String ecompErrorContext, EcompErrorEnum ecompErrorEnum, boolean logMissingParams,
			String... ecompDescriptionParams) {
		StringBuilder sb = new StringBuilder();
		Formatter formatter = new Formatter(sb, Locale.US);
		try {
			String description;

			Either<String, Boolean> setDescriptionParamsResult = setDescriptionParams(ecompErrorEnum,
					ecompDescriptionParams);
			if (setDescriptionParamsResult.isLeft()) {
				description = setDescriptionParamsResult.left().value();
			} else {
				EcompErrorEnum mismatchErrorEnum = EcompErrorEnum.EcompMismatchParam;
				if (logMissingParams) {
					logEcompError("logEcompError", mismatchErrorEnum, false, ecompErrorEnum.name());
				} else {
					log.info("Failed to log the error code {}", mismatchErrorEnum);
				}
				return;
			}
			EcompClassification classification = ecompErrorEnum.getClassification();

			EcompErrorSeverity severity = EcompErrorSeverity.ERROR;
			// Since there is no FATAL log level, this is how we distinguish the
			// FATAL errors
			if (classification == EcompClassification.FATAL) {
				description = FATAL_ERROR_PREFIX + description;
				severity = EcompErrorSeverity.FATAL;
			} else if (classification == EcompClassification.WARNING) {
				severity = EcompErrorSeverity.WARN;
			} else if (classification == EcompClassification.INFORMATION) {
				severity = EcompErrorSeverity.INFO;
			}

			String eCode = createEcode(ecompErrorEnum);

			MDC.put("alarmSeverity", ecompErrorEnum.getAlarmSeverity().name());
			// else it stays ERROR
			formatter.format(ECOMP_ERROR_TMPL, ecompErrorEnum.getEType(), ecompErrorEnum.name(), eCode,
					ecompErrorContext, description);

            log.error(severity, EcompLoggerErrorCode.getByValue(ecompErrorEnum.getEcompErrorCode().name()),
                    ecompErrorContext, ecompErrorContext, description);
		} finally {
			formatter.close();
			MDC.remove("alarmSeverity");
		}
	}

	public static String createEcode(EcompErrorEnum ecompErrorEnum) {

		EcompClassification classification = ecompErrorEnum.getClassification();
		String ecompErrorCode = ecompErrorEnum.getEcompErrorCode().name();

		String ecodeNumber = ecompErrorCode.substring(ecompErrorCode.indexOf("_") + 1);

        return "ASDC" + ecodeNumber + classification.getClassification();
	}

	private static Either<String, Boolean> setDescriptionParams(EcompErrorEnum ecompErrorEnum,
			String... descriptionParams) {
		String description = ecompErrorEnum.getEcompErrorCode().getDescription();

		// Counting number of params in description
		int countMatches = StringUtils.countMatches(description, AbsEcompErrorManager.PARAM_STR);
		// Catching cases when there are more params passed than there are in
		// the description (formatter will ignore extra params and won't throw
		// exception)
		if (countMatches != descriptionParams.length) {
			return Either.right(false);
		}
		// Setting params of the description if any
		StringBuilder sb = new StringBuilder();
		Formatter formatter = new Formatter(sb, Locale.US);
		try {
			formatter.format(description, (Object[]) descriptionParams).toString();
			return Either.left(formatter.toString());
		} catch (IllegalFormatException e) {
			// Number of passed params doesn't match number of params in config
			// file
			return Either.right(false);
		} finally {
			formatter.close();
		}

	}

}