Catalog alignment
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / exception / AbstractSdncException.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.exception;
22
23 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
25 import org.openecomp.sdc.common.util.GeneralUtility;
26
27 import java.util.Arrays;
28 import java.util.Formatter;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 public abstract class AbstractSdncException {
33
34         private String messageId;
35
36         private String text;
37
38         private final String ecompRequestId;
39
40         private String[] variables;
41
42         private static Logger log = Logger.getLogger(AbstractSdncException.class.getName());
43
44         private final static Pattern ERROR_PARAM_PATTERN = Pattern.compile("%\\d");
45
46         public AbstractSdncException() {
47                 this.ecompRequestId = GeneralUtility.getEcompRequestId();
48         }
49
50         public AbstractSdncException(String messageId, String text, String[] variables) {
51                 this();
52                 this.messageId = messageId;
53                 this.text = text;
54                 this.variables = validateParameters(messageId, text, variables);
55         }
56
57         private String[] validateParameters(String messageId, String text, String[] variables) {
58                 String[] res = null;
59                 Matcher m = ERROR_PARAM_PATTERN.matcher(text);
60                 int expectedParamsNum = 0;
61                 while (m.find()) {
62                         expectedParamsNum += 1;
63                 }
64                 int actualParamsNum = (variables != null) ? variables.length : 0;
65                 if (actualParamsNum < expectedParamsNum) {
66                         log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","",
67                                         "Received less parameters than expected for error with messageId {}, expected: {}, actual: {}. Missing parameters are padded with null values.",
68                                         messageId, expectedParamsNum, actualParamsNum);
69                 } else if (actualParamsNum > expectedParamsNum) {
70                         log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR,"","","",
71                                         "Received more parameters than expected for error with messageId {}, expected: {}, actual: {}. Extra parameters are ignored.",
72                                         messageId, expectedParamsNum, actualParamsNum);
73                 }
74                 if (variables != null) {
75                         res = Arrays.copyOf(variables, expectedParamsNum);
76                 }
77
78                 return res;
79         }
80
81         public String getMessageId() {
82                 return messageId;
83         }
84
85         public String getText() {
86                 return text;
87         }
88
89         public String getEcompRequestId() {
90                 return ecompRequestId;
91         }
92
93         public String[] getVariables() {
94                 return variables;
95         }
96
97         public void setMessageId(String messageId) {
98                 this.messageId = messageId;
99         }
100
101         public void setText(String text) {
102                 this.text = text;
103         }
104
105         public void setVariables(String[] variables) {
106                 this.variables = variables;
107         }
108
109         public String getFormattedErrorMessage() {
110                 String res;
111                 if (variables != null && variables.length > 0) {
112                         Formatter formatter = new Formatter();
113                         try {
114                                 res = formatter.format(this.text.replaceAll("%\\d", "%s"), (Object[]) this.variables).toString();
115                         } finally {
116                                 formatter.close();
117                         }
118                 } else {
119                         res = this.text;
120                 }
121                 return res;
122         }
123
124 }