d6fafcbb5e5b04b24e07e65a507a7bf123e30158
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.validator.tasks.moduleJson;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask;
25 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
26 import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
32 import org.openecomp.sdc.be.model.ComponentParametersView;
33 import org.openecomp.sdc.be.model.LifecycleStateEnum;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 import java.util.*;
41 import java.util.stream.Collectors;
42
43 /**
44  * Created by chaya on 7/18/2017.
45  */
46 public class ModuleJsonTask extends ServiceValidationTask {
47
48     private TopologyTemplateOperation topologyTemplateOperation;
49
50     @Autowired
51     public ModuleJsonTask(TopologyTemplateOperation topologyTemplateOperation) {
52         this.topologyTemplateOperation = topologyTemplateOperation;
53         this.name = "Service Module json Validation Task";
54     }
55
56     @Override
57     public VertexResult validate(GraphVertex vertex) {
58         if (!isAfterSubmitForTesting(vertex)) {
59             return new VertexResult(true);
60         }
61
62         ComponentParametersView paramView = new ComponentParametersView();
63         paramView.disableAll();
64         paramView.setIgnoreArtifacts(false);
65         paramView.setIgnoreGroups(false);
66         paramView.setIgnoreComponentInstances(false);
67         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
68         if (toscaElementEither.isRight()) {
69             return new VertexResult(false);
70         }
71         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
72         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
73         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
74
75         for (Map.Entry<String, MapGroupsDataDefinition> pair : Optional.ofNullable(instGroups).orElse(Collections.emptyMap()).entrySet()) {
76             MapGroupsDataDefinition groups = pair.getValue();
77             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
78                 return new VertexResult(findCoordinateModuleJson(pair, instDeploymentArtifacts, vertex));
79             }
80             return new VertexResult(true);
81         }
82         return new VertexResult(true);
83     }
84
85     private boolean findCoordinateModuleJson(Map.Entry<String, MapGroupsDataDefinition> pair, Map<String, MapArtifactDataDefinition> instDeploymentArtifacts, GraphVertex vertex) {
86         String groupKey = pair.getKey();
87         String[] split = groupKey.split("\\.");
88         String instanceName = split[split.length-1];
89         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
90         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
91             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values().stream().filter(artifact -> {
92                 String artifactName = artifact.getArtifactName();
93                 if (artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json")) {
94                     return true;
95                 }
96                 return false;
97             }).collect(Collectors.toList());
98             if (moduleJsonArtifacts.size() > 0) {
99                 String status = "Instance "+instanceName+" has a corresponding modules.json file: "+moduleJsonArtifacts.get(0).getArtifactName();
100                 ReportManager.writeReportLineToFile(status);
101                 return true;
102             }
103         }
104         String status = "Instance "+instanceName+" doesn't have a corresponding modules.json file";
105         ReportManager.writeReportLineToFile(status);
106         ReportManager.addFailedVertex(getTaskName(), vertex.getUniqueId());
107         return false;
108     }
109
110     private boolean isAfterSubmitForTesting(GraphVertex vertex){
111         List allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.READY_FOR_CERTIFICATION.name(),
112                 LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name(), LifecycleStateEnum.CERTIFIED.name()));
113         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
114     }
115 }