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