Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / openecomp / aai / champ / graph / impl / InMemoryChampGraphImpl.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.openecomp.aai.champ.graph.impl;
23
24 import java.io.IOException;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.stream.Stream;
31
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
35 import org.openecomp.aai.champ.ChampCapabilities;
36 import org.openecomp.aai.champ.exceptions.ChampIndexNotExistsException;
37 import org.openecomp.aai.champ.model.ChampObjectIndex;
38 import org.openecomp.aai.champ.model.ChampRelationshipIndex;
39 import org.openecomp.aai.champ.schema.ChampSchemaEnforcer;
40 import org.openecomp.aai.champ.schema.DefaultChampSchemaEnforcer;
41 import org.slf4j.Logger;
42
43 import com.att.nsa.cambria.client.CambriaPublisher;
44
45 public final class InMemoryChampGraphImpl extends AbstractTinkerpopChampGraph {
46
47         private static final ChampCapabilities CAPABILITIES = new ChampCapabilities() {
48
49                 @Override
50                 public boolean canDeleteObjectIndices() {
51                         return true;
52                 }
53
54                 @Override
55                 public boolean canDeleteRelationshipIndices() {
56                         return true;
57                 }
58         };
59
60         private final ConcurrentHashMap<String, ChampObjectIndex> objectIndices;
61         private final ConcurrentHashMap<String, ChampRelationshipIndex> relationshipIndices;
62
63         private final ChampSchemaEnforcer schemaEnforcer;
64         private final TinkerGraph graph;
65
66         private InMemoryChampGraphImpl(Builder builder) {
67             super(builder.graphConfiguration);
68                 this.graph = TinkerGraph.open();
69         
70                 this.objectIndices = new ConcurrentHashMap<String, ChampObjectIndex> ();
71                 this.relationshipIndices = new ConcurrentHashMap<String, ChampRelationshipIndex> ();
72
73                 this.schemaEnforcer = builder.schemaEnforcer;
74         }
75
76         public static class Builder {
77             private final Map<String, Object> graphConfiguration = new HashMap<String, Object> ();
78                 private ChampSchemaEnforcer schemaEnforcer = new DefaultChampSchemaEnforcer();
79
80                 public Builder() {}
81
82                 public Builder schemaEnforcer(ChampSchemaEnforcer schemaEnforcer) {
83                         this.schemaEnforcer = schemaEnforcer;
84                         return this;
85                 }
86
87                 public InMemoryChampGraphImpl build() {
88                         return new InMemoryChampGraphImpl(this);
89                 }
90         }
91
92         protected ChampSchemaEnforcer getSchemaEnforcer() {
93                 return schemaEnforcer;
94         }
95
96         @Override
97         protected TinkerGraph getGraph() {
98                 return graph;
99         }
100
101         private ConcurrentHashMap<String, ChampObjectIndex> getObjectIndices() {
102                 return objectIndices;
103         }
104
105         private ConcurrentHashMap<String, ChampRelationshipIndex> getRelationshipIndices() {
106                 return relationshipIndices;
107         }
108
109         public void executeStoreObjectIndex(ChampObjectIndex index) {
110                 if (isShutdown()) throw new IllegalStateException("Cannot call storeObjectIndex() after shutdown has been initiated");
111
112                 getGraph().createIndex(index.getField().getName(), Vertex.class);
113                 getObjectIndices().put(index.getName(), index);
114         }
115
116         @Override
117         public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
118                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndex() after shutdown has been initiated");
119
120                 if (getObjectIndices().containsKey(indexName))
121                         return Optional.of(getObjectIndices().get(indexName));
122                         
123                 return Optional.empty();
124         }
125
126         @Override
127         public Stream<ChampObjectIndex> retrieveObjectIndices() {
128                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveObjectIndices() after shutdown has been initiated");
129
130                 return getObjectIndices().values().stream();
131         }
132
133         public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
134                 if (isShutdown()) throw new IllegalStateException("Cannot call deleteObjectIndex() after shutdown has been initiated");
135
136                 final ChampObjectIndex objectIndex = getObjectIndices().remove(indexName);
137
138                 if (objectIndex == null) throw new ChampIndexNotExistsException();
139
140                 getGraph().dropIndex(objectIndex.getField().getName(), Vertex.class);
141         }
142
143         public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
144                 if (isShutdown()) throw new IllegalStateException("Cannot call storeRelationshipIndex() after shutdown has been initiated");
145
146                 getGraph().createIndex(index.getField().getName(), Edge.class);
147                 getRelationshipIndices().put(index.getName(), index);
148         }
149
150         @Override
151         public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
152                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndex() after shutdown has been initiated");
153
154                 if (getRelationshipIndices().containsKey(indexName)) {
155                         return Optional.of(getRelationshipIndices().get(indexName));
156                 }
157                 
158                 return Optional.empty();
159         }
160
161         @Override
162         public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
163                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveRelationshipIndices() after shutdown has been initiated");
164
165                 return getRelationshipIndices().values().stream();
166         }
167
168         public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {
169                 if (isShutdown()) throw new IllegalStateException("Cannot call deleteRelationshipIndex() after shutdown has been initiated");
170
171                 final ChampRelationshipIndex relationshipIndex = getRelationshipIndices().remove(indexName);
172
173                 if (relationshipIndex == null) throw new ChampIndexNotExistsException();
174                 
175                 getGraph().dropIndex(relationshipIndex.getField().getName(), Edge.class);
176         }
177
178         @Override
179         public ChampCapabilities capabilities() {
180                 return CAPABILITIES;
181         }
182 }