Reformat asdctool
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / main / ValidationTool.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 package org.openecomp.sdc.asdctool.main;
21
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import org.openecomp.sdc.asdctool.impl.validator.ValidationToolBL;
25 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
26 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationToolConfiguration;
27 import org.openecomp.sdc.asdctool.impl.validator.report.FileType;
28 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
29 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile;
30 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.CSVFile;
31 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
32 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFileWriter;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.common.impl.ExternalConfiguration;
35 import org.openecomp.sdc.common.impl.FSConfigurationSource;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
38
39 public class ValidationTool {
40
41     private static final Logger log = Logger.getLogger(ValidationTool.class.getName());
42
43     public static void main(String[] args) {
44         String outputPath = args[0];
45         String txtReportFilePath = ValidationConfigManager.txtReportFilePath(outputPath);
46         String csvReportFilePath = ValidationConfigManager.csvReportFilePath(outputPath, System::currentTimeMillis);
47         CSVFile csvFile = ReportFile.makeCsvFile(makeNioWriter(Paths.get(csvReportFilePath)));
48         TXTFile textFile = ReportFile.makeTxtFile(makeNioWriter(Paths.get(txtReportFilePath)));
49         String appConfigDir = args[1];
50         AnnotationConfigApplicationContext context = initContext(appConfigDir);
51         ValidationToolBL validationToolBL = context.getBean(ValidationToolBL.class);
52         log.info("Start Validation Tool");
53         Report report = Report.make();
54         boolean result = validationToolBL.validateAll(report, textFile);
55         textFile.reportEndOfToolRun(report);
56         csvFile.printAllResults(report);
57         if (result) {
58             log.info("Validation finished successfully");
59             System.exit(0);
60         } else {
61             log.info("Validation finished with warnings");
62             System.exit(2);
63         }
64     }
65
66     private static <A extends FileType> ReportFileWriter<A> makeNioWriter(Path path) {
67         return ReportFileWriter.makeNioWriter(path, ex -> log.info("write to file failed - {}", ex.getClass().getSimpleName(), ex));
68     }
69
70     private static AnnotationConfigApplicationContext initContext(String appConfigDir) {
71         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir));
72         return new AnnotationConfigApplicationContext(ValidationToolConfiguration.class);
73     }
74 }