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 {
44 private ArtifactCassandraDao artifactCassandraDao;
46 private TopologyTemplateOperation topologyTemplateOperation;
49 public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
50 TopologyTemplateOperation topologyTemplateOperation) {
51 this.artifactCassandraDao = artifactCassandraDao;
52 this.topologyTemplateOperation = topologyTemplateOperation;
55 public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) {
56 ArtifactsVertexResult result = new ArtifactsVertexResult(true);
57 for(ArtifactDataDefinition artifact:artifacts) {
58 boolean isArtifactExist = isArtifcatInCassandra(artifact.getEsId());
59 String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
60 "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
62 ReportManager.writeReportLineToFile(status);
63 if (!isArtifactExist) {
64 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
65 result.setStatus(false);
66 result.addNotFoundArtifact(artifact.getUniqueId());
72 public boolean isArtifcatInCassandra(String uniueId) {
73 Either<Long, CassandraOperationStatus> countOfArtifactsEither =
74 artifactCassandraDao.getCountOfArtifactById(uniueId);
75 if (countOfArtifactsEither.isRight()) {
77 System.out.print("Failed to retrieve artifact with id: "+uniueId+" from Cassandra" );
80 Long count = countOfArtifactsEither.left().value();
87 public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
88 List<ArtifactDataDefinition> artifacts = new ArrayList<>();
89 Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach( (key, dataDef) -> {
90 if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
91 artifacts.add(dataDef);
97 public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
98 ArtifactsVertexResult result = new ArtifactsVertexResult();
99 ComponentParametersView paramView = new ComponentParametersView();
100 paramView.disableAll();
101 paramView.setIgnoreArtifacts(false);
102 paramView.setIgnoreComponentInstances(false);
103 Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
104 if (toscaElementEither.isRight()) {
105 result.setStatus(false);
108 TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
109 Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
110 Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
111 Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
112 Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
113 Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
115 List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
117 allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
118 allArtifacts.addAll(addRelevantArtifacts(artifacts));
119 allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
121 if (instanceArtifacts != null) {
122 instanceArtifacts.forEach((key, artifactMap) -> {
123 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
127 if (instanceDeploymentArtifacts != null) {
128 instanceDeploymentArtifacts.forEach((key, artifactMap) -> {
129 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
133 return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);