From 9902e51b342e0047b7057f25c31145ea86face6a Mon Sep 17 00:00:00 2001 From: Rashmi Pujar Date: Mon, 21 Mar 2022 22:17:14 -0400 Subject: API healthcheck must return healthcheck report object 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 Change-Id: Ifdbb23c8302567b4aa0f686bf9e77cdac37d1a62 --- .../policy/api/main/exception/ServiceExceptionHandler.java | 7 +++---- .../java/org/onap/policy/api/main/rest/ApiRestController.java | 7 ++++--- .../org/onap/policy/api/main/rest/CommonRestController.java | 10 ++++++++-- .../main/rest/provider/healthcheck/HealthCheckProvider.java | 7 +++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/main/src/main/java/org/onap/policy/api/main/exception/ServiceExceptionHandler.java b/main/src/main/java/org/onap/policy/api/main/exception/ServiceExceptionHandler.java index f75013c9..1909e199 100644 --- a/main/src/main/java/org/onap/policy/api/main/exception/ServiceExceptionHandler.java +++ b/main/src/main/java/org/onap/policy/api/main/exception/ServiceExceptionHandler.java @@ -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 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 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 +} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java index d6f6d6af..a734b2d6 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -136,8 +136,9 @@ public class ApiRestController extends CommonRestController { public ResponseEntity getHealthCheck( @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) { - updateApiStatisticsCounter(Target.OTHER, HttpStatus.OK, HttpMethod.GET); - return makeOkResponse(requestId, healthCheckProvider.performHealthCheck()); + final var report = healthCheckProvider.performHealthCheck(); + updateApiStatisticsCounter(Target.OTHER, HttpStatus.resolve(report.getCode()), HttpMethod.GET); + return makeResponse(requestId, report, report.getCode()); } /** @@ -1151,4 +1152,4 @@ public class ApiRestController extends CommonRestController { } } } -} \ No newline at end of file +} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java index ce479bc0..ef288e47 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java @@ -31,6 +31,7 @@ import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; @@ -79,7 +80,12 @@ public class CommonRestController { protected final Coder coder = new StandardCoder(); protected ResponseEntity makeOkResponse(UUID requestId, T respEntity) { - return CommonRestController.addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId) + return makeResponse(requestId, respEntity, HttpStatus.OK.value()); + } + + protected ResponseEntity makeResponse(UUID requestId, T respEntity, int status) { + return CommonRestController + .addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(status)), requestId) .body(respEntity); } @@ -138,4 +144,4 @@ public class CommonRestController { CommonRestController.addVersionControlHeaders(ResponseEntity.status(status)), requestId != null ? UUID.fromString(requestId) : ex.getRequestId()).body(ex.getErrorResponse()); } -} \ No newline at end of file +} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/healthcheck/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/healthcheck/HealthCheckProvider.java index 88bb1c93..bbcfcd54 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/healthcheck/HealthCheckProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/healthcheck/HealthCheckProvider.java @@ -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 +} -- cgit 1.2.3-korg