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.tinkerpop.gremlin.structure.io.IoCore;
29 import org.onap.aai.dbgen.SchemaGenerator;
30 import org.onap.aai.logging.LogFormatTools;
32 import org.janusgraph.core.JanusGraphFactory;
33 import org.janusgraph.core.JanusGraph;
34 import org.janusgraph.core.JanusGraphTransaction;
35 import org.janusgraph.core.schema.JanusGraphManagement;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
40 public class InMemoryGraph {
42 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
43 private JanusGraph graph = null;
46 public InMemoryGraph(Builder builder) throws IOException {
48 * Create a In-memory graph
50 InputStream is = new FileInputStream(builder.propertyFile);
52 graph = JanusGraphFactory.open(builder.propertyFile);
54 Properties graphProps = new Properties();
56 JanusGraphManagement graphMgt = graph.openManagement();
57 if(builder.isSchemaEnabled){
58 LOGGER.info("Schema Enabled");
59 SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt);
61 JanusGraphTransaction transaction = graph.newTransaction();
62 LOGGER.info("Loading snapshot");
63 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
66 } catch (Exception e) {
67 // TODO : Changesysout to logger
69 "ERROR: Could not load datasnapshot to in memory graph. \n" + LogFormatTools.getStackTop(e));
70 throw new IllegalStateException("Could not load datasnapshot to in memory graph");
79 public static class Builder {
80 private String graphsonLocation = "";
81 private String propertyFile = "";
82 private boolean isSchemaEnabled = false;
85 * Builder constructor doesnt do anything
91 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled) throws IOException {
92 this.graphsonLocation = graphsonFile;
93 this.propertyFile = propertyFile;
94 this.isSchemaEnabled = isSchemaEnabled;
95 return new InMemoryGraph(this);
99 public JanusGraph getGraph() {