58c2ef031b66cbf3cdc1cc6355770fccfe7afd99
[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.tasks.ServiceValidationTask;
33 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
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 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, String outputFilePath) {
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, outputFilePath));
84             }
85         }
86         return new VertexResult(true);
87     }
88
89     private boolean findCoordinateModuleJson(Report report, Map.Entry<String, MapGroupsDataDefinition> pair,
90                                              Map<String, MapArtifactDataDefinition> instDeploymentArtifacts,
91                                              GraphVertex vertex, String outputFilePath) {
92         String groupKey = pair.getKey();
93         String[] split = groupKey.split("\\.");
94         String instanceName = split[split.length - 1];
95         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
96         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
97             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values()
98                     .stream().filter(artifact -> {
99                         String artifactName = artifact.getArtifactName();
100                         return (artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json"));
101                     }).collect(Collectors.toList());
102             if (!moduleJsonArtifacts.isEmpty()) {
103                 String status = "Instance " + instanceName + " has a corresponding modules.json file: "
104                         + moduleJsonArtifacts.get(0)
105                         .getArtifactName();
106                 ReportManager.writeReportLineToFile(status, outputFilePath);
107                 return true;
108             }
109         }
110         String status = "Instance " + instanceName + " doesn't have a corresponding modules.json file";
111         ReportManager.writeReportLineToFile(status, outputFilePath);
112         report.addFailure(getTaskName(), vertex.getUniqueId());
113         return false;
114     }
115
116     private boolean isAfterSubmitForTesting(GraphVertex vertex) {
117         List<String> allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.CERTIFIED.name()));
118         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
119     }
120 }