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 org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
26 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
27 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
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.model.ComponentParametersView;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.springframework.beans.factory.annotation.Autowired;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
43 import java.util.Optional;
46 * Created by chaya on 7/6/2017.
48 public class ArtifactValidationUtils {
50 private static final Logger logger = Logger.getLogger(ArtifactValidationUtils.class);
52 private ArtifactCassandraDao artifactCassandraDao;
54 private TopologyTemplateOperation topologyTemplateOperation;
57 public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
58 TopologyTemplateOperation topologyTemplateOperation) {
59 this.artifactCassandraDao = artifactCassandraDao;
60 this.topologyTemplateOperation = topologyTemplateOperation;
63 public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName,
64 List<ArtifactDataDefinition> artifacts, String outputFilePath) {
65 ArtifactsVertexResult result = new ArtifactsVertexResult(true);
66 for (ArtifactDataDefinition artifact : artifacts) {
67 boolean isArtifactExist = isArtifactInCassandra(artifact.getEsId());
68 String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
69 "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
70 ReportManager.writeReportLineToFile(status, outputFilePath);
71 if (!isArtifactExist) {
72 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
73 result.setStatus(false);
74 result.addNotFoundArtifact(artifact.getUniqueId());
80 public boolean isArtifactInCassandra(String uniqueId) {
81 Either<Long, CassandraOperationStatus> countOfArtifactsEither =
82 artifactCassandraDao.getCountOfArtifactById(uniqueId);
83 if (countOfArtifactsEither.isRight()) {
84 logger.debug("Failed to retrieve artifact with id: {} from Cassandra", uniqueId);
87 Long count = countOfArtifactsEither.left().value();
91 public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
92 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
93 Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach((key, dataDef) -> {
94 if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
95 artifacts.add(dataDef);
101 public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName,
102 String outputFilePath) {
103 ArtifactsVertexResult result = new ArtifactsVertexResult();
104 ComponentParametersView paramView = new ComponentParametersView();
105 paramView.disableAll();
106 paramView.setIgnoreArtifacts(false);
107 paramView.setIgnoreComponentInstances(false);
108 Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation
109 .getToscaElement(vertex.getUniqueId(), paramView);
110 if (toscaElementEither.isRight()) {
111 result.setStatus(false);
114 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
115 Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
116 Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
117 Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
118 Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
119 Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
121 List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
123 allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
124 allArtifacts.addAll(addRelevantArtifacts(artifacts));
125 allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
127 if (instanceArtifacts != null) {
128 instanceArtifacts.forEach((key, artifactMap) ->
129 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
132 if (instanceDeploymentArtifacts != null) {
133 instanceDeploymentArtifacts.forEach((key, artifactMap) ->
134 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
137 return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts, outputFilePath);