aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/tasks/artifacts/ArtifactValidationUtils.java
blob: 11c80eae70b78f1f051ee0e11b779f0910fc3a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts;

import fj.data.Either;
import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by chaya on 7/6/2017.
 */
public class ArtifactValidationUtils {

    private static final Logger logger = Logger.getLogger(ArtifactValidationUtils.class);

    private ArtifactCassandraDao artifactCassandraDao;

    private TopologyTemplateOperation topologyTemplateOperation;

    @Autowired
    public ArtifactValidationUtils(ArtifactCassandraDao artifactCassandraDao,
        TopologyTemplateOperation topologyTemplateOperation) {
        this.artifactCassandraDao = artifactCassandraDao;
        this.topologyTemplateOperation = topologyTemplateOperation;
    }

    public ArtifactsVertexResult validateArtifactsAreInCassandra(GraphVertex vertex, String taskName, List<ArtifactDataDefinition> artifacts) {
        ArtifactsVertexResult result = new ArtifactsVertexResult(true);
        for(ArtifactDataDefinition artifact:artifacts) {
            boolean isArtifactExist = isArtifactInCassandra(artifact.getEsId());
            String status = isArtifactExist ? "Artifact " + artifact.getEsId() + " is in Cassandra" :
                    "Artifact " + artifact.getEsId() + " doesn't exist in Cassandra";
            ReportManager.writeReportLineToFile(status);
            if (!isArtifactExist) {
                ReportManager.addFailedVertex(taskName, vertex.getUniqueId());
                result.setStatus(false);
                result.addNotFoundArtifact(artifact.getUniqueId());
            }
        }
        return result;
    }

    public boolean isArtifactInCassandra(String uniqueId) {
        Either<Long, CassandraOperationStatus> countOfArtifactsEither =
            artifactCassandraDao.getCountOfArtifactById(uniqueId);
        if (countOfArtifactsEither.isRight()) {
            logger.debug("Failed to retrieve artifact with id: {} from Cassandra", uniqueId);
            return false;
        }
        Long count = countOfArtifactsEither.left().value();
        return count >= 1;
    }

    public List<ArtifactDataDefinition> addRelevantArtifacts(Map<String, ArtifactDataDefinition> artifactsMap) {
        List<ArtifactDataDefinition> artifacts = new ArrayList<>();
        Optional.ofNullable(artifactsMap).orElse(Collections.emptyMap()).forEach((key, dataDef) -> {
            if (dataDef.getEsId() != null && !dataDef.getEsId().isEmpty()) {
                artifacts.add(dataDef);
            }
        });
        return artifacts;
    }

    public ArtifactsVertexResult validateTopologyTemplateArtifacts(GraphVertex vertex, String taskName) {
        ArtifactsVertexResult result = new ArtifactsVertexResult();
        ComponentParametersView paramView = new ComponentParametersView();
        paramView.disableAll();
        paramView.setIgnoreArtifacts(false);
        paramView.setIgnoreComponentInstances(false);
        Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
        if (toscaElementEither.isRight()) {
            result.setStatus(false);
            return result;
        }
        TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
        Map<String, ArtifactDataDefinition> deploymentArtifacts = element.getDeploymentArtifacts();
        Map<String, ArtifactDataDefinition> artifacts = element.getArtifacts();
        Map<String, ArtifactDataDefinition> apiArtifacts = element.getServiceApiArtifacts();
        Map<String, MapArtifactDataDefinition> instanceArtifacts = element.getInstanceArtifacts();
        Map<String, MapArtifactDataDefinition> instanceDeploymentArtifacts = element.getInstDeploymentArtifacts();

        List<ArtifactDataDefinition> allArtifacts = new ArrayList<>();

        allArtifacts.addAll(addRelevantArtifacts(deploymentArtifacts));
        allArtifacts.addAll(addRelevantArtifacts(artifacts));
        allArtifacts.addAll(addRelevantArtifacts(apiArtifacts));

        if (instanceArtifacts != null) {
            instanceArtifacts.forEach((key, artifactMap) ->
                    allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
        }

        if (instanceDeploymentArtifacts != null) {
            instanceDeploymentArtifacts.forEach((key, artifactMap) ->
                    allArtifacts.addAll(addRelevantArtifacts(artifactMap.getMapToscaDataDefinition())));
        }

        return validateArtifactsAreInCassandra(vertex, taskName, allArtifacts);
    }
}