2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * Modifications Copyright (C) 2019 IBM
8 * ===================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END============================================
22 package org.onap.aai.champcore.graph.impl;
24 import java.util.HashMap;
26 import java.util.Optional;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.stream.Stream;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
34 import org.onap.aai.champcore.ChampCapabilities;
35 import org.onap.aai.champcore.ChampTransaction;
36 import org.onap.aai.champcore.NoOpTinkerPopTransaction;
37 import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
38 import org.onap.aai.champcore.model.ChampObjectIndex;
39 import org.onap.aai.champcore.model.ChampRelationshipIndex;
40 import org.onap.aai.champcore.schema.ChampSchemaEnforcer;
41 import org.onap.aai.champcore.schema.DefaultChampSchemaEnforcer;
43 public final class InMemoryChampGraphImpl extends AbstractTinkerpopChampGraph {
45 private static final ChampCapabilities CAPABILITIES = new ChampCapabilities() {
48 public boolean canDeleteObjectIndices() {
53 public boolean canDeleteRelationshipIndices() {
58 private final ConcurrentHashMap<String, ChampObjectIndex> objectIndices;
59 private final ConcurrentHashMap<String, ChampRelationshipIndex> relationshipIndices;
61 private final ChampSchemaEnforcer schemaEnforcer;
62 private final TinkerGraph graph;
64 private InMemoryChampGraphImpl(Builder builder) {
65 super(builder.graphConfiguration);
66 this.graph = TinkerGraph.open();
68 this.objectIndices = new ConcurrentHashMap<>();
69 this.relationshipIndices = new ConcurrentHashMap<>();
71 this.schemaEnforcer = builder.schemaEnforcer;
75 public ChampTransaction getOrCreateTransactionInstance(Optional<ChampTransaction> transaction) {
77 return new NoOpTinkerPopTransaction(getGraph());
81 public static class Builder {
82 private final Map<String, Object> graphConfiguration = new HashMap<String, Object> ();
83 private ChampSchemaEnforcer schemaEnforcer = new DefaultChampSchemaEnforcer();
87 public Builder schemaEnforcer(ChampSchemaEnforcer schemaEnforcer) {
88 this.schemaEnforcer = schemaEnforcer;
92 public Builder properties(Map<String, Object> properties) {
94 this.graphConfiguration.putAll(properties);
98 public Builder property(String path, Object value) {
100 graphConfiguration.put(path, value);
104 public InMemoryChampGraphImpl build() {
105 return new InMemoryChampGraphImpl(this);
109 protected ChampSchemaEnforcer getSchemaEnforcer() {
110 return schemaEnforcer;
114 protected TinkerGraph getGraph() {
119 private ConcurrentHashMap<String, ChampObjectIndex> getObjectIndices() {
120 return objectIndices;
123 private ConcurrentHashMap<String, ChampRelationshipIndex> getRelationshipIndices() {
124 return relationshipIndices;
128 public void executeStoreObjectIndex(ChampObjectIndex index) {
130 if (isShutdown()) throw new IllegalStateException("Cannot call storeObjectIndex() after shutdown has been initiated");
132 getGraph().createIndex(index.getFields().get(0).getName(), Vertex.class);
133 getObjectIndices().put(index.getName(), index);
137 public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
138 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndex() after shutdown has been initiated");
140 if (getObjectIndices().containsKey(indexName))
141 return Optional.of(getObjectIndices().get(indexName));
143 return Optional.empty();
147 public Stream<ChampObjectIndex> retrieveObjectIndices() {
148 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndices() after shutdown has been initiated");
150 return getObjectIndices().values().stream();
153 public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
154 if (isShutdown()) throw new IllegalStateException("Cannot call deleteObjectIndex() after shutdown has been initiated");
156 final ChampObjectIndex objectIndex = getObjectIndices().remove(indexName);
158 if (objectIndex == null) throw new ChampIndexNotExistsException();
160 getGraph().dropIndex(objectIndex.getFields().get(0).getName(), Vertex.class);
163 public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
164 if (isShutdown()) throw new IllegalStateException("Cannot call storeRelationshipIndex() after shutdown has been initiated");
166 getGraph().createIndex(index.getField().getName(), Edge.class);
167 getRelationshipIndices().put(index.getName(), index);
171 public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
172 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndex() after shutdown has been initiated");
174 if (getRelationshipIndices().containsKey(indexName)) {
175 return Optional.of(getRelationshipIndices().get(indexName));
178 return Optional.empty();
182 public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
183 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndices() after shutdown has been initiated");
185 return getRelationshipIndices().values().stream();
188 public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {
189 if (isShutdown()) throw new IllegalStateException("Cannot call deleteRelationshipIndex() after shutdown has been initiated");
191 final ChampRelationshipIndex relationshipIndex = getRelationshipIndices().remove(indexName);
193 if (relationshipIndex == null) throw new ChampIndexNotExistsException();
195 getGraph().dropIndex(relationshipIndex.getField().getName(), Edge.class);
199 public ChampCapabilities capabilities() {
204 public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
205 return query.hasLabel((String)type, (String)type);
209 public void createDefaultIndexes() {