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