2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.portal.controller;
40 import java.util.ArrayList;
41 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
47 import org.springframework.context.annotation.EnableAspectJAutoProxy;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RestController;
51 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
52 import org.onap.portalapp.portal.listener.HealthMonitor;
53 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
54 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
55 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
56 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
57 import org.onap.portalapp.portal.utils.EcompPortalUtils;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import com.google.gson.Gson;
63 * This controller processes requests for the health-check feature implemented
64 * in the HealthMonitor, which runs in its own thread. These requests do not
65 * require any authentication nor an active user session.
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
71 public class HealthCheckController extends EPUnRestrictedBaseController {
73 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
75 private class HealthStatus {
76 public int statusCode;
77 @SuppressWarnings("unused")
80 public HealthStatus(int code, String body) {
81 this.statusCode = code;
86 private class HealthStatusInfo {
87 HealthStatusInfo(String healthCheckComponent) {
88 this.healthCheckComponent = healthCheckComponent;
89 this.healthCheckStatus = statusUp; // Default value
91 this.description = statusOk; // Default value
94 this.dbClusterStatus = "";
95 this.dbPermissions = "";
98 @SuppressWarnings("unused")
99 public String healthCheckComponent;
100 @SuppressWarnings("unused")
101 public String healthCheckStatus;
102 @SuppressWarnings("unused")
103 public String version;
104 @SuppressWarnings("unused")
105 public String description;
106 @SuppressWarnings("unused")
107 public String hostName;
108 @SuppressWarnings("unused")
109 public String ipAddress;
110 @SuppressWarnings("unused")
111 public String dbClusterStatus;
112 @SuppressWarnings("unused")
113 public String dbPermissions;
116 private final String statusUp = "UP";
117 private final String statusDown = "DOWN";
118 private final String statusOk = "OK";
120 @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
121 public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
122 HealthStatus healthStatus = new HealthStatus(500, "");
124 // Return the status as 500 if it suspended due to manual fail over
125 if (HealthMonitor.isSuspended) {
126 healthStatus.body = "Suspended";
127 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
128 MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
129 Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
134 boolean overallStatus = true;
136 List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
138 HealthStatusInfo beInfo = new HealthStatusInfo("BE");
139 beInfo.hostName = EcompPortalUtils.getMyHostName();
140 beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
141 if (!HealthMonitor.isBackEndUp()) {
142 overallStatus = false;
143 beInfo.healthCheckStatus = statusDown;
144 beInfo.description = "Check the logs for more details";
145 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
147 statusCollection.add(beInfo);
149 HealthStatusInfo feInfo = new HealthStatusInfo("FE");
150 if (!HealthMonitor.isFrontEndUp()) {
151 overallStatus = false;
152 feInfo.healthCheckStatus = statusDown;
153 feInfo.description = "Check the logs for more details";
154 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
156 statusCollection.add(feInfo);
158 HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
159 if (!HealthMonitor.isDatabaseUp()) {
160 overallStatus = false;
161 dbInfo.healthCheckStatus = statusDown;
162 dbInfo.description = "Check the logs for more details";
163 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
166 if (!HealthMonitor.isClusterStatusOk()) {
167 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
168 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
170 dbInfo.dbClusterStatus = statusOk;
173 if (!HealthMonitor.isDatabasePermissionsOk()) {
174 dbInfo.dbPermissions = "Problem, check the logs for more details";
175 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
177 dbInfo.dbPermissions = statusOk;
179 statusCollection.add(dbInfo);
181 HealthStatusInfo uebInfo = new HealthStatusInfo("UEB");
182 if (!HealthMonitor.isUebUp()) {
183 // As per test case review meeting, UEB is considered as
184 // critical as DB. Hence commenting
185 // overallStatus = false;
186 uebInfo.healthCheckStatus = statusDown;
187 uebInfo.description = "Check the logs for more details";
188 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError);
190 statusCollection.add(uebInfo);
194 json = new Gson().toJson(statusCollection);
195 } catch (Exception e) {
196 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
198 logger.info(EELFLoggerDelegate.debugLogger, json);
201 healthStatus = new HealthStatus(200, json);
203 healthStatus = new HealthStatus(500, json);
204 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
206 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
207 } catch (Exception e) {
208 logger.error(EELFLoggerDelegate.errorLogger, "healthCheck failed", e);
211 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
216 @RequestMapping(value = {
217 "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
218 public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
219 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
221 HealthMonitor.isSuspended = true;
222 healthStatus.statusCode = 200;
224 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
225 response.getStatus());
230 @RequestMapping(value = {
231 "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
232 public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
233 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
235 HealthMonitor.isSuspended = false;
236 healthStatus.statusCode = 200;
237 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
238 response.getStatus());
242 @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
243 public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
244 HealthStatus healthStatus = new HealthStatus(200, "OK");
245 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());