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