967a6c68e549129381e10c1294184c8d505eda70
[sdc.git] /
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.executers;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
25 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile.TXTFile;
26 import org.openecomp.sdc.asdctool.impl.validator.tasks.TopologyTemplateValidationTask;
27 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
28 import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult;
29 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
30 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
31 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
32 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
33 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
34 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
35 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.springframework.beans.factory.annotation.Autowired;
38
39 import java.util.ArrayList;
40 import java.util.EnumMap;
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45
46 public class TopologyTemplateValidatorExecuter {
47
48     private static final Logger log = Logger.getLogger(VfValidatorExecuter.class);
49
50     protected JanusGraphDao janusGraphDao;
51
52     protected String name;
53
54     @Autowired
55     public TopologyTemplateValidatorExecuter(JanusGraphDao janusGraphDao) {
56         this.janusGraphDao = janusGraphDao;
57     }
58
59     public void setName(String name) {
60         this.name = name;
61     }
62
63     public String getName() {
64         return name;
65     }
66
67     protected List<GraphVertex> getVerticesToValidate(ComponentTypeEnum type) {
68         Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
69         props.put(GraphPropertyEnum.COMPONENT_TYPE, type.name());
70         if (type.equals(ComponentTypeEnum.RESOURCE)) {
71             props.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
72         }
73
74         Either<List<GraphVertex>, JanusGraphOperationStatus> results = janusGraphDao
75             .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, props);
76         if (results.isRight()) {
77             log.error("getVerticesToValidate failed " + results.right().value());
78             return new ArrayList<>();
79         }
80         log.info("getVerticesToValidate: " + results.left().value().size() + " vertices to scan");
81         return results.left().value();
82     }
83
84     protected boolean validate(
85         Report report,
86         List<? extends TopologyTemplateValidationTask> tasks,
87         List<GraphVertex> vertices,
88         TXTFile reportFile,
89         String outputFilePath
90     ) {
91         reportFile.reportStartValidatorRun(getName(), vertices.size());
92         Set<String> failedTasks = new HashSet<>();
93         Set<String> successTasks = new HashSet<>();
94         boolean successAllVertices = true;
95         int vertexNum = 0;
96         int verticesSize = vertices.size();
97
98         for (GraphVertex vertex : vertices) {
99             vertexNum++;
100             boolean successAllTasks = true;
101             for (TopologyTemplateValidationTask task : tasks) {
102                 reportFile.reportStartTaskRun(vertex, task.getTaskName());
103                 VertexResult result = task.validate(report, vertex, outputFilePath);
104                 if (!result.getStatus()) {
105                     failedTasks.add(task.getTaskName());
106                     successAllVertices = false;
107                     successAllTasks = false;
108                 } else if (successAllTasks && vertexNum == verticesSize) {
109                     successTasks.add(task.getTaskName());
110                 }
111                 ReportManager.printValidationTaskStatus(vertex, task.getTaskName(), result.getStatus(), outputFilePath);
112                 report.addSuccess(vertex.getUniqueId(), task.getTaskName(), result);
113             }
114             String componentScanStatus = successAllTasks ? "success" : "failed";
115             log.info("Topology Template " + vertex.getUniqueId() + " Validation finished with " + componentScanStatus);
116         }
117         ReportManager.reportValidatorTypeSummary(getName(), failedTasks, successTasks, outputFilePath);
118         return successAllVertices;
119     }
120 }