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.artifacts;
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
25 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
26 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
30 import org.openecomp.sdc.be.model.ComponentParametersView;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.springframework.beans.factory.annotation.Autowired;
40 * Created by chaya on 7/6/2017.
42 public class ArtifactValidationUtils {
45 private ArtifactCassandraDao artifactCassandraDao;
48 private TopologyTemplateOperation topologyTemplateOperation;
50 public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) {
51 ArtifactsVertexResult result = new ArtifactsVertexResult(true);
52 for(ArtifactDataDefinition artifact:artifacts) {
53 boolean isArtifactExist = isArtifcatInCassandra(artifact.getEsId());
54 String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
55 "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
57 ReportManager.writeReportLineToFile(status);
58 if (!isArtifactExist) {
59 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
60 result.setStatus(false);
61 result.addNotFoundArtifact(artifact.getUniqueId());
67 public boolean isArtifcatInCassandra(String uniueId) {
68 Either<Long, CassandraOperationStatus> countOfArtifactsEither =
69 artifactCassandraDao.getCountOfArtifactById(uniueId);
70 if (countOfArtifactsEither.isRight()) {
72 System.out.print("Failed to retrieve artifact with id: "+uniueId+" from Cassandra" );
75 Long count = countOfArtifactsEither.left().value();
82 public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
83 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
84 Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach( (key, dataDef) -> {
85 if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
86 artifacts.add(dataDef);
92 public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
93 ArtifactsVertexResult result = new ArtifactsVertexResult();
94 ComponentParametersView paramView = new ComponentParametersView();
95 paramView.disableAll();
96 paramView.setIgnoreArtifacts(false);
97 paramView.setIgnoreComponentInstances(false);
98 Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
99 if (toscaElementEither.isRight()) {
100 result.setStatus(false);
103 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
104 Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
105 Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
106 Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
107 Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
108 Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
110 List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
112 allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
113 allArtifacts.addAll(addRelevantArtifacts(artifacts));
114 allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
116 if (instanceArtifacts != null) {
117 instanceArtifacts.forEach((key, artifactMap) -> {
118 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
122 if (instanceDeploymentArtifacts != null) {
123 instanceDeploymentArtifacts.forEach((key, artifactMap) -> {
124 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
128 return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);