820cd5b4d374d12652c3d33abe40a32f79b23651
[ui/dmaapbc.git] /
1 package org.onap.dcae.dmaapbc.dbcapp.controller;
2
3 import java.util.Date;
4
5 import javax.servlet.http.HttpServletRequest;
6
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;
11 import org.slf4j.MDC;
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;
18
19 /**
20  * This controller responds to probes for application health, returning a JSON
21  * body to indicate current status.
22  */
23 @RestController
24 @Configuration
25 @EnableAspectJAutoProxy
26 @RequestMapping("/")
27 public class HealthCheckController extends UnRestrictedBaseController {
28
29         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
30
31         private static final String HEALTH_CHECK_PATH = "/healthCheck";
32
33         @Autowired
34         private DmaapAccessService dmaapAccessService;
35
36         /**
37          * Model for JSON response with health-check results.
38          */
39         public class HealthStatus {
40                 // Either 200 or 500
41                 public int statusCode;
42                 // Additional detail in case of error, empty in case of success.
43                 public String message;
44
45                 public HealthStatus(int code, String msg) {
46                         this.statusCode = code;
47                         this.message = msg;
48                 }
49
50                 public int getStatusCode() {
51                         return statusCode;
52                 }
53
54                 public void setStatusCode(int code) {
55                         this.statusCode = code;
56                 }
57
58                 public String getMessage() {
59                         return message;
60                 }
61
62                 public void setMessage(String msg) {
63                         this.message = msg;
64                 }
65         }
66
67         /**
68          * Checks application health by making a trivial query to the database.
69          * 
70          * @param request
71          *            HttpServletRequest
72          * @return 200 if database access succeeds, 500 if it fails.
73          */
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;
79                 try {
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());
86                 }
87                 return healthStatus;
88         }
89
90         /**
91          * This implementation does not support suspend/resume.
92          * 
93          * @param request
94          *            HttpServletRequest
95          * @return Trivial success
96          */
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());
105                 return response;
106         }
107
108         /**
109          * This implementation does not support suspend/resume.
110          * 
111          * @param request
112          *            HttpServletRequest
113          * @return Trivial success
114          */
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());
123                 return response;
124         }
125
126         /**
127          * Answers ping request without checking the application health.
128          * 
129          * @param request
130          *            HttpServletRequest
131          * @return Trivial success
132          */
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());
140                 return response;
141         }
142 }