re base code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / validator / utils / ReportManager.java
1 package org.openecomp.sdc.asdctool.impl.validator.utils;
2
3 import org.apache.commons.lang.text.StrBuilder;
4 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
5 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
6
7 import java.io.IOException;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.nio.file.StandardOpenOption;
11 import java.util.*;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
16  * Created by chaya on 7/5/2017.
17  */
18 public class ReportManager {
19
20     private static Logger log = LoggerFactory.getLogger(ReportManager.class);
21     private static String reportOutputFilePath;
22     private static String csvReportFilePath;
23     private static Map<String, Set<String>> failedVerticesPerTask = new HashMap<>();
24     private static Map<String, Map<String, VertexResult>> resultsPerVertex = new HashMap<>();
25
26     public ReportManager() {
27         try {
28             initCsvFile();
29             initReportFile();
30         } catch (IOException e) {
31             log.info("Init file failed - {}" , e);
32         }
33     }
34
35     private void initReportFile() throws IOException {
36         reportOutputFilePath = ValidationConfigManager.getOutputFullFilePath();
37         StrBuilder sb = new StrBuilder();
38         sb.appendln("-----------------------Validation Tool Results:-------------------------");
39         Files.write(Paths.get(reportOutputFilePath), sb.toString().getBytes());
40     }
41
42     private void initCsvFile() throws IOException {
43         csvReportFilePath = ValidationConfigManager.getCsvReportFilePath();
44         StrBuilder sb = new StrBuilder();
45         sb.append("Vertex ID,"+"Task Name,"+"Success,"+"Result Details"+","+"Result Description");
46         sb.appendNewLine();
47         Files.write(Paths.get(csvReportFilePath), sb.toString().getBytes());
48     }
49
50     public static void reportTaskEnd(String vertexId, String taskName, VertexResult result) {
51         Map<String, VertexResult> vertexTasksResults =
52                 Optional.ofNullable(resultsPerVertex.get(vertexId)).orElse(new HashMap<>());
53         vertexTasksResults.put(taskName, result);
54         resultsPerVertex.put(vertexId, vertexTasksResults);
55     }
56
57     public static void addFailedVertex (String taskName, String vertexId) {
58         Set<String> failedVertices = failedVerticesPerTask.get(taskName);
59         if (failedVertices == null) {
60             failedVertices = new HashSet<>();
61         }
62         failedVertices.add(vertexId);
63         failedVerticesPerTask.put(taskName, failedVertices);
64     }
65
66     public static void printValidationTaskStatus(GraphVertex vertexScanned, String taskName, boolean success) {
67         String successStatus = success ? "success" : "failed";
68         String line = "-----------------------Vertex: "+vertexScanned.getUniqueId()+", Task " + taskName + " " +successStatus+"-----------------------";
69         StrBuilder sb = new StrBuilder();
70         writeReportLineToFile(sb.appendNewLine().toString());
71         sb.appendln(line);
72         sb.appendNewLine();
73         writeReportLineToFile(line);
74     }
75
76     public static void writeReportLineToFile(String message) {
77         try {
78             Files.write(Paths.get(reportOutputFilePath), new StrBuilder().appendNewLine().toString().getBytes(), StandardOpenOption.APPEND);
79             Files.write(Paths.get(reportOutputFilePath), message.getBytes(), StandardOpenOption.APPEND);
80         } catch (IOException e) {
81             log.info("write to file failed - {}" , e);
82         }
83     }
84
85     public static void reportValidatorTypeSummary(String validatorName, Set<String> failedTasksNames, Set<String> successTasksNames){
86         StrBuilder sb = new StrBuilder();
87         sb.appendNewLine().appendNewLine();
88         sb.appendln("-----------------------ValidatorExecuter " + validatorName + " Validation Summary-----------------------");
89         sb.appendln("Failed tasks: "+ failedTasksNames);
90         sb.appendln("Success tasks: "+ successTasksNames);
91         writeReportLineToFile(sb.toString());
92     }
93
94     public static void reportStartValidatorRun(String validatorName, int componenentsNum) {
95         StrBuilder sb = new StrBuilder();
96         sb.appendNewLine().appendNewLine();
97         sb.appendln("------ValidatorExecuter " + validatorName + " Validation Started, on "+componenentsNum+" components---------");
98         writeReportLineToFile(sb.toString());
99     }
100
101     public static void reportStartTaskRun(GraphVertex vertex, String taskName){
102         StrBuilder sb = new StrBuilder();
103         sb.appendNewLine().appendNewLine();
104         sb.appendln("-----------------------Vertex: "+vertex.getUniqueId()+", Task " + taskName + " Started-----------------------");
105         writeReportLineToFile(sb.toString());
106     }
107
108     public static void reportEndOfToolRun() {
109         StrBuilder sb = new StrBuilder();
110         sb.appendNewLine().appendNewLine();
111         sb.appendln("-----------------------------------Validator Tool Summary-----------------------------------");
112         failedVerticesPerTask.forEach((taskName, failedVertices) -> {
113             sb.append("Task: " + taskName);
114             sb.appendNewLine();
115             sb.append("FailedVertices: " + failedVertices);
116             sb.appendNewLine();
117         });
118         writeReportLineToFile(sb.toString());
119         printAllResults();
120     }
121
122     public static void printAllResults() {
123         resultsPerVertex.forEach((vertex, tasksResults)->{
124             tasksResults.forEach((task, result) -> {
125                 try {
126                     String resultLine = vertex +","+task+","+result.getStatus()+","+result.getResult();
127                     Files.write(Paths.get(csvReportFilePath), resultLine.getBytes(), StandardOpenOption.APPEND);
128                     Files.write(Paths.get(csvReportFilePath), new StrBuilder().appendNewLine().toString().getBytes(), StandardOpenOption.APPEND);
129                 } catch (IOException e) {
130                     log.info("write to file failed - {}" , e);
131                 }
132             });
133         });
134     }
135 }