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