API healthcheck must return healthcheck report object 43/127943/1
authorRashmi Pujar <rashmi.pujar1@bell.ca>
Tue, 22 Mar 2022 02:17:14 +0000 (22:17 -0400)
committerRashmi Pujar <rashmi.pujar1@bell.ca>
Tue, 22 Mar 2022 02:17:14 +0000 (22:17 -0400)
Retain previous behavior of returning the healthcheck
report object for the healthcheck API upon DB failures.
This is also needed for the enhanced readiness probe on
policy components which uses the healthcheck endpoint.

Issue-ID: POLICY-4030
Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca>
Change-Id: Ifdbb23c8302567b4aa0f686bf9e77cdac37d1a62

main/src/main/java/org/onap/policy/api/main/exception/ServiceExceptionHandler.java
main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java
main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java
main/src/main/java/org/onap/policy/api/main/rest/provider/healthcheck/HealthCheckProvider.java

index f75013c..1909e19 100644 (file)
@@ -26,7 +26,6 @@ import org.aspectj.lang.annotation.AfterThrowing;
 import org.aspectj.lang.annotation.Aspect;
 import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.errors.concepts.ErrorResponse;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.TransactionException;
 
@@ -42,7 +41,7 @@ public class ServiceExceptionHandler {
      * @param exception the exception
      */
     @AfterThrowing(pointcut = "execution(* org.onap.policy.api.main.service.*.*(..))", throwing = "exception")
-    public ResponseEntity<Object> handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
+    public void handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
         if (exception instanceof PolicyApiRuntimeException || exception instanceof PfModelRuntimeException) {
             throw exception;
         } else {
@@ -64,10 +63,10 @@ public class ServiceExceptionHandler {
      */
     @AfterThrowing(pointcut = "execution(* org.onap.policy.api.main..*.*(..))"
         + " && !execution(* org.onap.policy.api.main.rest.provider.statistics.*.*(..))", throwing = "exception")
-    public ResponseEntity<Object> handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
+    public void handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
         final var errorResponse = new ErrorResponse();
         errorResponse.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR);
         errorResponse.setErrorMessage(exception.getMessage());
         throw new PolicyApiRuntimeException(exception.getMessage(), exception.getCause(), errorResponse, null);
     }
-}
\ No newline at end of file
+}
index d6f6d6a..a734b2d 100644 (file)
@@ -136,8 +136,9 @@ public class ApiRestController extends CommonRestController {
     public ResponseEntity<HealthCheckReport> getHealthCheck(\r
         @RequestHeader(name = REQUEST_ID_NAME, required = false)\r
         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {\r
-        updateApiStatisticsCounter(Target.OTHER, HttpStatus.OK, HttpMethod.GET);\r
-        return makeOkResponse(requestId, healthCheckProvider.performHealthCheck());\r
+        final var report = healthCheckProvider.performHealthCheck();\r
+        updateApiStatisticsCounter(Target.OTHER, HttpStatus.resolve(report.getCode()), HttpMethod.GET);\r
+        return makeResponse(requestId, report, report.getCode());\r
     }\r
 \r
     /**\r
@@ -1151,4 +1152,4 @@ public class ApiRestController extends CommonRestController {
             }\r
         }\r
     }\r
-}
\ No newline at end of file
+}\r
index ce479bc..ef288e4 100644 (file)
@@ -31,6 +31,7 @@ import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
+import org.springframework.http.HttpStatus;\r
 import org.springframework.http.ResponseEntity;\r
 import org.springframework.web.bind.annotation.ExceptionHandler;\r
 import org.springframework.web.context.request.WebRequest;\r
@@ -79,7 +80,12 @@ public class CommonRestController {
     protected final Coder coder = new StandardCoder();\r
 \r
     protected <T> ResponseEntity<T> makeOkResponse(UUID requestId, T respEntity) {\r
-        return CommonRestController.addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)\r
+        return makeResponse(requestId, respEntity, HttpStatus.OK.value());\r
+    }\r
+\r
+    protected <T> ResponseEntity<T> makeResponse(UUID requestId, T respEntity, int status) {\r
+        return CommonRestController\r
+            .addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(status)), requestId)\r
             .body(respEntity);\r
     }\r
 \r
@@ -138,4 +144,4 @@ public class CommonRestController {
             CommonRestController.addVersionControlHeaders(ResponseEntity.status(status)),\r
             requestId != null ? UUID.fromString(requestId) : ex.getRequestId()).body(ex.getErrorResponse());\r
     }\r
-}
\ No newline at end of file
+}\r
index 88bb1c9..bbcfcd5 100644 (file)
@@ -29,7 +29,6 @@ import lombok.RequiredArgsConstructor;
 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
 import org.onap.policy.common.endpoints.report.HealthCheckReport;
 import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.models.base.PfModelRuntimeException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -75,9 +74,9 @@ public class HealthCheckProvider {
         try {
             toscaServiceTemplateService.getDefaultJpaToscaServiceTemplate();
             return true;
-        } catch (PfModelRuntimeException pfme) {
-            LOGGER.warn("Api to database connection check failed. Details - ", pfme);
+        } catch (Exception ex) {
+            LOGGER.warn("Api to database connection check failed. Details: ", ex);
             return false;
         }
     }
-}
\ No newline at end of file
+}