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.moduleJson;
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;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.List;
46 import java.util.Optional;
47 import java.util.stream.Collectors;
49 public class ModuleJsonTask extends ServiceValidationTask {
51 private TopologyTemplateOperation topologyTemplateOperation;
54 public ModuleJsonTask(TopologyTemplateOperation topologyTemplateOperation) {
55 this.topologyTemplateOperation = topologyTemplateOperation;
56 this.name = "Service Module json Validation Task";
60 public VertexResult validate(Report report, GraphVertex vertex, String outputFilePath) {
61 if (!isAfterSubmitForTesting(vertex)) {
62 return new VertexResult(true);
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);
75 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
76 Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
77 Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
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));
86 return new VertexResult(true);
88 return new VertexResult(true);
91 private boolean findCoordinateModuleJson(
93 Map.Entry<String, MapGroupsDataDefinition> pair,
94 Map<String, MapArtifactDataDefinition> instDeploymentArtifacts,
95 GraphVertex vertex, String outputFilePath
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")) {
109 }).collect(Collectors.toList());
110 if (moduleJsonArtifacts.size() > 0) {
112 "Instance " + instanceName + " has a corresponding modules.json file: " + moduleJsonArtifacts.get(0)
114 ReportManager.writeReportLineToFile(status, outputFilePath);
118 String status = "Instance " + instanceName + " doesn't have a corresponding modules.json file";
119 ReportManager.writeReportLineToFile(status, outputFilePath);
120 report.addFailure(getTaskName(), vertex.getUniqueId());
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));