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