62404e1e43724ce90772e50fa997148f1dcf1a8a
[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.module.json;
22
23 import fj.data.Either;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import java.util.stream.Collectors;
31 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
32 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile;
33 import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask;
34 import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
40 import org.openecomp.sdc.be.model.ComponentParametersView;
41 import org.openecomp.sdc.be.model.LifecycleStateEnum;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
44 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
45 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
46 import org.springframework.beans.factory.annotation.Autowired;
47
48 public class ModuleJsonTask extends ServiceValidationTask {
49
50     private final TopologyTemplateOperation topologyTemplateOperation;
51
52     @Autowired
53     public ModuleJsonTask(TopologyTemplateOperation topologyTemplateOperation) {
54         this.topologyTemplateOperation = topologyTemplateOperation;
55         this.name = "Service Module json Validation Task";
56     }
57
58     @Override
59     public VertexResult validate(Report report, GraphVertex vertex, ReportFile.TXTFile reportFile) {
60         if (!isAfterSubmitForTesting(vertex)) {
61             return new VertexResult(true);
62         }
63
64         ComponentParametersView paramView = new ComponentParametersView();
65         paramView.disableAll();
66         paramView.setIgnoreArtifacts(false);
67         paramView.setIgnoreGroups(false);
68         paramView.setIgnoreComponentInstances(false);
69         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation
70                 .getToscaElement(vertex.getUniqueId(), paramView);
71         if (toscaElementEither.isRight()) {
72             return new VertexResult(false);
73         }
74         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
75         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
76         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
77
78         for (Map.Entry<String, MapGroupsDataDefinition> pair : Optional.ofNullable(instGroups)
79                 .orElse(Collections.emptyMap()).entrySet()) {
80             MapGroupsDataDefinition groups = pair.getValue();
81             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
82                 return new VertexResult(
83                     findCoordinateModuleJson(report, pair, instDeploymentArtifacts, vertex, reportFile));
84             }
85         }
86         return new VertexResult(true);
87     }
88
89     private boolean findCoordinateModuleJson(
90         Report report,
91         Map.Entry<String, MapGroupsDataDefinition> pair,
92         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts,
93         GraphVertex vertex,
94         ReportFile.TXTFile reportFile
95     ) {
96         String groupKey = pair.getKey();
97         String[] split = groupKey.split("\\.");
98         String instanceName = split[split.length - 1];
99         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
100         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
101             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values()
102                 .stream().filter(artifact -> {
103                     String artifactName = artifact.getArtifactName();
104                     return artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json");
105                 }).collect(Collectors.toList());
106             if (moduleJsonArtifacts.size() > 0) {
107                 String status =
108                     "Instance " + instanceName + " has a corresponding modules.json file: " + moduleJsonArtifacts.get(0)
109                         .getArtifactName();
110                 reportFile.writeReportLineToFile(status);
111                 return true;
112             }
113         }
114         String status = "Instance " + instanceName + " doesn't have a corresponding modules.json file";
115         reportFile.writeReportLineToFile(status);
116         report.addFailure(getTaskName(), vertex.getUniqueId());
117         return false;
118     }
119
120     private boolean isAfterSubmitForTesting(GraphVertex vertex) {
121         List<String> allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.CERTIFIED.name()));
122         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
123     }
124 }