2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.asdctool.impl.validator.tasks.module.json;
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;
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;
48 public class ModuleJsonTask extends ServiceValidationTask {
50 private TopologyTemplateOperation topologyTemplateOperation;
53 public ModuleJsonTask(TopologyTemplateOperation topologyTemplateOperation) {
54 this.topologyTemplateOperation = topologyTemplateOperation;
55 this.name = "Service Module json Validation Task";
59 public VertexResult validate(Report report, GraphVertex vertex, String outputFilePath) {
60 if (!isAfterSubmitForTesting(vertex)) {
61 return new VertexResult(true);
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);
74 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
75 Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
76 Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
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));
86 return new VertexResult(true);
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)
106 ReportManager.writeReportLineToFile(status, outputFilePath);
110 String status = "Instance " + instanceName + " doesn't have a corresponding modules.json file";
111 ReportManager.writeReportLineToFile(status, outputFilePath);
112 report.addFailure(getTaskName(), vertex.getUniqueId());
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));