diff options
Diffstat (limited to 'ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java')
-rw-r--r-- | ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java | 408 |
1 files changed, 201 insertions, 207 deletions
diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java index 0810e3d..df5ec64 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java @@ -22,7 +22,6 @@ package org.onap.ccsdk.dashboard.controller; - import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; @@ -58,220 +57,215 @@ import org.springframework.web.bind.annotation.RestController; @EnableAspectJAutoProxy @RequestMapping("/") public class HealthCheckController extends DashboardRestrictedBaseController { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class); - /** - * Application name - */ - protected static final String APP_NAME = "ecd-app"; + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class); + + /** + * Application name + */ + protected static final String APP_NAME = "ecd-app"; + + private static Date begin, end; + private static final String APP_HEALTH_CHECK_PATH = "/health"; + private static final String APP_SRVC_HEALTH_CHECK_PATH = "/health-info"; + + private static final String APP_DB_QRY = "from App where id = 1"; + public static final String APP_METADATA = "APP.METADATA"; + + @Autowired + private DataAccessService dataAccessService; + + private AbstractCacheManager cacheManager; + + /** + * application health by simply responding with a JSON object indicating status + * + * @param request HttpServletRequest + * @return HealthStatus object always + */ + @RequestMapping(value = { APP_HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) { + return new HealthStatus(200, + SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check passed "); + } + + /** + * Checks application health by executing a sample query with local DB + * + * @param request HttpServletRequest + * @return 200 if database access succeeds, 500 if it fails. + */ + /* + * public HealthStatus healthCheck(HttpServletRequest request, + * HttpServletResponse response) { //preLogAudit(request); HealthStatus + * healthStatus = new HealthStatus(200, + * SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + + * " health check passed "); try { logger.debug(EELFLoggerDelegate.debugLogger, + * "Performing health check"); App app; Object appObj = + * getCacheManager().getObject(APP_METADATA); if (appObj == null) { app = + * findApp(); if (app != null) { getCacheManager().putObject(APP_METADATA, app); + * } else { healthStatus = new HealthStatus(503, + * SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + + * " health check failed to query App from database"); } } + * + * if (isDbConnUp()) { healthStatus = new HealthStatus(200, + * SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + + * " health check passed "); } else { healthStatus = new HealthStatus(503, + * SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + + * " health check failed to run db query"); } } catch (Exception ex) { + * MDC.put(SystemProperties.STATUS_CODE, "ERROR"); MDC.put("TargetEntity", + * "Health Check"); MDC.put("TargetServiceName", "Health Check"); + * MDC.put("ErrorCode", "300"); MDC.put("ErrorCategory", "ERROR"); + * MDC.put("ErrorDescription", "Health check failed!"); + * logger.error(EELFLoggerDelegate.errorLogger, + * "Failed to perform health check"); healthStatus = new HealthStatus(503, + * "health check failed: " + ex.toString()); } finally { postLogAudit(request); + * } if (healthStatus.getStatusCode() != 200) { + * response.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE); + * response.sendError(HttpStatus.SC_SERVICE_UNAVAILABLE, + * objectMapper.writeValueAsString(healthStatus)); } + * + * return healthStatus; } + */ + /** + * Checks application health and availability of dependent services + * + * @param request HttpServletRequest + * @return 200 if database access succeeds, 500 if it fails. + */ + @RequestMapping(value = { APP_SRVC_HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus srvcHealthCheck(HttpServletRequest request, HttpServletResponse response) throws Exception { + preLogAudit(request); + HealthStatus healthStatus = null; + StringBuffer sb = new StringBuffer(); + try { + logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); + if (isDbConnUp()) { + sb.append(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)) + .append(" health check passed; "); + healthStatus = new HealthStatus(200, + SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + sb.toString()); + } else { + sb.append(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)) + .append(" health check failed to run db query; "); + healthStatus = new HealthStatus(503, + SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + sb.toString()); + } + ControllerEndpointCredentials[] cec = getControllerEndpoints(); - private static Date begin, end; - private static final String APP_HEALTH_CHECK_PATH = "/health"; - private static final String APP_SRVC_HEALTH_CHECK_PATH = "/health-info"; + for (int i = 0; i < cec.length; ++i) { + // Check if API Handler is reachable + if (!isServiceReachable(cec[i].getUrl())) { + MDC.put(SystemProperties.STATUS_CODE, "ERROR"); + MDC.put("TargetEntity", "API Handler"); + MDC.put("TargetServiceName", "API Handler"); + MDC.put("ErrorCode", "300"); + MDC.put("ErrorCategory", "ERROR"); + MDC.put("ErrorDescription", "API Handler unreachable!"); + sb.append(" API Handler unreachable; "); + logger.error(EELFLoggerDelegate.errorLogger, "Failed to ping API Handler"); + } + // Check if Inventory is reachable + if (!isServiceReachable(cec[i].getInventoryUrl() + "/dcae-services")) { + MDC.put(SystemProperties.STATUS_CODE, "ERROR"); + MDC.put("TargetEntity", "DCAE Inventory"); + MDC.put("TargetServiceName", "DCAE Inventory"); + MDC.put("ErrorCode", "300"); + MDC.put("ErrorCategory", "ERROR"); + MDC.put("ErrorDescription", "DCAE Inventory unreachable!"); + sb.append(" DCAE Inventory unreachable; "); + logger.error(EELFLoggerDelegate.errorLogger, "Failed to ping DCAE Inventory"); + } + // Check if Deployment Handler is reachable + if (!isServiceReachable(cec[i].getDhandlerUrl())) { + MDC.put(SystemProperties.STATUS_CODE, "ERROR"); + MDC.put("TargetEntity", "Deployment Handler"); + MDC.put("TargetServiceName", "Deployment Handler"); + MDC.put("ErrorCode", "300"); + MDC.put("ErrorCategory", "ERROR"); + MDC.put("ErrorDescription", "Deployment Handler unreachable!"); + sb.append(" Deployment Handler unreachable; "); + logger.error(EELFLoggerDelegate.errorLogger, "Failed to ping Deployment Handler"); + } + } + } catch (Exception ex) { + MDC.put(SystemProperties.STATUS_CODE, "ERROR"); + MDC.put("TargetEntity", "Health Check"); + MDC.put("TargetServiceName", "Health Check"); + MDC.put("ErrorCode", "300"); + MDC.put("ErrorCategory", "ERROR"); + MDC.put("ErrorDescription", "Health check failed!"); + logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check"); + sb.append(" "); + sb.append(ex.toString()); + healthStatus = new HealthStatus(503, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + + " health check failed: " + sb.toString()); + } finally { + postLogAudit(request); + } + if (healthStatus.getStatusCode() != 200) { + response.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE); + response.sendError(HttpStatus.SC_SERVICE_UNAVAILABLE, objectMapper.writeValueAsString(healthStatus)); + } + return healthStatus; + } - private static final String APP_DB_QRY = "from App where id = 1"; - public static final String APP_METADATA = "APP.METADATA"; - - @Autowired - private DataAccessService dataAccessService; - - private AbstractCacheManager cacheManager; - - /** - * application health by simply responding with a JSON object indicating status - * - * @param request - * HttpServletRequest - * @return HealthStatus object always - */ - @RequestMapping(value = { APP_HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") - public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) { - return new HealthStatus(200, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check passed "); - } - - /** - * Checks application health by executing a sample query with local DB - * - * @param request - * HttpServletRequest - * @return 200 if database access succeeds, 500 if it fails. - */ - /* - public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) { - //preLogAudit(request); - HealthStatus healthStatus = new HealthStatus(200, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check passed "); - try { - logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); - App app; - Object appObj = getCacheManager().getObject(APP_METADATA); - if (appObj == null) { - app = findApp(); - if (app != null) { - getCacheManager().putObject(APP_METADATA, app); - } else { - healthStatus = new HealthStatus(503, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check failed to query App from database"); - } - } + private boolean isDbConnUp() { + @SuppressWarnings("unchecked") + List<App> list = dataAccessService.executeQuery(APP_DB_QRY, null); + if (list.size() > 0) { + return true; + } else { + return false; + } + } - if (isDbConnUp()) { - healthStatus = new HealthStatus(200, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check passed "); - } else { - healthStatus = new HealthStatus(503, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check failed to run db query"); - } - } catch (Exception ex) { - MDC.put(SystemProperties.STATUS_CODE, "ERROR"); - MDC.put("TargetEntity", "Health Check"); - MDC.put("TargetServiceName", "Health Check"); - MDC.put("ErrorCode", "300"); - MDC.put("ErrorCategory", "ERROR"); - MDC.put("ErrorDescription", "Health check failed!"); - logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check"); - healthStatus = new HealthStatus(503, "health check failed: " + ex.toString()); - } finally { - postLogAudit(request); - } - if (healthStatus.getStatusCode() != 200) { - response.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE); - response.sendError(HttpStatus.SC_SERVICE_UNAVAILABLE, objectMapper.writeValueAsString(healthStatus)); - } + private App findApp() { + @SuppressWarnings("unchecked") + List<App> list = dataAccessService.executeQuery(APP_DB_QRY, null); + return (list == null || list.isEmpty()) ? null : (App) list.get(0); + } - return healthStatus; - } - */ - /** - * Checks application health and availability of dependent services - * - * @param request - * HttpServletRequest - * @return 200 if database access succeeds, 500 if it fails. - */ - @RequestMapping(value = { APP_SRVC_HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") - public HealthStatus srvcHealthCheck(HttpServletRequest request, HttpServletResponse response) throws Exception { - preLogAudit(request); - HealthStatus healthStatus = null; - StringBuffer sb = new StringBuffer(); - try { - logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); - if (isDbConnUp()) { - sb.append(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)).append( " health check passed; "); - healthStatus = new HealthStatus(200, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + sb.toString()); - } else { - sb.append(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)).append(" health check failed to run db query; "); - healthStatus = new HealthStatus(503, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + sb.toString()); - } - ControllerEndpointCredentials[] cec = getControllerEndpoints(); + public static boolean isServiceReachable(String targetUrl) throws IOException { + HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(targetUrl).openConnection(); + httpUrlConnection.setRequestMethod("HEAD"); - for(int i = 0; i < cec.length; ++i) { - // Check if API Handler is reachable - if (!isServiceReachable(cec[i].getUrl())) { - MDC.put(SystemProperties.STATUS_CODE, "ERROR"); - MDC.put("TargetEntity", "API Handler"); - MDC.put("TargetServiceName", "API Handler"); - MDC.put("ErrorCode", "300"); - MDC.put("ErrorCategory", "ERROR"); - MDC.put("ErrorDescription", "API Handler unreachable!"); - sb.append(" API Handler unreachable; "); - logger.error(EELFLoggerDelegate.errorLogger, "Failed to ping API Handler"); - } - // Check if Inventory is reachable - if (!isServiceReachable(cec[i].getInventoryUrl()+"/dcae-services")) { - MDC.put(SystemProperties.STATUS_CODE, "ERROR"); - MDC.put("TargetEntity", "DCAE Inventory"); - MDC.put("TargetServiceName", "DCAE Inventory"); - MDC.put("ErrorCode", "300"); - MDC.put("ErrorCategory", "ERROR"); - MDC.put("ErrorDescription", "DCAE Inventory unreachable!"); - sb.append(" DCAE Inventory unreachable; "); - logger.error(EELFLoggerDelegate.errorLogger, "Failed to ping DCAE Inventory"); - } - // Check if Deployment Handler is reachable - if (!isServiceReachable(cec[i].getDhandlerUrl())) { - MDC.put(SystemProperties.STATUS_CODE, "ERROR"); - MDC.put("TargetEntity", "Deployment Handler"); - MDC.put("TargetServiceName", "Deployment Handler"); - MDC.put("ErrorCode", "300"); - MDC.put("ErrorCategory", "ERROR"); - MDC.put("ErrorDescription", "Deployment Handler unreachable!"); - sb.append(" Deployment Handler unreachable; "); - logger.error(EELFLoggerDelegate.errorLogger, "Failed to ping Deployment Handler"); - } - } - } catch (Exception ex) { - MDC.put(SystemProperties.STATUS_CODE, "ERROR"); - MDC.put("TargetEntity", "Health Check"); - MDC.put("TargetServiceName", "Health Check"); - MDC.put("ErrorCode", "300"); - MDC.put("ErrorCategory", "ERROR"); - MDC.put("ErrorDescription", "Health check failed!"); - logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check"); - sb.append(" "); - sb.append(ex.toString()); - healthStatus = new HealthStatus(503, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check failed: " + sb.toString()); - } finally { - postLogAudit(request); - } - if (healthStatus.getStatusCode() != 200) { - response.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE); - response.sendError(HttpStatus.SC_SERVICE_UNAVAILABLE, objectMapper.writeValueAsString(healthStatus)); - } - return healthStatus; - } + try { + int responseCode = httpUrlConnection.getResponseCode(); + return responseCode == HttpURLConnection.HTTP_OK; + } catch (UnknownHostException noInternetConnection) { + return false; + } + } - private boolean isDbConnUp() { - @SuppressWarnings("unchecked") - List<App> list = dataAccessService.executeQuery(APP_DB_QRY, null); - if (list.size() > 0) { - return true; - } else { - return false; - } - } - - private App findApp() { - @SuppressWarnings("unchecked") - List<App> list = dataAccessService.executeQuery(APP_DB_QRY, null); - return (list == null || list.isEmpty()) ? null : (App) list.get(0); - } - - public static boolean isServiceReachable(String targetUrl) throws IOException { - HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(targetUrl).openConnection(); - httpUrlConnection.setRequestMethod("HEAD"); + @Autowired + public void setCacheManager(AbstractCacheManager cacheManager) { + this.cacheManager = cacheManager; + } - try { - int responseCode = httpUrlConnection.getResponseCode(); - return responseCode == HttpURLConnection.HTTP_OK; - } catch (UnknownHostException noInternetConnection) { - return false; - } - } - - @Autowired - public void setCacheManager(AbstractCacheManager cacheManager) { - this.cacheManager = cacheManager; - } + public AbstractCacheManager getCacheManager() { + return cacheManager; + } - public AbstractCacheManager getCacheManager() { - return cacheManager; - } - - public void preLogAudit(HttpServletRequest request) { - begin = new Date(); - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DashboardRestrictedBaseController.logDateFormat.format(begin)); - MDC.put(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP, DashboardRestrictedBaseController.logDateFormat.format(begin)); - MDC.put(SystemProperties.STATUS_CODE, "COMPLETE"); - //logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - } + public void preLogAudit(HttpServletRequest request) { + begin = new Date(); + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, + DashboardRestrictedBaseController.logDateFormat.format(begin)); + MDC.put(SystemProperties.METRICSLOG_BEGIN_TIMESTAMP, + DashboardRestrictedBaseController.logDateFormat.format(begin)); + MDC.put(SystemProperties.STATUS_CODE, "COMPLETE"); + // logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + } - public void postLogAudit(HttpServletRequest request) { - end = new Date(); - MDC.put("AlertSeverity", "0"); - MDC.put("TargetEntity", "Health Check"); - MDC.put("TargetServiceName", "Health Check"); - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DashboardRestrictedBaseController.logDateFormat.format(end)); - MDC.put(SystemProperties.METRICSLOG_END_TIMESTAMP, DashboardRestrictedBaseController.logDateFormat.format(end)); - MDC.put(SystemProperties.MDC_TIMER, Long.toString((end.getTime() - begin.getTime()))); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - logger.info(EELFLoggerDelegate.metricsLogger, request.getMethod() + request.getRequestURI()); - } + public void postLogAudit(HttpServletRequest request) { + end = new Date(); + MDC.put("AlertSeverity", "0"); + MDC.put("TargetEntity", "Health Check"); + MDC.put("TargetServiceName", "Health Check"); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DashboardRestrictedBaseController.logDateFormat.format(end)); + MDC.put(SystemProperties.METRICSLOG_END_TIMESTAMP, DashboardRestrictedBaseController.logDateFormat.format(end)); + MDC.put(SystemProperties.MDC_TIMER, Long.toString((end.getTime() - begin.getTime()))); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + logger.info(EELFLoggerDelegate.metricsLogger, request.getMethod() + request.getRequestURI()); + } } |