2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.dbmap;
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;
30 import java.util.Properties;
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;
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;
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.
57 public class AAIGraph {
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;
69 * Instantiates a new AAI graph.
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;
79 if (cachedConfig == null) {
80 cachedConfig = AAIConstants.CACHED_DB_CONFIG;
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);
89 private static class Helper {
90 private static final AAIGraph INSTANCE = new AAIGraph();
94 * Gets the single instance of AAIGraph.
96 * @return single instance of AAIGraph
98 public static AAIGraph getInstance() {
100 return Helper.INSTANCE;
103 public static boolean isInit() {
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
111 PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath).forService(serviceName).withGraphType(name).buildConfiguration();
112 TitanGraph graph = TitanFactory.open(propertiesConfiguration);
114 Properties graphProps = new Properties();
115 propertiesConfiguration.getKeys().forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
117 if ("inmemory".equals(graphProps.get("storage.backend"))) {
118 // Load the propertyKeys, indexes and edge-Labels into the DB
120 loadSnapShotToInMemoryGraph(graph, graphProps);
124 throw new AAIException("AAI_5102");
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");
135 private void loadSnapShotToInMemoryGraph(TitanGraph graph, Properties graphProps) {
136 if (logger.isDebugEnabled()) {
137 logger.debug("Load Snapshot to InMemory Graph");
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) {
150 "ERROR: Could not load datasnapshot to in memory graph. \n"
151 + ExceptionUtils.getFullStackTrace(e));
158 private void loadSchema(TitanGraph graph) {
159 // Load the propertyKeys, indexes and edge-Labels into the DB
160 TitanManagement graphMgt = graph.openManagement();
162 System.out.println("-- loading schema into Titan");
163 SchemaGenerator.loadSchemaIntoTitan( graph, graphMgt );
167 * Close all of the graph connections made in the instance.
169 public void graphShutdown() {
170 graphs.values().stream().filter(TitanGraph::isOpen).forEach(TitanGraph::close);
178 public TitanGraph getGraph() {
179 return graphs.get(REALTIME_DB);
182 public void graphShutdown(DBConnectionType connectionType) {
184 graphs.get(this.getGraphName(connectionType)).close();
187 public TitanGraph getGraph(DBConnectionType connectionType) {
188 return graphs.get(this.getGraphName(connectionType));
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;
202 private void logAndPrint(EELFLogger logger, String msg) {
203 System.out.println(msg);