Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / InMemoryDBEngine.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.serialization.engines;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
32 import org.janusgraph.core.JanusGraph;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.query.builder.*;
35 import org.onap.aai.serialization.db.InMemoryGraphSingleton;
36 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
37 import org.onap.aai.serialization.engines.query.QueryEngine;
38
39 public class InMemoryDBEngine extends TransactionalGraphEngine {
40
41     /**
42      * Instantiates a new JanusGraph DB engine.
43      *
44      * @param style
45      *        the style
46      * @param loader
47      *        the loader
48      */
49     private JanusGraph graph = null;
50
51     public InMemoryDBEngine(QueryStyle style, Loader loader, JanusGraph graph) {
52         super(style, loader, InMemoryGraphSingleton.getInstance(graph));
53         this.graph = graph;
54     }
55
56     /**
57      * Instantiates a new JanusGraph DB engine.
58      *
59      * @param style
60      *        the style
61      * @param loader
62      *        the loader
63      * @param connect
64      *        the connect
65      */
66     public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, JanusGraph graph) {
67         super(style, loader);
68         if (connect) {
69             this.singleton = InMemoryGraphSingleton.getInstance(graph);
70         }
71         this.graph = graph;
72     }
73
74     @Override
75     public QueryEngine getQueryEngine() {
76
77         if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
78
79             GraphTraversalSource traversalSource = graph.traversal();
80             return new GraphTraversalQueryEngine(traversalSource);
81
82         } else {
83             throw new IllegalArgumentException("Query Engine type not recognized");
84         }
85
86     }
87
88     @Override
89     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
90         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
91             return new GremlinTraversal<>(loader, graph.traversal());
92         } else if (style.equals(QueryStyle.TRAVERSAL)) {
93             return new TraversalQuery<>(loader, graph.traversal());
94         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
95             return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
96         } else {
97             throw new IllegalArgumentException("Query Builder type is Not recognized");
98         }
99
100     }
101
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public boolean setListProperty(Vertex v, String name, List<?> objs) {
107
108         // clear out list full replace style
109
110         Iterator<VertexProperty<Object>> iterator = v.properties(name);
111         while (iterator.hasNext()) {
112             iterator.next().remove();
113         }
114         if (objs != null) {
115             for (Object obj : objs) {
116                 v.property(name, obj);
117             }
118         }
119         return true;
120     }
121
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public List<Object> getListProperty(Vertex v, String name) {
127
128         List<Object> result = new ArrayList<>();
129
130         Iterator<VertexProperty<Object>> iterator = v.properties(name);
131
132         while (iterator.hasNext()) {
133             result.add(iterator.next().value());
134         }
135
136         if (result.isEmpty()) {
137             result = null;
138         }
139
140         return result;
141
142     }
143
144     @Override
145     public QueryBuilder<Vertex> getQueryBuilder() {
146         return getQueryBuilder(this.loader);
147     }
148
149     @Override
150     public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
151         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
152             return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
153         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
154             return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
155         } else if (style.equals(QueryStyle.TRAVERSAL)) {
156             return new TraversalQuery<>(loader, graph.traversal());
157         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
158             return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
159         } else {
160             throw new IllegalArgumentException("Query Builder type not recognized");
161         }
162
163     }
164
165     @Override
166     public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
167         return getQueryBuilder(this.loader, start);
168     }
169
170     public GraphTraversalSource getTraversalSource() {
171         return graph.traversal();
172     }
173
174     @Override
175     public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
176         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
177             return new GremlinTraversal<>(loader, graph.traversal(), start);
178         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
179             return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
180         } else if (style.equals(QueryStyle.TRAVERSAL)) {
181             return new TraversalQuery<>(loader, graph.traversal(), start);
182         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
183             return new TraversalURIOptimizedQuery<>(loader, graph.traversal(), start);
184         } else {
185             throw new IllegalArgumentException("Query Builder type not recognized");
186         }
187
188     }
189
190     @Override
191     public Graph startTransaction() {
192         if (this.tx() == null) {
193             this.currentTx = graph.newTransaction();
194             this.currentTraversal = this.tx().traversal();
195             this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());
196         }
197         return currentTx;
198     }
199
200 }