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