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