fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / config / generation / GenerateEcompErrorsCsv.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.generation;
22
23 import org.openecomp.sdc.common.config.EcompErrorEnum;
24 import org.openecomp.sdc.common.config.EcompErrorEnum.AlarmSeverity;
25 import org.openecomp.sdc.common.config.EcompErrorEnum.ErrorType;
26 import org.openecomp.sdc.common.config.EcompErrorLogUtil;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.text.DateFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.List;
38
39 public class GenerateEcompErrorsCsv {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(GenerateEcompErrorsCsv.class);
42
43     private static final String DATE_FORMAT = "dd-M-yyyy-hh-mm-ss";
44
45     private static final String NEW_LINE = System.getProperty("line.separator");
46
47     private static void usage() {
48         System.out.println("java org.openecomp.sdc.common.config.generation.GenerateEcompErrorsCsv <target folder>");
49         System.exit(1);
50     }
51
52     public static void main(String[] args) {
53
54         String targetFolder = "target";
55         if (args.length > 1) {
56             targetFolder = args[0];
57         }
58
59         GenerateEcompErrorsCsv ecompErrorsCsv = new GenerateEcompErrorsCsv();
60
61         ecompErrorsCsv.generateEcompErrorsCsvFile(targetFolder, true);
62     }
63
64     public static class EcompErrorRow {
65
66         private String errorName;
67         private String errorCode;
68         private String description;
69         private ErrorType errorType;
70         private AlarmSeverity alarmSeverity;
71         private String cleanErrorCode;
72         private String resolution;
73
74         public String getErrorName() {
75             return errorName;
76         }
77
78         public void setErrorName(String errorName) {
79             this.errorName = errorName;
80         }
81
82         public String getErrorCode() {
83             return errorCode;
84         }
85
86         public void setErrorCode(String errorCode) {
87             this.errorCode = errorCode;
88         }
89
90         public String getDescription() {
91             return description;
92         }
93
94         public void setDescription(String description) {
95             this.description = description;
96         }
97
98         public ErrorType getErrorType() {
99             return errorType;
100         }
101
102         public void setErrorType(ErrorType errorType) {
103             this.errorType = errorType;
104         }
105
106         public AlarmSeverity getAlarmSeverity() {
107             return alarmSeverity;
108         }
109
110         public void setAlarmSeverity(AlarmSeverity alarmSeverity) {
111             this.alarmSeverity = alarmSeverity;
112         }
113
114         public String getCleanErrorCode() {
115             return cleanErrorCode;
116         }
117
118         public void setCleanErrorCode(String cleanErrorCode) {
119             this.cleanErrorCode = cleanErrorCode;
120         }
121
122         public String getResolution() {
123             return resolution;
124         }
125
126         public void setResolution(String resolution) {
127             this.resolution = resolution;
128         }
129
130     }
131
132     public boolean generateEcompErrorsCsvFile(String targetFolder, boolean addTimeToFileName) {
133
134         targetFolder += File.separator;
135
136         boolean result = false;
137         String dateFormatted = "";
138
139         if (addTimeToFileName) {
140             DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
141
142             Date date = new Date();
143
144             dateFormatted = "." + dateFormat.format(date);
145
146         }
147
148         String outputFile = targetFolder + "ecompErrorCodes" + dateFormatted + ".csv";
149
150         try (FileWriter writer = new FileWriter(outputFile)) {
151
152             List<EcompErrorRow> errors = new ArrayList<>();
153
154             for (EcompErrorEnum ecompErrorEnum : EcompErrorEnum.values()) {
155
156                 EcompErrorRow ecompErrorRow = new EcompErrorRow();
157
158                 String errorCode = EcompErrorLogUtil.createEcode(ecompErrorEnum);
159
160                 EcompErrorEnum clearCodeEnum = ecompErrorEnum.getClearCode();
161                 String cleanErrorCode = null;
162                 if (clearCodeEnum != null) {
163                     cleanErrorCode = EcompErrorLogUtil.createEcode(clearCodeEnum);
164                 }
165
166                 ecompErrorRow.setAlarmSeverity(ecompErrorEnum.getAlarmSeverity());
167                 ecompErrorRow.setCleanErrorCode(cleanErrorCode);
168                 ecompErrorRow.setDescription(ecompErrorEnum.getEcompErrorCode().getDescription());
169                 ecompErrorRow.setErrorCode(errorCode);
170                 ecompErrorRow.setErrorName(ecompErrorEnum.name().toString());
171                 ecompErrorRow.setErrorType(ecompErrorEnum.geteType());
172                 ecompErrorRow.setResolution(ecompErrorEnum.getEcompErrorCode().getResolution());
173
174                 errors.add(ecompErrorRow);
175             }
176
177             writeHeaders(writer);
178
179             for (EcompErrorRow ecompErrorRow : errors) {
180                 writer.append(addInvertedCommas(ecompErrorRow.getErrorCode()));
181                 writer.append(',');
182                 writer.append(addInvertedCommas(ecompErrorRow.getErrorType().toString()));
183                 writer.append(',');
184                 writer.append(addInvertedCommas(ecompErrorRow.getDescription()));
185                 writer.append(',');
186                 writer.append(addInvertedCommas(ecompErrorRow.getResolution()));
187                 writer.append(',');
188                 writer.append(addInvertedCommas(ecompErrorRow.getAlarmSeverity().toString()));
189                 writer.append(',');
190                 writer.append(addInvertedCommas(ecompErrorRow.getErrorName()));
191                 writer.append(',');
192                 writer.append(addInvertedCommas(ecompErrorRow.getCleanErrorCode()));
193                 writer.append(NEW_LINE);
194             }
195
196             result = true;
197
198         } catch (Exception e) {
199             LOGGER.info("generate Ecomp Errors Csv File failed - {}", e);
200         }
201
202         return result;
203     }
204
205     private void writeHeaders(FileWriter writer) throws IOException {
206
207         writer.append("\"ERROR CODE\"");
208         writer.append(',');
209         writer.append("\"ERROR TYPE\"");
210         writer.append(',');
211         writer.append("\"DESCRIPTION\"");
212         writer.append(',');
213         writer.append("\"RESOLUTION\"");
214         writer.append(',');
215         writer.append("\"ALARM SEVERITY\"");
216         writer.append(',');
217         writer.append("\"ERROR NAME\"");
218         writer.append(',');
219         writer.append("\"CLEAN CODE\"");
220         writer.append(NEW_LINE);
221     }
222
223     private String addInvertedCommas(String str) {
224
225         if (str == null) {
226             return "\"\"";
227         }
228
229         return "\"" + str + "\"";
230     }
231
232 }