diff options
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; + } + } } |