Add check to verify api to db connectivity 93/123393/3
authorRam Krishna Verma <ram_krishna.verma@bell.ca>
Thu, 19 Aug 2021 15:00:52 +0000 (11:00 -0400)
committerRam Krishna Verma <ram_krishna.verma@bell.ca>
Thu, 19 Aug 2021 19:56:50 +0000 (15:56 -0400)
Adding a method in policy/api HealthCheckProvider
to verify database connectivity.

The same can then be reported in policy consolidated heath check
done by PAP as well.

Issue-ID: POLICY-2896
Change-Id: Id80ade57829b37ace15ae19caeefd08af61aebd5
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java

index f4019d9..b5a198a 100644 (file)
 
 package org.onap.policy.api.main.rest.provider;
 
+import org.onap.policy.api.main.rest.PolicyFetchMode;
 import org.onap.policy.api.main.startstop.ApiActivator;
 import org.onap.policy.common.endpoints.report.HealthCheckReport;
 import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Class to fetch health check of api service.
@@ -34,10 +39,13 @@ import org.onap.policy.common.utils.network.NetworkUtil;
  */
 public class HealthCheckProvider {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckProvider.class);
+
     private static final String NOT_ALIVE = "not alive";
     private static final String ALIVE = "alive";
     private static final String URL = NetworkUtil.getHostname();
     private static final String NAME = "Policy API";
+    private static final String DB_CONN_FAILURE = "unable to connect with database";
 
     /**
      * Performs the health check of api service.
@@ -48,9 +56,32 @@ public class HealthCheckProvider {
         final var report = new HealthCheckReport();
         report.setName(NAME);
         report.setUrl(URL);
-        report.setHealthy(ApiActivator.isAlive());
-        report.setCode(ApiActivator.isAlive() ? 200 : 500);
-        report.setMessage(ApiActivator.isAlive() ? ALIVE : NOT_ALIVE);
+        boolean heathStatus = ApiActivator.isAlive();
+        if (heathStatus) {
+            boolean dbConnectionStatus = verifyApiDatabase();
+            report.setHealthy(dbConnectionStatus);
+            report.setCode(dbConnectionStatus ? 200 : 503);
+            report.setMessage(dbConnectionStatus ? ALIVE : DB_CONN_FAILURE);
+        } else {
+            report.setHealthy(heathStatus);
+            report.setCode(500);
+            report.setMessage(NOT_ALIVE);
+        }
         return report;
     }
+
+    /**
+     * Verifies the connectivity between api component & policy database.
+     *
+     * @return boolean signaling the verification result
+     */
+    private boolean verifyApiDatabase() {
+        try (var policyProvider = new PolicyProvider()) {
+            policyProvider.fetchPolicies(null, null, null, null, PolicyFetchMode.BARE);
+            return true;
+        } catch (PfModelException | PfModelRuntimeException pfme) {
+            LOGGER.warn("Api to database connection check failed. Details - ", pfme);
+            return false;
+        }
+    }
 }