diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-08-19 11:00:52 -0400 |
---|---|---|
committer | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2021-08-19 15:56:50 -0400 |
commit | 2877524892371091bb41d2cad3a45c3c5835feb8 (patch) | |
tree | fb3a816aa633533f50128480505749b1800e12d4 /main | |
parent | a64891e0525af75c5ea8becf3e9234ac0c1297b4 (diff) |
Add check to verify api to db connectivity
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>
Diffstat (limited to 'main')
-rw-r--r-- | main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java index f4019d9a..b5a198a2 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java @@ -24,9 +24,14 @@ 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; + } + } } |