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