fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / config / EcompErrorLogUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.config;
22
23 import fj.data.Either;
24 import org.apache.commons.lang3.StringUtils;
25 import org.openecomp.sdc.common.config.EcompErrorConfiguration.EcompErrorSeverity;
26 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.slf4j.MDC;
29
30 import java.util.Formatter;
31 import java.util.IllegalFormatException;
32 import java.util.Locale;
33
34 public class EcompErrorLogUtil {
35
36     private static final String ECOMP_ERROR_TMPL = "ETYPE = \"%s\" ENAME = \"%s\" ECODE = \"%s\" ECONTEXT = \"%s\" EDESC = \"%s\"";
37     private static Logger log = Logger.getLogger(EcompErrorLogUtil.class.getName());
38     private static final String FATAL_ERROR_PREFIX = "FATAL ERROR!! ";
39
40     public static void logEcompError(EcompErrorName ecompErrorName, EcompErrorInfo ecompErrorInfo,
41                                      String ecompErrorContext, String... ecompDescriptionParams) {
42         if (ecompErrorInfo != null) {
43             StringBuilder sb = new StringBuilder();
44             Formatter formatter = new Formatter(sb, Locale.US);
45             try {
46                 String description = ecompErrorInfo.getDescription();
47                 String severityStr = ecompErrorInfo.getSeverity();
48                 EcompErrorConfiguration.EcompErrorSeverity severity = EcompErrorSeverity.ERROR;
49                 // Since there is no FATAL log level, this is how we distinguish
50                 // the FATAL errors
51                 if (severityStr.equals(EcompErrorConfiguration.EcompErrorSeverity.FATAL.name())) {
52                     description = FATAL_ERROR_PREFIX + description;
53                     severity = EcompErrorSeverity.FATAL;
54                 } else if (severityStr.equals(EcompErrorConfiguration.EcompErrorSeverity.WARN.name())) {
55                     severity = EcompErrorSeverity.WARN;
56                 } else if (severityStr.equals(EcompErrorConfiguration.EcompErrorSeverity.INFO.name())) {
57                     severity = EcompErrorSeverity.INFO;
58                 }
59
60                 MDC.put("alarmSeverity", ecompErrorInfo.getAlarmSeverity());
61                 // else it stays ERROR
62                 formatter.format(ECOMP_ERROR_TMPL, ecompErrorInfo.getType(), ecompErrorName.name(),
63                         ecompErrorInfo.getCode(), ecompErrorContext, description);
64                 log.error(severity, EcompLoggerErrorCode.getByValue(ecompErrorInfo.getCode()),
65                         ecompErrorContext, ecompErrorContext, description);
66             } finally {
67                 formatter.close();
68                 MDC.remove("alarmSeverity");
69             }
70         }
71     }
72
73     public static void logEcompError(String ecompErrorContext, EcompErrorEnum ecompErrorEnum,
74                                      String... ecompDescriptionParams) {
75         logEcompError(ecompErrorContext, ecompErrorEnum, true, ecompDescriptionParams);
76     }
77
78     public static void logEcompError(String ecompErrorContext, EcompErrorEnum ecompErrorEnum, boolean logMissingParams,
79                                      String... ecompDescriptionParams) {
80         StringBuilder sb = new StringBuilder();
81         Formatter formatter = new Formatter(sb, Locale.US);
82         try {
83             String description;
84
85             Either<String, Boolean> setDescriptionParamsResult = setDescriptionParams(ecompErrorEnum,
86                     ecompDescriptionParams);
87             if (setDescriptionParamsResult.isLeft()) {
88                 description = setDescriptionParamsResult.left().value();
89             } else {
90                 EcompErrorEnum mismatchErrorEnum = EcompErrorEnum.EcompMismatchParam;
91                 if (logMissingParams) {
92                     logEcompError("logEcompError", mismatchErrorEnum, false, ecompErrorEnum.name());
93                 } else {
94                     log.info("Failed to log the error code {}", mismatchErrorEnum);
95                 }
96                 return;
97             }
98             EcompClassification classification = ecompErrorEnum.getClassification();
99
100             EcompErrorConfiguration.EcompErrorSeverity severity = EcompErrorSeverity.ERROR;
101             // Since there is no FATAL log level, this is how we distinguish the
102             // FATAL errors
103             if (classification == EcompClassification.FATAL) {
104                 description = FATAL_ERROR_PREFIX + description;
105                 severity = EcompErrorSeverity.FATAL;
106             } else if (classification == EcompClassification.WARNING) {
107                 severity = EcompErrorSeverity.WARN;
108             } else if (classification == EcompClassification.INFORMATION) {
109                 severity = EcompErrorSeverity.INFO;
110             }
111
112             String eCode = createEcode(ecompErrorEnum);
113
114             MDC.put("alarmSeverity", ecompErrorEnum.getAlarmSeverity().name());
115             // else it stays ERROR
116             formatter.format(ECOMP_ERROR_TMPL, ecompErrorEnum.geteType(), ecompErrorEnum.name(), eCode,
117                     ecompErrorContext, description);
118
119             log.error(severity, EcompLoggerErrorCode.getByValue(ecompErrorEnum.getEcompErrorCode().name()),
120                     ecompErrorContext, ecompErrorContext, description);
121         } finally {
122             formatter.close();
123             MDC.remove("alarmSeverity");
124         }
125     }
126
127     public static String createEcode(EcompErrorEnum ecompErrorEnum) {
128
129         EcompClassification classification = ecompErrorEnum.getClassification();
130         String ecompErrorCode = ecompErrorEnum.getEcompErrorCode().name();
131
132         String ecodeNumber = ecompErrorCode.substring(ecompErrorCode.indexOf("_") + 1);
133
134         return "ASDC" + ecodeNumber + classification.getClassification();
135     }
136
137     private static Either<String, Boolean> setDescriptionParams(EcompErrorEnum ecompErrorEnum,
138                                                                 String... descriptionParams) {
139         String description = ecompErrorEnum.getEcompErrorCode().getDescription();
140
141         // Counting number of params in description
142         int countMatches = StringUtils.countMatches(description, AbsEcompErrorManager.PARAM_STR);
143         // Catching cases when there are more params passed than there are in
144         // the description (formatter will ignore extra params and won't throw
145         // exception)
146         if (countMatches != descriptionParams.length) {
147             return Either.right(false);
148         }
149         // Setting params of the description if any
150         StringBuilder sb = new StringBuilder();
151         Formatter formatter = new Formatter(sb, Locale.US);
152         try {
153             formatter.format(description, (Object[]) descriptionParams).toString();
154             return Either.left(formatter.toString());
155         } catch (IllegalFormatException e) {
156             // Number of passed params doesn't match number of params in config
157             // file
158             return Either.right(false);
159         } finally {
160             formatter.close();
161         }
162
163     }
164
165 }