Catalog alignment
[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  * Modifications Copyright (c) 2019 Samsung
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.asdctool.impl.validator.utils;
23
24 import org.apache.commons.lang.text.StrBuilder;
25 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
26 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.nio.file.StandardOpenOption;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.Map;
37 import java.util.Optional;
38 import java.util.Set;
39
40 /**
41  * Created by chaya on 7/5/2017.
42  */
43 public class ReportManager {
44
45     private static Logger log = LoggerFactory.getLogger(ReportManager.class);
46     private static String reportOutputFilePath;
47     private static String csvReportFilePath;
48     private static Map<String, Set<String>> failedVerticesPerTask = new HashMap<>();
49     private static Map<String, Map<String, VertexResult>> resultsPerVertex = new HashMap<>();
50
51     public ReportManager() {
52         try {
53             initCsvFile();
54             initReportFile();
55         } catch (IOException e) {
56             e.printStackTrace();
57             log.info("Init file failed - {}", e.getClass().getSimpleName(), e);
58         }
59     }
60
61     private void initReportFile() throws IOException {
62         reportOutputFilePath = ValidationConfigManager.getOutputFullFilePath();
63         StrBuilder sb = new StrBuilder();
64         sb.appendln("-----------------------Validation Tool Results:-------------------------");
65         Files.write(Paths.get(reportOutputFilePath), sb.toString().getBytes());
66     }
67
68     private void initCsvFile() throws IOException {
69         csvReportFilePath = ValidationConfigManager.getCsvReportFilePath();
70         StrBuilder sb = new StrBuilder();
71         sb.append("Vertex ID,Task Name,Success,Result Details,Result Description");
72         sb.appendNewLine();
73         Files.write(Paths.get(csvReportFilePath), sb.toString().getBytes());
74     }
75
76     public static void reportTaskEnd(String vertexId, String taskName, VertexResult result) {
77         Map<String, VertexResult> vertexTasksResults =
78                 Optional.ofNullable(resultsPerVertex.get(vertexId)).orElse(new HashMap<>());
79         vertexTasksResults.put(taskName, result);
80         resultsPerVertex.put(vertexId, vertexTasksResults);
81     }
82
83     public static void addFailedVertex (String taskName, String vertexId) {
84         Set<String> failedVertices = failedVerticesPerTask.get(taskName);
85         if (failedVertices == null) {
86             failedVertices = new HashSet<>();
87         }
88         failedVertices.add(vertexId);
89         failedVerticesPerTask.put(taskName, failedVertices);
90     }
91
92     public static void printValidationTaskStatus(GraphVertex vertexScanned, String taskName, boolean success) {
93         String successStatus = success ? "success" : "failed";
94         String line = "-----------------------Vertex: "+vertexScanned.getUniqueId()+", Task " + taskName + " " +successStatus+"-----------------------";
95         StrBuilder sb = new StrBuilder();
96         sb.appendln(line);
97         writeReportLineToFile(line);
98     }
99
100     public static void writeReportLineToFile(String message) {
101         try {
102             Files.write(Paths.get(reportOutputFilePath), new StrBuilder().appendNewLine().toString().getBytes(), StandardOpenOption.APPEND);
103             Files.write(Paths.get(reportOutputFilePath), message.getBytes(), StandardOpenOption.APPEND);
104         } catch (IOException e) {
105             e.printStackTrace();
106             log.info("write to file failed - {}", e.getClass().getSimpleName(), e);
107         }
108     }
109
110     public static void reportValidatorTypeSummary(String validatorName, Set<String> failedTasksNames, Set<String> successTasksNames){
111         StrBuilder sb = new StrBuilder();
112         sb.appendln("-----------------------ValidatorExecuter " + validatorName + " Validation Summary-----------------------");
113         sb.appendln("Failed tasks: "+ failedTasksNames);
114         sb.appendln("Success tasks: "+ successTasksNames);
115         writeReportLineToFile(sb.toString());
116     }
117
118     public static void reportStartValidatorRun(String validatorName, int componenentsNum) {
119         StrBuilder sb = new StrBuilder();
120         sb.appendln("------ValidatorExecuter " + validatorName + " Validation Started, on "+componenentsNum+" components---------");
121         writeReportLineToFile(sb.toString());
122     }
123
124     public static void reportStartTaskRun(GraphVertex vertex, String taskName){
125         StrBuilder sb = new StrBuilder();
126         sb.appendln("-----------------------Vertex: "+vertex.getUniqueId()+", Task " + taskName + " Started-----------------------");
127         writeReportLineToFile(sb.toString());
128     }
129
130     public static void reportEndOfToolRun() {
131         StrBuilder sb = new StrBuilder();
132         sb.appendln("-----------------------------------Validator Tool Summary-----------------------------------");
133         failedVerticesPerTask.forEach((taskName, failedVertices) -> {
134             sb.append("Task: " + taskName);
135             sb.appendNewLine();
136             sb.append("FailedVertices: " + failedVertices);
137             sb.appendNewLine();
138         });
139         writeReportLineToFile(sb.toString());
140         printAllResults();
141     }
142
143     public static void printAllResults() {
144         resultsPerVertex.forEach((vertex, tasksResults) -> tasksResults.forEach((task, result) -> {
145             try {
146                 String resultLine = vertex + "," + task + "," + result.getStatus() + "," + result.getResult();
147                 Files.write(Paths.get(csvReportFilePath), resultLine.getBytes(),
148                     StandardOpenOption.APPEND);
149                 Files.write(Paths.get(csvReportFilePath),
150                     new StrBuilder().appendNewLine().toString().getBytes(),
151                     StandardOpenOption.APPEND);
152             } catch (IOException e) {
153                     e.printStackTrace();
154                 log.info("write to file failed - {}", e.getClass().getSimpleName(), e);
155             }
156         }));
157     }
158 }