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