1 package org.openecomp.sdc.be.components.path.beans;
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;
22 import javax.annotation.PostConstruct;
23 import java.util.ArrayList;
24 import java.util.HashMap;
26 @Component("titan-client")
27 public class InMemoryTitanGraphClient extends TitanGraphClient {
30 private static final Logger logger = LoggerFactory.getLogger(InMemoryTitanGraphClient.class);
32 private static final String OK = "GOOD";
34 public InMemoryTitanGraphClient() {
38 private TitanGraph graph;
39 TitanClientStrategy titanClientStrategy;
41 public InMemoryTitanGraphClient(TitanClientStrategy titanClientStrategy) {
43 this.titanClientStrategy = titanClientStrategy;
44 logger.info("** TitanGraphClient created");
48 public TitanOperationStatus createGraph() {
50 logger.info("** createGraph started **");
51 graph = TitanFactory.build().set("storage.backend", "inmemory").open();
54 logger.info("** in memory graph created");
55 return TitanOperationStatus.OK;
60 public void cleanupGraph() {
64 TitanCleanup.clear(graph);
68 public TitanOperationStatus createGraph(String titanCfgFile) {
69 logger.info("** open graph with {} started", titanCfgFile);
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;
78 } catch (Exception e) {
80 logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile, e);
81 return TitanOperationStatus.NOT_CONNECTED;
84 logger.info("** Titan graph created ");
86 return TitanOperationStatus.OK;
90 public Either<TitanGraph, TitanOperationStatus> getGraph() {
92 return Either.left(graph);
94 return Either.right(TitanOperationStatus.NOT_CREATED);
98 public TitanOperationStatus commit() {
102 return TitanOperationStatus.OK;
103 } catch (Exception e) {
104 return handleTitanException(e);
107 return TitanOperationStatus.NOT_CREATED;
111 public TitanOperationStatus rollback() {
115 graph.tx().rollback();
116 return TitanOperationStatus.OK;
117 } catch (Exception e) {
118 return handleTitanException(e);
121 return TitanOperationStatus.NOT_CREATED;
125 public static TitanOperationStatus handleTitanException(Exception e) {
126 if (e instanceof TitanConfigurationException) {
127 return TitanOperationStatus.TITAN_CONFIGURATION;
129 if (e instanceof SchemaViolationException) {
130 return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
132 if (e instanceof PermanentLockingException) {
133 return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
135 if (e instanceof IDPoolExhaustedException) {
136 return TitanOperationStatus.GENERAL_ERROR;
138 if (e instanceof InvalidElementException) {
139 return TitanOperationStatus.INVALID_ELEMENT;
141 if (e instanceof InvalidIDException) {
142 return TitanOperationStatus.INVALID_ID;
144 if (e instanceof QueryException) {
145 return TitanOperationStatus.INVALID_QUERY;
147 if (e instanceof ResourceUnavailableException) {
148 return TitanOperationStatus.RESOURCE_UNAVAILABLE;
150 if (e instanceof IllegalArgumentException) {
151 // TODO check the error message??
152 return TitanOperationStatus.ILLEGAL_ARGUMENT;
155 return TitanOperationStatus.GENERAL_ERROR;
158 public boolean getHealth() {
162 private boolean isGraphOpen() {
167 private static final String TITAN_HEALTH_CHECK_STR = "titanHealthCheck";
170 private void createTitanSchema() {
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();
182 propKey = graphMgt.getPropertyKey(prop.getProperty());
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();
189 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
195 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
203 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();