Update license date and text
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / graph / impl / InMemoryChampGraphImpl.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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============================================
20  */
21 package org.onap.aai.champcore.graph.impl;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.stream.Stream;
29
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.ChampObject;
39 import org.onap.aai.champcore.model.ChampObjectIndex;
40 import org.onap.aai.champcore.model.ChampRelationshipIndex;
41 import org.onap.aai.champcore.schema.ChampSchemaEnforcer;
42 import org.onap.aai.champcore.schema.DefaultChampSchemaEnforcer;
43
44 public final class InMemoryChampGraphImpl extends AbstractTinkerpopChampGraph {
45
46         private static final ChampCapabilities CAPABILITIES = new ChampCapabilities() {
47
48                 @Override
49                 public boolean canDeleteObjectIndices() {
50                         return true;
51                 }
52
53                 @Override
54                 public boolean canDeleteRelationshipIndices() {
55                         return true;
56                 }
57         };
58
59         private final ConcurrentHashMap<String, ChampObjectIndex> objectIndices;
60         private final ConcurrentHashMap<String, ChampRelationshipIndex> relationshipIndices;
61
62         private final ChampSchemaEnforcer schemaEnforcer;
63         private final TinkerGraph graph;
64
65         private InMemoryChampGraphImpl(Builder builder) {
66             super(builder.graphConfiguration);
67                 this.graph = TinkerGraph.open();
68         
69                 this.objectIndices = new ConcurrentHashMap<String, ChampObjectIndex> ();
70                 this.relationshipIndices = new ConcurrentHashMap<String, ChampRelationshipIndex> ();
71
72                 this.schemaEnforcer = builder.schemaEnforcer;
73         }
74
75         @Override
76     public ChampTransaction getOrCreateTransactionInstance(Optional<ChampTransaction> transaction) {
77
78           return new NoOpTinkerPopTransaction(getGraph());
79
80         }
81            
82         public static class Builder {
83             private final Map<String, Object> graphConfiguration = new HashMap<String, Object> ();
84                 private ChampSchemaEnforcer schemaEnforcer = new DefaultChampSchemaEnforcer();
85
86                 public Builder() {}
87
88                 public Builder schemaEnforcer(ChampSchemaEnforcer schemaEnforcer) {
89                         this.schemaEnforcer = schemaEnforcer;
90                         return this;
91                 }
92
93               public Builder properties(Map<String, Object> properties) {
94             
95             this.graphConfiguration.putAll(properties);
96             return this;
97         }
98
99         public Builder property(String path, Object value) {
100            
101             graphConfiguration.put(path, value);
102             return this;
103         }
104         
105                 public InMemoryChampGraphImpl build() {
106                         return new InMemoryChampGraphImpl(this);
107                 }
108         }
109
110         protected ChampSchemaEnforcer getSchemaEnforcer() {
111                 return schemaEnforcer;
112         }
113
114         @Override
115         protected TinkerGraph getGraph() {
116                 return graph;
117         }
118
119         
120         private ConcurrentHashMap<String, ChampObjectIndex> getObjectIndices() {
121                 return objectIndices;
122         }
123
124         private ConcurrentHashMap<String, ChampRelationshipIndex> getRelationshipIndices() {
125                 return relationshipIndices;
126         }
127
128         @Override
129         public void executeStoreObjectIndex(ChampObjectIndex index) {
130           
131                 if (isShutdown()) throw new IllegalStateException("Cannot call storeObjectIndex() after shutdown has been initiated");
132
133                 getGraph().createIndex(index.getField().getName(), Vertex.class);
134                 getObjectIndices().put(index.getName(), index);
135         }
136
137         @Override
138         public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
139                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndex() after shutdown has been initiated");
140
141                 if (getObjectIndices().containsKey(indexName))
142                         return Optional.of(getObjectIndices().get(indexName));
143                         
144                 return Optional.empty();
145         }
146
147         @Override
148         public Stream<ChampObjectIndex> retrieveObjectIndices() {
149                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndices() after shutdown has been initiated");
150
151                 return getObjectIndices().values().stream();
152         }
153
154         public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
155                 if (isShutdown()) throw new IllegalStateException("Cannot call deleteObjectIndex() after shutdown has been initiated");
156
157                 final ChampObjectIndex objectIndex = getObjectIndices().remove(indexName);
158
159                 if (objectIndex == null) throw new ChampIndexNotExistsException();
160
161                 getGraph().dropIndex(objectIndex.getField().getName(), Vertex.class);
162         }
163
164         public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
165                 if (isShutdown()) throw new IllegalStateException("Cannot call storeRelationshipIndex() after shutdown has been initiated");
166
167                 getGraph().createIndex(index.getField().getName(), Edge.class);
168                 getRelationshipIndices().put(index.getName(), index);
169         }
170
171         @Override
172         public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
173                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndex() after shutdown has been initiated");
174
175                 if (getRelationshipIndices().containsKey(indexName)) {
176                         return Optional.of(getRelationshipIndices().get(indexName));
177                 }
178                 
179                 return Optional.empty();
180         }
181
182         @Override
183         public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
184                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndices() after shutdown has been initiated");
185
186                 return getRelationshipIndices().values().stream();
187         }
188
189         public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {
190                 if (isShutdown()) throw new IllegalStateException("Cannot call deleteRelationshipIndex() after shutdown has been initiated");
191
192                 final ChampRelationshipIndex relationshipIndex = getRelationshipIndices().remove(indexName);
193
194                 if (relationshipIndex == null) throw new ChampIndexNotExistsException();
195                 
196                 getGraph().dropIndex(relationshipIndex.getField().getName(), Edge.class);
197         }
198
199         @Override
200         public ChampCapabilities capabilities() {
201                 return CAPABILITIES;
202         }
203
204         @Override
205         public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
206                 return query.hasLabel((String)type, (String)type);
207         }
208 }