187359f1277d94c911b91a0dabc0897a4514a3b7
[sdc.git] /
1 package org.openecomp.sdc.asdctool.impl.validator.tasks.moduleJson;
2
3 import fj.data.Either;
4 import org.apache.cassandra.cql3.CQL3Type;
5 import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask;
6 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
7 import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult;
8 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
9 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
10 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
11 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
12 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
13 import org.openecomp.sdc.be.model.ComponentParametersView;
14 import org.openecomp.sdc.be.model.LifecycleStateEnum;
15 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
16 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
17 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
18 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
19 import org.springframework.beans.factory.annotation.Autowired;
20
21 import java.util.*;
22 import java.util.stream.Collectors;
23
24 /**
25  * Created by chaya on 7/18/2017.
26  */
27 public class ModuleJsonTask extends ServiceValidationTask {
28
29     @Autowired
30     private TopologyTemplateOperation topologyTemplateOperation;
31
32     public ModuleJsonTask() {
33         this.name = "Service Module json Validation Task";
34     }
35
36     @Override
37     public VertexResult validate(GraphVertex vertex) {
38         if (!isAfterSubmitForTesting(vertex)) {
39             return new VertexResult(true);
40         }
41
42         ComponentParametersView paramView = new ComponentParametersView();
43         paramView.disableAll();
44         paramView.setIgnoreArtifacts(false);
45         paramView.setIgnoreGroups(false);
46         paramView.setIgnoreComponentInstances(false);
47         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
48         if (toscaElementEither.isRight()) {
49             return new VertexResult(false);
50         }
51         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
52         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
53         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
54
55         for (Map.Entry<String, MapGroupsDataDefinition> pair : Optional.ofNullable(instGroups).orElse(Collections.emptyMap()).entrySet()) {
56             MapGroupsDataDefinition groups = pair.getValue();
57             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
58                 return new VertexResult(findCoordinateModuleJson(pair, instDeploymentArtifacts, vertex));
59             }
60             return new VertexResult(true);
61         }
62         return new VertexResult(true);
63     }
64
65     private boolean findCoordinateModuleJson(Map.Entry<String, MapGroupsDataDefinition> pair, Map<String, MapArtifactDataDefinition> instDeploymentArtifacts, GraphVertex vertex) {
66         String groupKey = pair.getKey();
67         String[] split = groupKey.split("\\.");
68         String instanceName = split[split.length-1];
69         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
70         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
71             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values().stream().filter(artifact -> {
72                 String artifactName = artifact.getArtifactName();
73                 if (artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json")) {
74                     return true;
75                 }
76                 return false;
77             }).collect(Collectors.toList());
78             if (moduleJsonArtifacts.size() > 0) {
79                 String status = "Instance "+instanceName+" has a corresponding modules.json file: "+moduleJsonArtifacts.get(0).getArtifactName();
80                 ReportManager.writeReportLineToFile(status);
81                 return true;
82             }
83         }
84         String status = "Instance "+instanceName+" doesn't have a corresponding modules.json file";
85         ReportManager.writeReportLineToFile(status);
86         ReportManager.addFailedVertex(getTaskName(), vertex.getUniqueId());
87         return false;
88     }
89
90     private boolean isAfterSubmitForTesting(GraphVertex vertex){
91         List allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.READY_FOR_CERTIFICATION.name(),
92                 LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name(), LifecycleStateEnum.CERTIFIED.name()));
93         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
94     }
95 }