Update from titan to using janusgraph
[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 package org.onap.aai.dbmap;
21
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import java.util.Properties;
27
28 import org.apache.commons.lang.exception.ExceptionUtils;
29 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
30 import org.onap.aai.dbgen.SchemaGenerator;
31 import org.onap.aai.logging.LogFormatTools;
32
33 import org.janusgraph.core.JanusGraphFactory;
34 import org.janusgraph.core.JanusGraph;
35 import org.janusgraph.core.JanusGraphTransaction;
36 import org.janusgraph.core.schema.JanusGraphManagement;
37
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40
41 public class InMemoryGraph {
42
43         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
44         private JanusGraph graph = null;
45
46
47         public InMemoryGraph(Builder builder) throws IOException  {
48                 /*
49                  * Create a In-memory graph
50                  */
51                 InputStream is = new FileInputStream(builder.propertyFile);
52                 try {
53                         graph = JanusGraphFactory.open(builder.propertyFile);
54                         
55                         Properties graphProps = new Properties();
56                         graphProps.load(is);
57                         JanusGraphManagement graphMgt = graph.openManagement();
58             if(builder.isSchemaEnabled){
59                 LOGGER.info("Schema Enabled");
60                 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt);
61             }
62                         JanusGraphTransaction transaction = graph.newTransaction();
63                         LOGGER.info("Loading snapshot");
64                         transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
65                         transaction.commit();
66                         
67                 } catch (Exception e) {
68                         // TODO : Changesysout to logger
69                         LOGGER.error(
70                                         "ERROR: Could not load datasnapshot to in memory graph. \n" + LogFormatTools.getStackTop(e));
71                         throw new IllegalStateException("Could not load datasnapshot to in memory graph");
72
73                 }
74                 finally{
75                         is.close();
76                 }
77
78         }
79
80         public static class Builder {
81                 private String graphsonLocation = "";
82                 private String propertyFile = "";
83                 private boolean isSchemaEnabled = false;
84
85                 /*
86                  * Builder constructor doesnt do anything
87                  */
88                 public Builder() {
89                         //Do nothing
90                 }
91
92                 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled) throws IOException {
93                         this.graphsonLocation = graphsonFile;
94                         this.propertyFile = propertyFile;
95                         this.isSchemaEnabled = isSchemaEnabled;
96                         return new InMemoryGraph(this);
97                 }
98         }
99
100         public JanusGraph getGraph() {
101                 return graph;
102         }
103
104 }