Enhance SchemaGenerator to also generate indices for relationships
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / AAIGraph.java
index ca4e02f..d9994f5 100644 (file)
 
 package org.onap.aai.dbmap;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 import java.io.FileNotFoundException;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Properties;
-import org.apache.commons.configuration.ConfigurationException;
+
 import org.apache.commons.configuration.PropertiesConfiguration;
-import org.apache.commons.lang.exception.ExceptionUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
 import org.janusgraph.core.JanusGraph;
 import org.janusgraph.core.JanusGraphFactory;
 import org.janusgraph.core.schema.JanusGraphManagement;
+import org.onap.aai.config.SpringContextAware;
 import org.onap.aai.dbgen.SchemaGenerator;
+import org.onap.aai.dbgen.SchemaGenerator4Hist;
 import org.onap.aai.exceptions.AAIException;
 import org.onap.aai.util.AAIConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Database Mapping class which acts as the middle man between the REST
@@ -48,16 +48,14 @@ import org.onap.aai.util.AAIConstants;
  * object to load, commit/rollback and shutdown for each request. The data model
  * rules such as keys/required properties are handled by calling DBMeth methods
  * which are driven by a specification file in json.
- * 
- * 
+ *
+ *
  */
 public class AAIGraph {
 
-    private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
-    protected static final String COMPONENT = "aaidbmap";
-    protected Map<String, JanusGraph> graphs = new HashMap<>();
-    private static final String REALTIME_DB = "realtime";
-    private static final String CACHED_DB = "cached";
+    private static final Logger logger = LoggerFactory.getLogger(AAIGraph.class);
+    private static final String IN_MEMORY = "inmemory";
+    protected JanusGraph graph;
     private static boolean isInit = false;
 
     /**
@@ -67,15 +65,10 @@ public class AAIGraph {
         try {
             String serviceName = System.getProperty("aai.service.name", "NA");
             String rtConfig = System.getProperty("realtime.db.config");
-            String cachedConfig = System.getProperty("cached.db.config");
             if (rtConfig == null) {
                 rtConfig = AAIConstants.REALTIME_DB_CONFIG;
             }
-            if (cachedConfig == null) {
-                cachedConfig = AAIConstants.CACHED_DB_CONFIG;
-            }
-            this.loadGraph(REALTIME_DB, rtConfig, serviceName);
-            this.loadGraph(CACHED_DB, cachedConfig, serviceName);
+            this.loadGraph(rtConfig, serviceName);
         } catch (Exception e) {
             throw new RuntimeException("Failed to instantiate graphs", e);
         }
@@ -103,20 +96,19 @@ public class AAIGraph {
         return isInit;
     }
 
-    private void loadGraph(final String name, final String configPath, final String serviceName)
-        throws AAIException, ConfigurationException {
+    private void loadGraph(String configPath, String serviceName) throws Exception {
         // Graph being opened by JanusGraphFactory is being placed in hashmap to be used later
         // These graphs shouldn't be closed until the application shutdown
         try {
-            final PropertiesConfiguration propertiesConfiguration =
-                new AAIGraphConfig.Builder(configPath).forService(serviceName).withGraphType(name).buildConfiguration();
-            final JanusGraph graph = JanusGraphFactory.open(propertiesConfiguration);
+            PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath)
+                    .forService(serviceName).withGraphType("realtime").buildConfiguration();
+            graph = JanusGraphFactory.open(propertiesConfiguration);
 
-            final Properties graphProps = new Properties();
+            Properties graphProps = new Properties();
             propertiesConfiguration.getKeys()
-                .forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
+                    .forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
 
-            if ("inmemory".equals(graphProps.get("storage.backend"))) {
+            if (IN_MEMORY.equals(graphProps.get("storage.backend"))) {
                 // Load the propertyKeys, indexes and edge-Labels into the DB
                 loadSchema(graph);
                 loadSnapShotToInMemoryGraph(graph, graphProps);
@@ -126,13 +118,12 @@ public class AAIGraph {
                 throw new AAIException("AAI_5102");
             }
 
-            graphs.put(name, graph);
-        } catch (final FileNotFoundException fnfe) {
-            throw new AAIException("AAI_4001");
+        } catch (FileNotFoundException e) {
+            throw new AAIException("AAI_4001", e);
         }
     }
 
-    private void loadSnapShotToInMemoryGraph(final JanusGraph graph, final Properties graphProps) {
+    private void loadSnapShotToInMemoryGraph(JanusGraph graph, Properties graphProps) {
         if (logger.isDebugEnabled()) {
             logger.debug("Load Snapshot to InMemory Graph");
         }
@@ -141,32 +132,39 @@ public class AAIGraph {
             if ("true".equals(value)) {
                 try (Graph transaction = graph.newTransaction()) {
                     String location = System.getProperty("snapshot.location");
-                    logAndPrint(logger, "Loading snapshot to inmemory graph.");
+                    logger.info("Loading snapshot to inmemory graph.");
                     transaction.io(IoCore.graphson()).readGraph(location);
                     transaction.tx().commit();
-                    logAndPrint(logger, "Snapshot loaded to inmemory graph.");
+                    logger.info("Snapshot loaded to inmemory graph.");
                 } catch (Exception e) {
-                    logAndPrint(logger, "ERROR: Could not load datasnapshot to in memory graph. \n"
-                            + ExceptionUtils.getFullStackTrace(e));
+                    logger.info(String.format("ERROR: Could not load datasnapshot to in memory graph. %n%s",
+                            ExceptionUtils.getStackTrace(e)));
                     throw new RuntimeException(e);
                 }
             }
         }
     }
 
-    private void loadSchema(final JanusGraph graph) {
+    private void loadSchema(JanusGraph graph) {
         // Load the propertyKeys, indexes and edge-Labels into the DB
-        JanusGraphManagement graphMgt = graph.openManagement();
-
-        System.out.println("-- loading schema into JanusGraph");
-        SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, "inmemory");
+        boolean dbNotEmpty = graph.traversal().V().limit(1).hasNext();
+        logger.info("-- loading schema into JanusGraph");
+        if ("true".equals(
+            SpringContextAware.getApplicationContext().getEnvironment().getProperty("history.enabled", "false"))) {
+            JanusGraphManagement graphMgt = graph.openManagement();
+            SchemaGenerator4Hist.loadSchemaIntoJanusGraph(graphMgt, IN_MEMORY);
+        } else {
+            SchemaGenerator.loadSchemaIntoJanusGraph(graph, IN_MEMORY, dbNotEmpty);
+        }
     }
 
     /**
      * Close all of the graph connections made in the instance.
      */
     public void graphShutdown() {
-        graphs.values().stream().filter(JanusGraph::isOpen).forEach(JanusGraph::close);
+        if (graph != null && graph.isOpen()) {
+            graph.close();
+        }
     }
 
     /**
@@ -175,31 +173,7 @@ public class AAIGraph {
      * @return the graph
      */
     public JanusGraph getGraph() {
-        return graphs.get(REALTIME_DB);
+        return graph;
     }
 
-    public void graphShutdown(final DBConnectionType connectionType) {
-
-        graphs.get(this.getGraphName(connectionType)).close();
-    }
-
-    public JanusGraph getGraph(final DBConnectionType connectionType) {
-        return graphs.get(this.getGraphName(connectionType));
-    }
-
-    private String getGraphName(final DBConnectionType connectionType) {
-        String graphName = "";
-        if (DBConnectionType.CACHED.equals(connectionType)) {
-            graphName = this.CACHED_DB;
-        } else if (DBConnectionType.REALTIME.equals(connectionType)) {
-            graphName = this.REALTIME_DB;
-        }
-
-        return graphName;
-    }
-
-    private void logAndPrint(final EELFLogger logger, final String msg) {
-        System.out.println(msg);
-        logger.info(msg);
-    }
 }