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.SchemaGenerator;
31 import org.onap.aai.logging.LogFormatTools;
33 import com.thinkaurelius.titan.core.TitanFactory;
34 import com.thinkaurelius.titan.core.TitanGraph;
35 import com.thinkaurelius.titan.core.TitanTransaction;
36 import com.thinkaurelius.titan.core.schema.TitanManagement;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
41 public class InMemoryGraph {
43 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
44 private TitanGraph graph = null;
47 public InMemoryGraph(Builder builder) throws IOException {
49 * Create a In-memory graph
51 InputStream is = new FileInputStream(builder.propertyFile);
53 graph = TitanFactory.open(builder.propertyFile);
55 Properties graphProps = new Properties();
57 TitanManagement graphMgt = graph.openManagement();
58 if(builder.isSchemaEnabled){
59 LOGGER.info("Schema Enabled");
60 SchemaGenerator.loadSchemaIntoTitan(graph, graphMgt);
62 TitanTransaction transaction = graph.newTransaction();
63 LOGGER.info("Loading snapshot");
64 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
67 } catch (Exception e) {
68 // TODO : Changesysout to logger
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");
80 public static class Builder {
81 private String graphsonLocation = "";
82 private String propertyFile = "";
83 private boolean isSchemaEnabled = false;
86 * Builder constructor doesnt do anything
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);
100 public TitanGraph getGraph() {