aboutsummaryrefslogtreecommitdiffstats
path: root/champ-lib/champ-core/src/main/java/org/onap/aai/champcore/graph/impl/InMemoryChampGraphImpl.java
blob: 84df1d1213e76408f0747444665b6dc121bb1ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * Modifications Copyright (C) 2019 IBM
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */
package org.onap.aai.champcore.graph.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.onap.aai.champcore.ChampCapabilities;
import org.onap.aai.champcore.ChampTransaction;
import org.onap.aai.champcore.NoOpTinkerPopTransaction;
import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
import org.onap.aai.champcore.model.ChampObjectIndex;
import org.onap.aai.champcore.model.ChampRelationshipIndex;
import org.onap.aai.champcore.schema.ChampSchemaEnforcer;
import org.onap.aai.champcore.schema.DefaultChampSchemaEnforcer;

public final class InMemoryChampGraphImpl extends AbstractTinkerpopChampGraph {

	private static final ChampCapabilities CAPABILITIES = new ChampCapabilities() {

		@Override
		public boolean canDeleteObjectIndices() {
			return true;
		}

		@Override
		public boolean canDeleteRelationshipIndices() {
			return true;
		}
	};

	private final ConcurrentHashMap<String, ChampObjectIndex> objectIndices;
	private final ConcurrentHashMap<String, ChampRelationshipIndex> relationshipIndices;

	private final ChampSchemaEnforcer schemaEnforcer;
	private final TinkerGraph graph;

	private InMemoryChampGraphImpl(Builder builder) {
	    super(builder.graphConfiguration);
		this.graph = TinkerGraph.open();
	
		this.objectIndices = new ConcurrentHashMap<>();
		this.relationshipIndices = new ConcurrentHashMap<>();

		this.schemaEnforcer = builder.schemaEnforcer;
	}

	@Override
    public ChampTransaction getOrCreateTransactionInstance(Optional<ChampTransaction> transaction) {

	  return new NoOpTinkerPopTransaction(getGraph());

	}
	   
	public static class Builder {
	    private final Map<String, Object> graphConfiguration = new HashMap<String, Object> ();
		private ChampSchemaEnforcer schemaEnforcer = new DefaultChampSchemaEnforcer();

		public Builder() {}

		public Builder schemaEnforcer(ChampSchemaEnforcer schemaEnforcer) {
			this.schemaEnforcer = schemaEnforcer;
			return this;
		}

	      public Builder properties(Map<String, Object> properties) {
            
            this.graphConfiguration.putAll(properties);
            return this;
        }

        public Builder property(String path, Object value) {
           
            graphConfiguration.put(path, value);
            return this;
        }
        
		public InMemoryChampGraphImpl build() {
			return new InMemoryChampGraphImpl(this);
		}
	}

	protected ChampSchemaEnforcer getSchemaEnforcer() {
		return schemaEnforcer;
	}

	@Override
	protected TinkerGraph getGraph() {
		return graph;
	}

	
	private ConcurrentHashMap<String, ChampObjectIndex> getObjectIndices() {
		return objectIndices;
	}

	private ConcurrentHashMap<String, ChampRelationshipIndex> getRelationshipIndices() {
		return relationshipIndices;
	}

	@Override
	public void executeStoreObjectIndex(ChampObjectIndex index) {
	  
		if (isShutdown()) throw new IllegalStateException("Cannot call storeObjectIndex() after shutdown has been initiated");

		getGraph().createIndex(index.getFields().get(0).getName(), Vertex.class);
		getObjectIndices().put(index.getName(), index);
	}

	@Override
	public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
		if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndex() after shutdown has been initiated");

		if (getObjectIndices().containsKey(indexName))
			return Optional.of(getObjectIndices().get(indexName));
			
		return Optional.empty();
	}

	@Override
	public Stream<ChampObjectIndex> retrieveObjectIndices() {
		if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndices() after shutdown has been initiated");

		return getObjectIndices().values().stream();
	}

	public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
		if (isShutdown()) throw new IllegalStateException("Cannot call deleteObjectIndex() after shutdown has been initiated");

		final ChampObjectIndex objectIndex = getObjectIndices().remove(indexName);

		if (objectIndex == null) throw new ChampIndexNotExistsException();

		getGraph().dropIndex(objectIndex.getFields().get(0).getName(), Vertex.class);
	}

	public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
		if (isShutdown()) throw new IllegalStateException("Cannot call storeRelationshipIndex() after shutdown has been initiated");

		getGraph().createIndex(index.getField().getName(), Edge.class);
		getRelationshipIndices().put(index.getName(), index);
	}

	@Override
	public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
		if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndex() after shutdown has been initiated");

		if (getRelationshipIndices().containsKey(indexName)) {
			return Optional.of(getRelationshipIndices().get(indexName));
		}
		
		return Optional.empty();
	}

	@Override
	public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
		if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndices() after shutdown has been initiated");

		return getRelationshipIndices().values().stream();
	}

	public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {
		if (isShutdown()) throw new IllegalStateException("Cannot call deleteRelationshipIndex() after shutdown has been initiated");

		final ChampRelationshipIndex relationshipIndex = getRelationshipIndices().remove(indexName);

		if (relationshipIndex == null) throw new ChampIndexNotExistsException();
		
		getGraph().dropIndex(relationshipIndex.getField().getName(), Edge.class);
	}

	@Override
	public ChampCapabilities capabilities() {
		return CAPABILITIES;
	}

	@Override
	public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
		return query.hasLabel((String)type, (String)type);
	}

  @Override
  public void createDefaultIndexes() {
    
  }
}