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