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.FileNotFoundException;
23 import java.io.IOException;
24 import java.util.HashMap;
26 import java.util.Properties;
28 import org.apache.commons.configuration.PropertiesConfiguration;
29 import org.apache.commons.lang.exception.ExceptionUtils;
30 import org.apache.tinkerpop.gremlin.structure.Graph;
31 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
32 import org.onap.aai.dbgen.SchemaGenerator;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.util.AAIConstants;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import org.janusgraph.core.JanusGraphFactory;
39 import org.janusgraph.core.JanusGraph;
40 import org.janusgraph.core.schema.JanusGraphManagement;
43 * Database Mapping class which acts as the middle man between the REST
44 * interface objects and JanusGraph DB objects. This class provides methods to commit
45 * the objects received on the REST interface into the JanusGraph graph database as
46 * vertices and edges. Transactions are also managed here by using a JanusGraph
47 * object to load, commit/rollback and shutdown for each request. The data model
48 * rules such as keys/required properties are handled by calling DBMeth methods
49 * which are driven by a specification file in json.
53 public class AAIGraph {
55 private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
56 protected static final String COMPONENT = "aaidbmap";
57 protected Map<String, JanusGraph> graphs = new HashMap<>();
58 private static final String REALTIME_DB = "realtime";
59 private static final String CACHED_DB = "cached";
60 private static boolean isInit = false;
65 * Instantiates a new AAI graph.
69 String serviceName = System.getProperty("aai.service.name", "NA");
70 String rtConfig = System.getProperty("realtime.db.config");
71 String cachedConfig = System.getProperty("cached.db.config");
72 if (rtConfig == null) {
73 rtConfig = AAIConstants.REALTIME_DB_CONFIG;
75 if (cachedConfig == null) {
76 cachedConfig = AAIConstants.CACHED_DB_CONFIG;
78 this.loadGraph(REALTIME_DB, rtConfig, serviceName);
79 this.loadGraph(CACHED_DB, cachedConfig, serviceName);
80 } catch (Exception e) {
81 throw new RuntimeException("Failed to instantiate graphs", e);
85 private static class Helper {
86 private static final AAIGraph INSTANCE = new AAIGraph();
90 * Gets the single instance of AAIGraph.
92 * @return single instance of AAIGraph
94 public static AAIGraph getInstance() {
96 return Helper.INSTANCE;
99 public static boolean isInit() {
103 private void loadGraph(String name, String configPath, String serviceName) throws Exception {
104 // Graph being opened by JanusGraphFactory is being placed in hashmap to be used later
105 // These graphs shouldn't be closed until the application shutdown
107 PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath).forService(serviceName).withGraphType(name).buildConfiguration();
108 JanusGraph graph = JanusGraphFactory.open(propertiesConfiguration);
110 Properties graphProps = new Properties();
111 propertiesConfiguration.getKeys().forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
113 if ("inmemory".equals(graphProps.get("storage.backend"))) {
114 // Load the propertyKeys, indexes and edge-Labels into the DB
116 loadSnapShotToInMemoryGraph(graph, graphProps);
120 throw new AAIException("AAI_5102");
123 graphs.put(name, graph);
124 } catch (FileNotFoundException fnfe) {
125 throw new AAIException("AAI_4001");
126 } catch (IOException e) {
127 throw new AAIException("AAI_4002");
131 private void loadSnapShotToInMemoryGraph(JanusGraph graph, Properties graphProps) {
132 if (logger.isDebugEnabled()) {
133 logger.debug("Load Snapshot to InMemory Graph");
135 if (graphProps.containsKey("load.snapshot.file")) {
136 String value = graphProps.getProperty("load.snapshot.file");
137 if ("true".equals(value)) {
138 try (Graph transaction = graph.newTransaction()) {
139 String location = System.getProperty("snapshot.location");
140 logAndPrint(logger, "Loading snapshot to inmemory graph.");
141 transaction.io(IoCore.graphson()).readGraph(location);
142 transaction.tx().commit();
143 logAndPrint(logger, "Snapshot loaded to inmemory graph.");
144 } catch (Exception e) {
146 "ERROR: Could not load datasnapshot to in memory graph. \n"
147 + ExceptionUtils.getFullStackTrace(e));
154 private void loadSchema(JanusGraph graph) {
155 // Load the propertyKeys, indexes and edge-Labels into the DB
156 JanusGraphManagement graphMgt = graph.openManagement();
158 System.out.println("-- loading schema into JanusGraph");
159 SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt );
163 * Close all of the graph connections made in the instance.
165 public void graphShutdown() {
166 graphs.values().stream().filter(JanusGraph::isOpen).forEach(JanusGraph::close);
174 public JanusGraph getGraph() {
175 return graphs.get(REALTIME_DB);
178 public void graphShutdown(DBConnectionType connectionType) {
180 graphs.get(this.getGraphName(connectionType)).close();
183 public JanusGraph getGraph(DBConnectionType connectionType) {
184 return graphs.get(this.getGraphName(connectionType));
187 private String getGraphName(DBConnectionType connectionType) {
188 String graphName = "";
189 if (DBConnectionType.CACHED.equals(connectionType)) {
190 graphName = this.CACHED_DB;
191 } else if (DBConnectionType.REALTIME.equals(connectionType)) {
192 graphName = this.REALTIME_DB;
198 private void logAndPrint(EELFLogger logger, String msg) {
199 System.out.println(msg);