Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / path / beans / InMemoryTitanGraphClient.java
1 package org.openecomp.sdc.be.components.path.beans;
2
3
4 import com.thinkaurelius.titan.core.InvalidElementException;
5 import com.thinkaurelius.titan.core.InvalidIDException;
6 import com.thinkaurelius.titan.core.PropertyKey;
7 import com.thinkaurelius.titan.core.QueryException;
8 import com.thinkaurelius.titan.core.SchemaViolationException;
9 import com.thinkaurelius.titan.core.TitanConfigurationException;
10 import com.thinkaurelius.titan.core.TitanFactory;
11 import com.thinkaurelius.titan.core.TitanGraph;
12 import com.thinkaurelius.titan.core.schema.ConsistencyModifier;
13 import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
14 import com.thinkaurelius.titan.core.schema.TitanManagement;
15 import com.thinkaurelius.titan.core.util.TitanCleanup;
16 import com.thinkaurelius.titan.diskstorage.ResourceUnavailableException;
17 import com.thinkaurelius.titan.diskstorage.locking.PermanentLockingException;
18 import com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException;
19 import fj.data.Either;
20 import org.apache.tinkerpop.gremlin.structure.Vertex;
21 import org.openecomp.sdc.be.dao.TitanClientStrategy;
22 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
23 import org.openecomp.sdc.be.dao.titan.TitanGraphClient;
24 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Component;
28
29 import javax.annotation.PostConstruct;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32
33 @Component("titan-client")
34 public class InMemoryTitanGraphClient extends TitanGraphClient {
35
36
37     private static final Logger logger = LoggerFactory.getLogger(InMemoryTitanGraphClient.class);
38
39     private static final String OK = "GOOD";
40
41     public InMemoryTitanGraphClient() {
42     }
43
44
45     private TitanGraph graph;
46     TitanClientStrategy titanClientStrategy;
47
48     public InMemoryTitanGraphClient(TitanClientStrategy titanClientStrategy) {
49         super();
50         this.titanClientStrategy = titanClientStrategy;
51         logger.info("** TitanGraphClient created");
52     }
53
54     @PostConstruct
55     public TitanOperationStatus createGraph() {
56
57         logger.info("** createGraph started **");
58         graph = TitanFactory.build().set("storage.backend", "inmemory").open();
59         createTitanSchema();
60
61         logger.info("** in memory graph created");
62         return TitanOperationStatus.OK;
63
64     }
65
66
67     public void cleanupGraph() {
68         if (graph != null) {
69             // graph.shutdown();
70             graph.close();
71             TitanCleanup.clear(graph);
72         }
73     }
74
75     public TitanOperationStatus createGraph(String titanCfgFile) {
76         logger.info("** open graph with {} started", titanCfgFile);
77         try {
78             logger.info("openGraph : try to load file {}", titanCfgFile);
79             graph = TitanFactory.open(titanCfgFile);
80             if (graph.isClosed()) {
81                 logger.error("titan graph was not initialized");
82                 return TitanOperationStatus.NOT_CREATED;
83             }
84
85         } catch (Exception e) {
86             this.graph = null;
87             logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile, e);
88             return TitanOperationStatus.NOT_CONNECTED;
89         }
90
91         logger.info("** Titan graph created ");
92
93         return TitanOperationStatus.OK;
94     }
95
96
97     public Either<TitanGraph, TitanOperationStatus> getGraph() {
98         if (graph != null) {
99             return Either.left(graph);
100         } else {
101             return Either.right(TitanOperationStatus.NOT_CREATED);
102         }
103     }
104
105     public TitanOperationStatus commit() {
106         if (graph != null) {
107             try {
108                 graph.tx().commit();
109                 return TitanOperationStatus.OK;
110             } catch (Exception e) {
111                 return handleTitanException(e);
112             }
113         } else {
114             return TitanOperationStatus.NOT_CREATED;
115         }
116     }
117
118     public TitanOperationStatus rollback() {
119         if (graph != null) {
120             try {
121                 // graph.rollback();
122                 graph.tx().rollback();
123                 return TitanOperationStatus.OK;
124             } catch (Exception e) {
125                 return handleTitanException(e);
126             }
127         } else {
128             return TitanOperationStatus.NOT_CREATED;
129         }
130     }
131
132     public static TitanOperationStatus handleTitanException(Exception e) {
133         if (e instanceof TitanConfigurationException) {
134             return TitanOperationStatus.TITAN_CONFIGURATION;
135         }
136         if (e instanceof SchemaViolationException) {
137             return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
138         }
139         if (e instanceof PermanentLockingException) {
140             return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
141         }
142         if (e instanceof IDPoolExhaustedException) {
143             return TitanOperationStatus.GENERAL_ERROR;
144         }
145         if (e instanceof InvalidElementException) {
146             return TitanOperationStatus.INVALID_ELEMENT;
147         }
148         if (e instanceof InvalidIDException) {
149             return TitanOperationStatus.INVALID_ID;
150         }
151         if (e instanceof QueryException) {
152             return TitanOperationStatus.INVALID_QUERY;
153         }
154         if (e instanceof ResourceUnavailableException) {
155             return TitanOperationStatus.RESOURCE_UNAVAILABLE;
156         }
157         if (e instanceof IllegalArgumentException) {
158             // TODO check the error message??
159             return TitanOperationStatus.ILLEGAL_ARGUMENT;
160         }
161
162         return TitanOperationStatus.GENERAL_ERROR;
163     }
164
165     public boolean getHealth() {
166         return true;
167     }
168
169     private boolean isGraphOpen() {
170         return true;
171     }
172
173
174     private static final String TITAN_HEALTH_CHECK_STR = "titanHealthCheck";
175
176
177     private void createTitanSchema() {
178
179         TitanManagement graphMgt = graph.openManagement();
180         TitanGraphIndex index = null;
181         for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
182             PropertyKey propKey = null;
183             if (!graphMgt.containsPropertyKey(prop.getProperty())) {
184                 Class<?> clazz = prop.getClazz();
185                 if (!ArrayList.class.getName().equals(clazz.getName()) && !HashMap.class.getName().equals(clazz.getName())) {
186                     propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
187                 }
188             } else {
189                 propKey = graphMgt.getPropertyKey(prop.getProperty());
190             }
191             if (prop.isIndexed()) {
192                 if (!graphMgt.containsGraphIndex(prop.getProperty())) {
193                     if (prop.isUnique()) {
194                         index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex();
195
196                         graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
197                         // only
198                         // one
199                         // name
200                         // per
201                         // vertex
202                         graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
203                         // name
204                         // uniqueness
205                         // in
206                         // the
207                         // graph
208
209                     } else {
210                         graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();
211                     }
212                 }
213             }
214         }
215         graphMgt.commit();
216     }
217
218 }