2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.dbmap;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.HashMap;
34 import java.util.Properties;
36 import org.apache.commons.configuration.PropertiesConfiguration;
37 import org.apache.commons.lang.exception.ExceptionUtils;
38 import org.apache.tinkerpop.gremlin.structure.Graph;
39 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
40 import org.janusgraph.core.JanusGraph;
41 import org.janusgraph.core.JanusGraphFactory;
42 import org.janusgraph.core.schema.JanusGraphManagement;
43 import org.onap.aai.dbgen.SchemaGenerator;
44 import org.onap.aai.exceptions.AAIException;
45 import org.onap.aai.util.AAIConstants;
48 * Database Mapping class which acts as the middle man between the REST
49 * interface objects and JanusGraph DB objects. This class provides methods to commit
50 * the objects received on the REST interface into the JanusGraph graph database as
51 * vertices and edges. Transactions are also managed here by using a JanusGraph
52 * object to load, commit/rollback and shutdown for each request. The data model
53 * rules such as keys/required properties are handled by calling DBMeth methods
54 * which are driven by a specification file in json.
58 public class AAIGraph {
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
61 protected static final String COMPONENT = "aaidbmap";
62 protected Map<String, JanusGraph> graphs = new HashMap<>();
63 private static final String REALTIME_DB = "realtime";
64 private static final String CACHED_DB = "cached";
65 private static boolean isInit = false;
68 * Instantiates a new AAI graph.
72 String serviceName = System.getProperty("aai.service.name", "NA");
73 String rtConfig = System.getProperty("realtime.db.config");
74 String cachedConfig = System.getProperty("cached.db.config");
75 if (rtConfig == null) {
76 rtConfig = AAIConstants.REALTIME_DB_CONFIG;
78 if (cachedConfig == null) {
79 cachedConfig = AAIConstants.CACHED_DB_CONFIG;
81 this.loadGraph(REALTIME_DB, rtConfig, serviceName);
82 this.loadGraph(CACHED_DB, cachedConfig, serviceName);
83 } catch (Exception e) {
84 throw new RuntimeException("Failed to instantiate graphs", e);
88 private static class Helper {
89 private static final AAIGraph INSTANCE = new AAIGraph();
97 * Gets the single instance of AAIGraph.
99 * @return single instance of AAIGraph
101 public static AAIGraph getInstance() {
103 return Helper.INSTANCE;
106 public static boolean isInit() {
110 private void loadGraph(String name, String configPath, String serviceName) throws Exception {
111 // Graph being opened by JanusGraphFactory is being placed in hashmap to be used later
112 // These graphs shouldn't be closed until the application shutdown
114 PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath)
115 .forService(serviceName).withGraphType(name).buildConfiguration();
116 JanusGraph graph = JanusGraphFactory.open(propertiesConfiguration);
118 Properties graphProps = new Properties();
119 propertiesConfiguration.getKeys()
120 .forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
122 if ("inmemory".equals(graphProps.get("storage.backend"))) {
123 // Load the propertyKeys, indexes and edge-Labels into the DB
125 loadSnapShotToInMemoryGraph(graph, graphProps);
129 throw new AAIException("AAI_5102");
132 graphs.put(name, graph);
133 } catch (FileNotFoundException fnfe) {
134 throw new AAIException("AAI_4001");
135 } catch (IOException e) {
136 throw new AAIException("AAI_4002");
140 private void loadSnapShotToInMemoryGraph(JanusGraph graph, Properties graphProps) {
141 if (logger.isDebugEnabled()) {
142 logger.debug("Load Snapshot to InMemory Graph");
144 if (graphProps.containsKey("load.snapshot.file")) {
145 String value = graphProps.getProperty("load.snapshot.file");
146 if ("true".equals(value)) {
147 try (Graph transaction = graph.newTransaction()) {
148 String location = System.getProperty("snapshot.location");
149 logAndPrint(logger, "Loading snapshot to inmemory graph.");
150 transaction.io(IoCore.graphson()).readGraph(location);
151 transaction.tx().commit();
152 logAndPrint(logger, "Snapshot loaded to inmemory graph.");
153 } catch (Exception e) {
154 logAndPrint(logger, "ERROR: Could not load datasnapshot to in memory graph. \n"
155 + ExceptionUtils.getFullStackTrace(e));
156 throw new RuntimeException(e);
162 private void loadSchema(JanusGraph graph) {
163 // Load the propertyKeys, indexes and edge-Labels into the DB
164 JanusGraphManagement graphMgt = graph.openManagement();
166 System.out.println("-- loading schema into JanusGraph");
167 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, "inmemory");
171 * Close all of the graph connections made in the instance.
173 public void graphShutdown() {
174 graphs.values().stream().filter(JanusGraph::isOpen).forEach(JanusGraph::close);
182 public JanusGraph getGraph() {
183 return graphs.get(REALTIME_DB);
186 public void graphShutdown(DBConnectionType connectionType) {
188 graphs.get(this.getGraphName(connectionType)).close();
191 public JanusGraph getGraph(DBConnectionType connectionType) {
192 return graphs.get(this.getGraphName(connectionType));
195 private String getGraphName(DBConnectionType connectionType) {
196 String graphName = "";
197 if (DBConnectionType.CACHED.equals(connectionType)) {
198 graphName = this.CACHED_DB;
199 } else if (DBConnectionType.REALTIME.equals(connectionType)) {
200 graphName = this.REALTIME_DB;
206 private void logAndPrint(EELFLogger logger, String msg) {
207 System.out.println(msg);