Added oparent to sdc main
[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  * ================================================================================
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     @Autowired
45     private ArtifactCassandraDao artifactCassandraDao;
46
47     @Autowired
48     private TopologyTemplateOperation topologyTemplateOperation;
49
50     public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) {
51         ArtifactsVertexResult result = new ArtifactsVertexResult(true);
52         for(ArtifactDataDefinition artifact:artifacts) {
53             boolean isArtifactExist = isArtifcatInCassandra(artifact.getEsId());
54             String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
55                     "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
56
57             ReportManager.writeReportLineToFile(status);
58             if (!isArtifactExist) {
59                 ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
60                 result.setStatus(false);
61                 result.addNotFoundArtifact(artifact.getUniqueId());
62             }
63         }
64         return result;
65     }
66
67     public boolean isArtifcatInCassandra(String uniueId) {
68         Either<Long, CassandraOperationStatus> countOfArtifactsEither =
69                 artifactCassandraDao.getCountOfArtifactById(uniueId);
70         if (countOfArtifactsEither.isRight()) {
71             // print to console
72             System.out.print("Failed to retrieve artifact with id: "+uniueId+" from Cassandra" );
73             return false;
74         }
75         Long count = countOfArtifactsEither.left().value();
76         if (count <1) {
77             return false;
78         }
79         return true;
80     }
81
82     public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
83         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
84         Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach( (key, dataDef) -> {
85             if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
86                 artifacts.add(dataDef);
87             }
88         });
89         return artifacts;
90     }
91
92     public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
93         ArtifactsVertexResult result = new ArtifactsVertexResult();
94         ComponentParametersView paramView = new ComponentParametersView();
95         paramView.disableAll();
96         paramView.setIgnoreArtifacts(false);
97         paramView.setIgnoreComponentInstances(false);
98         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
99         if (toscaElementEither.isRight()) {
100             result.setStatus(false);
101             return result;
102         }
103         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
104         Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
105         Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
106         Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
107         Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
108         Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();
109
110         List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();
111
112         allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
113         allArtifacts.addAll(addRelevantArtifacts(artifacts));
114         allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));
115
116         if (instanceArtifacts != null) {
117             instanceArtifacts.forEach((key, artifactMap) -> {
118                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
119             });
120         }
121
122         if (instanceDeploymentArtifacts != null) {
123             instanceDeploymentArtifacts.forEach((key, artifactMap) -> {
124                 allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition()));
125             });
126         }
127
128         return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);
129     }
130 }