bf3d267ab9089549f019e0e94ac751401e394156
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts;
23
24 import fj.data.Either;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import org.openecomp.sdc.asdctool.impl.validator.report.Report;
31 import org.openecomp.sdc.asdctool.impl.validator.report.ReportFile;
32 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
33 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
37 import org.openecomp.sdc.be.model.ComponentParametersView;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.beans.factory.annotation.Autowired;
44
45 public class ArtifactValidationUtils {
46
47     private static final Logger logger = Logger.getLogger(ArtifactValidationUtils.class);
48
49     private final ArtifactCassandraDao artifactCassandraDao;
50
51     private final TopologyTemplateOperation topologyTemplateOperation;
52
53     @Autowired
54     public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
55         TopologyTemplateOperation topologyTemplateOperation) {
56         this.artifactCassandraDao = artifactCassandraDao;
57         this.topologyTemplateOperation = topologyTemplateOperation;
58     }
59
60     public ArtifactsVertexResult validateArtifactsAreInCassandra(
61         Report report,
62         GraphVertex vertex,
63         String taskName,
64         List<ArtifactDataDefinition> artifacts,
65         ReportFile.TXTFile reportFile
66     ) {
67         ArtifactsVertexResult result = new ArtifactsVertexResult(true);
68         for (ArtifactDataDefinition artifact : artifacts) {
69             boolean isArtifactExist = isArtifactInCassandra(artifact.getEsId());
70             String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
71                 "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
72             reportFile.writeReportLineToFile(status);
73             if (!isArtifactExist) {
74                 report.addFailure(taskName, vertex.getUniqueId());
75                 result.setStatus(false);
76                 result.addNotFoundArtifact(artifact.getUniqueId());
77             }
78         }
79         return result;
80     }
81
82     public boolean isArtifactInCassandra(String uniqueId) {
83         Either<Long, CassandraOperationStatus> countOfArtifactsEither =
84             artifactCassandraDao.getCountOfArtifactById(uniqueId);
85         if (countOfArtifactsEither.isRight()) {
86             logger.debug("Failed to retrieve artifact with id: {} from Cassandra", uniqueId);
87             return false;
88         }
89         Long count = countOfArtifactsEither.left().value();
90         return count >= 1;
91     }
92
93     public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
94         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
95         Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach((key, dataDef) -> {
96             if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
97                 artifacts.add(dataDef);
98             }
99         });
100         return artifacts;
101     }
102
103     public ArtifactsVertexResult validateTopologyTemplateArtifacts(
104         Report report,
105         GraphVertex vertex,
106         String taskName,
107         ReportFile.TXTFile reportFile
108     ) {
109         ArtifactsVertexResult result = new ArtifactsVertexResult();
110         ComponentParametersView paramView = new ComponentParametersView();
111         paramView.disableAll();
112         paramView.setIgnoreArtifacts(false);
113         paramView.setIgnoreComponentInstances(false);
114         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation
115             .getToscaElement(vertex.getUniqueId(), paramView);
116         if (toscaElementEither.isRight()) {
117             result.setStatus(false);
118             return result;
119         }
120         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
121         Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
122         Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
123         Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
124         Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
125         Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
126
127         List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
128
129         allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
130         allArtifacts.addAll(addRelevantArtifacts(artifacts));
131         allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
132
133         if (instanceArtifacts != null) {
134             instanceArtifacts.forEach((key, artifactMap) ->
135                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
136         }
137
138         if (instanceDeploymentArtifacts != null) {
139             instanceDeploymentArtifacts.forEach((key, artifactMap) ->
140                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
141         }
142
143         return validateArtifactsAreInCassandra(report, vertex, taskName, allArtifacts, reportFile);
144     }
145 }