aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/main
diff options
context:
space:
mode:
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>2024-11-19 14:20:15 +0100
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>2024-11-19 15:08:06 +0100
commit56da09aae132118a55af3cad601fff7b1cc70390 (patch)
tree1a95fac9d948cc0e0af265517050cdda368ba109 /aai-core/src/main
parenta559cb58bcf4a55ce58b9fc3b3480db371827edd (diff)
Move GraphChecker to aai-common
- define GraphCheck class that tests connection to storage backend - disable caching for the db transaction to make sure that the real connection is tested - define GraphConfig class that defines a Graph bean. This is an alternative to the AAIGraph class Issue-ID: AAI-4060 Change-Id: Id6d20adc8a8bab8bc914fb47353c37fd80905896 Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Diffstat (limited to 'aai-core/src/main')
-rw-r--r--aai-core/src/main/java/org/onap/aai/config/GraphConfig.java106
-rw-r--r--aai-core/src/main/java/org/onap/aai/util/GraphChecker.java93
-rw-r--r--aai-core/src/main/resources/application.properties2
3 files changed, 201 insertions, 0 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/config/GraphConfig.java b/aai-core/src/main/java/org/onap/aai/config/GraphConfig.java
new file mode 100644
index 00000000..352821f5
--- /dev/null
+++ b/aai-core/src/main/java/org/onap/aai/config/GraphConfig.java
@@ -0,0 +1,106 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2024 Deutsche Telekom. 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.aai.config;
+
+import java.io.FileNotFoundException;
+
+import javax.annotation.PreDestroy;
+
+import org.apache.commons.configuration2.ex.ConfigurationException;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.janusgraph.core.JanusGraph;
+import org.janusgraph.core.JanusGraphFactory;
+import org.janusgraph.core.schema.JanusGraphManagement;
+import org.onap.aai.dbgen.SchemaGenerator;
+import org.onap.aai.dbgen.SchemaGenerator4Hist;
+import org.onap.aai.dbmap.AAIGraphConfig;
+import org.onap.aai.exceptions.AAIException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@Configuration
+public class GraphConfig {
+
+ private static final String IN_MEMORY = "inmemory";
+
+ @Value("${spring.application.name}")
+ private String serviceName;
+
+ @Value("${aai.graph.properties.path}")
+ private String configPath;
+
+ @Bean
+ public org.apache.commons.configuration2.Configuration getGraphProperties()
+ throws FileNotFoundException, ConfigurationException {
+
+ return new AAIGraphConfig.Builder(configPath)
+ .forService(serviceName)
+ .withGraphType("realtime")
+ .buildConfiguration();
+ }
+
+ @Bean
+ public JanusGraph janusGraph(org.apache.commons.configuration2.Configuration graphConfiguration) throws AAIException {
+ JanusGraph graph = JanusGraphFactory.open(graphConfiguration);
+
+ boolean loadSchema = false;
+ if (loadSchema) {
+ if (IN_MEMORY.equals(graphConfiguration.getProperty("storage.backend"))) {
+ // Load the propertyKeys, indexes and edge-Labels into the DB
+ loadSchema(graph);
+ }
+ }
+
+ if (graph == null) {
+ throw new AAIException("AAI_5102");
+ }
+
+ return graph;
+ }
+
+ @Bean
+ public Graph graph(JanusGraph janusGraph) {
+ return janusGraph;
+ }
+
+ @Bean
+ public GraphTraversalSource graphTraversalSource(Graph graph) {
+ return graph.traversal();
+ }
+
+ private void loadSchema(JanusGraph graph) {
+ // Load the propertyKeys, indexes and edge-Labels into the DB
+ boolean dbNotEmpty = graph.traversal().V().limit(1).hasNext();
+ log.info("-- loading schema into JanusGraph");
+ if ("true".equals(
+ SpringContextAware.getApplicationContext().getEnvironment().getProperty("history.enabled", "false"))) {
+ JanusGraphManagement graphMgt = graph.openManagement();
+ SchemaGenerator4Hist.loadSchemaIntoJanusGraph(graphMgt, IN_MEMORY);
+ } else {
+ SchemaGenerator.loadSchemaIntoJanusGraph(graph, IN_MEMORY, dbNotEmpty);
+ }
+ }
+}
diff --git a/aai-core/src/main/java/org/onap/aai/util/GraphChecker.java b/aai-core/src/main/java/org/onap/aai/util/GraphChecker.java
new file mode 100644
index 00000000..43caebc5
--- /dev/null
+++ b/aai-core/src/main/java/org/onap/aai/util/GraphChecker.java
@@ -0,0 +1,93 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2022 Bell Canada
+ * Modification Copyright (C) 2024 Deutsche Telekom SA
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.util;
+
+import org.janusgraph.core.JanusGraph;
+import org.janusgraph.core.JanusGraphException;
+import org.janusgraph.core.JanusGraphTransaction;
+import org.onap.aai.logging.ErrorLogHelper;
+import org.springframework.stereotype.Component;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class GraphChecker {
+
+ private final JanusGraph graph;
+
+ /**
+ * Checks whether a connection to the graph database can be made.
+ *
+ * @return
+ * <li>true, if database is available</li>
+ * <li>false, if database is NOT available</li>
+ */
+ public boolean isAaiGraphDbAvailable() {
+ boolean dbAvailable;
+ JanusGraphTransaction transaction = null;
+ try {
+ // disable caching and other features that are not needed for this check
+ transaction = graph
+ .buildTransaction()
+ .readOnly()
+ .consistencyChecks(false)
+ .vertexCacheSize(0)
+ .skipDBCacheRead()
+ .start();
+ dbAvailable = transaction.traversal().V().limit(1).hasNext();
+ } catch (JanusGraphException e) {
+ String message = "Database is not available (after JanusGraph exception)";
+ ErrorLogHelper.logError("500", message + ": " + e.getMessage());
+ log.error(message, e);
+ dbAvailable = false;
+ } catch (Error e) {
+ // Following error occurs when aai resources is starting:
+ // - UnsatisfiedLinkError (for org.onap.aai.dbmap.AAIGraph$Helper instantiation)
+ // Following errors are raised when aai resources is starting and cassandra is not
+ // running:
+ // - ExceptionInInitializerError
+ // - NoClassDefFoundError (definition for org.onap.aai.dbmap.AAIGraph$Helper is not
+ // found)
+ String message = "Database is not available (after error)";
+ ErrorLogHelper.logError("500", message + ": " + e.getMessage());
+ log.error(message, e);
+ dbAvailable = false;
+ } catch (Exception e) {
+ String message = "Database availability can not be determined";
+ ErrorLogHelper.logError("500", message + ": " + e.getMessage());
+ log.error(message, e);
+ dbAvailable = false;
+ } finally {
+ if (transaction != null && !transaction.isClosed()) {
+ // check if transaction is open then closed instead of flag
+ try {
+ transaction.rollback();
+ } catch (Exception e) {
+ String message = "Exception occurred while closing transaction";
+ log.error(message, e);
+ ErrorLogHelper.logError("500", message + ": " + e.getMessage());
+ }
+ }
+ }
+ return dbAvailable;
+ }
+}
diff --git a/aai-core/src/main/resources/application.properties b/aai-core/src/main/resources/application.properties
new file mode 100644
index 00000000..ec085cc5
--- /dev/null
+++ b/aai-core/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+spring.application.name=aai-core
+aai.graph.properties.path=src/main/resources/janusgraph-realtime.properties