Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / AAIGraph.java
index e62e8e7..46fc2c6 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.dbmap;
 
@@ -29,6 +27,7 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
@@ -58,8 +57,9 @@ public class AAIGraph {
        private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
        protected static final String COMPONENT = "aaidbmap";
        protected Map<String, TitanGraph> graphs = new HashMap<>();
-       private final String REALTIME_DB = "realtime";
-       private final String CACHED_DB = "cached";
+       private static final String REALTIME_DB = "realtime";
+       private static final String CACHED_DB = "cached";
+       private static boolean isInit = false;
 
 
 
@@ -68,6 +68,7 @@ public class AAIGraph {
         */
        private 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) {
@@ -76,8 +77,8 @@ public class AAIGraph {
                        if (cachedConfig == null) {
                                cachedConfig = AAIConstants.CACHED_DB_CONFIG;
                        }
-                       this.loadGraph(REALTIME_DB, rtConfig);
-                       this.loadGraph(CACHED_DB, cachedConfig);
+                       this.loadGraph(REALTIME_DB, rtConfig, serviceName);
+                       this.loadGraph(CACHED_DB, cachedConfig, serviceName);
                } catch (Exception e) {
                        throw new RuntimeException("Failed to instantiate graphs", e);
                }
@@ -93,17 +94,23 @@ public class AAIGraph {
         * @return single instance of AAIGraph
         */
        public static AAIGraph getInstance() {
+               isInit = true;
                return Helper.INSTANCE;
        }
+
+       public static boolean isInit() {
+               return isInit;
+       }
        
-       private void loadGraph(String name, String configPath) throws AAIException {
+       private void loadGraph(String name, String configPath, String serviceName) throws Exception {
            // Graph being opened by TitanFactory is being placed in hashmap to be used later
                // These graphs shouldn't be closed until the application shutdown
-           TitanGraph graph = TitanFactory.open(configPath);
-               try (InputStream is = new FileInputStream(configPath)) {
+               try {
+                       PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath).forService(serviceName).withGraphType(name).buildConfiguration();
+                       TitanGraph graph = TitanFactory.open(propertiesConfiguration);
 
                        Properties graphProps = new Properties();
-                       graphProps.load(is);
+                       propertiesConfiguration.getKeys().forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
 
                        if ("inmemory".equals(graphProps.get("storage.backend"))) {
                                // Load the propertyKeys, indexes and edge-Labels into the DB
@@ -155,10 +162,10 @@ public class AAIGraph {
        }
 
        /**
-        * Graph shutdown.
+        * Close all of the graph connections made in the instance.
         */
        public void graphShutdown() {
-               graphs.get(REALTIME_DB).close();
+               graphs.values().stream().filter(TitanGraph::isOpen).forEach(TitanGraph::close);
        }
 
        /**