d45c896799e607547607d67853d2681033acac89
[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.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Optional;
46 import java.util.stream.Collectors;
47
48 /**
49  * Created by chaya on 7/18/2017.
50  */
51 public class ModuleJsonTask extends ServiceValidationTask {
52
53     private TopologyTemplateOperation topologyTemplateOperation;
54
55     @Autowired
56     public ModuleJsonTask(TopologyTemplateOperation topologyTemplateOperation) {
57         this.topologyTemplateOperation = topologyTemplateOperation;
58         this.name = "Service Module json Validation Task";
59     }
60
61     @Override
62     public VertexResult validate(GraphVertex vertex) {
63         if (!isAfterSubmitForTesting(vertex)) {
64             return new VertexResult(true);
65         }
66
67         ComponentParametersView paramView = new ComponentParametersView();
68         paramView.disableAll();
69         paramView.setIgnoreArtifacts(false);
70         paramView.setIgnoreGroups(false);
71         paramView.setIgnoreComponentInstances(false);
72         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
73         if (toscaElementEither.isRight()) {
74             return new VertexResult(false);
75         }
76         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
77         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
78         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
79
80         for (Map.Entry<String, MapGroupsDataDefinition> pair : Optional.ofNullable(instGroups).orElse(Collections.emptyMap()).entrySet()) {
81             MapGroupsDataDefinition groups = pair.getValue();
82             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
83                 return new VertexResult(findCoordinateModuleJson(pair, instDeploymentArtifacts, vertex));
84             }
85             return new VertexResult(true);
86         }
87         return new VertexResult(true);
88     }
89
90     private boolean findCoordinateModuleJson(Map.Entry<String, MapGroupsDataDefinition> pair, Map<String, MapArtifactDataDefinition> instDeploymentArtifacts, GraphVertex vertex) {
91         String groupKey = pair.getKey();
92         String[] split = groupKey.split("\\.");
93         String instanceName = split[split.length-1];
94         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
95         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
96             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values().stream().filter(artifact -> {
97                 String artifactName = artifact.getArtifactName();
98                 if (artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json")) {
99                     return true;
100                 }
101                 return false;
102             }).collect(Collectors.toList());
103             if (moduleJsonArtifacts.size() > 0) {
104                 String status = "Instance "+instanceName+" has a corresponding modules.json file: "+moduleJsonArtifacts.get(0).getArtifactName();
105                 ReportManager.writeReportLineToFile(status);
106                 return true;
107             }
108         }
109         String status = "Instance "+instanceName+" doesn't have a corresponding modules.json file";
110         ReportManager.writeReportLineToFile(status);
111         ReportManager.addFailedVertex(getTaskName(), vertex.getUniqueId());
112         return false;
113     }
114
115     private boolean isAfterSubmitForTesting(GraphVertex vertex){
116         List allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.CERTIFIED.name()));
117         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
118     }
119 }