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=========================================================
21 package org.onap.aai.dbmap;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Properties;
31 import org.apache.commons.lang.exception.ExceptionUtils;
32 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
33 import org.janusgraph.core.JanusGraph;
34 import org.janusgraph.core.JanusGraphFactory;
35 import org.janusgraph.core.JanusGraphTransaction;
36 import org.janusgraph.core.schema.JanusGraphManagement;
37 import org.onap.aai.dbgen.GraphSONPartialIO;
38 import org.onap.aai.dbgen.SchemaGenerator;
39 import org.onap.aai.logging.LogFormatTools;
41 public class InMemoryGraph {
43 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
44 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(graph, graphMgt, graphProps.getProperty("storage.backend"));
61 JanusGraphTransaction transaction = graph.newTransaction();
62 LOGGER.info("Loading snapshot");
63 if (builder.isPartialGraph) {
64 if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
65 transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
67 transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
71 if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
72 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
74 transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
79 } catch (Exception e) {
80 // TODO : Changesysout to logger
82 LOGGER.error("ERROR: Could not load datasnapshot to in memory graph. \n" + LogFormatTools.getStackTop(e));
83 throw new IllegalStateException("Could not load datasnapshot to in memory graph");
91 public static class Builder {
92 private String graphsonLocation = "";
93 private String propertyFile = "";
94 private boolean isSchemaEnabled = false;
95 private InputStream seqInputStream = null;
96 private boolean isPartialGraph = false;
99 * Builder constructor doesnt do anything
105 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled)
107 this.graphsonLocation = graphsonFile;
108 this.propertyFile = propertyFile;
109 this.isSchemaEnabled = isSchemaEnabled;
110 return new InMemoryGraph(this);
113 public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled) throws IOException {
114 this.graphsonLocation = null;
115 this.propertyFile = propertyFile;
116 this.isSchemaEnabled = isSchemaEnabled;
117 this.seqInputStream = sis;
118 return new InMemoryGraph(this);
121 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled,
122 boolean isPartialGraph) throws IOException {
123 this.graphsonLocation = graphsonFile;
124 this.propertyFile = propertyFile;
125 this.isSchemaEnabled = isSchemaEnabled;
126 this.isPartialGraph = isPartialGraph;
127 return new InMemoryGraph(this);
130 public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled,
131 boolean isPartialGraph) throws IOException {
132 this.graphsonLocation = null;
133 this.propertyFile = propertyFile;
134 this.isSchemaEnabled = isSchemaEnabled;
135 this.seqInputStream = sis;
136 this.isPartialGraph = isPartialGraph;
137 return new InMemoryGraph(this);
141 public JanusGraph getGraph() {