aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/titan/HealingTitanGenericDao.java
blob: 37546a5c2ae7ca76f7049d429200dda6ed216f0c (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
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * 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.
 */
package org.openecomp.sdc.be.dao.titan;

import com.thinkaurelius.titan.core.TitanVertex;
import fj.data.Either;
import java.util.List;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
import org.openecomp.sdc.be.dao.jsongraph.heal.Heal;
import org.openecomp.sdc.be.dao.jsongraph.heal.HealConstants;
import org.openecomp.sdc.be.dao.jsongraph.heal.HealVersion;
import org.openecomp.sdc.be.dao.jsongraph.heal.HealVersionBuilder;
import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("titan-generic-dao")
public class HealingTitanGenericDao extends TitanGenericDao {

    @Autowired
    private HealingPipelineDao healingPipelineDao;

    public HealingTitanGenericDao(TitanGraphClient titanClient) {
        super(titanClient);
    }

    @Override
    public ImmutablePair<TitanVertex, Edge> getChildVertex(TitanVertex childVertex, GraphEdgeLabels edgeType) {
        ImmutablePair<TitanVertex, Edge> childVertexEdgeImmutablePair = super.getChildVertex(childVertex, edgeType);
        final TitanVertex graphVertex = childVertexEdgeImmutablePair.left;
        healingPipelineDao.performGraphReadHealing(graphVertex, edgeType);
        healingPipelineDao.setHealingVersion(graphVertex);
        return childVertexEdgeImmutablePair;
    }

    @Override
    public <T extends GraphNode> Either<List<ImmutablePair<T, GraphEdge>>, TitanOperationStatus> getChildrenNodes(String key, String uniqueId, GraphEdgeLabels edgeType, NodeTypeEnum nodeTypeEnum, Class<T> clazz, boolean withEdges) {
        Either<List<ImmutablePair<T, GraphEdge>>, TitanOperationStatus> either = super.getChildrenNodes(key, uniqueId, edgeType, nodeTypeEnum, clazz, withEdges);
        if (either.isRight()) {
            return either;
        }
        List<ImmutablePair<T, GraphEdge>> list = either.left().value();
        list.forEach(this::transformPair);
        return either;
    }

    @Override
    public <T extends GraphNode> Either<ImmutablePair<T, GraphEdge>, TitanOperationStatus> getChild(String key, String uniqueId, GraphEdgeLabels edgeType, NodeTypeEnum nodeTypeEnum, Class<T> clazz) {
        Either<ImmutablePair<T, GraphEdge>, TitanOperationStatus> eitherChild = super.getChild(key, uniqueId, edgeType, nodeTypeEnum, clazz);
        if (eitherChild.isRight()) {
            return eitherChild;
        }
        ImmutablePair<T, GraphEdge> pair = eitherChild.left().value();
        GraphNode graphNode = pair.left;
        GraphEdge graphEdge = pair.right;
        healingPipelineDao.performGraphReadHealing(graphNode, graphEdge);
        healingPipelineDao.setHealingVersion(graphNode);
        return eitherChild;
    }

    private <T extends GraphNode> void transformPair(ImmutablePair<T, GraphEdge> either) {
        GraphEdge edgeType = either.right;
        GraphNode childVertex = either.left;
        Integer healingVersioInt = childVertex.getHealingVersion();
        HealVersionBuilder.build(healingVersioInt);
        healingPipelineDao.performGraphReadHealing(childVertex, edgeType);
        healingPipelineDao.setHealingVersion(childVertex);
    }

    @Override
    public Either<List<ImmutablePair<TitanVertex, Edge>>, TitanOperationStatus> getChildrenVertecies(String key, String uniqueId, GraphEdgeLabels edgeType) {
        Either<List<ImmutablePair<TitanVertex, Edge>>, TitanOperationStatus> either = super.getChildrenVertecies(key, uniqueId, edgeType);
        if (either.isRight()) {
            return either;
        }
        List<ImmutablePair<TitanVertex, Edge>> list = either.left().value();
        list.forEach(this::transformVertexPair);
        return either;
    }

    private void transformVertexPair(ImmutablePair<TitanVertex, Edge> either) {
        String edgeType = either.right.label();
        TitanVertex childVertex = either.left;
        VertexProperty<Integer> healingVersionProperty = childVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty());
        Integer healingVersioInt = healingVersionProperty.orElse(HealConstants.DEFAULT_HEAL_VERSION);
        HealVersionBuilder.build(healingVersioInt);
        healingPipelineDao.performGraphReadHealing(childVertex, edgeType);
        healingPipelineDao.setHealingVersion(childVertex);
    }

    @Override
    public <T extends GraphNode> Either<T, TitanOperationStatus> updateNode(GraphNode node, Class<T> clazz) {
        healingPipelineDao.setHealingVersion(node);
        return super.updateNode(node, clazz);
    }

    @Override
    public TitanOperationStatus updateVertex(GraphNode node, Vertex vertex) {
        healingPipelineDao.setHealingVersion(node);
        return super.updateVertex(node, vertex);
    }


    public void setHealingPipelineDao(HealingPipelineDao healingPipelineDao) {
        this.healingPipelineDao = healingPipelineDao;
    }
}