1 package org.openecomp.sdc.asdctool.impl.validator.tasks.moduleJson;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
8 import java.util.Optional;
9 import java.util.stream.Collectors;
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;
27 import fj.data.Either;
30 * Created by chaya on 7/18/2017.
32 public class ModuleJsonTask extends ServiceValidationTask {
35 private TopologyTemplateOperation topologyTemplateOperation;
37 public ModuleJsonTask() {
38 this.name = "Service Module json Validation Task";
42 public VertexResult validate(GraphVertex vertex) {
43 if (!isAfterSubmitForTesting(vertex)) {
44 return new VertexResult(true);
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);
56 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
57 Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
58 Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
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));
65 return new VertexResult(true);
67 return new VertexResult(true);
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")) {
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);
89 String status = "Instance "+instanceName+" doesn't have a corresponding modules.json file";
90 ReportManager.writeReportLineToFile(status);
91 ReportManager.addFailedVertex(getTaskName(), vertex.getUniqueId());
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));