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