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============================================
38 package org.onap.portal.controller;
40 import com.google.gson.Gson;
41 import java.util.ArrayList;
42 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import org.onap.music.main.MusicUtil;
46 import org.onap.portal.logging.format.EPAppMessagesEnum;
47 import org.onap.portal.logging.logic.EPLogUtil;
48 import org.onap.portal.scheduler.healthMonitor.HealthMonitor;
49 import org.onap.portal.utils.EPCommonSystemProperties;
50 import org.onap.portal.utils.EcompPortalUtils;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.springframework.context.annotation.Configuration;
54 import org.springframework.context.annotation.EnableAspectJAutoProxy;
55 import org.springframework.web.bind.annotation.RequestMapping;
56 import org.springframework.web.bind.annotation.RequestMethod;
57 import org.springframework.web.bind.annotation.RestController;
61 @EnableAspectJAutoProxy
62 public class HealthCheckController {
64 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
66 private class HealthStatus {
68 public int statusCode;
69 @SuppressWarnings("unused")
72 public HealthStatus(int code, String body) {
73 this.statusCode = code;
78 private class HealthStatusInfo {
80 HealthStatusInfo(String healthCheckComponent) {
81 this.healthCheckComponent = healthCheckComponent;
82 this.healthCheckStatus = statusUp; // Default value
84 this.description = statusOk; // Default value
87 this.dbClusterStatus = "";
88 this.dbPermissions = "";
91 @SuppressWarnings("unused")
92 public String healthCheckComponent;
93 @SuppressWarnings("unused")
94 public String healthCheckStatus;
95 @SuppressWarnings("unused")
96 public String version;
97 @SuppressWarnings("unused")
98 public String description;
99 @SuppressWarnings("unused")
100 public String hostName;
101 @SuppressWarnings("unused")
102 public String ipAddress;
103 @SuppressWarnings("unused")
104 public String dbClusterStatus;
105 @SuppressWarnings("unused")
106 public String dbPermissions;
109 private final String statusUp = "UP";
110 private final String statusDown = "DOWN";
111 private final String statusOk = "OK";
113 @RequestMapping(value = {"/portalApi/healthCheck"}, method = RequestMethod.GET, produces = "application/json")
114 public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
115 HealthStatus healthStatus = new HealthStatus(500, "");
117 // Return the status as 500 if it suspended due to manual fail over
118 if (HealthMonitor.isSuspended()) {
119 healthStatus.body = "Suspended";
120 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
121 MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
122 Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
127 boolean overallStatus = true;
129 List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
131 HealthStatusInfo beInfo = new HealthStatusInfo("BE");
132 beInfo.hostName = EcompPortalUtils.getMyHostName();
133 beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
134 if (!HealthMonitor.isBackEndUp()) {
135 overallStatus = false;
136 beInfo.healthCheckStatus = statusDown;
137 beInfo.description = "Check the logs for more details";
138 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
140 statusCollection.add(beInfo);
142 HealthStatusInfo feInfo = new HealthStatusInfo("FE");
143 if (!HealthMonitor.isFrontEndUp()) {
144 overallStatus = false;
145 feInfo.healthCheckStatus = statusDown;
146 feInfo.description = "Check the logs for more details";
147 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
149 statusCollection.add(feInfo);
151 HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
152 if (!HealthMonitor.isDatabaseUp()) {
153 overallStatus = false;
154 dbInfo.healthCheckStatus = statusDown;
155 dbInfo.description = "Check the logs for more details";
156 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
158 if (!HealthMonitor.isDbPermissionsOk()) {
159 dbInfo.dbPermissions = "Problem, check the logs for more details";
160 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
162 dbInfo.dbPermissions = statusOk;
164 statusCollection.add(dbInfo);
166 if (org.onap.portalapp.music.util.MusicUtil.isMusicEnable()) {
167 HealthStatusInfo CassandraStatusInfo = new HealthStatusInfo("Music-Cassandra");
168 //CassandraStatusInfo.hostName = EcompPortalUtils.getMyHostName();
169 CassandraStatusInfo.ipAddress = MusicUtil.getMyCassaHost();
171 if (!HealthMonitor.isCassandraStatusOk()) {
172 overallStatus = false;
173 CassandraStatusInfo.healthCheckStatus = statusDown;
174 CassandraStatusInfo.description = "Check the logs for more details";
175 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
177 statusCollection.add(CassandraStatusInfo);
182 json = new Gson().toJson(statusCollection);
183 } catch (Exception e) {
184 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
186 logger.info(EELFLoggerDelegate.debugLogger, json);
189 healthStatus = new HealthStatus(200, json);
191 healthStatus = new HealthStatus(500, json);
192 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
194 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
195 } catch (Exception e) {
196 logger.error(EELFLoggerDelegate.errorLogger, "healthCheck failed", e);
199 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
204 @RequestMapping(value = {
205 "/portalApi/healthCheckSuspend"}, method = RequestMethod.GET, produces = "application/json")
206 public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
207 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
209 HealthMonitor.setSuspended(true);
210 healthStatus.statusCode = 200;
212 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
213 response.getStatus());
218 @RequestMapping(value = {
219 "/portalApi/healthCheckResume"}, method = RequestMethod.GET, produces = "application/json")
220 public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
221 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
223 HealthMonitor.setSuspended(false);
224 healthStatus.statusCode = 200;
225 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
226 response.getStatus());
230 @RequestMapping(value = {"/portalApi/ping"}, method = RequestMethod.GET, produces = "application/json")
231 public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
232 HealthStatus healthStatus = new HealthStatus(200, "OK");
233 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());