Initial OpenECOMP SDC commit
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / config / AbsEcompErrorManager.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 java.util.Formatter;
24 import java.util.IllegalFormatException;
25 import java.util.Locale;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.sdc.common.config.EcompErrorConfiguration.EcompAlarmSeverity;
29 import org.openecomp.sdc.common.config.EcompErrorConfiguration.EcompErrorSeverity;
30 import org.openecomp.sdc.common.config.EcompErrorConfiguration.EcompErrorType;
31
32 import com.jcabi.aspects.Loggable;
33
34 @Loggable(prepend = true, value = Loggable.TRACE, trim = false)
35 public abstract class AbsEcompErrorManager implements IEcompErrorManager {
36
37         public static final String PARAM_STR = "%s";
38
39         public abstract IEcompConfigurationManager getConfigurationManager();
40
41         @Deprecated
42         @Override
43         public void processEcompError(EcompErrorName ecompErrorName, String ecompErrorContext,
44                         String... descriptionParams) {
45
46                 /*
47                  * //Getting the relevant config manager IEcompConfigurationManager
48                  * configurationManager = getConfigurationManager();
49                  * 
50                  * //Getting the error by name EcompErrorInfo ecompErrorInfo =
51                  * configurationManager.getEcompErrorConfiguration().getEcompErrorInfo(
52                  * ecompErrorName.name());
53                  * 
54                  * if (ecompErrorInfo != null){ ecompErrorInfo =
55                  * setDescriptionParams(ecompErrorInfo, ecompErrorName.name(),
56                  * descriptionParams); EcompErrorLogUtil.logEcompError(ecompErrorName,
57                  * ecompErrorInfo, ecompErrorContext); } else {
58                  * EcompErrorLogUtil.logEcompError(EcompErrorName.EcompErrorNotFound,
59                  * getErrorInfoForUnknownErrorName(ecompErrorName.name()),
60                  * ecompErrorContext); }
61                  */
62
63         }
64
65         private EcompErrorInfo setDescriptionParams(EcompErrorInfo ecompErrorInfo, String ecompErrorName,
66                         String... descriptionParams) {
67                 String description = ecompErrorInfo.getDescription();
68                 // Counting number of params in description
69                 int countMatches = StringUtils.countMatches(description, PARAM_STR);
70                 // Catching cases when there are more params passed than there are in
71                 // the description (formatter will ignore extra params and won't throw
72                 // exception)
73                 if (countMatches != descriptionParams.length) {
74                         return getErrorInfoForDescriptionParamsMismatch(ecompErrorName);
75                 }
76                 // Setting params of the description if any
77                 StringBuilder sb = new StringBuilder();
78                 Formatter formatter = new Formatter(sb, Locale.US);
79                 try {
80                         formatter.format(description, (Object[]) descriptionParams).toString();
81                         ecompErrorInfo.setDescription(formatter.toString());
82                 } catch (IllegalFormatException e) {
83                         // Number of passed params doesn't match number of params in config
84                         // file
85                         return getErrorInfoForDescriptionParamsMismatch(ecompErrorName);
86                 } finally {
87                         formatter.close();
88                 }
89                 return ecompErrorInfo;
90         }
91
92         private EcompErrorInfo getErrorInfoForUnknownErrorName(String ecompErrorName) {
93                 EcompErrorInfo ecompErrorInfo = new EcompErrorInfo();
94                 ecompErrorInfo.setCode(EcompErrorConfiguration.ECODE_PREFIX + "3001");
95                 ecompErrorInfo.setType(EcompErrorType.CONFIG_ERROR.name());
96                 ecompErrorInfo.setSeverity(EcompErrorSeverity.ERROR.name());
97                 ecompErrorInfo.setAlarmSeverity(EcompAlarmSeverity.MAJOR.name());
98                 ecompErrorInfo.setDescription(new StringBuilder().append("Ecomp error element  not found in YAML, name: ")
99                                 .append(ecompErrorName).toString());
100                 return ecompErrorInfo;
101         }
102
103         private EcompErrorInfo getErrorInfoForDescriptionParamsMismatch(String ecompErrorName) {
104                 EcompErrorInfo ecompErrorInfo = new EcompErrorInfo();
105                 ecompErrorInfo.setCode(EcompErrorConfiguration.ECODE_PREFIX + "3002");
106                 ecompErrorInfo.setType(EcompErrorType.CONFIG_ERROR.name());
107                 ecompErrorInfo.setSeverity(EcompErrorSeverity.ERROR.name());
108                 ecompErrorInfo.setAlarmSeverity(EcompAlarmSeverity.MAJOR.name());
109                 ecompErrorInfo.setDescription(new StringBuilder()
110                                 .append("Ecomp error description params mismatch between code and YAML or wrong format, name: ")
111                                 .append(ecompErrorName).toString());
112                 return ecompErrorInfo;
113         }
114
115         public void processEcompError(String context, EcompErrorEnum ecompErrorEnum, String... descriptionParams) {
116
117                 EcompErrorLogUtil.logEcompError(context, ecompErrorEnum, descriptionParams);
118
119         }
120
121 }