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