2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
21 package org.onap.aai.champtitan.graph.impl;
23 import java.security.SecureRandom;
24 import java.time.temporal.ChronoUnit;
26 import java.util.Map.Entry;
27 import java.util.concurrent.ExecutionException;
28 import java.util.stream.Stream;
29 import java.util.stream.StreamSupport;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.onap.aai.champcore.ChampCapabilities;
35 import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
36 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
37 import org.onap.aai.champcore.graph.impl.AbstractTinkerpopChampGraph;
38 import org.onap.aai.champcore.model.ChampCardinality;
39 import org.onap.aai.champcore.model.ChampObject;
40 import org.onap.aai.champcore.model.ChampObjectConstraint;
41 import org.onap.aai.champcore.model.ChampObjectIndex;
42 import org.onap.aai.champcore.model.ChampPropertyConstraint;
43 import org.onap.aai.champcore.model.ChampRelationship;
44 import org.onap.aai.champcore.model.ChampRelationshipConstraint;
45 import org.onap.aai.champcore.model.ChampRelationshipIndex;
46 import org.onap.aai.champcore.model.ChampSchema;
47 import org.onap.aai.champcore.schema.ChampSchemaEnforcer;
48 import org.onap.aai.champcore.schema.DefaultChampSchemaEnforcer;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.thinkaurelius.titan.core.Cardinality;
53 import com.thinkaurelius.titan.core.EdgeLabel;
54 import com.thinkaurelius.titan.core.PropertyKey;
55 import com.thinkaurelius.titan.core.SchemaViolationException;
56 import com.thinkaurelius.titan.core.TitanEdge;
57 import com.thinkaurelius.titan.core.TitanFactory;
58 import com.thinkaurelius.titan.core.TitanGraph;
59 import com.thinkaurelius.titan.core.TitanVertex;
60 import com.thinkaurelius.titan.core.schema.SchemaAction;
61 import com.thinkaurelius.titan.core.schema.SchemaStatus;
62 import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
63 import com.thinkaurelius.titan.core.schema.TitanManagement;
64 import com.thinkaurelius.titan.graphdb.database.management.ManagementSystem;
66 public final class TitanChampGraphImpl extends AbstractTinkerpopChampGraph {
68 private static final Logger LOGGER = LoggerFactory.getLogger(TitanChampGraphImpl.class);
69 private static final String TITAN_UNIQUE_SUFFIX = "graph.unique-instance-id-suffix";
70 private static final String TITAN_CASSANDRA_KEYSPACE = "storage.cassandra.keyspace";
71 private static final String TITAN_HBASE_TABLE = "storage.hbase.table";
72 private static final ChampSchemaEnforcer SCHEMA_ENFORCER = new DefaultChampSchemaEnforcer();
73 private static final int REGISTER_OBJECT_INDEX_TIMEOUT_SECS = 30;
75 private static final ChampCapabilities CAPABILITIES = new ChampCapabilities() {
78 public boolean canDeleteObjectIndices() {
83 public boolean canDeleteRelationshipIndices() {
88 private final TitanGraph graph;
90 public TitanChampGraphImpl(Builder builder) {
91 super(builder.graphConfiguration);
92 final TitanFactory.Builder titanGraphBuilder = TitanFactory.build();
94 for (Entry<String, Object> titanGraphProperty : builder.graphConfiguration.entrySet()) {
95 titanGraphBuilder.set(titanGraphProperty.getKey(), titanGraphProperty.getValue());
98 titanGraphBuilder.set(TITAN_UNIQUE_SUFFIX, ((short) new SecureRandom().nextInt(Short.MAX_VALUE)+""));
100 final Object storageBackend = builder.graphConfiguration.get("storage.backend");
102 if ("cassandra".equals(storageBackend) ||
103 "cassandrathrift".equals(storageBackend) ||
104 "astyanax".equals(storageBackend) ||
105 "embeddedcassandra".equals(storageBackend)) {
106 titanGraphBuilder.set(TITAN_CASSANDRA_KEYSPACE, builder.graphName);
107 } else if ("hbase".equals(storageBackend)) {
108 titanGraphBuilder.set(TITAN_HBASE_TABLE, builder.graphName);
109 } else if ("berkleyje".equals(storageBackend)) {
110 throw new RuntimeException("storage.backend=berkleyje cannot handle multiple graphs on a single DB, not usable");
111 } else if ("inmemory".equals(storageBackend)) {
113 throw new RuntimeException("Unknown storage.backend=" + storageBackend);
116 LOGGER.info("Instantiated data access layer for Titan graph data store with backend: " + storageBackend);
118 this.graph = titanGraphBuilder.open();
121 public static class Builder {
122 private final String graphName;
124 private final Map<String, Object> graphConfiguration = new HashMap<String, Object> ();
126 public Builder(String graphName) {
127 this.graphName = graphName;
130 public Builder(String graphName, Map<String, Object> properties) {
131 this.graphName = graphName;
132 properties(properties);
135 public Builder properties(Map<String, Object> properties) {
136 if (properties.containsKey(TITAN_CASSANDRA_KEYSPACE))
137 throw new IllegalArgumentException("Cannot use path " + TITAN_CASSANDRA_KEYSPACE
138 + " in initial configuration - this path is used"
139 + " to specify graph names");
141 this.graphConfiguration.putAll(properties);
145 public Builder property(String path, Object value) {
146 if (path.equals(TITAN_CASSANDRA_KEYSPACE))
147 throw new IllegalArgumentException("Cannot use path " + TITAN_CASSANDRA_KEYSPACE
148 + " in initial configuration - this path is used"
149 + " to specify graph names");
150 graphConfiguration.put(path, value);
154 public TitanChampGraphImpl build() {
155 return new TitanChampGraphImpl(this);
160 protected TitanGraph getGraph() {
165 protected ChampSchemaEnforcer getSchemaEnforcer() {
166 return SCHEMA_ENFORCER;
169 public void executeStoreObjectIndex(ChampObjectIndex index) {
170 if (isShutdown()) throw new IllegalStateException("Cannot call storeObjectIndex() after shutdown has been initiated");
172 final TitanGraph graph = getGraph();
173 final TitanManagement createIndexMgmt = graph.openManagement();
174 final PropertyKey pk = createIndexMgmt.getOrCreatePropertyKey(index.getField().getName());
176 if (createIndexMgmt.getGraphIndex(index.getName()) != null) {
177 createIndexMgmt.rollback();
178 return; //Ignore, index already exists
181 createIndexMgmt.buildIndex(index.getName(), Vertex.class).addKey(pk).buildCompositeIndex();
183 createIndexMgmt.commit();
186 awaitIndexCreation(index.getName());
190 public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
191 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndex() after shutdown has been initiated");
193 final TitanManagement retrieveIndexMgmt = getGraph().openManagement();
194 final TitanGraphIndex index = retrieveIndexMgmt.getGraphIndex(indexName);
196 if (index == null) return Optional.empty();
197 if (index.getIndexedElement() != TitanVertex.class) return Optional.empty();
199 return Optional.of(ChampObjectIndex.create()
201 .onType(ChampObject.ReservedTypes.ANY.toString())
202 .forField(index.getFieldKeys()[0].name())
207 public Stream<ChampObjectIndex> retrieveObjectIndices() {
208 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndices() after shutdown has been initiated");
210 final TitanManagement createIndexMgmt = getGraph().openManagement();
211 final Iterator<TitanGraphIndex> indices = createIndexMgmt.getGraphIndexes(Vertex.class).iterator();
213 final Iterator<ChampObjectIndex> objIter = new Iterator<ChampObjectIndex> () {
215 private ChampObjectIndex next;
218 public boolean hasNext() {
219 if (indices.hasNext()) {
220 final TitanGraphIndex index = indices.next();
222 next = ChampObjectIndex.create()
223 .ofName(index.name())
224 .onType(ChampObject.ReservedTypes.ANY.toString())
225 .forField(index.getFieldKeys()[0].name())
235 public ChampObjectIndex next() {
236 if (next == null) throw new NoSuchElementException();
242 return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
243 objIter, Spliterator.ORDERED | Spliterator.NONNULL), false);
246 public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
247 if (isShutdown()) throw new IllegalStateException("Cannot call deleteObjectIndex() after shutdown has been initiated");
249 throw new UnsupportedOperationException("Cannot delete indices using the TitanChampImpl");
252 public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
253 if (isShutdown()) throw new IllegalStateException("Cannot call storeRelationshipIndex() after shutdown has been initiated");
255 final TitanGraph graph = getGraph();
256 final TitanManagement createIndexMgmt = graph.openManagement();
257 final PropertyKey pk = createIndexMgmt.getOrCreatePropertyKey(index.getField().getName());
259 if (createIndexMgmt.getGraphIndex(index.getName()) != null) return; //Ignore, index already exists
260 createIndexMgmt.buildIndex(index.getName(), Edge.class).addKey(pk).buildCompositeIndex();
262 createIndexMgmt.commit();
265 awaitIndexCreation(index.getName());
269 public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
270 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndex() after shutdown has been initiated");
272 final TitanManagement retrieveIndexMgmt = getGraph().openManagement();
273 final TitanGraphIndex index = retrieveIndexMgmt.getGraphIndex(indexName);
275 if (index == null) return Optional.empty();
276 if (index.getIndexedElement() != TitanEdge.class) return Optional.empty();
278 return Optional.of(ChampRelationshipIndex.create()
280 .onType(ChampObject.ReservedTypes.ANY.toString())
281 .forField(index.getFieldKeys()[0].name())
286 public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
287 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndices() after shutdown has been initiated");
289 final TitanManagement createIndexMgmt = getGraph().openManagement();
290 final Iterator<TitanGraphIndex> indices = createIndexMgmt.getGraphIndexes(Edge.class).iterator();
292 final Iterator<ChampRelationshipIndex> objIter = new Iterator<ChampRelationshipIndex> () {
294 private ChampRelationshipIndex next;
297 public boolean hasNext() {
298 if (indices.hasNext()) {
299 final TitanGraphIndex index = indices.next();
301 next = ChampRelationshipIndex.create()
302 .ofName(index.name())
303 .onType(ChampRelationship.ReservedTypes.ANY.toString())
304 .forField(index.getFieldKeys()[0].name())
314 public ChampRelationshipIndex next() {
315 if (next == null) throw new NoSuchElementException();
321 return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
322 objIter, Spliterator.ORDERED | Spliterator.NONNULL), false);
325 public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {
326 if (isShutdown()) throw new IllegalStateException("Cannot call deleteRelationshipIndex() after shutdown has been initiated");
328 throw new UnsupportedOperationException("Cannot delete indices using the TitanChampImpl");
331 private Cardinality getTitanCardinality(ChampCardinality cardinality) {
332 switch (cardinality) {
334 return Cardinality.LIST;
336 return Cardinality.SET;
338 return Cardinality.SINGLE;
340 throw new RuntimeException("Unknown ChampCardinality " + cardinality);
344 private void awaitIndexCreation(String indexName) {
345 //Wait for the index to become available
347 if (ManagementSystem.awaitGraphIndexStatus(graph, indexName)
348 .status(SchemaStatus.ENABLED)
349 .timeout(1, ChronoUnit.SECONDS)
352 return; //Empty graphs immediately ENABLE indices
355 if (!ManagementSystem.awaitGraphIndexStatus(graph, indexName)
356 .status(SchemaStatus.REGISTERED)
357 .timeout(REGISTER_OBJECT_INDEX_TIMEOUT_SECS, ChronoUnit.SECONDS)
360 LOGGER.warn("Object index was created, but timed out while waiting for it to be registered");
363 } catch (InterruptedException e) {
364 LOGGER.warn("Interrupted while waiting for object index creation status");
365 Thread.currentThread().interrupt();
369 //Reindex the existing data
372 final TitanManagement updateIndexMgmt = graph.openManagement();
373 updateIndexMgmt.updateIndex(updateIndexMgmt.getGraphIndex(indexName),SchemaAction.REINDEX).get();
374 updateIndexMgmt.commit();
375 } catch (InterruptedException e) {
376 LOGGER.warn("Interrupted while reindexing for object index");
377 Thread.currentThread().interrupt();
379 } catch (ExecutionException e) {
380 LOGGER.warn("Exception occurred during reindexing procedure for creating object index " + indexName, e);
384 ManagementSystem.awaitGraphIndexStatus(graph, indexName)
385 .status(SchemaStatus.ENABLED)
386 .timeout(10, ChronoUnit.MINUTES)
388 } catch (InterruptedException e) {
389 LOGGER.warn("Interrupted while waiting for index to transition to ENABLED state");
390 Thread.currentThread().interrupt();
396 public ChampCapabilities capabilities() {
401 public void storeSchema(ChampSchema schema) throws ChampSchemaViolationException {
402 if (isShutdown()) throw new IllegalStateException("Cannot call storeSchema() after shutdown has been initiated");
404 final ChampSchema currentSchema = retrieveSchema();
405 final TitanManagement mgmt = getGraph().openManagement();
408 for (ChampObjectConstraint objConstraint : schema.getObjectConstraints().values()) {
409 for (ChampPropertyConstraint propConstraint : objConstraint.getPropertyConstraints()) {
410 final Optional<ChampObjectConstraint> currentObjConstraint = currentSchema.getObjectConstraint(objConstraint.getType());
412 if (currentObjConstraint.isPresent()) {
413 final Optional<ChampPropertyConstraint> currentPropConstraint = currentObjConstraint.get().getPropertyConstraint(propConstraint.getField().getName());
415 if (currentPropConstraint.isPresent() && currentPropConstraint.get().compareTo(propConstraint) != 0) {
416 throw new ChampSchemaViolationException("Cannot update already existing property on object type " + objConstraint.getType() + ": " + propConstraint);
420 final String newPropertyKeyName = propConstraint.getField().getName();
422 if (mgmt.getPropertyKey(newPropertyKeyName) != null) continue; //Check Titan to see if another node created this property key
424 mgmt.makePropertyKey(newPropertyKeyName)
425 .dataType(propConstraint.getField().getJavaType())
426 .cardinality(getTitanCardinality(propConstraint.getCardinality()))
431 for (ChampRelationshipConstraint relConstraint : schema.getRelationshipConstraints().values()) {
433 final Optional<ChampRelationshipConstraint> currentRelConstraint = currentSchema.getRelationshipConstraint(relConstraint.getType());
435 for (ChampPropertyConstraint propConstraint : relConstraint.getPropertyConstraints()) {
437 if (currentRelConstraint.isPresent()) {
438 final Optional<ChampPropertyConstraint> currentPropConstraint = currentRelConstraint.get().getPropertyConstraint(propConstraint.getField().getName());
440 if (currentPropConstraint.isPresent() && currentPropConstraint.get().compareTo(propConstraint) != 0) {
441 throw new ChampSchemaViolationException("Cannot update already existing property on relationship type " + relConstraint.getType());
445 final String newPropertyKeyName = propConstraint.getField().getName();
447 if (mgmt.getPropertyKey(newPropertyKeyName) != null) continue; //Check Titan to see if another node created this property key
449 mgmt.makePropertyKey(newPropertyKeyName)
450 .dataType(propConstraint.getField().getJavaType())
451 .cardinality(getTitanCardinality(propConstraint.getCardinality()))
455 final EdgeLabel edgeLabel = mgmt.getEdgeLabel(relConstraint.getType());
457 if (edgeLabel != null) mgmt.makeEdgeLabel(relConstraint.getType())
464 super.storeSchema(schema);
465 } catch (SchemaViolationException | ChampSchemaViolationException e) {
467 throw new ChampSchemaViolationException(e);
472 public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
473 return query.hasLabel((String) type);