2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (c) 2019 Samsung
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts;
24 import fj.data.Either;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.Optional;
30 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
31 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile;
32 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
33 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
37 import org.openecomp.sdc.be.model.ComponentParametersView;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.beans.factory.annotation.Autowired;
45 public class ArtifactValidationUtils {
47 private static final Logger logger = Logger.getLogger(ArtifactValidationUtils.class);
49 private final ArtifactCassandraDao artifactCassandraDao;
51 private final TopologyTemplateOperation topologyTemplateOperation;
54 public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
55 TopologyTemplateOperation topologyTemplateOperation) {
56 this.artifactCassandraDao = artifactCassandraDao;
57 this.topologyTemplateOperation = topologyTemplateOperation;
60 public ArtifactsVertexResult validateArtifactsAreInCassandra(
64 List<ArtifactDataDefinition> artifacts,
65 ReportFile.TXTFile reportFile
67 ArtifactsVertexResult result = new ArtifactsVertexResult(true);
68 for (ArtifactDataDefinition artifact : artifacts) {
69 boolean isArtifactExist = isArtifactInCassandra(artifact.getEsId());
70 String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
71 "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
72 reportFile.writeReportLineToFile(status);
73 if (!isArtifactExist) {
74 report.addFailure(taskName, vertex.getUniqueId());
75 result.setStatus(false);
76 result.addNotFoundArtifact(artifact.getUniqueId());
82 public boolean isArtifactInCassandra(String uniqueId) {
83 Either<Long, CassandraOperationStatus> countOfArtifactsEither =
84 artifactCassandraDao.getCountOfArtifactById(uniqueId);
85 if (countOfArtifactsEither.isRight()) {
86 logger.debug("Failed to retrieve artifact with id: {} from Cassandra", uniqueId);
89 Long count = countOfArtifactsEither.left().value();
93 public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
94 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
95 Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach((key, dataDef) -> {
96 if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
97 artifacts.add(dataDef);
103 public ArtifactsVertexResult validateTopologyTemplateArtifacts(
107 ReportFile.TXTFile reportFile
109 ArtifactsVertexResult result = new ArtifactsVertexResult();
110 ComponentParametersView paramView = new ComponentParametersView();
111 paramView.disableAll();
112 paramView.setIgnoreArtifacts(false);
113 paramView.setIgnoreComponentInstances(false);
114 Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation
115 .getToscaElement(vertex.getUniqueId(), paramView);
116 if (toscaElementEither.isRight()) {
117 result.setStatus(false);
120 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
121 Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
122 Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
123 Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
124 Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
125 Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
127 List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
129 allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
130 allArtifacts.addAll(addRelevantArtifacts(artifacts));
131 allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
133 if (instanceArtifacts != null) {
134 instanceArtifacts.forEach((key, artifactMap) ->
135 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
138 if (instanceDeploymentArtifacts != null) {
139 instanceDeploymentArtifacts.forEach((key, artifactMap) ->
140 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
143 return validateArtifactsAreInCassandra(report, vertex, taskName, allArtifacts, reportFile);