1 package org.onap.dcae.dmaapbc.dbcapp.controller;
5 import javax.servlet.http.HttpServletRequest;
7 import org.onap.dcae.dmaapbc.dbcapp.service.DmaapAccessService;
8 import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController;
9 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.openecomp.portalsdk.core.util.SystemProperties;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.context.annotation.Configuration;
14 import org.springframework.context.annotation.EnableAspectJAutoProxy;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.RestController;
20 * This controller responds to probes for application health, returning a JSON
21 * body to indicate current status.
25 @EnableAspectJAutoProxy
27 public class HealthCheckController extends UnRestrictedBaseController {
29 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
31 private static final String HEALTH_CHECK_PATH = "/healthCheck";
34 private DmaapAccessService dmaapAccessService;
37 * Model for JSON response with health-check results.
39 public class HealthStatus {
41 public int statusCode;
42 // Additional detail in case of error, empty in case of success.
43 public String message;
45 public HealthStatus(int code, String msg) {
46 this.statusCode = code;
50 public int getStatusCode() {
54 public void setStatusCode(int code) {
55 this.statusCode = code;
58 public String getMessage() {
62 public void setMessage(String msg) {
68 * Checks application health by making a trivial query to the database.
72 * @return 200 if database access succeeds, 500 if it fails.
74 @RequestMapping(value = { HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json")
75 public HealthStatus healthCheck(HttpServletRequest request) {
76 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
77 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
78 HealthStatus healthStatus = null;
80 logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check");
81 dmaapAccessService.getDmaapAccessCount();
82 healthStatus = new HealthStatus(200, "health check succeeded");
83 } catch (Exception ex) {
84 logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex);
85 healthStatus = new HealthStatus(500, "health check failed: " + ex.toString());
91 * This implementation does not support suspend/resume.
95 * @return Trivial success
97 @RequestMapping(value = {
98 HEALTH_CHECK_PATH + "/suspend" }, method = RequestMethod.GET, produces = "application/json")
99 public HealthStatus healthCheckSuspend(HttpServletRequest request) {
100 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
101 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
102 HealthStatus response = new HealthStatus(200, "suspend not implemented");
103 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
104 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
109 * This implementation does not support suspend/resume.
113 * @return Trivial success
115 @RequestMapping(value = {
116 HEALTH_CHECK_PATH + "/resume" }, method = RequestMethod.GET, produces = "application/json")
117 public HealthStatus healthCheckResume(HttpServletRequest request) {
118 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
119 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
120 HealthStatus response = new HealthStatus(200, "resume not implemented");
121 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
122 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
127 * Answers ping request without checking the application health.
131 * @return Trivial success
133 @RequestMapping(value = { HEALTH_CHECK_PATH + "/ping" }, method = RequestMethod.GET, produces = "application/json")
134 public HealthStatus ping(HttpServletRequest request) {
135 MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
136 logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DataBusHomeController.APP_NAME);
137 HealthStatus response = new HealthStatus(200, "ping received");
138 MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, DataBusHomeController.logDateFormat.format(new Date()));
139 logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());