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