2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.config.generation;
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;
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;
39 public class GenerateEcompErrorsCsv {
41 private static Logger log = LoggerFactory.getLogger(GenerateEcompErrorsCsv.class);
43 private static String DATE_FORMAT = "dd-M-yyyy-hh-mm-ss";
45 private static String NEW_LINE = System.getProperty("line.separator");
47 private static void usage() {
48 System.out.println("java org.openecomp.sdc.common.config.generation.GenerateEcompErrorsCsv <target folder>");
52 public static void main(String[] args) {
54 String targetFolder = "target";
55 if (args.length > 1) {
56 targetFolder = args[0];
59 GenerateEcompErrorsCsv ecompErrorsCsv = new GenerateEcompErrorsCsv();
61 ecompErrorsCsv.generateEcompErrorsCsvFile(targetFolder, true);
64 public static class EcompErrorRow {
70 AlarmSeverity alarmSeverity;
71 String cleanErrorCode;
74 public String getErrorName() {
78 public void setErrorName(String errorName) {
79 this.errorName = errorName;
82 public String getErrorCode() {
86 public void setErrorCode(String errorCode) {
87 this.errorCode = errorCode;
90 public String getDescription() {
94 public void setDescription(String description) {
95 this.description = description;
98 public ErrorType getErrorType() {
102 public void setErrorType(ErrorType errorType) {
103 this.errorType = errorType;
106 public AlarmSeverity getAlarmSeverity() {
107 return alarmSeverity;
110 public void setAlarmSeverity(AlarmSeverity alarmSeverity) {
111 this.alarmSeverity = alarmSeverity;
114 public String getCleanErrorCode() {
115 return cleanErrorCode;
118 public void setCleanErrorCode(String cleanErrorCode) {
119 this.cleanErrorCode = cleanErrorCode;
122 public String getResolution() {
126 public void setResolution(String resolution) {
127 this.resolution = resolution;
132 public boolean generateEcompErrorsCsvFile(String targetFolder, boolean addTimeToFileName) {
134 targetFolder += File.separator;
136 boolean result = false;
137 String dateFormatted = "";
139 if (addTimeToFileName) {
140 DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
142 Date date = new Date();
144 dateFormatted = "." + dateFormat.format(date);
148 String outputFile = targetFolder + "ecompErrorCodes" + dateFormatted + ".csv";
150 FileWriter writer = null;
153 writer = new FileWriter(outputFile);
155 List<EcompErrorRow> errors = new ArrayList<>();
157 for (EcompErrorEnum ecompErrorEnum : EcompErrorEnum.values()) {
159 EcompErrorRow ecompErrorRow = new EcompErrorRow();
161 String errorCode = EcompErrorLogUtil.createEcode(ecompErrorEnum);
163 EcompErrorEnum clearCodeEnum = ecompErrorEnum.getClearCode();
164 String cleanErrorCode = null;
165 if (clearCodeEnum != null) {
166 cleanErrorCode = EcompErrorLogUtil.createEcode(clearCodeEnum);
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());
177 errors.add(ecompErrorRow);
180 writeHeaders(writer);
182 for (EcompErrorRow ecompErrorRow : errors) {
183 writer.append(addInvertedCommas(ecompErrorRow.getErrorCode()));
185 writer.append(addInvertedCommas(ecompErrorRow.getErrorType().toString()));
187 writer.append(addInvertedCommas(ecompErrorRow.getDescription()));
189 writer.append(addInvertedCommas(ecompErrorRow.getResolution()));
191 writer.append(addInvertedCommas(ecompErrorRow.getAlarmSeverity().toString()));
193 writer.append(addInvertedCommas(ecompErrorRow.getErrorName()));
195 writer.append(addInvertedCommas(ecompErrorRow.getCleanErrorCode()));
196 writer.append(NEW_LINE);
201 } catch (Exception e) {
202 log.info("generate Ecomp Errors Csv File failed - {}" , e);
205 if (writer != null) {
209 } catch (IOException e) {
210 log.info("close FileOutputStream failed - {}" , e);
219 private void writeHeaders(FileWriter writer) throws IOException {
221 writer.append("\"ERROR CODE\"");
223 writer.append("\"ERROR TYPE\"");
225 writer.append("\"DESCRIPTION\"");
227 writer.append("\"RESOLUTION\"");
229 writer.append("\"ALARM SEVERITY\"");
231 writer.append("\"ERROR NAME\"");
233 writer.append("\"CLEAN CODE\"");
234 writer.append(NEW_LINE);
237 private String addInvertedCommas(String str) {
243 return "\"" + str + "\"";