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