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