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