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 Map<String, EcompErrorInfo> errors = new HashMap<>();
33 static final String ECODE_PREFIX = "ASDC_";
34 private static final Pattern ECODE_PATTERN = Pattern.compile("^" + ECODE_PREFIX + "\\d{4}$");
36 public Map<String, EcompErrorInfo> getErrors() {
40 public void setErrors(Map<String, EcompErrorInfo> errors) {
41 // Validating ecomp-error-configuration.yaml
42 for (Map.Entry<String, EcompErrorInfo> ecompErrorInfo : errors.entrySet()) {
43 String ecompErrorName = ecompErrorInfo.getKey();
44 EcompErrorInfo res = validateEcompErrorInfo(ecompErrorName, ecompErrorInfo.getValue());
47 EcompErrorLogUtil.logEcompError(EcompErrorName.EcompConfigFileFormat, res, this.getClass().getName());
55 public EcompErrorInfo getEcompErrorInfo(String key) {
56 EcompErrorInfo clone = null;
57 EcompErrorInfo other = errors.get(key);
59 clone = new EcompErrorInfo();
60 clone.cloneData(other);
65 protected EcompErrorInfo validateEcompErrorInfo(String ecompErrorName, EcompErrorInfo ecompErrorInfoToValidate) {
66 if (ecompErrorInfoToValidate == null) {
67 return getErrorInfoForConfigFile("error " + ecompErrorName + " not found ");
69 String type = ecompErrorInfoToValidate.getType();
71 return getErrorInfoForConfigFile("empty error type for error " + ecompErrorName
72 + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
75 EcompErrorType.valueOf(type);
76 } catch (IllegalArgumentException e) {
77 return getErrorInfoForConfigFile("error type " + type + " is invalid for error " + ecompErrorName
78 + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
81 String severity = ecompErrorInfoToValidate.getSeverity();
82 if (severity == null) {
83 return getErrorInfoForConfigFile("empty error severity for error " + ecompErrorName
84 + ", value should be one of the following: " + Arrays.asList(EcompErrorSeverity.values()));
87 EcompErrorSeverity.valueOf(severity);
88 } catch (IllegalArgumentException e) {
89 return getErrorInfoForConfigFile("error severity " + severity + " is invalid for error " + ecompErrorName
90 + ", value should be one of the following: " + Arrays.asList(EcompErrorSeverity.values()));
92 String alarmSeverity = ecompErrorInfoToValidate.getAlarmSeverity();
93 if (alarmSeverity == null) {
94 return getErrorInfoForConfigFile("empty error alarm for error " + ecompErrorName
95 + ", , value should be one of the following: " + Arrays.asList(EcompAlarmSeverity.values()));
98 EcompAlarmSeverity.valueOf(alarmSeverity);
99 } catch (IllegalArgumentException e) {
100 return getErrorInfoForConfigFile("error alarm severity " + alarmSeverity + " is invalid for error "
101 + ecompErrorName + ", , value should be one of the following: "
102 + Arrays.asList(EcompAlarmSeverity.values()));
105 String code = ecompErrorInfoToValidate.getCode();
106 if (code != null && ECODE_PATTERN.matcher(code).matches()) {
107 String[] split = code.split("_");
108 int parseInt = Integer.parseInt(split[1]);
109 if (parseInt < 3010 || parseInt > 9999) {
110 return getErrorInfoForInvalidCode(code, ecompErrorName);
113 return getErrorInfoForInvalidCode(code, ecompErrorName);
118 private EcompErrorInfo getErrorInfoForInvalidCode(String code, String ecompErrorName) {
119 return getErrorInfoForConfigFile("error code " + code + " is invalid for error " + ecompErrorName
120 + ", should be in format ASDC_[3010-9999]");
123 private EcompErrorInfo getErrorInfoForConfigFile(String errorMessage) {
124 EcompErrorInfo ecompErrorInfo = new EcompErrorInfo();
125 ecompErrorInfo.setCode(ECODE_PREFIX + "3000");
126 ecompErrorInfo.setType(EcompErrorType.CONFIG_ERROR.name());
127 ecompErrorInfo.setSeverity(EcompErrorSeverity.FATAL.name());
128 ecompErrorInfo.setAlarmSeverity(EcompAlarmSeverity.CRITICAL.name());
129 ecompErrorInfo.setDescription(errorMessage);
130 return ecompErrorInfo;
134 public String toString() {
135 return "EcompErrorConfiguration [errors=" + errors + "]";
138 /*******************************
142 public enum EcompErrorType {
143 RECOVERY, CONFIG_ERROR, SYSTEM_ERROR, DATA_ERROR, CONNECTION_PROBLEM, AUTHENTICATION_PROBLEM;
146 public enum EcompAlarmSeverity {
147 CRITICAL, MAJOR, MINOR, INFORMATIONAL, NONE;
150 public enum EcompErrorSeverity {
151 INFO, WARN, ERROR, FATAL;
154 public enum EcompErrorSeverityPrefix {
158 public static void main(String[] args) {
159 System.out.println(Arrays.asList(EcompErrorType.values()));