re base code
[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
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class GenerateEcompErrorsCsv {
40
41         private static Logger log = LoggerFactory.getLogger(GenerateEcompErrorsCsv.class);
42
43         private static String DATE_FORMAT = "dd-M-yyyy-hh-mm-ss";
44
45         private static 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                 String errorName;
67                 String errorCode;
68                 String description;
69                 ErrorType errorType;
70                 AlarmSeverity alarmSeverity;
71                 String cleanErrorCode;
72                 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                 FileWriter writer = null;
151
152                 try {
153                         writer = new FileWriter(outputFile);
154
155                         List<EcompErrorRow> errors = new ArrayList<>();
156
157                         for (EcompErrorEnum ecompErrorEnum : EcompErrorEnum.values()) {
158
159                                 EcompErrorRow ecompErrorRow = new EcompErrorRow();
160
161                                 String errorCode = EcompErrorLogUtil.createEcode(ecompErrorEnum);
162
163                                 EcompErrorEnum clearCodeEnum = ecompErrorEnum.getClearCode();
164                                 String cleanErrorCode = null;
165                                 if (clearCodeEnum != null) {
166                                         cleanErrorCode = EcompErrorLogUtil.createEcode(clearCodeEnum);
167                                 }
168
169                                 ecompErrorRow.setAlarmSeverity(ecompErrorEnum.getAlarmSeverity());
170                                 ecompErrorRow.setCleanErrorCode(cleanErrorCode);
171                                 ecompErrorRow.setDescription(ecompErrorEnum.getEcompErrorCode().getDescription());
172                                 ecompErrorRow.setErrorCode(errorCode);
173                                 ecompErrorRow.setErrorName(ecompErrorEnum.name().toString());
174                                 ecompErrorRow.setErrorType(ecompErrorEnum.geteType());
175                                 ecompErrorRow.setResolution(ecompErrorEnum.getEcompErrorCode().getResolution());
176
177                                 errors.add(ecompErrorRow);
178                         }
179
180                         writeHeaders(writer);
181
182                         for (EcompErrorRow ecompErrorRow : errors) {
183                                 writer.append(addInvertedCommas(ecompErrorRow.getErrorCode()));
184                                 writer.append(',');
185                                 writer.append(addInvertedCommas(ecompErrorRow.getErrorType().toString()));
186                                 writer.append(',');
187                                 writer.append(addInvertedCommas(ecompErrorRow.getDescription()));
188                                 writer.append(',');
189                                 writer.append(addInvertedCommas(ecompErrorRow.getResolution()));
190                                 writer.append(',');
191                                 writer.append(addInvertedCommas(ecompErrorRow.getAlarmSeverity().toString()));
192                                 writer.append(',');
193                                 writer.append(addInvertedCommas(ecompErrorRow.getErrorName()));
194                                 writer.append(',');
195                                 writer.append(addInvertedCommas(ecompErrorRow.getCleanErrorCode()));
196                                 writer.append(NEW_LINE);
197                         }
198
199                         result = true;
200
201                 } catch (Exception e) {
202                         log.info("generate Ecomp Errors Csv File failed - {}" , e);
203
204                 } finally {
205                         if (writer != null) {
206                                 try {
207                                         writer.flush();
208                                         writer.close();
209                                 } catch (IOException e) {
210                                         log.info("close FileOutputStream failed - {}" , e);
211                                 }
212
213                         }
214                 }
215
216                 return result;
217         }
218
219         private void writeHeaders(FileWriter writer) throws IOException {
220
221                 writer.append("\"ERROR CODE\"");
222                 writer.append(',');
223                 writer.append("\"ERROR TYPE\"");
224                 writer.append(',');
225                 writer.append("\"DESCRIPTION\"");
226                 writer.append(',');
227                 writer.append("\"RESOLUTION\"");
228                 writer.append(',');
229                 writer.append("\"ALARM SEVERITY\"");
230                 writer.append(',');
231                 writer.append("\"ERROR NAME\"");
232                 writer.append(',');
233                 writer.append("\"CLEAN CODE\"");
234                 writer.append(NEW_LINE);
235         }
236
237         private String addInvertedCommas(String str) {
238
239                 if (str == null) {
240                         return "\"\"";
241                 }
242
243                 return "\"" + str + "\"";
244         }
245
246 }