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);
42 private static String DATE_FORMAT = "dd-M-yyyy-hh-mm-ss";
44 private static String NEW_LINE = System.getProperty("line.separator");
46 private static void usage() {
47 System.out.println("java org.openecomp.sdc.common.config.generation.GenerateEcompErrorsCsv <target folder>");
51 public static void main(String[] args) {
53 String targetFolder = "target";
54 if (args.length > 1) {
55 targetFolder = args[0];
58 GenerateEcompErrorsCsv ecompErrorsCsv = new GenerateEcompErrorsCsv();
60 ecompErrorsCsv.generateEcompErrorsCsvFile(targetFolder, true);
63 public static class EcompErrorRow {
69 AlarmSeverity alarmSeverity;
70 String cleanErrorCode;
73 public String getErrorName() {
77 public void setErrorName(String errorName) {
78 this.errorName = errorName;
81 public String getErrorCode() {
85 public void setErrorCode(String errorCode) {
86 this.errorCode = errorCode;
89 public String getDescription() {
93 public void setDescription(String description) {
94 this.description = description;
97 public ErrorType getErrorType() {
101 public void setErrorType(ErrorType errorType) {
102 this.errorType = errorType;
105 public AlarmSeverity getAlarmSeverity() {
106 return alarmSeverity;
109 public void setAlarmSeverity(AlarmSeverity alarmSeverity) {
110 this.alarmSeverity = alarmSeverity;
113 public String getCleanErrorCode() {
114 return cleanErrorCode;
117 public void setCleanErrorCode(String cleanErrorCode) {
118 this.cleanErrorCode = cleanErrorCode;
121 public String getResolution() {
125 public void setResolution(String resolution) {
126 this.resolution = resolution;
131 public boolean generateEcompErrorsCsvFile(String targetFolder, boolean addTimeToFileName) {
133 targetFolder += File.separator;
135 boolean result = false;
136 String dateFormatted = "";
138 if (addTimeToFileName) {
139 DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
141 Date date = new Date();
143 dateFormatted = "." + dateFormat.format(date);
147 String outputFile = targetFolder + "ecompErrorCodes" + dateFormatted + ".csv";
149 try(FileWriter writer = new FileWriter(outputFile)) {
151 List<EcompErrorRow> errors = new ArrayList<>();
153 for (EcompErrorEnum ecompErrorEnum : EcompErrorEnum.values()) {
155 EcompErrorRow ecompErrorRow = new EcompErrorRow();
157 String errorCode = EcompErrorLogUtil.createEcode(ecompErrorEnum);
159 EcompErrorEnum clearCodeEnum = ecompErrorEnum.getClearCode();
160 String cleanErrorCode = null;
161 if (clearCodeEnum != null) {
162 cleanErrorCode = EcompErrorLogUtil.createEcode(clearCodeEnum);
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());
173 errors.add(ecompErrorRow);
176 writeHeaders(writer);
178 for (EcompErrorRow ecompErrorRow : errors) {
179 writer.append(addInvertedCommas(ecompErrorRow.getErrorCode()));
181 writer.append(addInvertedCommas(ecompErrorRow.getErrorType().toString()));
183 writer.append(addInvertedCommas(ecompErrorRow.getDescription()));
185 writer.append(addInvertedCommas(ecompErrorRow.getResolution()));
187 writer.append(addInvertedCommas(ecompErrorRow.getAlarmSeverity().toString()));
189 writer.append(addInvertedCommas(ecompErrorRow.getErrorName()));
191 writer.append(addInvertedCommas(ecompErrorRow.getCleanErrorCode()));
192 writer.append(NEW_LINE);
197 } catch (Exception e) {
198 log.info("generate Ecomp Errors Csv File failed - {}" , e);
204 private void writeHeaders(FileWriter writer) throws IOException {
206 writer.append("\"ERROR CODE\"");
208 writer.append("\"ERROR TYPE\"");
210 writer.append("\"DESCRIPTION\"");
212 writer.append("\"RESOLUTION\"");
214 writer.append("\"ALARM SEVERITY\"");
216 writer.append("\"ERROR NAME\"");
218 writer.append("\"CLEAN CODE\"");
219 writer.append(NEW_LINE);
222 private String addInvertedCommas(String str) {
228 return "\"" + str + "\"";