Catalog alignment
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / config / EcompErrorConfiguration.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 org.openecomp.sdc.common.api.BasicConfiguration;
24 import org.openecomp.sdc.common.log.enums.EcompErrorSeverity;
25
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.regex.Pattern;
30
31 public class EcompErrorConfiguration extends BasicConfiguration {
32
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}$");
36
37         public Map<String, EcompErrorInfo> getErrors() {
38                 return errors;
39         }
40
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());
46                         if (res != null) {
47                                 // Validation failed!
48                                 EcompErrorLogUtil.logEcompError(EcompErrorName.EcompConfigFileFormat, res, this.getClass().getName());
49                                 return;
50                         }
51                 }
52                 // Validation passed
53                 this.errors = errors;
54         }
55
56         public EcompErrorInfo getEcompErrorInfo(String key) {
57                 EcompErrorInfo clone = null;
58                 EcompErrorInfo other = errors.get(key);
59                 if (other != null) {
60                         clone = new EcompErrorInfo();
61                         clone.cloneData(other);
62                 }
63                 return clone;
64         }
65
66         protected EcompErrorInfo validateEcompErrorInfo(String ecompErrorName, EcompErrorInfo ecompErrorInfoToValidate) {
67                 if (ecompErrorInfoToValidate == null) {
68                         return getErrorInfoForConfigFile("error " + ecompErrorName + " not found ");
69                 }
70                 String type = ecompErrorInfoToValidate.getType();
71                 if (type == null) {
72                         return getErrorInfoForConfigFile("empty error type for error " + ecompErrorName
73                                         + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
74                 }
75                 try {
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()));
80                 }
81
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()));
86                 }
87                 try {
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()));
92                 }
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()));
97                 }
98                 try {
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()));
104                 }
105
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);
112                         }
113                 } else {
114                         return getErrorInfoForInvalidCode(code, ecompErrorName);
115                 }
116                 return null;
117         }
118
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]");
122         }
123
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;
132         }
133
134         @Override
135         public String toString() {
136                 return "EcompErrorConfiguration [errors=" + errors + "]";
137         }
138
139         /*******************************
140          * enums
141          */
142
143         public enum EcompErrorType {
144                 RECOVERY, CONFIG_ERROR, SYSTEM_ERROR, DATA_ERROR, CONNECTION_PROBLEM, AUTHENTICATION_PROBLEM;
145         }
146
147         public enum EcompAlarmSeverity {
148                 CRITICAL, MAJOR, MINOR, INFORMATIONAL, NONE;
149         }
150
151         public enum EcompErrorSeverity {
152                 INFO, WARN, ERROR, FATAL;
153         }
154
155         public enum EcompErrorSeverityPrefix {
156                 I, W, E, F;
157         }
158
159         public static void main(String[] args) {
160                 System.out.println(Arrays.asList(EcompErrorType.values()));
161         }
162 }