1 package org.openecomp.sdc.be.components.path.beans;
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;
29 import javax.annotation.PostConstruct;
30 import java.util.ArrayList;
31 import java.util.HashMap;
33 @Component("titan-client")
34 public class InMemoryTitanGraphClient extends TitanGraphClient {
37 private static final Logger logger = LoggerFactory.getLogger(InMemoryTitanGraphClient.class);
39 private static final String OK = "GOOD";
41 public InMemoryTitanGraphClient() {
45 private TitanGraph graph;
46 TitanClientStrategy titanClientStrategy;
48 public InMemoryTitanGraphClient(TitanClientStrategy titanClientStrategy) {
50 this.titanClientStrategy = titanClientStrategy;
51 logger.info("** TitanGraphClient created");
55 public TitanOperationStatus createGraph() {
57 logger.info("** createGraph started **");
58 graph = TitanFactory.build().set("storage.backend", "inmemory").open();
61 logger.info("** in memory graph created");
62 return TitanOperationStatus.OK;
67 public void cleanupGraph() {
71 TitanCleanup.clear(graph);
75 public TitanOperationStatus createGraph(String titanCfgFile) {
76 logger.info("** open graph with {} started", titanCfgFile);
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;
85 } catch (Exception e) {
87 logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile, e);
88 return TitanOperationStatus.NOT_CONNECTED;
91 logger.info("** Titan graph created ");
93 return TitanOperationStatus.OK;
97 public Either<TitanGraph, TitanOperationStatus> getGraph() {
99 return Either.left(graph);
101 return Either.right(TitanOperationStatus.NOT_CREATED);
105 public TitanOperationStatus commit() {
109 return TitanOperationStatus.OK;
110 } catch (Exception e) {
111 return handleTitanException(e);
114 return TitanOperationStatus.NOT_CREATED;
118 public TitanOperationStatus rollback() {
122 graph.tx().rollback();
123 return TitanOperationStatus.OK;
124 } catch (Exception e) {
125 return handleTitanException(e);
128 return TitanOperationStatus.NOT_CREATED;
132 public static TitanOperationStatus handleTitanException(Exception e) {
133 if (e instanceof TitanConfigurationException) {
134 return TitanOperationStatus.TITAN_CONFIGURATION;
136 if (e instanceof SchemaViolationException) {
137 return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
139 if (e instanceof PermanentLockingException) {
140 return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
142 if (e instanceof IDPoolExhaustedException) {
143 return TitanOperationStatus.GENERAL_ERROR;
145 if (e instanceof InvalidElementException) {
146 return TitanOperationStatus.INVALID_ELEMENT;
148 if (e instanceof InvalidIDException) {
149 return TitanOperationStatus.INVALID_ID;
151 if (e instanceof QueryException) {
152 return TitanOperationStatus.INVALID_QUERY;
154 if (e instanceof ResourceUnavailableException) {
155 return TitanOperationStatus.RESOURCE_UNAVAILABLE;
157 if (e instanceof IllegalArgumentException) {
158 // TODO check the error message??
159 return TitanOperationStatus.ILLEGAL_ARGUMENT;
162 return TitanOperationStatus.GENERAL_ERROR;
165 public boolean getHealth() {
169 private boolean isGraphOpen() {
174 private static final String TITAN_HEALTH_CHECK_STR = "titanHealthCheck";
177 private void createTitanSchema() {
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();
189 propKey = graphMgt.getPropertyKey(prop.getProperty());
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();
196 graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures
202 graphMgt.setConsistency(index, ConsistencyModifier.LOCK); // Ensures
210 graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex();