2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.asdctool.impl.validator.utils;
23 import org.apache.commons.lang.text.StrBuilder;
24 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
25 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.nio.file.StandardOpenOption;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Created by chaya on 7/5/2017.
38 public class ReportManager {
40 private static Logger log = LoggerFactory.getLogger(ReportManager.class);
41 private static String reportOutputFilePath;
42 private static String csvReportFilePath;
43 private static Map<String, Set<String>> failedVerticesPerTask = new HashMap<>();
44 private static Map<String, Map<String, VertexResult>> resultsPerVertex = new HashMap<>();
46 public ReportManager() {
50 } catch (IOException e) {
51 log.info("Init file failed - {}" , e);
55 private void initReportFile() throws IOException {
56 reportOutputFilePath = ValidationConfigManager.getOutputFullFilePath();
57 StrBuilder sb = new StrBuilder();
58 sb.appendln("-----------------------Validation Tool Results:-------------------------");
59 Files.write(Paths.get(reportOutputFilePath), sb.toString().getBytes());
62 private void initCsvFile() throws IOException {
63 csvReportFilePath = ValidationConfigManager.getCsvReportFilePath();
64 StrBuilder sb = new StrBuilder();
65 sb.append("Vertex ID,"+"Task Name,"+"Success,"+"Result Details"+","+"Result Description");
67 Files.write(Paths.get(csvReportFilePath), sb.toString().getBytes());
70 public static void reportTaskEnd(String vertexId, String taskName, VertexResult result) {
71 Map<String, VertexResult> vertexTasksResults =
72 Optional.ofNullable(resultsPerVertex.get(vertexId)).orElse(new HashMap<>());
73 vertexTasksResults.put(taskName, result);
74 resultsPerVertex.put(vertexId, vertexTasksResults);
77 public static void addFailedVertex (String taskName, String vertexId) {
78 Set<String> failedVertices = failedVerticesPerTask.get(taskName);
79 if (failedVertices == null) {
80 failedVertices = new HashSet<>();
82 failedVertices.add(vertexId);
83 failedVerticesPerTask.put(taskName, failedVertices);
86 public static void printValidationTaskStatus(GraphVertex vertexScanned, String taskName, boolean success) {
87 String successStatus = success ? "success" : "failed";
88 String line = "-----------------------Vertex: "+vertexScanned.getUniqueId()+", Task " + taskName + " " +successStatus+"-----------------------";
89 StrBuilder sb = new StrBuilder();
90 writeReportLineToFile(sb.appendNewLine().toString());
93 writeReportLineToFile(line);
96 public static void writeReportLineToFile(String message) {
98 Files.write(Paths.get(reportOutputFilePath), new StrBuilder().appendNewLine().toString().getBytes(), StandardOpenOption.APPEND);
99 Files.write(Paths.get(reportOutputFilePath), message.getBytes(), StandardOpenOption.APPEND);
100 } catch (IOException e) {
101 log.info("write to file failed - {}" , e);
105 public static void reportValidatorTypeSummary(String validatorName, Set<String> failedTasksNames, Set<String> successTasksNames){
106 StrBuilder sb = new StrBuilder();
107 sb.appendNewLine().appendNewLine();
108 sb.appendln("-----------------------ValidatorExecuter " + validatorName + " Validation Summary-----------------------");
109 sb.appendln("Failed tasks: "+ failedTasksNames);
110 sb.appendln("Success tasks: "+ successTasksNames);
111 writeReportLineToFile(sb.toString());
114 public static void reportStartValidatorRun(String validatorName, int componenentsNum) {
115 StrBuilder sb = new StrBuilder();
116 sb.appendNewLine().appendNewLine();
117 sb.appendln("------ValidatorExecuter " + validatorName + " Validation Started, on "+componenentsNum+" components---------");
118 writeReportLineToFile(sb.toString());
121 public static void reportStartTaskRun(GraphVertex vertex, String taskName){
122 StrBuilder sb = new StrBuilder();
123 sb.appendNewLine().appendNewLine();
124 sb.appendln("-----------------------Vertex: "+vertex.getUniqueId()+", Task " + taskName + " Started-----------------------");
125 writeReportLineToFile(sb.toString());
128 public static void reportEndOfToolRun() {
129 StrBuilder sb = new StrBuilder();
130 sb.appendNewLine().appendNewLine();
131 sb.appendln("-----------------------------------Validator Tool Summary-----------------------------------");
132 failedVerticesPerTask.forEach((taskName, failedVertices) -> {
133 sb.append("Task: " + taskName);
135 sb.append("FailedVertices: " + failedVertices);
138 writeReportLineToFile(sb.toString());
142 public static void printAllResults() {
143 resultsPerVertex.forEach((vertex, tasksResults)->{
144 tasksResults.forEach((task, result) -> {
146 String resultLine = vertex +","+task+","+result.getStatus()+","+result.getResult();
147 Files.write(Paths.get(csvReportFilePath), resultLine.getBytes(), StandardOpenOption.APPEND);
148 Files.write(Paths.get(csvReportFilePath), new StrBuilder().appendNewLine().toString().getBytes(), StandardOpenOption.APPEND);
149 } catch (IOException e) {
150 log.info("write to file failed - {}" , e);