Improve PAP healthcheck api to verify DB connectivity 64/127064/4
authorPrakhar Pandey <prakhar.pandey@bell.ca>
Thu, 10 Feb 2022 19:14:13 +0000 (14:14 -0500)
committerPrakhar Pandey <prakhar.pandey@bell.ca>
Fri, 11 Feb 2022 16:36:06 +0000 (11:36 -0500)
This commit improves PAP healthcheck API to enable verification of PAP to DB connectivity.

Issue-ID: POLICY-3763
Signed-off-by: Prakhar Pandey <prakhar.pandey@bell.ca>
Change-Id: I14353572a00e68a89161bcffd2ec3476b4a4c303

main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java
main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckRestControllerV1.java
main/src/main/java/org/onap/policy/pap/main/rest/PolicyComponentsHealthCheckProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/TestHealthCheckRestControllerV1.java

index dbbb49e..f7cfb36 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
- *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2021-2022 Bell Canada. 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.
@@ -25,8 +25,12 @@ package org.onap.policy.pap.main.rest;
 import org.onap.policy.common.endpoints.report.HealthCheckReport;
 import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.startstop.PapActivator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 /**
@@ -42,21 +46,45 @@ public class HealthCheckProvider {
     private static final String URL = NetworkUtil.getHostname();
     private static final String NAME = "Policy PAP";
 
+    @Autowired
+    private PolicyStatusProvider policyStatusProvider;
+    private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckProvider.class);
+
     /**
      * Performs the health check of PAP service.
      *
+     * @param checkDbConnectivity flag to enable pap to db connectivity verification
      * @return Report containing health check status
      */
-    public HealthCheckReport performHealthCheck() {
+    public HealthCheckReport performHealthCheck(boolean checkDbConnectivity) {
         final var report = new HealthCheckReport();
         report.setName(NAME);
         report.setUrl(URL);
 
         boolean alive = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive();
 
+        if (alive && checkDbConnectivity) {
+            alive = verifyPapDbConnectivity();
+        }
+
         report.setHealthy(alive);
-        report.setCode(alive ? 200 : 500);
+        report.setCode(alive ? 200 : 503);
         report.setMessage(alive ? ALIVE : NOT_ALIVE);
         return report;
     }
+
+    /**
+     * Verifies the connectivity between pap component & policy database.
+     *
+     * @return boolean signaling the verification result
+     */
+    private boolean verifyPapDbConnectivity() {
+        try {
+            policyStatusProvider.getPolicyStatus();
+            return true;
+        } catch (PfModelRuntimeException pfModelRuntimeException) {
+            LOGGER.warn("Policy pap to database connection check failed. Details - ", pfModelRuntimeException);
+            return false;
+        }
+    }
 }
index 19e4764..896f2aa 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019,2021 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
- *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2021-2022 Bell Canada. 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.
@@ -53,7 +53,7 @@ public class HealthCheckRestControllerV1  extends PapRestControllerV1 {
         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
     public ResponseEntity<HealthCheckReport> healthcheck() {
-        return ResponseEntity.ok().body(provider.performHealthCheck());
+        return ResponseEntity.ok().body(provider.performHealthCheck(true));
     }
 
 }
index d2a730e..3ad740d 100644 (file)
@@ -145,8 +145,8 @@ public class PolicyComponentsHealthCheckProvider {
             throw new PfModelRuntimeException(Status.BAD_REQUEST, "Client Health check interrupted ", exp);
         }
 
-        // Check PAP itself
-        HealthCheckReport papReport = new HealthCheckProvider().performHealthCheck();
+        // Check PAP itself excluding connectivity to Policy DB
+        HealthCheckReport papReport = new HealthCheckProvider().performHealthCheck(false);
         papReport
             .setUrl(isHttps ? "https://" : "http://" + papReport.getUrl() + ":" + port + POLICY_PAP_HEALTHCHECK_URI);
         if (!papReport.isHealthy()) {
index 6dd7103..bc97c63 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
+ *  Modifications Copyright (C) 2022 Bell Canada. 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.
 package org.onap.policy.pap.main.rest;
 
 import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
 
 import javax.ws.rs.client.Invocation;
 import org.junit.Test;
 import org.onap.policy.common.endpoints.report.HealthCheckReport;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.springframework.boot.test.mock.mockito.MockBean;
 
 /**
  * Class to perform unit test of {@link PapRestServer}.
@@ -36,6 +40,9 @@ public class TestHealthCheckRestControllerV1 extends CommonPapRestServer {
 
     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
 
+    @MockBean
+    private PolicyStatusProvider policyStatusProvider;
+
     @Test
     public void testSwagger() throws Exception {
         super.testSwagger(HEALTHCHECK_ENDPOINT);
@@ -52,13 +59,21 @@ public class TestHealthCheckRestControllerV1 extends CommonPapRestServer {
     }
 
     @Test
-    public void testHealthCheck_500() throws Exception {
+    public void testHealthCheckActivatorFailure() throws Exception {
 
         markActivatorDead();
 
         final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
         final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
-        validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
+        validateHealthCheckReport(NAME, SELF, false, 503, NOT_ALIVE, report);
+    }
+
+    @Test
+    public void testHealthCheckDbConnectionFailure() throws Exception {
+        when(policyStatusProvider.getPolicyStatus()).thenThrow(PfModelRuntimeException.class);
+        final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT);
+        final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
+        validateHealthCheckReport(NAME, SELF, false, 503, NOT_ALIVE, report);
     }
 
     private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,