Merge "Release 1.14.0 maven artifact"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / AAIGraph.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.dbmap;
24
25 import java.io.FileNotFoundException;
26 import java.util.Properties;
27
28 import org.apache.commons.configuration.PropertiesConfiguration;
29 import org.apache.commons.lang3.exception.ExceptionUtils;
30 import org.apache.tinkerpop.gremlin.structure.Graph;
31 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
32 import org.janusgraph.core.JanusGraph;
33 import org.janusgraph.core.JanusGraphFactory;
34 import org.janusgraph.core.schema.JanusGraphManagement;
35 import org.onap.aai.config.SpringContextAware;
36 import org.onap.aai.dbgen.SchemaGenerator;
37 import org.onap.aai.dbgen.SchemaGenerator4Hist;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.util.AAIConstants;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Database Mapping class which acts as the middle man between the REST
45  * interface objects and JanusGraph DB objects. This class provides methods to commit
46  * the objects received on the REST interface into the JanusGraph graph database as
47  * vertices and edges. Transactions are also managed here by using a JanusGraph
48  * object to load, commit/rollback and shutdown for each request. The data model
49  * rules such as keys/required properties are handled by calling DBMeth methods
50  * which are driven by a specification file in json.
51  *
52  *
53  */
54 public class AAIGraph {
55
56     private static final Logger logger = LoggerFactory.getLogger(AAIGraph.class);
57     private static final String IN_MEMORY = "inmemory";
58     protected JanusGraph graph;
59     private static boolean isInit = false;
60
61     /**
62      * Instantiates a new AAI graph.
63      */
64     private AAIGraph() {
65         try {
66             String serviceName = System.getProperty("aai.service.name", "NA");
67             String rtConfig = System.getProperty("realtime.db.config");
68             if (rtConfig == null) {
69                 rtConfig = AAIConstants.REALTIME_DB_CONFIG;
70             }
71             this.loadGraph(rtConfig, serviceName);
72         } catch (Exception e) {
73             logger.error("Failed to instantiate graph", e);
74             throw new RuntimeException("Failed to instantiate graphs", e);
75         }
76     }
77
78     private static class Helper {
79         private static final AAIGraph INSTANCE = new AAIGraph();
80
81         private Helper() {
82
83         }
84     }
85
86     /**
87      * Gets the single instance of AAIGraph.
88      *
89      * @return single instance of AAIGraph
90      */
91     public static AAIGraph getInstance() {
92         isInit = true;
93         return Helper.INSTANCE;
94     }
95
96     public static boolean isInit() {
97         return isInit;
98     }
99
100     private void loadGraph(String configPath, String serviceName) throws Exception {
101         // Graph being opened by JanusGraphFactory is being placed in hashmap to be used later
102         // These graphs shouldn't be closed until the application shutdown
103         try {
104             PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath)
105                     .forService(serviceName).withGraphType("realtime").buildConfiguration();
106             graph = JanusGraphFactory.open(propertiesConfiguration);
107
108             Properties graphProps = new Properties();
109             propertiesConfiguration.getKeys()
110                     .forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
111
112             if (IN_MEMORY.equals(graphProps.get("storage.backend"))) {
113                 // Load the propertyKeys, indexes and edge-Labels into the DB
114                 loadSchema(graph);
115                 loadSnapShotToInMemoryGraph(graph, graphProps);
116             }
117
118             if (graph == null) {
119                 throw new AAIException("AAI_5102");
120             }
121
122         } catch (FileNotFoundException e) {
123             throw new AAIException("AAI_4001", e);
124         }
125     }
126
127     private void loadSnapShotToInMemoryGraph(JanusGraph graph, Properties graphProps) {
128         if (logger.isDebugEnabled()) {
129             logger.debug("Load Snapshot to InMemory Graph");
130         }
131         if (graphProps.containsKey("load.snapshot.file")) {
132             String value = graphProps.getProperty("load.snapshot.file");
133             if ("true".equals(value)) {
134                 try (Graph transaction = graph.newTransaction()) {
135                     String location = System.getProperty("snapshot.location");
136                     logger.info("Loading snapshot to inmemory graph.");
137                     transaction.io(IoCore.graphson()).readGraph(location);
138                     transaction.tx().commit();
139                     logger.info("Snapshot loaded to inmemory graph.");
140                 } catch (Exception e) {
141                     logger.info(String.format("ERROR: Could not load datasnapshot to in memory graph. %n%s",
142                             ExceptionUtils.getStackTrace(e)));
143                     throw new RuntimeException(e);
144                 }
145             }
146         }
147     }
148
149     private void loadSchema(JanusGraph graph) {
150         // Load the propertyKeys, indexes and edge-Labels into the DB
151         boolean dbNotEmpty = graph.traversal().V().limit(1).hasNext();
152         logger.info("-- loading schema into JanusGraph");
153         if ("true".equals(
154             SpringContextAware.getApplicationContext().getEnvironment().getProperty("history.enabled", "false"))) {
155             JanusGraphManagement graphMgt = graph.openManagement();
156             SchemaGenerator4Hist.loadSchemaIntoJanusGraph(graphMgt, IN_MEMORY);
157         } else {
158             SchemaGenerator.loadSchemaIntoJanusGraph(graph, IN_MEMORY, dbNotEmpty);
159         }
160     }
161
162     /**
163      * Close all of the graph connections made in the instance.
164      */
165     public void graphShutdown() {
166         if (graph != null && graph.isOpen()) {
167             graph.close();
168         }
169     }
170
171     /**
172      * Gets the graph.
173      *
174      * @return the graph
175      */
176     public JanusGraph getGraph() {
177         return graph;
178     }
179
180 }