2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.dbmap;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
28 import java.util.Properties;
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;
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;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
43 public class InMemoryGraph {
45 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
46 private TitanGraph graph = null;
49 public InMemoryGraph(Builder builder) throws IOException {
51 * Create a In-memory graph
53 InputStream is = new FileInputStream(builder.propertyFile);
55 graph = TitanFactory.open(builder.propertyFile);
57 Properties graphProps = new Properties();
59 TitanManagement graphMgt = graph.openManagement();
60 if(builder.isSchemaEnabled){
61 LOGGER.info("Schema Enabled");
62 SchemaGenerator.loadSchemaIntoTitan(graph, graphMgt);
64 TitanTransaction transaction = graph.newTransaction();
65 LOGGER.info("Loading snapshot");
66 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
69 } catch (Exception e) {
70 // TODO : Changesysout to logger
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");
82 public static class Builder {
83 private String graphsonLocation = "";
84 private String propertyFile = "";
85 private boolean isSchemaEnabled = false;
88 * Builder constructor doesnt do anything
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);
102 public TitanGraph getGraph() {