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