1 package org.openecomp.sdc.be.components.path.beans;
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;
23 import javax.annotation.PostConstruct;
24 import java.util.ArrayList;
25 import java.util.HashMap;
27 @Component("janusgraph-client")
28 public class InMemoryJanusGraphClient extends JanusGraphClient {
31 private static final Logger logger = LoggerFactory.getLogger(InMemoryJanusGraphClient.class);
33 private static final String OK = "GOOD";
35 public InMemoryJanusGraphClient() {
39 private JanusGraph graph;
40 JanusGraphClientStrategy janusGraphClientStrategy;
42 public InMemoryJanusGraphClient(JanusGraphClientStrategy janusGraphClientStrategy) {
44 this.janusGraphClientStrategy = janusGraphClientStrategy;
45 logger.info("** JanusGraphClient created");
49 public JanusGraphOperationStatus createGraph() {
51 logger.info("** createGraph started **");
52 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
53 createJanusGraphSchema();
55 logger.info("** in memory graph created");
56 return JanusGraphOperationStatus.OK;
61 public void cleanupGraph() {
66 JanusGraphFactory.drop(graph);
67 } catch (BackendException e) {
73 public JanusGraphOperationStatus createGraph(String janusGraphCfgFile) {
74 logger.info("** open graph with {} started", janusGraphCfgFile);
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;
83 } catch (Exception e) {
85 logger.info("createGraph : failed to open JanusGraph graph with configuration file: {}", janusGraphCfgFile, e);
86 return JanusGraphOperationStatus.NOT_CONNECTED;
89 logger.info("** JanusGraph graph created ");
91 return JanusGraphOperationStatus.OK;
95 public Either<JanusGraph, JanusGraphOperationStatus> getGraph() {
97 return Either.left(graph);
99 return Either.right(JanusGraphOperationStatus.NOT_CREATED);
103 public JanusGraphOperationStatus commit() {
107 return JanusGraphOperationStatus.OK;
108 } catch (Exception e) {
109 return handleJanusGraphException(e);
112 return JanusGraphOperationStatus.NOT_CREATED;
116 public JanusGraphOperationStatus rollback() {
120 graph.tx().rollback();
121 return JanusGraphOperationStatus.OK;
122 } catch (Exception e) {
123 return handleJanusGraphException(e);
126 return JanusGraphOperationStatus.NOT_CREATED;
130 public static JanusGraphOperationStatus handleJanusGraphException(Exception e) {
131 if (e instanceof JanusGraphConfigurationException) {
132 return JanusGraphOperationStatus.JANUSGRAPH_CONFIGURATION;
134 if (e instanceof SchemaViolationException) {
135 return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION;
137 if (e instanceof PermanentLockingException) {
138 return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION;
140 if (e instanceof IDPoolExhaustedException) {
141 return JanusGraphOperationStatus.GENERAL_ERROR;
143 if (e instanceof InvalidElementException) {
144 return JanusGraphOperationStatus.INVALID_ELEMENT;
146 if (e instanceof InvalidIDException) {
147 return JanusGraphOperationStatus.INVALID_ID;
149 if (e instanceof QueryException) {
150 return JanusGraphOperationStatus.INVALID_QUERY;
152 if (e instanceof ResourceUnavailableException) {
153 return JanusGraphOperationStatus.RESOURCE_UNAVAILABLE;
155 if (e instanceof IllegalArgumentException) {
156 // TODO check the error message??
157 return JanusGraphOperationStatus.ILLEGAL_ARGUMENT;
160 return JanusGraphOperationStatus.GENERAL_ERROR;
163 public boolean getHealth() {
167 private boolean isGraphOpen() {
172 private static final String JANUSGRAPH_HEALTH_CHECK_STR = "janusGraphHealthCheck";
175 private void createJanusGraphSchema() {
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();
187 propKey = graphMgt.getPropertyKey(prop.getProperty());
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();
194 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
200 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
208 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();