DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / main / java / org / onap / sdc / dcae / errormng / AbstractSdncException.java
1 package org.onap.sdc.dcae.errormng;
2
3
4 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
5 import org.onap.sdc.common.onaplog.OnapLoggerError;
6 import org.onap.sdc.common.onaplog.Enums.LogLevel;
7
8 import java.util.Arrays;
9 import java.util.Formatter;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 public class AbstractSdncException {
14         private String messageId;
15
16         private String text;
17
18         private String[] variables;
19
20         private static OnapLoggerError errLogger = OnapLoggerError.getInstance();
21         private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
22
23         private final static Pattern ERROR_PARAM_PATTERN = Pattern.compile("%\\d");
24
25         public AbstractSdncException() {
26         }
27
28         public AbstractSdncException(String messageId, String text, String[] variables) {
29                 super();
30                 this.messageId = messageId;
31                 this.text = text;
32                 this.variables = validateParameters(messageId, text, variables);
33         }
34
35         private String[] validateParameters(String messageId, String text, String[] variables) {
36                 String[] res = null;
37                 Matcher m = ERROR_PARAM_PATTERN.matcher(text);
38                 int expectedParamsNum = 0;
39                 while (m.find()) {
40                         expectedParamsNum += 1;
41                 }
42                 int actualParamsNum = (variables != null) ? variables.length : 0;
43                 if (actualParamsNum < expectedParamsNum) {
44                         errLogger.log(LogLevel.WARN, this.getClass().getName(),
45                                         "Received less parameters than expected for error with messageId {}, expected: {}, actual: {}. Missing parameters are padded with null values.",
46                                         messageId, expectedParamsNum, actualParamsNum);
47                 } else if (actualParamsNum > expectedParamsNum) {
48                         errLogger.log(LogLevel.WARN, this.getClass().getName(),
49                                         "Received more parameters than expected for error with messageId {}, expected: {}, actual: {}. Extra parameters are ignored.",
50                                         messageId, expectedParamsNum, actualParamsNum);
51                 }
52                 if (variables != null) {
53                         res = Arrays.copyOf(variables, expectedParamsNum);
54                 }
55
56                 return res;
57         }
58
59         public String getMessageId() {
60                 return this.messageId;
61         }
62
63         public String getText() {
64                 return text;
65         }
66
67         public String[] getVariables() {
68                 return variables;
69         }
70
71         public void setMessageId(String messageId) {
72                 this.messageId = messageId;
73         }
74
75         public void setText(String text) {
76                 this.text = text;
77         }
78
79         public void setVariables(String[] variables) {
80                 this.variables = variables;
81         }
82
83         public String getFormattedErrorMessage() {
84                 String res;
85                 if (variables != null && variables.length > 0) {
86                         Formatter formatter = new Formatter();
87                         try {
88                                 res = formatter.format(this.text.replaceAll("%\\d", "%s"), (Object[]) this.variables).toString();
89                         } finally {
90                                 formatter.close();
91                         }
92                 } else {
93                         res = this.text;
94                 }
95                 return res;
96         }
97 }