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;
24 import org.openecomp.sdc.common.log.enums.EcompErrorSeverity;
26 import java.util.Arrays;
27 import java.util.HashMap;
29 import java.util.regex.Pattern;
31 public class EcompErrorConfiguration extends BasicConfiguration {
33 private Map<String, EcompErrorInfo> errors = new HashMap<>();
34 static final String ECODE_PREFIX = "ASDC_";
35 private static final Pattern ECODE_PATTERN = Pattern.compile("^" + ECODE_PREFIX + "\\d{4}$");
37 public Map<String, EcompErrorInfo> getErrors() {
41 public void setErrors(Map<String, EcompErrorInfo> errors) {
42 // Validating ecomp-error-configuration.yaml
43 for (Map.Entry<String, EcompErrorInfo> ecompErrorInfo : errors.entrySet()) {
44 String ecompErrorName = ecompErrorInfo.getKey();
45 EcompErrorInfo res = validateEcompErrorInfo(ecompErrorName, ecompErrorInfo.getValue());
48 EcompErrorLogUtil.logEcompError(EcompErrorName.EcompConfigFileFormat, res, this.getClass().getName());
56 public EcompErrorInfo getEcompErrorInfo(String key) {
57 EcompErrorInfo clone = null;
58 EcompErrorInfo other = errors.get(key);
60 clone = new EcompErrorInfo();
61 clone.cloneData(other);
66 protected EcompErrorInfo validateEcompErrorInfo(String ecompErrorName, EcompErrorInfo ecompErrorInfoToValidate) {
67 if (ecompErrorInfoToValidate == null) {
68 return getErrorInfoForConfigFile("error " + ecompErrorName + " not found ");
70 String type = ecompErrorInfoToValidate.getType();
72 return getErrorInfoForConfigFile("empty error type for error " + ecompErrorName
73 + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
76 EcompErrorType.valueOf(type);
77 } catch (IllegalArgumentException e) {
78 return getErrorInfoForConfigFile("error type " + type + " is invalid for error " + ecompErrorName
79 + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
82 String severity = ecompErrorInfoToValidate.getSeverity();
83 if (severity == null) {
84 return getErrorInfoForConfigFile("empty error severity for error " + ecompErrorName
85 + ", value should be one of the following: " + Arrays.asList(EcompErrorSeverity.values()));
88 EcompErrorSeverity.valueOf(severity);
89 } catch (IllegalArgumentException e) {
90 return getErrorInfoForConfigFile("error severity " + severity + " is invalid for error " + ecompErrorName
91 + ", value should be one of the following: " + Arrays.asList(EcompErrorSeverity.values()));
93 String alarmSeverity = ecompErrorInfoToValidate.getAlarmSeverity();
94 if (alarmSeverity == null) {
95 return getErrorInfoForConfigFile("empty error alarm for error " + ecompErrorName
96 + ", , value should be one of the following: " + Arrays.asList(EcompAlarmSeverity.values()));
99 EcompAlarmSeverity.valueOf(alarmSeverity);
100 } catch (IllegalArgumentException e) {
101 return getErrorInfoForConfigFile("error alarm severity " + alarmSeverity + " is invalid for error "
102 + ecompErrorName + ", , value should be one of the following: "
103 + Arrays.asList(EcompAlarmSeverity.values()));
106 String code = ecompErrorInfoToValidate.getCode();
107 if (code != null && ECODE_PATTERN.matcher(code).matches()) {
108 String[] split = code.split("_");
109 int parseInt = Integer.parseInt(split[1]);
110 if (parseInt < 3010 || parseInt > 9999) {
111 return getErrorInfoForInvalidCode(code, ecompErrorName);
114 return getErrorInfoForInvalidCode(code, ecompErrorName);
119 private EcompErrorInfo getErrorInfoForInvalidCode(String code, String ecompErrorName) {
120 return getErrorInfoForConfigFile("error code " + code + " is invalid for error " + ecompErrorName
121 + ", should be in format ASDC_[3010-9999]");
124 private EcompErrorInfo getErrorInfoForConfigFile(String errorMessage) {
125 EcompErrorInfo ecompErrorInfo = new EcompErrorInfo();
126 ecompErrorInfo.setCode(ECODE_PREFIX + "3000");
127 ecompErrorInfo.setType(EcompErrorType.CONFIG_ERROR.name());
128 ecompErrorInfo.setSeverity(EcompErrorSeverity.FATAL.name());
129 ecompErrorInfo.setAlarmSeverity(EcompAlarmSeverity.CRITICAL.name());
130 ecompErrorInfo.setDescription(errorMessage);
131 return ecompErrorInfo;
135 public String toString() {
136 return "EcompErrorConfiguration [errors=" + errors + "]";
139 /*******************************
143 public enum EcompErrorType {
144 RECOVERY, CONFIG_ERROR, SYSTEM_ERROR, DATA_ERROR, CONNECTION_PROBLEM, AUTHENTICATION_PROBLEM;
147 public enum EcompAlarmSeverity {
148 CRITICAL, MAJOR, MINOR, INFORMATIONAL, NONE;
151 public enum EcompErrorSeverity {
152 INFO, WARN, ERROR, FATAL;
155 public enum EcompErrorSeverityPrefix {
159 public static void main(String[] args) {
160 System.out.println(Arrays.asList(EcompErrorType.values()));