2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdc.asdctool.impl.validator.utils;
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;
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;
37 import java.util.Optional;
41 * Created by chaya on 7/5/2017.
43 public class ReportManager {
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<>();
51 public ReportManager() {
55 } catch (IOException e) {
57 log.info("Init file failed - {}", e.getClass().getSimpleName(), e);
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());
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");
73 Files.write(Paths.get(csvReportFilePath), sb.toString().getBytes());
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);
83 public static void addFailedVertex (String taskName, String vertexId) {
84 Set<String> failedVertices = failedVerticesPerTask.get(taskName);
85 if (failedVertices == null) {
86 failedVertices = new HashSet<>();
88 failedVertices.add(vertexId);
89 failedVerticesPerTask.put(taskName, failedVertices);
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();
97 writeReportLineToFile(line);
100 public static void writeReportLineToFile(String message) {
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) {
106 log.info("write to file failed - {}", e.getClass().getSimpleName(), e);
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());
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());
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());
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);
136 sb.append("FailedVertices: " + failedVertices);
139 writeReportLineToFile(sb.toString());
143 public static void printAllResults() {
144 resultsPerVertex.forEach((vertex, tasksResults) -> tasksResults.forEach((task, result) -> {
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) {
154 log.info("write to file failed - {}", e.getClass().getSimpleName(), e);