Enhance SchemaGenerator to also generate indices for relationships
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / AAIGraph.java
index 5bb4b65..d9994f5 100644 (file)
 
 package org.onap.aai.dbmap;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.io.FileNotFoundException;
+import java.util.Properties;
+
 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;
@@ -36,9 +37,8 @@ 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 java.io.FileNotFoundException;
-import java.util.Properties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Database Mapping class which acts as the middle man between the REST
@@ -54,6 +54,7 @@ import java.util.Properties;
 public class AAIGraph {
 
     private static final Logger logger = LoggerFactory.getLogger(AAIGraph.class);
+    private static final String IN_MEMORY = "inmemory";
     protected JanusGraph graph;
     private static boolean isInit = false;
 
@@ -107,7 +108,7 @@ public class AAIGraph {
             propertiesConfiguration.getKeys()
                     .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);
@@ -131,13 +132,13 @@ public class AAIGraph {
             if ("true".equals(value)) {
                 try (Graph transaction = graph.newTransaction()) {
                     String location = System.getProperty("snapshot.location");
-                    logAndPrint("Loading snapshot to inmemory graph.");
+                    logger.info("Loading snapshot to inmemory graph.");
                     transaction.io(IoCore.graphson()).readGraph(location);
                     transaction.tx().commit();
-                    logAndPrint("Snapshot loaded to inmemory graph.");
+                    logger.info("Snapshot loaded to inmemory graph.");
                 } catch (Exception e) {
-                    logAndPrint("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);
                 }
             }
@@ -146,13 +147,14 @@ public class AAIGraph {
 
     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");
-        if ("true".equals(SpringContextAware.getApplicationContext().getEnvironment().getProperty("history.enabled", "false"))) {
-            SchemaGenerator4Hist.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, graphMgt, "inmemory");
+            SchemaGenerator.loadSchemaIntoJanusGraph(graph, IN_MEMORY, dbNotEmpty);
         }
     }
 
@@ -174,8 +176,4 @@ public class AAIGraph {
         return graph;
     }
 
-    private void logAndPrint(String msg) {
-        System.out.println(msg);
-        logger.info(msg);
-    }
 }