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, List<ArtifactDataDefinition> artifacts) {
64 ArtifactsVertexResult result = new ArtifactsVertexResult(true);
65 for(ArtifactDataDefinition artifact:artifacts) {
66 boolean isArtifactExist = isArtifactInCassandra(artifact.getEsId());
67 String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
68 "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
69 ReportManager.writeReportLineToFile(status);
70 if (!isArtifactExist) {
71 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
72 result.setStatus(false);
73 result.addNotFoundArtifact(artifact.getUniqueId());
79 public boolean isArtifactInCassandra(String uniqueId) {
80 Either<Long, CassandraOperationStatus> countOfArtifactsEither =
81 artifactCassandraDao.getCountOfArtifactById(uniqueId);
82 if (countOfArtifactsEither.isRight()) {
83 logger.debug("Failed to retrieve artifact with id: {} from Cassandra", uniqueId);
86 Long count = countOfArtifactsEither.left().value();
90 public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
91 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
92 Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach((key, dataDef) -> {
93 if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
94 artifacts.add(dataDef);
100 public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
101 ArtifactsVertexResult result = new ArtifactsVertexResult();
102 ComponentParametersView paramView = new ComponentParametersView();
103 paramView.disableAll();
104 paramView.setIgnoreArtifacts(false);
105 paramView.setIgnoreComponentInstances(false);
106 Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
107 if (toscaElementEither.isRight()) {
108 result.setStatus(false);
111 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
112 Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
113 Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
114 Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
115 Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
116 Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
118 List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
120 allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
121 allArtifacts.addAll(addRelevantArtifacts(artifacts));
122 allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
124 if (instanceArtifacts != null) {
125 instanceArtifacts.forEach((key, artifactMap) ->
126 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
129 if (instanceDeploymentArtifacts != null) {
130 instanceDeploymentArtifacts.forEach((key, artifactMap) ->
131 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
134 return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);