aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/path/beans/InMemoryTitanGraphClient.java
blob: 4e3923d2ea6d8d1d352c5dc0c90e385bbd504502 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package org.openecomp.sdc.be.components.path.beans;


import com.thinkaurelius.titan.core.InvalidElementException;
import com.thinkaurelius.titan.core.InvalidIDException;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.QueryException;
import com.thinkaurelius.titan.core.SchemaViolationException;
import com.thinkaurelius.titan.core.TitanConfigurationException;
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.schema.ConsistencyModifier;
import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.thinkaurelius.titan.core.util.TitanCleanup;
import com.thinkaurelius.titan.diskstorage.ResourceUnavailableException;
import com.thinkaurelius.titan.diskstorage.locking.PermanentLockingException;
import com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException;
import fj.data.Either;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.openecomp.sdc.be.dao.TitanClientStrategy;
import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
import org.openecomp.sdc.be.dao.titan.TitanGraphClient;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;

@Component("titan-client")
public class InMemoryTitanGraphClient extends TitanGraphClient {


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

    private static final String OK = "GOOD";

    public InMemoryTitanGraphClient() {
    }


    private TitanGraph graph;
    TitanClientStrategy titanClientStrategy;

    public InMemoryTitanGraphClient(TitanClientStrategy titanClientStrategy) {
        super();
        this.titanClientStrategy = titanClientStrategy;
        logger.info("** TitanGraphClient created");
    }

    @PostConstruct
    public TitanOperationStatus createGraph() {

        logger.info("** createGraph started **");
        graph = TitanFactory.build().set("storage.backend", "inmemory").open();
        createTitanSchema();

        logger.info("** in memory graph created");
        return TitanOperationStatus.OK;

    }


    public void cleanupGraph() {
        if (graph != null) {
            // graph.shutdown();
            graph.close();
            TitanCleanup.clear(graph);
        }
    }

    public TitanOperationStatus createGraph(String titanCfgFile) {
        logger.info("** open graph with {} started", titanCfgFile);
        try {
            logger.info("openGraph : try to load file {}", titanCfgFile);
            graph = TitanFactory.open(titanCfgFile);
            if (graph.isClosed()) {
                logger.error("titan graph was not initialized");
                return TitanOperationStatus.NOT_CREATED;
            }

        } catch (Exception e) {
            this.graph = null;
            logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile, e);
            return TitanOperationStatus.NOT_CONNECTED;
        }

        logger.info("** Titan graph created ");

        return TitanOperationStatus.OK;
    }


    public Either<TitanGraph, TitanOperationStatus> getGraph() {
        if (graph != null) {
            return Either.left(graph);
        } else {
            return Either.right(TitanOperationStatus.NOT_CREATED);
        }
    }

    public TitanOperationStatus commit() {
        if (graph != null) {
            try {
                graph.tx().commit();
                return TitanOperationStatus.OK;
            } catch (Exception e) {
                return handleTitanException(e);
            }
        } else {
            return TitanOperationStatus.NOT_CREATED;
        }
    }

    public TitanOperationStatus rollback() {
        if (graph != null) {
            try {
                // graph.rollback();
                graph.tx().rollback();
                return TitanOperationStatus.OK;
            } catch (Exception e) {
                return handleTitanException(e);
            }
        } else {
            return TitanOperationStatus.NOT_CREATED;
        }
    }

    public static TitanOperationStatus handleTitanException(Exception e) {
        if (e instanceof TitanConfigurationException) {
            return TitanOperationStatus.TITAN_CONFIGURATION;
        }
        if (e instanceof SchemaViolationException) {
            return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
        }
        if (e instanceof PermanentLockingException) {
            return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
        }
        if (e instanceof IDPoolExhaustedException) {
            return TitanOperationStatus.GENERAL_ERROR;
        }
        if (e instanceof InvalidElementException) {
            return TitanOperationStatus.INVALID_ELEMENT;
        }
        if (e instanceof InvalidIDException) {
            return TitanOperationStatus.INVALID_ID;
        }
        if (e instanceof QueryException) {
            return TitanOperationStatus.INVALID_QUERY;
        }
        if (e instanceof ResourceUnavailableException) {
            return TitanOperationStatus.RESOURCE_UNAVAILABLE;
        }
        if (e instanceof IllegalArgumentException) {
            // TODO check the error message??
            return TitanOperationStatus.ILLEGAL_ARGUMENT;
        }

        return TitanOperationStatus.GENERAL_ERROR;
    }

    public boolean getHealth() {
        return true;
    }

    private boolean isGraphOpen() {
        return true;
    }


    private static final String TITAN_HEALTH_CHECK_STR = "titanHealthCheck";


    private void createTitanSchema() {

        TitanManagement graphMgt = graph.openManagement();
        TitanGraphIndex index = null;
        for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
            PropertyKey propKey = null;
            if (!graphMgt.containsPropertyKey(prop.getProperty())) {
                Class<?> clazz = prop.getClazz();
                if (!ArrayList.class.getName().equals(clazz.getName()) && !HashMap.class.getName().equals(clazz.getName())) {
                    propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
                }
            } else {
                propKey = graphMgt.getPropertyKey(prop.getProperty());
            }
            if (prop.isIndexed()) {
                if (!graphMgt.containsGraphIndex(prop.getProperty())) {
                    if (prop.isUnique()) {
                        index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex();

                        graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
                        // only
                        // one
                        // name
                        // per
                        // vertex
                        graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
                        // name
                        // uniqueness
                        // in
                        // the
                        // graph

                    } else {
                        graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();
                    }
                }
            }
        }
        graphMgt.commit();
    }

}