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.GetMapping;
50 import org.springframework.web.bind.annotation.RequestMethod;
51 import org.springframework.web.bind.annotation.RestController;
52 import org.onap.music.main.MusicUtil;
53 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
54 import org.onap.portalapp.portal.listener.HealthMonitor;
55 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
56 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
57 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
58 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
59 import org.onap.portalapp.portal.utils.EcompPortalUtils;
60 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
62 import com.google.gson.Gson;
65 * This controller processes requests for the health-check feature implemented
66 * in the HealthMonitor, which runs in its own thread. These requests do not
67 * require any authentication nor an active user session.
70 @org.springframework.context.annotation.Configuration
71 @EnableAspectJAutoProxy
73 public class HealthCheckController extends EPUnRestrictedBaseController {
75 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
77 private class HealthStatus {
78 public int statusCode;
79 @SuppressWarnings("unused")
82 public HealthStatus(int code, String body) {
83 this.statusCode = code;
88 private class HealthStatusInfo {
89 HealthStatusInfo(String healthCheckComponent) {
90 this.healthCheckComponent = healthCheckComponent;
91 this.healthCheckStatus = statusUp; // Default value
93 this.description = statusOk; // Default value
96 this.dbClusterStatus = "";
97 this.dbPermissions = "";
100 @SuppressWarnings("unused")
101 public String healthCheckComponent;
102 @SuppressWarnings("unused")
103 public String healthCheckStatus;
104 @SuppressWarnings("unused")
105 public String version;
106 @SuppressWarnings("unused")
107 public String description;
108 @SuppressWarnings("unused")
109 public String hostName;
110 @SuppressWarnings("unused")
111 public String ipAddress;
112 @SuppressWarnings("unused")
113 public String dbClusterStatus;
114 @SuppressWarnings("unused")
115 public String dbPermissions;
118 private final String statusUp = "UP";
119 private final String statusDown = "DOWN";
120 private final String statusOk = "OK";
122 @GetMapping(value = { "/portalApi/healthCheck" }, produces = "application/json")
123 public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
124 HealthStatus healthStatus = new HealthStatus(500, "");
126 // Return the status as 500 if it suspended due to manual fail over
127 if (HealthMonitor.isSuspended()) {
128 healthStatus.body = "Suspended";
129 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
130 MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
131 Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
136 boolean overallStatus = true;
138 List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
140 HealthStatusInfo beInfo = new HealthStatusInfo("BE");
141 beInfo.hostName = EcompPortalUtils.getMyHostName();
142 beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
143 if (!HealthMonitor.isBackEndUp()) {
144 overallStatus = false;
145 beInfo.healthCheckStatus = statusDown;
146 beInfo.description = "Check the logs for more details";
147 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
149 statusCollection.add(beInfo);
151 HealthStatusInfo feInfo = new HealthStatusInfo("FE");
152 if (!HealthMonitor.isFrontEndUp()) {
153 overallStatus = false;
154 feInfo.healthCheckStatus = statusDown;
155 feInfo.description = "Check the logs for more details";
156 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
158 statusCollection.add(feInfo);
160 HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
161 if (!HealthMonitor.isDatabaseUp()) {
162 overallStatus = false;
163 dbInfo.healthCheckStatus = statusDown;
164 dbInfo.description = "Check the logs for more details";
165 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
168 // if (!HealthMonitor.isClusterStatusOk()) {
169 // dbInfo.dbClusterStatus = "Problem, check the logs for more details";
170 // EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
172 // dbInfo.dbClusterStatus = statusOk;
175 if (!HealthMonitor.isDbPermissionsOk()) {
176 dbInfo.dbPermissions = "Problem, check the logs for more details";
177 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
179 dbInfo.dbPermissions = statusOk;
181 statusCollection.add(dbInfo);
183 if(org.onap.portalapp.music.util.MusicUtil.isMusicEnable()){
184 HealthStatusInfo CassandraStatusInfo = new HealthStatusInfo("Music-Cassandra");
185 //CassandraStatusInfo.hostName = EcompPortalUtils.getMyHostName();
186 CassandraStatusInfo.ipAddress = MusicUtil.getMyCassaHost();
188 if (!HealthMonitor.isCassandraStatusOk()) {
189 overallStatus = false;
190 CassandraStatusInfo.healthCheckStatus = statusDown;
191 CassandraStatusInfo.description = "Check the logs for more details";
192 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
194 statusCollection.add(CassandraStatusInfo);
197 * HealthStatusInfo zookeeperStatusInfo = new
198 * HealthStatusInfo("Music-zookeeper"); --zookeeperStatusInfo.hostName =
199 * EcompPortalUtils.getMyHostName(); zookeeperStatusInfo.ipAddress =
200 * MusicUtil.getMyZkHost(); if (!HealthMonitor.isZookeeperStatusOk()) {
201 * overallStatus = false; zookeeperStatusInfo.healthCheckStatus = statusDown;
202 * zookeeperStatusInfo.description = "Check the logs for more details";
203 * EPLogUtil.logEcompError(logger,
204 * EPAppMessagesEnum.MusicHealthCheckZookeeperError); }
205 * statusCollection.add(zookeeperStatusInfo);
211 json = new Gson().toJson(statusCollection);
212 } catch (Exception e) {
213 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
215 logger.info(EELFLoggerDelegate.debugLogger, json);
218 healthStatus = new HealthStatus(200, json);
220 healthStatus = new HealthStatus(500, json);
221 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
223 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
224 } catch (Exception e) {
225 logger.error(EELFLoggerDelegate.errorLogger, "healthCheck failed", e);
228 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
233 @GetMapping(value = {
234 "/portalApi/healthCheckSuspend" }, produces = "application/json")
235 public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
236 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
238 HealthMonitor.setSuspended(true);
239 healthStatus.statusCode = 200;
241 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
242 response.getStatus());
247 @GetMapping(value = {
248 "/portalApi/healthCheckResume" }, produces = "application/json")
249 public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
250 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
252 HealthMonitor.setSuspended(false);
253 healthStatus.statusCode = 200;
254 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
255 response.getStatus());
259 @GetMapping(value = { "/portalApi/ping" }, produces = "application/json")
260 public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
261 HealthStatus healthStatus = new HealthStatus(200, "OK");
262 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());