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 java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.Optional;
30 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
31 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
32 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
33 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
34 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
36 import org.openecomp.sdc.be.model.ComponentParametersView;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42 import org.springframework.beans.factory.annotation.Autowired;
44 import fj.data.Either;
47 * Created by chaya on 7/6/2017.
49 public class ArtifactValidationUtils {
51 private static final Logger logger = Logger.getLogger(ArtifactValidationUtils.class);
53 private ArtifactCassandraDao artifactCassandraDao;
55 private TopologyTemplateOperation topologyTemplateOperation;
58 public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
59 TopologyTemplateOperation topologyTemplateOperation) {
60 this.artifactCassandraDao = artifactCassandraDao;
61 this.topologyTemplateOperation = topologyTemplateOperation;
64 public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) {
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);
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 ArtifactsVertexResult result = new ArtifactsVertexResult();
103 ComponentParametersView paramView = new ComponentParametersView();
104 paramView.disableAll();
105 paramView.setIgnoreArtifacts(false);
106 paramView.setIgnoreComponentInstances(false);
107 Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
108 if (toscaElementEither.isRight()) {
109 result.setStatus(false);
112 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
113 Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
114 Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
115 Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
116 Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
117 Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
119 List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
121 allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
122 allArtifacts.addAll(addRelevantArtifacts(artifacts));
123 allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
125 if (instanceArtifacts != null) {
126 instanceArtifacts.forEach((key, artifactMap) ->
127 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
130 if (instanceDeploymentArtifacts != null) {
131 instanceDeploymentArtifacts.forEach((key, artifactMap) ->
132 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
135 return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);