675ac1f25de510eb9ce1241b62f5d570462be6d4
[sdc.git] /
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.be.dao.jsongraph.GraphVertex;
6 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
7 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
8 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
9 import org.openecomp.sdc.be.model.ComponentParametersView;
10 import org.openecomp.sdc.be.model.LifecycleStateEnum;
11 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
12 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
13 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
14 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
15 import org.springframework.beans.factory.annotation.Autowired;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Map;
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     @Override
32     public boolean validate(GraphVertex vertex) {
33         ComponentParametersView paramView = new ComponentParametersView();
34         paramView.disableAll();
35         paramView.setIgnoreArtifacts(false);
36         paramView.setIgnoreGroups(false);
37         paramView.setIgnoreComponentInstances(false);
38         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
39         if (toscaElementEither.isRight()) {
40             return false;
41         }
42         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
43         if (!isAfterSubmitForTesting(element)) {
44             return false;
45         }
46         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
47         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
48
49         for (Map.Entry<String, MapGroupsDataDefinition> pair : instGroups.entrySet()) {
50             String groupKey = pair.getKey();
51             MapGroupsDataDefinition groups = pair.getValue();
52             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
53                 MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
54                 if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
55                     List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values().stream().filter(artifact -> {
56                         String artifactName = artifact.getArtifactName();
57                         if (artifactName.startsWith(groupKey) && artifactName.endsWith("module.json")) {
58                             return true;
59                         }
60                         return false;
61                     }).collect(Collectors.toList());
62                     if (moduleJsonArtifacts.size() > 0) {
63                         return true;
64                     }
65                 }
66             }
67         }
68         return true;
69     }
70
71     private boolean isAfterSubmitForTesting(TopologyTemplate element){
72         List allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.READY_FOR_CERTIFICATION,
73                 LifecycleStateEnum.CERTIFICATION_IN_PROGRESS, LifecycleStateEnum.CERTIFIED));
74         return allowedStates.contains(element.getLifecycleState());
75     }
76 }