Fixed Sonar "Blocker Bugs"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / InMemoryGraph.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.dbmap;
22
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;
36
37 public class InMemoryGraph {
38
39     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
40     private JanusGraph graph = null;
41
42     public InMemoryGraph(final Builder builder) throws IOException {
43         /*
44          * Create a In-memory graph
45          */
46         try (final InputStream is = new FileInputStream(builder.propertyFile);) {
47             graph = JanusGraphFactory.open(builder.propertyFile);
48
49             final Properties graphProps = new Properties();
50             graphProps.load(is);
51             final JanusGraphManagement graphMgt = graph.openManagement();
52             if (builder.isSchemaEnabled) {
53                 LOGGER.info("Schema Enabled");
54                 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, graphProps.getProperty("storage.backend"));
55             }
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);
61                     } else {
62                         transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
63                             graph);
64                     }
65                 } else {
66                     if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
67                         transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
68                     } else {
69                         transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
70                     }
71                 }
72                 transaction.commit();
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");
76             }
77
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");
81         }
82     }
83
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;
90
91         /*
92          * Builder constructor doesnt do anything
93          */
94         public Builder() {
95             // Do nothing
96         }
97
98         public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled)
99                 throws IOException {
100             this.graphsonLocation = graphsonFile;
101             this.propertyFile = propertyFile;
102             this.isSchemaEnabled = isSchemaEnabled;
103             return new InMemoryGraph(this);
104         }
105
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);
112         }
113
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);
121         }
122
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);
131         }
132     }
133
134     public JanusGraph getGraph() {
135         return graph;
136     }
137
138 }