Integrate aai-schema-ingest library into aai-core
[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  * 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 package org.onap.aai.dbmap;
21
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import org.apache.commons.configuration.PropertiesConfiguration;
31 import org.apache.commons.lang.exception.ExceptionUtils;
32 import org.apache.tinkerpop.gremlin.structure.Graph;
33 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
34 import org.onap.aai.dbgen.SchemaGenerator;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.util.AAIConstants;
37
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import org.janusgraph.core.JanusGraphFactory;
41 import org.janusgraph.core.JanusGraph;
42 import org.janusgraph.core.schema.JanusGraphManagement;
43
44 /**
45  * Database Mapping class which acts as the middle man between the REST
46  * interface objects and JanusGraph DB objects. This class provides methods to commit
47  * the objects received on the REST interface into the JanusGraph graph database as
48  * vertices and edges. Transactions are also managed here by using a JanusGraph
49  * object to load, commit/rollback and shutdown for each request. The data model
50  * rules such as keys/required properties are handled by calling DBMeth methods
51  * which are driven by a specification file in json.
52  * 
53  
54  */
55 public class AAIGraph {
56
57         private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
58         protected static final String COMPONENT = "aaidbmap";
59         protected Map<String, JanusGraph> graphs = new HashMap<>();
60         private static final String REALTIME_DB = "realtime";
61         private static final String CACHED_DB = "cached";
62         private static boolean isInit = false;
63
64
65
66         /**
67          * Instantiates a new AAI graph.
68          */
69         private AAIGraph() {
70                 try {
71                         String serviceName = System.getProperty("aai.service.name", "NA");
72                         String rtConfig = System.getProperty("realtime.db.config");
73                         String cachedConfig = System.getProperty("cached.db.config");
74                         if (rtConfig == null) {
75                                 rtConfig = AAIConstants.REALTIME_DB_CONFIG;
76                         }
77                         if (cachedConfig == null) {
78                                 cachedConfig = AAIConstants.CACHED_DB_CONFIG;
79                         }
80                         this.loadGraph(REALTIME_DB, rtConfig, serviceName);
81                         this.loadGraph(CACHED_DB, cachedConfig, serviceName);
82                 } catch (Exception e) {
83                         throw new RuntimeException("Failed to instantiate graphs", e);
84                 }
85         }
86         
87         private static class Helper {
88                 private static final AAIGraph INSTANCE = new AAIGraph();
89         }
90         
91         /**
92          * Gets the single instance of AAIGraph.
93          *
94          * @return single instance of AAIGraph
95          */
96         public static AAIGraph getInstance() {
97                 isInit = true;
98                 return Helper.INSTANCE;
99         }
100
101         public static boolean isInit() {
102                 return isInit;
103         }
104         
105         private void loadGraph(String name, String configPath, String serviceName) throws Exception {
106             // Graph being opened by JanusGraphFactory is being placed in hashmap to be used later
107                 // These graphs shouldn't be closed until the application shutdown
108                 try {
109                         PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath).forService(serviceName).withGraphType(name).buildConfiguration();
110                         JanusGraph graph = JanusGraphFactory.open(propertiesConfiguration);
111
112                         Properties graphProps = new Properties();
113                         propertiesConfiguration.getKeys().forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
114
115                         if ("inmemory".equals(graphProps.get("storage.backend"))) {
116                                 // Load the propertyKeys, indexes and edge-Labels into the DB
117                                 loadSchema(graph);
118                                 loadSnapShotToInMemoryGraph(graph, graphProps);
119                         }
120
121                         if (graph == null) {
122                                 throw new AAIException("AAI_5102");
123                         }
124
125                         graphs.put(name, graph);
126                 } catch (FileNotFoundException fnfe) {
127                         throw new AAIException("AAI_4001");
128             } catch (IOException e) {
129                         throw new AAIException("AAI_4002");
130             }
131         }
132
133         private void loadSnapShotToInMemoryGraph(JanusGraph graph, Properties graphProps) {
134                 if (logger.isDebugEnabled()) {
135                         logger.debug("Load Snapshot to InMemory Graph");
136                 }
137                 if (graphProps.containsKey("load.snapshot.file")) {
138                         String value = graphProps.getProperty("load.snapshot.file");
139                         if ("true".equals(value)) {
140                                 try (Graph transaction = graph.newTransaction()) {
141                                         String location = System.getProperty("snapshot.location");
142                                         logAndPrint(logger, "Loading snapshot to inmemory graph.");
143                                         transaction.io(IoCore.graphson()).readGraph(location);
144                                         transaction.tx().commit();
145                                         logAndPrint(logger, "Snapshot loaded to inmemory graph.");
146                                 } catch (Exception e) {
147                                         logAndPrint(logger,
148                                                 "ERROR: Could not load datasnapshot to in memory graph. \n"
149                                                         + ExceptionUtils.getFullStackTrace(e));
150                                         throw new RuntimeException(e);
151                                 }
152                         }
153                 }
154         }
155
156         private void loadSchema(JanusGraph graph) {
157                 // Load the propertyKeys, indexes and edge-Labels into the DB
158                 JanusGraphManagement graphMgt = graph.openManagement();
159                 
160                 System.out.println("-- loading schema into JanusGraph");
161                 SchemaGenerator.loadSchemaIntoJanusGraph( graph, graphMgt, "inmemory");
162         }
163
164         /**
165          * Close all of the graph connections made in the instance.
166          */
167         public void graphShutdown() {
168                 graphs.values().stream().filter(JanusGraph::isOpen).forEach(JanusGraph::close);
169         }
170
171         /**
172          * Gets the graph.
173          *
174          * @return the graph
175          */
176         public JanusGraph getGraph() {
177                 return graphs.get(REALTIME_DB);
178         }
179         
180         public void graphShutdown(DBConnectionType connectionType) {
181                 
182                 graphs.get(this.getGraphName(connectionType)).close();
183         }
184         
185         public JanusGraph getGraph(DBConnectionType connectionType) {
186                 return graphs.get(this.getGraphName(connectionType));
187         }
188         
189         private String getGraphName(DBConnectionType connectionType) {
190                 String graphName = "";
191                 if (DBConnectionType.CACHED.equals(connectionType)) {
192                         graphName = this.CACHED_DB;
193                 } else if (DBConnectionType.REALTIME.equals(connectionType)) {
194                         graphName = this.REALTIME_DB;
195                 }
196                 
197                 return graphName;
198         }
199         
200         private void logAndPrint(EELFLogger logger, String msg) {
201                 System.out.println(msg);
202                 logger.info(msg);
203         }
204 }