diff options
author | Francis Toth <francis.toth@yoppworks.com> | 2020-04-20 19:16:47 -0400 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-06-18 05:40:40 +0000 |
commit | 841d054f6bf5231c96abf1e91e1715c02809046a (patch) | |
tree | 66b681561778e6aa72470ee049927f5e1db05430 /asdctool/src/main/java | |
parent | bc9783d71b65e0ab70dcd5bd6fcb8ff0e09eaf4b (diff) |
Decouple CSV Report file writing and formatting logic
Signed-off-by: Francis Toth <francis.toth@yoppworks.com>
Change-Id: I6168b71bb54192a806ff6aa437788bedad933c76
Issue-ID: SDC-2499
Diffstat (limited to 'asdctool/src/main/java')
6 files changed, 170 insertions, 30 deletions
diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/FileType.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/FileType.java new file mode 100644 index 0000000000..7598ae1bb8 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/FileType.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Bell Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.asdctool.impl.validator.report; + +/** + * This interface provides some marker interfaces to prevent any type information lose when instantiating + * a {@link org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriter }. + */ +public interface FileType { + interface CSV extends FileType { } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/Report.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/Report.java index 68d7210e81..cf6fb0e8b1 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/Report.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/Report.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * SDC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020 Bell Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFile.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFile.java new file mode 100644 index 0000000000..a0703f47e7 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFile.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Bell Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.asdctool.impl.validator.report; + +/** + * Provides business logic in regards to file writing required by the validation tools + */ +public class ReportFile { + + static public CSVFile makeCsvFile(ReportFileWriter<FileType.CSV> writer) { + writer.writeln("Vertex ID,Task Name,Success,Result Details,Result Description"); + return new CSVFile(writer); + } + + /** + * Provides csv writing business logic related to {@link org.openecomp.sdc.asdctool.main.ValidationTool} + */ + public static final class CSVFile extends ReportFile { + + private final ReportFileWriter<FileType.CSV> writer; + + private CSVFile(ReportFileWriter<FileType.CSV> writer) { + this.writer = writer; + } + + public void printAllResults(Report report) { + report.forEachSuccess((vertex, task, result) -> { + String resultLine = vertex + "," + task + "," + result.getStatus() + "," + result.getResult(); + writer.writeln(resultLine); + }); + } + } +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriter.java new file mode 100644 index 0000000000..e1a0090cb9 --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/report/ReportFileWriter.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) Bell Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + + +package org.openecomp.sdc.asdctool.impl.validator.report; + +import static java.nio.file.StandardOpenOption.APPEND; +import static java.nio.file.StandardOpenOption.CREATE; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.function.Consumer; + +/** + * Describes a writer for report's data + * @param <A> phantom type which is only used for type-safety to prevent mixing writers for TXT Report files + * and CSV Report files + */ +@SuppressWarnings("unused") +public abstract class ReportFileWriter<A extends FileType> { + + abstract public void write(String s); + + public void writeln(String s) { + write(s + "\n"); + } + + /** + * + * @param filePath The resulting file path + * @param onError error handling callback + * @param <A> phantom type which is only used for type-safety + */ + public static <A extends FileType> ReportFileWriter<A> makeNioWriter( + Path filePath, Consumer<IOException> onError + ) { + return new ReportFileWriter<A>() { + @Override + public void write(String line) { + try { + StandardOpenOption soo = Files.exists(filePath) ? APPEND : CREATE; + Files.write(filePath, line.getBytes(), soo); + } catch (IOException ex) { + onError.accept(ex); + } + } + }; + } +} + diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java index a1300cb70c..5b78df8c73 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/utils/ReportManager.java @@ -36,13 +36,12 @@ public class ReportManager { private static final Logger log = LoggerFactory.getLogger(ReportManager.class); - public static ReportManager make(String csvReportFilePath, String txtReportFilePath) { - return new ReportManager(csvReportFilePath, txtReportFilePath); + public static ReportManager make(String txtReportFilePath) { + return new ReportManager(txtReportFilePath); } - private ReportManager(String csvReportFilePath, String txtReportFilePath) { + private ReportManager(String txtReportFilePath) { try { - initCsvFile(csvReportFilePath); initReportFile(txtReportFilePath); } catch (IOException e) { log.info("Init file failed - {}", e.getClass().getSimpleName(), e); @@ -55,13 +54,6 @@ public class ReportManager { Files.write(Paths.get(txtReportFilePath), sb.toString().getBytes()); } - private void initCsvFile(String csvReportFilePath) throws IOException { - StrBuilder sb = new StrBuilder(); - sb.append("Vertex ID,Task Name,Success,Result Details,Result Description"); - sb.appendNewLine(); - Files.write(Paths.get(csvReportFilePath), sb.toString().getBytes()); - } - public static void printValidationTaskStatus(GraphVertex vertexScanned, String taskName, boolean success, String outputFilePath) { String successStatus = success ? "success" : "failed"; @@ -107,7 +99,7 @@ public class ReportManager { writeReportLineToFile(sb.toString(), outputFilePath); } - public static void reportEndOfToolRun(Report report, String csvReportFilePath, String outputFilePath) { + public static void reportEndOfToolRun(Report report, String outputFilePath) { StrBuilder sb = new StrBuilder(); sb.appendln("-----------------------------------Validator Tool Summary-----------------------------------"); report.forEachFailure((taskName, failedVertices) -> { @@ -117,21 +109,5 @@ public class ReportManager { sb.appendNewLine(); }); writeReportLineToFile(sb.toString(), outputFilePath); - printAllResults(report, csvReportFilePath); - } - - public static void printAllResults(Report report, String csvReportFilePath) { - report.forEachSuccess((vertex, task, result) -> { - try { - String resultLine = vertex + "," + task + "," + result.getStatus() + "," + result.getResult(); - Files.write(Paths.get(csvReportFilePath), resultLine.getBytes(), - StandardOpenOption.APPEND); - Files.write(Paths.get(csvReportFilePath), - new StrBuilder().appendNewLine().toString().getBytes(), - StandardOpenOption.APPEND); - } catch (IOException e) { - log.info("write to file failed - {}", e.getClass().getSimpleName(), e); - } - }); } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java index f37a82d09b..754d163e77 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ValidationTool.java @@ -20,10 +20,16 @@ package org.openecomp.sdc.asdctool.main; +import java.nio.file.Path; +import java.nio.file.Paths; import org.openecomp.sdc.asdctool.impl.validator.ValidationToolBL; import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager; import org.openecomp.sdc.asdctool.impl.validator.config.ValidationToolConfiguration; +import org.openecomp.sdc.asdctool.impl.validator.report.FileType; import org.openecomp.sdc.asdctool.impl.validator.report.Report; +import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile; +import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.CSVFile; +import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriter; import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.api.ConfigurationSource; @@ -42,6 +48,8 @@ public class ValidationTool { String txtReportFilePath = ValidationConfigManager.txtReportFilePath(outputPath); String csvReportFilePath = ValidationConfigManager.csvReportFilePath(outputPath, System::currentTimeMillis); + CSVFile csvFile = ReportFile.makeCsvFile(makeNioWriter(Paths.get(csvReportFilePath))); + String appConfigDir = args[1]; AnnotationConfigApplicationContext context = initContext(appConfigDir); ValidationToolBL validationToolBL = context.getBean(ValidationToolBL.class); @@ -49,7 +57,8 @@ public class ValidationTool { log.info("Start Validation Tool"); Report report = Report.make(); boolean result = validationToolBL.validateAll(report, txtReportFilePath); - ReportManager.reportEndOfToolRun(report, csvReportFilePath, txtReportFilePath); + ReportManager.reportEndOfToolRun(report, txtReportFilePath); + csvFile.printAllResults(report); if (result) { log.info("Validation finished successfully"); System.exit(0); @@ -59,6 +68,12 @@ public class ValidationTool { } } + private static <A extends FileType> ReportFileWriter<A> makeNioWriter(Path path) { + return ReportFileWriter.makeNioWriter(path, ex -> + log.info("write to file failed - {}", ex.getClass().getSimpleName(), ex) + ); + } + private static AnnotationConfigApplicationContext initContext(String appConfigDir) { ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); |