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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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;
39 public class GenerateEcompErrorsCsv {
41 private static final Logger LOGGER = LoggerFactory.getLogger(GenerateEcompErrorsCsv.class);
43 private static final String DATE_FORMAT = "dd-M-yyyy-hh-mm-ss";
45 private static final 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 {
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;
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 try (FileWriter writer = new FileWriter(outputFile)) {
152 List<EcompErrorRow> errors = new ArrayList<>();
154 for (EcompErrorEnum ecompErrorEnum : EcompErrorEnum.values()) {
156 EcompErrorRow ecompErrorRow = new EcompErrorRow();
158 String errorCode = EcompErrorLogUtil.createEcode(ecompErrorEnum);
160 EcompErrorEnum clearCodeEnum = ecompErrorEnum.getClearCode();
161 String cleanErrorCode = null;
162 if (clearCodeEnum != null) {
163 cleanErrorCode = EcompErrorLogUtil.createEcode(clearCodeEnum);
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());
174 errors.add(ecompErrorRow);
177 writeHeaders(writer);
179 for (EcompErrorRow ecompErrorRow : errors) {
180 writer.append(addInvertedCommas(ecompErrorRow.getErrorCode()));
182 writer.append(addInvertedCommas(ecompErrorRow.getErrorType().toString()));
184 writer.append(addInvertedCommas(ecompErrorRow.getDescription()));
186 writer.append(addInvertedCommas(ecompErrorRow.getResolution()));
188 writer.append(addInvertedCommas(ecompErrorRow.getAlarmSeverity().toString()));
190 writer.append(addInvertedCommas(ecompErrorRow.getErrorName()));
192 writer.append(addInvertedCommas(ecompErrorRow.getCleanErrorCode()));
193 writer.append(NEW_LINE);
198 } catch (Exception e) {
199 LOGGER.info("generate Ecomp Errors Csv File failed - {}", e);
205 private void writeHeaders(FileWriter writer) throws IOException {
207 writer.append("\"ERROR CODE\"");
209 writer.append("\"ERROR TYPE\"");
211 writer.append("\"DESCRIPTION\"");
213 writer.append("\"RESOLUTION\"");
215 writer.append("\"ALARM SEVERITY\"");
217 writer.append("\"ERROR NAME\"");
219 writer.append("\"CLEAN CODE\"");
220 writer.append(NEW_LINE);
223 private String addInvertedCommas(String str) {
229 return "\"" + str + "\"";