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.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.music.main.MusicUtil;
52 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
53 import org.onap.portalapp.portal.listener.HealthMonitor;
54 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
55 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
56 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
57 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
58 import org.onap.portalapp.portal.utils.EcompPortalUtils;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
61 import com.google.gson.Gson;
64 * This controller processes requests for the health-check feature implemented
65 * in the HealthMonitor, which runs in its own thread. These requests do not
66 * require any authentication nor an active user session.
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
72 public class HealthCheckController extends EPUnRestrictedBaseController {
74 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
76 private class HealthStatus {
77 public int statusCode;
78 @SuppressWarnings("unused")
81 public HealthStatus(int code, String body) {
82 this.statusCode = code;
87 private class HealthStatusInfo {
88 HealthStatusInfo(String healthCheckComponent) {
89 this.healthCheckComponent = healthCheckComponent;
90 this.healthCheckStatus = statusUp; // Default value
92 this.description = statusOk; // Default value
95 this.dbClusterStatus = "";
96 this.dbPermissions = "";
99 @SuppressWarnings("unused")
100 public String healthCheckComponent;
101 @SuppressWarnings("unused")
102 public String healthCheckStatus;
103 @SuppressWarnings("unused")
104 public String version;
105 @SuppressWarnings("unused")
106 public String description;
107 @SuppressWarnings("unused")
108 public String hostName;
109 @SuppressWarnings("unused")
110 public String ipAddress;
111 @SuppressWarnings("unused")
112 public String dbClusterStatus;
113 @SuppressWarnings("unused")
114 public String dbPermissions;
117 private final String statusUp = "UP";
118 private final String statusDown = "DOWN";
119 private final String statusOk = "OK";
121 @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
122 public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
123 HealthStatus healthStatus = new HealthStatus(500, "");
125 // Return the status as 500 if it suspended due to manual fail over
126 if (HealthMonitor.isSuspended()) {
127 healthStatus.body = "Suspended";
128 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
129 MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
130 Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
135 boolean overallStatus = true;
137 List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
139 HealthStatusInfo beInfo = new HealthStatusInfo("BE");
140 beInfo.hostName = EcompPortalUtils.getMyHostName();
141 beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
142 if (!HealthMonitor.isBackEndUp()) {
143 overallStatus = false;
144 beInfo.healthCheckStatus = statusDown;
145 beInfo.description = "Check the logs for more details";
146 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
148 statusCollection.add(beInfo);
150 HealthStatusInfo feInfo = new HealthStatusInfo("FE");
151 if (!HealthMonitor.isFrontEndUp()) {
152 overallStatus = false;
153 feInfo.healthCheckStatus = statusDown;
154 feInfo.description = "Check the logs for more details";
155 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
157 statusCollection.add(feInfo);
159 HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
160 if (!HealthMonitor.isDatabaseUp()) {
161 overallStatus = false;
162 dbInfo.healthCheckStatus = statusDown;
163 dbInfo.description = "Check the logs for more details";
164 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
167 // if (!HealthMonitor.isClusterStatusOk()) {
168 // dbInfo.dbClusterStatus = "Problem, check the logs for more details";
169 // EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
171 // dbInfo.dbClusterStatus = statusOk;
174 if (!HealthMonitor.isDbPermissionsOk()) {
175 dbInfo.dbPermissions = "Problem, check the logs for more details";
176 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
178 dbInfo.dbPermissions = statusOk;
180 statusCollection.add(dbInfo);
182 if(org.onap.portalapp.music.util.MusicUtil.isMusicEnable()){
183 HealthStatusInfo CassandraStatusInfo = new HealthStatusInfo("Music-Cassandra");
184 //CassandraStatusInfo.hostName = EcompPortalUtils.getMyHostName();
185 CassandraStatusInfo.ipAddress = MusicUtil.getMyCassaHost();
187 if (!HealthMonitor.isCassandraStatusOk()) {
188 overallStatus = false;
189 CassandraStatusInfo.healthCheckStatus = statusDown;
190 CassandraStatusInfo.description = "Check the logs for more details";
191 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
193 statusCollection.add(CassandraStatusInfo);
196 * HealthStatusInfo zookeeperStatusInfo = new
197 * HealthStatusInfo("Music-zookeeper"); --zookeeperStatusInfo.hostName =
198 * EcompPortalUtils.getMyHostName(); zookeeperStatusInfo.ipAddress =
199 * MusicUtil.getMyZkHost(); if (!HealthMonitor.isZookeeperStatusOk()) {
200 * overallStatus = false; zookeeperStatusInfo.healthCheckStatus = statusDown;
201 * zookeeperStatusInfo.description = "Check the logs for more details";
202 * EPLogUtil.logEcompError(logger,
203 * EPAppMessagesEnum.MusicHealthCheckZookeeperError); }
204 * statusCollection.add(zookeeperStatusInfo);
210 json = new Gson().toJson(statusCollection);
211 } catch (Exception e) {
212 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
214 logger.info(EELFLoggerDelegate.debugLogger, json);
217 healthStatus = new HealthStatus(200, json);
219 healthStatus = new HealthStatus(500, json);
220 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
222 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
223 } catch (Exception e) {
224 logger.error(EELFLoggerDelegate.errorLogger, "healthCheck failed", e);
227 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
232 @RequestMapping(value = {
233 "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
234 public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
235 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
237 HealthMonitor.setSuspended(true);
238 healthStatus.statusCode = 200;
240 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
241 response.getStatus());
246 @RequestMapping(value = {
247 "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
248 public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
249 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
251 HealthMonitor.setSuspended(false);
252 healthStatus.statusCode = 200;
253 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
254 response.getStatus());
258 @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
259 public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
260 HealthStatus healthStatus = new HealthStatus(200, "OK");
261 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());