2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.controller;
22 import java.util.ArrayList;
23 import java.util.List;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.RestController;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalapp.controller.EPUnRestrictedBaseController;
36 import org.openecomp.portalapp.portal.listener.HealthMonitor;
37 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
38 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
39 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
40 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
41 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
42 import com.google.gson.Gson;
45 * This controller processes requests for the health-check feature implemented
46 * in the HealthMonitor, which runs in its own thread. These requests do not
47 * require any authentication nor an active user session.
50 @org.springframework.context.annotation.Configuration
51 @EnableAspectJAutoProxy
53 public class HealthCheckController extends EPUnRestrictedBaseController {
55 private class HealthStatus {
56 public int statusCode;
57 @SuppressWarnings("unused")
60 public HealthStatus(int code, String body) {
61 this.statusCode = code;
66 private class HealthStatusInfo {
67 HealthStatusInfo(String healthCheckComponent) {
68 this.healthCheckComponent = healthCheckComponent;
69 this.healthCheckStatus = statusUp; // Default value
71 this.description = statusOk; // Default value
74 this.dbClusterStatus = "";
75 this.dbPermissions = "";
78 @SuppressWarnings("unused")
79 public String healthCheckComponent;
80 @SuppressWarnings("unused")
81 public String healthCheckStatus;
82 @SuppressWarnings("unused")
83 public String version;
84 @SuppressWarnings("unused")
85 public String description;
86 @SuppressWarnings("unused")
87 public String hostName;
88 @SuppressWarnings("unused")
89 public String ipAddress;
90 @SuppressWarnings("unused")
91 public String dbClusterStatus;
92 @SuppressWarnings("unused")
93 public String dbPermissions;
96 private final String statusUp = "UP";
97 private final String statusDown = "DOWN";
98 private final String statusOk = "OK";
100 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
102 @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
103 public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
104 HealthStatus healthStatus = new HealthStatus(500, "");
106 // Return the status as 500 if it suspended due to manual fail over
107 if (HealthMonitor.isSuspended) {
108 healthStatus.body = "Suspended";
109 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
110 MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
111 Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
116 boolean overallStatus = true;
118 List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
120 HealthStatusInfo beInfo = new HealthStatusInfo("BE");
121 beInfo.hostName = EcompPortalUtils.getMyHostName();
122 beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
123 if (!HealthMonitor.isBackEndUp()) {
124 overallStatus = false;
125 beInfo.healthCheckStatus = statusDown;
126 beInfo.description = "Check the logs for more details";
127 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
129 statusCollection.add(beInfo);
131 HealthStatusInfo feInfo = new HealthStatusInfo("FE");
132 if (!HealthMonitor.isFrontEndUp()) {
133 overallStatus = false;
134 feInfo.healthCheckStatus = statusDown;
135 feInfo.description = "Check the logs for more details";
136 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
138 statusCollection.add(feInfo);
140 HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
141 if (!HealthMonitor.isDatabaseUp()) {
142 overallStatus = false;
143 dbInfo.healthCheckStatus = statusDown;
144 dbInfo.description = "Check the logs for more details";
145 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
148 if (!HealthMonitor.isClusterStatusOk()) {
149 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
150 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
152 dbInfo.dbClusterStatus = statusOk;
155 if (!HealthMonitor.isDatabasePermissionsOk()) {
156 dbInfo.dbPermissions = "Problem, check the logs for more details";
157 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
159 dbInfo.dbPermissions = statusOk;
161 statusCollection.add(dbInfo);
163 HealthStatusInfo uebInfo = new HealthStatusInfo("UEB");
164 if (!HealthMonitor.isUebUp()) {
165 // As per test case review meeting, UEB is considered as
166 // critical as DB. Hence commenting
167 // overallStatus = false;
168 uebInfo.healthCheckStatus = statusDown;
169 uebInfo.description = "Check the logs for more details";
170 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError);
172 statusCollection.add(uebInfo);
176 json = new Gson().toJson(statusCollection);
177 } catch (Exception e) {
178 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
180 logger.info(EELFLoggerDelegate.debugLogger, json);
183 healthStatus = new HealthStatus(200, json);
185 healthStatus = new HealthStatus(500, json);
186 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
188 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
189 } catch (Exception e) {
190 logger.error(EELFLoggerDelegate.errorLogger,
191 "Exception occurred while performing the healthcheck. Details: "
192 + EcompPortalUtils.getStackTrace(e));
195 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
200 @RequestMapping(value = {
201 "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
202 public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
203 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
205 HealthMonitor.isSuspended = true;
206 healthStatus.statusCode = 200;
208 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
209 response.getStatus());
214 @RequestMapping(value = {
215 "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
216 public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
217 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
219 HealthMonitor.isSuspended = false;
220 healthStatus.statusCode = 200;
221 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
222 response.getStatus());
226 @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
227 public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
228 HealthStatus healthStatus = new HealthStatus(200, "OK");
229 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());