6da79cf681c98b59ca3e6f353c8346115ee6240e
[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.utils.ReportManager;
26 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
27 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
31 import org.openecomp.sdc.be.model.ComponentParametersView;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.springframework.beans.factory.annotation.Autowired;
38
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Optional;
44
45 /**
46  * Created by chaya on 7/6/2017.
47  */
48 public class ArtifactValidationUtils {
49
50     private static final Logger logger = Logger.getLogger(ArtifactValidationUtils.class);
51
52     private ArtifactCassandraDao artifactCassandraDao;
53
54     private TopologyTemplateOperation topologyTemplateOperation;
55
56     @Autowired
57     public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
58         TopologyTemplateOperation topologyTemplateOperation) {
59         this.artifactCassandraDao = artifactCassandraDao;
60         this.topologyTemplateOperation = topologyTemplateOperation;
61     }
62
63     public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName,
64         List<ArtifactDataDefinition> artifacts, String outputFilePath) {
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, outputFilePath);
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         String outputFilePath) {
103         ArtifactsVertexResult result = new ArtifactsVertexResult();
104         ComponentParametersView paramView = new ComponentParametersView();
105         paramView.disableAll();
106         paramView.setIgnoreArtifacts(false);
107         paramView.setIgnoreComponentInstances(false);
108         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation
109             .getToscaElement(vertex.getUniqueId(), paramView);
110         if (toscaElementEither.isRight()) {
111             result.setStatus(false);
112             return result;
113         }
114         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
115         Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
116         Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
117         Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
118         Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
119         Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
120
121         List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
122
123         allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
124         allArtifacts.addAll(addRelevantArtifacts(artifacts));
125         allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
126
127         if (instanceArtifacts != null) {
128             instanceArtifacts.forEach((key, artifactMap) ->
129                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
130         }
131
132         if (instanceDeploymentArtifacts != null) {
133             instanceDeploymentArtifacts.forEach((key, artifactMap) ->
134                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
135         }
136
137         return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts, outputFilePath);
138     }
139 }