51a2c221b340dca333327a2fda2852075b215051
[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             JanusGraphManagement graphMgt = graph.openManagement();
54             if (builder.isSchemaEnabled) {
55                 LOGGER.info("Schema Enabled");
56                 SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt, graphProps.getProperty("storage.backend"));
57             }
58             try (JanusGraphTransaction transaction = graph.newTransaction()) {
59                 LOGGER.info("Loading snapshot");
60                 if (builder.isPartialGraph) {
61                     if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
62                         transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
63                     } else {
64                         transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
65                                 graph);
66                     }
67                 } else {
68                     if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
69                         transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
70                     } else {
71                         transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
72                     }
73                 }
74                 transaction.commit();
75             }
76
77         } catch (Exception e) {
78             LOGGER.error(String.format("ERROR: Could not load datasnapshot to in memory graph. %n%s",
79                     LogFormatTools.getStackTop(e)));
80             throw new IllegalStateException("Could not load datasnapshot to in memory graph");
81
82         }
83
84     }
85
86     public static class Builder {
87         private String graphsonLocation = "";
88         private String propertyFile = "";
89         private boolean isSchemaEnabled = false;
90         private InputStream seqInputStream = null;
91         private boolean isPartialGraph = false;
92
93         /*
94          * Builder constructor doesnt do anything
95          */
96         public Builder() {
97             // Do nothing
98         }
99
100         public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled)
101                 throws IOException {
102             this.graphsonLocation = graphsonFile;
103             this.propertyFile = propertyFile;
104             this.isSchemaEnabled = isSchemaEnabled;
105             return new InMemoryGraph(this);
106         }
107
108         public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled) throws IOException {
109             this.graphsonLocation = null;
110             this.propertyFile = propertyFile;
111             this.isSchemaEnabled = isSchemaEnabled;
112             this.seqInputStream = sis;
113             return new InMemoryGraph(this);
114         }
115
116         public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled,
117                 boolean isPartialGraph) throws IOException {
118             this.graphsonLocation = graphsonFile;
119             this.propertyFile = propertyFile;
120             this.isSchemaEnabled = isSchemaEnabled;
121             this.isPartialGraph = isPartialGraph;
122             return new InMemoryGraph(this);
123         }
124
125         public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled,
126                 boolean isPartialGraph) throws IOException {
127             this.graphsonLocation = null;
128             this.propertyFile = propertyFile;
129             this.isSchemaEnabled = isSchemaEnabled;
130             this.seqInputStream = sis;
131             this.isPartialGraph = isPartialGraph;
132             return new InMemoryGraph(this);
133         }
134     }
135
136     public JanusGraph getGraph() {
137         return graph;
138     }
139
140 }