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