Enhance SchemaGenerator to also generate indices for relationships
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / InMemoryGraph.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.dbmap;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Properties;
27
28 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
29 import org.janusgraph.core.JanusGraph;
30 import org.janusgraph.core.JanusGraphFactory;
31 import org.janusgraph.core.JanusGraphTransaction;
32 import org.janusgraph.core.schema.JanusGraphManagement;
33 import org.onap.aai.dbgen.GraphSONPartialIO;
34 import org.onap.aai.dbgen.SchemaGenerator;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class InMemoryGraph {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(InMemoryGraph.class);
42     private JanusGraph graph = null;
43
44     public InMemoryGraph(Builder builder) throws IOException {
45         /*
46          * Create a In-memory graph
47          */
48         try (InputStream is = new FileInputStream(builder.propertyFile)) {
49             graph = JanusGraphFactory.open(builder.propertyFile);
50
51             Properties graphProps = new Properties();
52             graphProps.load(is);
53             if (builder.isSchemaEnabled) {
54                 LOGGER.info("Schema Enabled");
55                 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphProps.getProperty("storage.backend"), false);
56             }
57             try (JanusGraphTransaction transaction = graph.newTransaction()) {
58                 LOGGER.info("Loading snapshot");
59                 if (builder.isPartialGraph) {
60                     if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
61                         transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
62                     } else {
63                         transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
64                                 graph);
65                     }
66                 } else {
67                     if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
68                         transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
69                     } else {
70                         transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
71                     }
72                 }
73                 transaction.commit();
74             }
75
76         } catch (Exception e) {
77             LOGGER.error(String.format("ERROR: Could not load datasnapshot to in memory graph. %n%s",
78                     LogFormatTools.getStackTop(e)));
79             throw new IllegalStateException("Could not load datasnapshot to in memory graph");
80
81         }
82
83     }
84
85     public static class Builder {
86         private String graphsonLocation = "";
87         private String propertyFile = "";
88         private boolean isSchemaEnabled = false;
89         private InputStream seqInputStream = null;
90         private boolean isPartialGraph = false;
91
92         /*
93          * Builder constructor doesnt do anything
94          */
95         public Builder() {
96             // Do nothing
97         }
98
99         public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled)
100                 throws IOException {
101             this.graphsonLocation = graphsonFile;
102             this.propertyFile = propertyFile;
103             this.isSchemaEnabled = isSchemaEnabled;
104             return new InMemoryGraph(this);
105         }
106
107         public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled) throws IOException {
108             this.graphsonLocation = null;
109             this.propertyFile = propertyFile;
110             this.isSchemaEnabled = isSchemaEnabled;
111             this.seqInputStream = sis;
112             return new InMemoryGraph(this);
113         }
114
115         public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled,
116                 boolean isPartialGraph) throws IOException {
117             this.graphsonLocation = graphsonFile;
118             this.propertyFile = propertyFile;
119             this.isSchemaEnabled = isSchemaEnabled;
120             this.isPartialGraph = isPartialGraph;
121             return new InMemoryGraph(this);
122         }
123
124         public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled,
125                 boolean isPartialGraph) throws IOException {
126             this.graphsonLocation = null;
127             this.propertyFile = propertyFile;
128             this.isSchemaEnabled = isSchemaEnabled;
129             this.seqInputStream = sis;
130             this.isPartialGraph = isPartialGraph;
131             return new InMemoryGraph(this);
132         }
133     }
134
135     public JanusGraph getGraph() {
136         return graph;
137     }
138
139 }