2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.dbmap;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
26 import java.util.Properties;
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;
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;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
42 public class InMemoryGraph {
44 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
45 private JanusGraph graph = null;
48 public InMemoryGraph(Builder builder) throws IOException {
50 * Create a In-memory graph
52 InputStream is = new FileInputStream(builder.propertyFile);
54 graph = JanusGraphFactory.open(builder.propertyFile);
56 Properties graphProps = new Properties();
58 JanusGraphManagement graphMgt = graph.openManagement();
59 if(builder.isSchemaEnabled){
60 LOGGER.info("Schema Enabled");
61 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, graphProps.getProperty("storage.backend"));
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);
70 transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream, graph);
74 if ( (builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0) ) {
75 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
78 transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
83 } catch (Exception e) {
84 // TODO : Changesysout to logger
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");
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;
106 * Builder constructor doesnt do anything
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);
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);
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);
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);
145 public JanusGraph getGraph() {