2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.asdctool.impl.validator.executor;
22 import java.util.ArrayList;
23 import java.util.EnumMap;
24 import java.util.HashSet;
25 import java.util.List;
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;
43 public class TopologyTemplateValidatorExecutor implements ValidatorExecutor {
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;
50 private final String name;
52 private TopologyTemplateValidatorExecutor(JanusGraphDao janusGraphDao, String name, ComponentTypeEnum componentType,
53 List<? extends TopologyTemplateValidationTask> tasks) {
54 this.janusGraphDao = janusGraphDao;
56 this.componentType = componentType;
60 @Autowired(required = false)
61 public static ValidatorExecutor serviceValidatorExecutor(JanusGraphDao janusGraphDao) {
62 return new TopologyTemplateValidatorExecutor(janusGraphDao, "SERVICE_VALIDATOR", ComponentTypeEnum.SERVICE, new ArrayList<>());
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);
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;
78 int verticesSize = vertices.size();
79 for (GraphVertex vertex : vertices) {
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());
92 reportFile.printValidationTaskStatus(vertex, task.getTaskName(), result.getStatus());
93 report.addSuccess(vertex.getUniqueId(), task.getTaskName(), result);
95 String componentScanStatus = successAllTasks ? "success" : "failed";
96 log.info("Topology Template {} Validation finished with {}", vertex.getUniqueId(), componentScanStatus);
98 reportFile.reportValidatorTypeSummary(name, failedTasks, successTasks);
99 return successAllVertices;
102 private List<GraphVertex> getVerticesToValidate() {
103 return janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, buildProps()).either(vs -> {
104 log.info("getVerticesToValidate: {} vertices to scan", vs.size());
107 log.error("getVerticesToValidate failed {}", sos);
108 return new ArrayList<>();
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);