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 java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Properties;
28 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
29 import org.janusgraph.core.JanusGraph;
30 import org.janusgraph.core.JanusGraphFactory;
31 import org.janusgraph.core.JanusGraphTransaction;
32 import org.janusgraph.core.schema.JanusGraphManagement;
33 import org.onap.aai.dbgen.GraphSONPartialIO;
34 import org.onap.aai.dbgen.SchemaGenerator;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class InMemoryGraph {
41 private static final Logger LOGGER = LoggerFactory.getLogger(InMemoryGraph.class);
42 private JanusGraph graph = null;
44 public InMemoryGraph(Builder builder) throws IOException {
46 * Create a In-memory graph
48 try (InputStream is = new FileInputStream(builder.propertyFile)) {
49 graph = JanusGraphFactory.open(builder.propertyFile);
51 Properties graphProps = new Properties();
53 if (builder.isSchemaEnabled) {
54 LOGGER.info("Schema Enabled");
55 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphProps.getProperty("storage.backend"), false);
57 try (JanusGraphTransaction transaction = graph.newTransaction()) {
58 LOGGER.info("Loading snapshot");
59 if (builder.isPartialGraph) {
60 if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
61 transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
63 transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
67 if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
68 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
70 transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
76 } catch (Exception e) {
77 LOGGER.error(String.format("ERROR: Could not load datasnapshot to in memory graph. %n%s",
78 LogFormatTools.getStackTop(e)));
79 throw new IllegalStateException("Could not load datasnapshot to in memory graph");
85 public static class Builder {
86 private String graphsonLocation = "";
87 private String propertyFile = "";
88 private boolean isSchemaEnabled = false;
89 private InputStream seqInputStream = null;
90 private boolean isPartialGraph = false;
93 * Builder constructor doesnt do anything
99 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled)
101 this.graphsonLocation = graphsonFile;
102 this.propertyFile = propertyFile;
103 this.isSchemaEnabled = isSchemaEnabled;
104 return new InMemoryGraph(this);
107 public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled) throws IOException {
108 this.graphsonLocation = null;
109 this.propertyFile = propertyFile;
110 this.isSchemaEnabled = isSchemaEnabled;
111 this.seqInputStream = sis;
112 return new InMemoryGraph(this);
115 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled,
116 boolean isPartialGraph) throws IOException {
117 this.graphsonLocation = graphsonFile;
118 this.propertyFile = propertyFile;
119 this.isSchemaEnabled = isSchemaEnabled;
120 this.isPartialGraph = isPartialGraph;
121 return new InMemoryGraph(this);
124 public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled,
125 boolean isPartialGraph) throws IOException {
126 this.graphsonLocation = null;
127 this.propertyFile = propertyFile;
128 this.isSchemaEnabled = isSchemaEnabled;
129 this.seqInputStream = sis;
130 this.isPartialGraph = isPartialGraph;
131 return new InMemoryGraph(this);
135 public JanusGraph getGraph() {