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