025e0b61e1e7085fe59fd28e4329dc0f75d63d27
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
25 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
26 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
30 import org.openecomp.sdc.be.model.ComponentParametersView;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.springframework.beans.factory.annotation.Autowired;
36
37 import java.util.*;
38
39 /**
40  * Created by chaya on 7/6/2017.
41  */
42 public class ArtifactValidationUtils {
43
44     private ArtifactCassandraDao artifactCassandraDao;
45
46     private TopologyTemplateOperation topologyTemplateOperation;
47
48     @Autowired
49     public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
50         TopologyTemplateOperation topologyTemplateOperation) {
51         this.artifactCassandraDao = artifactCassandraDao;
52         this.topologyTemplateOperation = topologyTemplateOperation;
53     }
54
55     public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) {
56         ArtifactsVertexResult result = new ArtifactsVertexResult(true);
57         for(ArtifactDataDefinition artifact:artifacts) {
58             boolean isArtifactExist = isArtifcatInCassandra(artifact.getEsId());
59             String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
60                     "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
61
62             ReportManager.writeReportLineToFile(status);
63             if (!isArtifactExist) {
64                 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
65                 result.setStatus(false);
66                 result.addNotFoundArtifact(artifact.getUniqueId());
67             }
68         }
69         return result;
70     }
71
72     public boolean isArtifcatInCassandra(String uniueId) {
73         Either<Long, CassandraOperationStatus> countOfArtifactsEither =
74                 artifactCassandraDao.getCountOfArtifactById(uniueId);
75         if (countOfArtifactsEither.isRight()) {
76             // print to console
77             System.out.print("Failed to retrieve artifact with id: "+uniueId+" from Cassandra" );
78             return false;
79         }
80         Long count = countOfArtifactsEither.left().value();
81         if (count <1) {
82             return false;
83         }
84         return true;
85     }
86
87     public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
88         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
89         Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach( (key, dataDef) -> {
90             if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
91                 artifacts.add(dataDef);
92             }
93         });
94         return artifacts;
95     }
96
97     public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
98         ArtifactsVertexResult result = new ArtifactsVertexResult();
99         ComponentParametersView paramView = new ComponentParametersView();
100         paramView.disableAll();
101         paramView.setIgnoreArtifacts(false);
102         paramView.setIgnoreComponentInstances(false);
103         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
104         if (toscaElementEither.isRight()) {
105             result.setStatus(false);
106             return result;
107         }
108         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
109         Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
110         Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
111         Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
112         Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
113         Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
114
115         List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
116
117         allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
118         allArtifacts.addAll(addRelevantArtifacts(artifacts));
119         allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
120
121         if (instanceArtifacts != null) {
122             instanceArtifacts.forEach((key, artifactMap) -> {
123                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
124             });
125         }
126
127         if (instanceDeploymentArtifacts != null) {
128             instanceDeploymentArtifacts.forEach((key, artifactMap) -> {
129                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
130             });
131         }
132
133         return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);
134     }
135 }