Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / validator / tasks / artifacts / ArtifactValidationUtils.java
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, List<ArtifactDataDefinition> artifacts) {
64         ArtifactsVertexResult result = new ArtifactsVertexResult(true);
65         for(ArtifactDataDefinition artifact:artifacts) {
66             boolean isArtifactExist = isArtifactInCassandra(artifact.getEsId());
67             String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
68                     "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
69             ReportManager.writeReportLineToFile(status);
70             if (!isArtifactExist) {
71                 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
72                 result.setStatus(false);
73                 result.addNotFoundArtifact(artifact.getUniqueId());
74             }
75         }
76         return result;
77     }
78
79     public boolean isArtifactInCassandra(String uniqueId) {
80         Either<Long, CassandraOperationStatus> countOfArtifactsEither =
81             artifactCassandraDao.getCountOfArtifactById(uniqueId);
82         if (countOfArtifactsEither.isRight()) {
83             logger.debug("Failed to retrieve artifact with id: {} from Cassandra", uniqueId);
84             return false;
85         }
86         Long count = countOfArtifactsEither.left().value();
87         return count >= 1;
88     }
89
90     public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
91         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
92         Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach((key, dataDef) -> {
93             if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
94                 artifacts.add(dataDef);
95             }
96         });
97         return artifacts;
98     }
99
100     public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
101         ArtifactsVertexResult result = new ArtifactsVertexResult();
102         ComponentParametersView paramView = new ComponentParametersView();
103         paramView.disableAll();
104         paramView.setIgnoreArtifacts(false);
105         paramView.setIgnoreComponentInstances(false);
106         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
107         if (toscaElementEither.isRight()) {
108             result.setStatus(false);
109             return result;
110         }
111         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
112         Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
113         Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
114         Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
115         Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
116         Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
117
118         List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
119
120         allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
121         allArtifacts.addAll(addRelevantArtifacts(artifacts));
122         allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
123
124         if (instanceArtifacts != null) {
125             instanceArtifacts.forEach((key, artifactMap) ->
126                     allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
127         }
128
129         if (instanceDeploymentArtifacts != null) {
130             instanceDeploymentArtifacts.forEach((key, artifactMap) ->
131                     allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
132         }
133
134         return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);
135     }
136 }