2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.config;
23 import java.util.Formatter;
24 import java.util.IllegalFormatException;
25 import java.util.Locale;
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;
32 import com.jcabi.aspects.Loggable;
34 @Loggable(prepend = true, value = Loggable.TRACE, trim = false)
35 public abstract class AbsEcompErrorManager implements IEcompErrorManager {
37 public static final String PARAM_STR = "%s";
39 public abstract IEcompConfigurationManager getConfigurationManager();
43 public void processEcompError(EcompErrorName ecompErrorName, String ecompErrorContext,
44 String... descriptionParams) {
47 * //Getting the relevant config manager IEcompConfigurationManager
48 * configurationManager = getConfigurationManager();
50 * //Getting the error by name EcompErrorInfo ecompErrorInfo =
51 * configurationManager.getEcompErrorConfiguration().getEcompErrorInfo(
52 * ecompErrorName.name());
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); }
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
73 if (countMatches != descriptionParams.length) {
74 return getErrorInfoForDescriptionParamsMismatch(ecompErrorName);
76 // Setting params of the description if any
77 StringBuilder sb = new StringBuilder();
78 Formatter formatter = new Formatter(sb, Locale.US);
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
85 return getErrorInfoForDescriptionParamsMismatch(ecompErrorName);
89 return ecompErrorInfo;
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;
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;
115 public void processEcompError(String context, EcompErrorEnum ecompErrorEnum, String... descriptionParams) {
117 EcompErrorLogUtil.logEcompError(context, ecompErrorEnum, descriptionParams);