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 org.openecomp.sdc.common.api.BasicConfiguration;
25 import java.util.Arrays;
26 import java.util.HashMap;
28 import java.util.regex.Pattern;
30 public class EcompErrorConfiguration extends BasicConfiguration {
32 private static final int LOWEST_ERROR_VALID_CODE = 3010;
33 private static final int HIGHEST_ERROR_VALID_CODE = 9999;
34 private Map<String, EcompErrorInfo> errors = new HashMap<>();
35 static final String ECODE_PREFIX = "ASDC_";
36 private static final Pattern ECODE_PATTERN = Pattern.compile("^" + ECODE_PREFIX + "\\d{4}$");
38 public Map<String, EcompErrorInfo> getErrors() {
42 public void setErrors(Map<String, EcompErrorInfo> errors) {
43 // Validating ecomp-error-configuration.yaml
44 for (Map.Entry<String, EcompErrorInfo> ecompErrorInfo : errors.entrySet()) {
45 String ecompErrorName = ecompErrorInfo.getKey();
46 EcompErrorInfo res = validateEcompErrorInfo(ecompErrorName, ecompErrorInfo.getValue());
49 EcompErrorLogUtil.logEcompError(EcompErrorName.EcompConfigFileFormat, res, this.getClass().getName());
57 public EcompErrorInfo getEcompErrorInfo(String key) {
58 EcompErrorInfo clone = null;
59 EcompErrorInfo other = errors.get(key);
61 clone = new EcompErrorInfo();
62 clone.cloneData(other);
67 protected EcompErrorInfo validateEcompErrorInfo(String ecompErrorName, EcompErrorInfo ecompErrorInfoToValidate) {
68 if (ecompErrorInfoToValidate == null) {
69 return getErrorInfoForConfigFile("error " + ecompErrorName + " not found ");
71 String type = ecompErrorInfoToValidate.getType();
73 return getErrorInfoForConfigFile("empty error type for error " + ecompErrorName
74 + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
77 EcompErrorType.valueOf(type);
78 } catch (IllegalArgumentException e) {
79 return getErrorInfoForConfigFile("error type " + type + " is invalid for error " + ecompErrorName
80 + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
83 String severity = ecompErrorInfoToValidate.getSeverity();
84 if (severity == null) {
85 return getErrorInfoForConfigFile("empty error severity for error " + ecompErrorName
86 + ", value should be one of the following: " + Arrays.asList(EcompErrorSeverity.values()));
89 EcompErrorSeverity.valueOf(severity);
90 } catch (IllegalArgumentException e) {
91 return getErrorInfoForConfigFile("error severity " + severity + " is invalid for error " + ecompErrorName
92 + ", value should be one of the following: " + Arrays.asList(EcompErrorSeverity.values()));
94 String alarmSeverity = ecompErrorInfoToValidate.getAlarmSeverity();
95 if (alarmSeverity == null) {
96 return getErrorInfoForConfigFile("empty error alarm for error " + ecompErrorName
97 + ", , value should be one of the following: " + Arrays.asList(EcompAlarmSeverity.values()));
100 EcompAlarmSeverity.valueOf(alarmSeverity);
101 } catch (IllegalArgumentException e) {
102 return getErrorInfoForConfigFile("error alarm severity " + alarmSeverity + " is invalid for error "
103 + ecompErrorName + ", , value should be one of the following: "
104 + Arrays.asList(EcompAlarmSeverity.values()));
107 String code = ecompErrorInfoToValidate.getCode();
108 if (code != null && ECODE_PATTERN.matcher(code).matches()) {
109 String[] split = code.split("_");
110 int parseInt = Integer.parseInt(split[1]);
111 if (parseInt < LOWEST_ERROR_VALID_CODE || parseInt > HIGHEST_ERROR_VALID_CODE) {
112 return getErrorInfoForInvalidCode(code, ecompErrorName);
115 return getErrorInfoForInvalidCode(code, ecompErrorName);
120 private EcompErrorInfo getErrorInfoForInvalidCode(String code, String ecompErrorName) {
121 return getErrorInfoForConfigFile("error code " + code + " is invalid for error " + ecompErrorName
122 + ", should be in format ASDC_[3010-9999]");
125 private EcompErrorInfo getErrorInfoForConfigFile(String errorMessage) {
126 EcompErrorInfo ecompErrorInfo = new EcompErrorInfo();
127 ecompErrorInfo.setCode(ECODE_PREFIX + "3000");
128 ecompErrorInfo.setType(EcompErrorType.CONFIG_ERROR.name());
129 ecompErrorInfo.setSeverity(EcompErrorSeverity.FATAL.name());
130 ecompErrorInfo.setAlarmSeverity(EcompAlarmSeverity.CRITICAL.name());
131 ecompErrorInfo.setDescription(errorMessage);
132 return ecompErrorInfo;
136 public String toString() {
137 return "EcompErrorConfiguration [errors=" + errors + "]";
140 /*******************************
144 public enum EcompErrorType {
145 RECOVERY, CONFIG_ERROR, SYSTEM_ERROR, DATA_ERROR, CONNECTION_PROBLEM, AUTHENTICATION_PROBLEM;
148 public enum EcompAlarmSeverity {
149 CRITICAL, MAJOR, MINOR, INFORMATIONAL, NONE;
152 public enum EcompErrorSeverity {
153 INFO, WARN, ERROR, FATAL;
156 public enum EcompErrorSeverityPrefix {
160 public static void main(String[] args) {
161 System.out.println(Arrays.asList(EcompErrorType.values()));