Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / validator / utils / ReportManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.validator.utils;
22
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;
26
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.nio.file.StandardOpenOption;
31 import java.util.*;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Created by chaya on 7/5/2017.
37  */
38 public class ReportManager {
39
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<>();
45
46     public ReportManager() {
47         try {
48             initCsvFile();
49             initReportFile();
50         } catch (IOException e) {
51             log.info("Init file failed - {}" , e);
52         }
53     }
54
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());
60     }
61
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");
66         sb.appendNewLine();
67         Files.write(Paths.get(csvReportFilePath), sb.toString().getBytes());
68     }
69
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);
75     }
76
77     public static void addFailedVertex (String taskName, String vertexId) {
78         Set<String> failedVertices = failedVerticesPerTask.get(taskName);
79         if (failedVertices == null) {
80             failedVertices = new HashSet<>();
81         }
82         failedVertices.add(vertexId);
83         failedVerticesPerTask.put(taskName, failedVertices);
84     }
85
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());
91         sb.appendln(line);
92         sb.appendNewLine();
93         writeReportLineToFile(line);
94     }
95
96     public static void writeReportLineToFile(String message) {
97         try {
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);
102         }
103     }
104
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());
112     }
113
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());
119     }
120
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());
126     }
127
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);
134             sb.appendNewLine();
135             sb.append("FailedVertices: " + failedVertices);
136             sb.appendNewLine();
137         });
138         writeReportLineToFile(sb.toString());
139         printAllResults();
140     }
141
142     public static void printAllResults() {
143         resultsPerVertex.forEach((vertex, tasksResults)->{
144             tasksResults.forEach((task, result) -> {
145                 try {
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);
151                 }
152             });
153         });
154     }
155 }