fixing warnings from checkstyle in common-app-api
[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 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}$");
37
38     public Map<String, EcompErrorInfo> getErrors() {
39         return errors;
40     }
41
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());
47             if (res != null) {
48                 // Validation failed!
49                 EcompErrorLogUtil.logEcompError(EcompErrorName.EcompConfigFileFormat, res, this.getClass().getName());
50                 return;
51             }
52         }
53         // Validation passed
54         this.errors = errors;
55     }
56
57     public EcompErrorInfo getEcompErrorInfo(String key) {
58         EcompErrorInfo clone = null;
59         EcompErrorInfo other = errors.get(key);
60         if (other != null) {
61             clone = new EcompErrorInfo();
62             clone.cloneData(other);
63         }
64         return clone;
65     }
66
67     protected EcompErrorInfo validateEcompErrorInfo(String ecompErrorName, EcompErrorInfo ecompErrorInfoToValidate) {
68         if (ecompErrorInfoToValidate == null) {
69             return getErrorInfoForConfigFile("error " + ecompErrorName + " not found ");
70         }
71         String type = ecompErrorInfoToValidate.getType();
72         if (type == null) {
73             return getErrorInfoForConfigFile("empty error type for error " + ecompErrorName
74                     + ", value should be one of the following: " + Arrays.asList(EcompErrorType.values()));
75         }
76         try {
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()));
81         }
82
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()));
87         }
88         try {
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()));
93         }
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()));
98         }
99         try {
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()));
105         }
106
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);
113             }
114         } else {
115             return getErrorInfoForInvalidCode(code, ecompErrorName);
116         }
117         return null;
118     }
119
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]");
123     }
124
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;
133     }
134
135     @Override
136     public String toString() {
137         return "EcompErrorConfiguration [errors=" + errors + "]";
138     }
139
140     /*******************************
141      * enums
142      */
143
144     public enum EcompErrorType {
145         RECOVERY, CONFIG_ERROR, SYSTEM_ERROR, DATA_ERROR, CONNECTION_PROBLEM, AUTHENTICATION_PROBLEM;
146     }
147
148     public enum EcompAlarmSeverity {
149         CRITICAL, MAJOR, MINOR, INFORMATIONAL, NONE;
150     }
151
152     public enum EcompErrorSeverity {
153         INFO, WARN, ERROR, FATAL;
154     }
155
156     public enum EcompErrorSeverityPrefix {
157         I, W, E, F;
158     }
159
160     public static void main(String[] args) {
161         System.out.println(Arrays.asList(EcompErrorType.values()));
162     }
163 }