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;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Properties;
29 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
30 import org.janusgraph.core.JanusGraph;
31 import org.janusgraph.core.JanusGraphFactory;
32 import org.janusgraph.core.JanusGraphTransaction;
33 import org.janusgraph.core.schema.JanusGraphManagement;
34 import org.onap.aai.dbgen.GraphSONPartialIO;
35 import org.onap.aai.dbgen.SchemaGenerator;
37 public class InMemoryGraph {
39 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
40 private JanusGraph graph = null;
42 public InMemoryGraph(final Builder builder) throws IOException {
44 * Create a In-memory graph
46 try (final InputStream is = new FileInputStream(builder.propertyFile);) {
47 graph = JanusGraphFactory.open(builder.propertyFile);
49 final Properties graphProps = new Properties();
51 final JanusGraphManagement graphMgt = graph.openManagement();
52 if (builder.isSchemaEnabled) {
53 LOGGER.info("Schema Enabled");
54 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, graphProps.getProperty("storage.backend"));
56 try (final JanusGraphTransaction transaction = graph.newTransaction();) {
57 LOGGER.info("Loading snapshot");
58 if (builder.isPartialGraph) {
59 if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
60 transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
62 transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
66 if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
67 transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
69 transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
73 } catch (final IOException e) {
74 LOGGER.error("ERROR: Could not load datasnapshot to in memory graph. \n", e);
75 throw new IllegalStateException("Could not load datasnapshot to in memory graph");
78 } catch (final IOException e) {
79 LOGGER.error("ERROR: Could not load datasnapshot to in memory graph. \n", e);
80 throw new IllegalStateException("Could not load datasnapshot to in memory graph");
84 public static class Builder {
85 private String graphsonLocation = "";
86 private String propertyFile = "";
87 private boolean isSchemaEnabled = false;
88 private InputStream seqInputStream = null;
89 private boolean isPartialGraph = false;
92 * Builder constructor doesnt do anything
98 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled)
100 this.graphsonLocation = graphsonFile;
101 this.propertyFile = propertyFile;
102 this.isSchemaEnabled = isSchemaEnabled;
103 return new InMemoryGraph(this);
106 public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled) throws IOException {
107 this.graphsonLocation = null;
108 this.propertyFile = propertyFile;
109 this.isSchemaEnabled = isSchemaEnabled;
110 this.seqInputStream = sis;
111 return new InMemoryGraph(this);
114 public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled,
115 boolean isPartialGraph) throws IOException {
116 this.graphsonLocation = graphsonFile;
117 this.propertyFile = propertyFile;
118 this.isSchemaEnabled = isSchemaEnabled;
119 this.isPartialGraph = isPartialGraph;
120 return new InMemoryGraph(this);
123 public InMemoryGraph build(InputStream sis, String propertyFile, boolean isSchemaEnabled,
124 boolean isPartialGraph) throws IOException {
125 this.graphsonLocation = null;
126 this.propertyFile = propertyFile;
127 this.isSchemaEnabled = isSchemaEnabled;
128 this.seqInputStream = sis;
129 this.isPartialGraph = isPartialGraph;
130 return new InMemoryGraph(this);
134 public JanusGraph getGraph() {