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