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