2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modification Copyright © 2020 IBM.
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
40 package org.onap.portalapp.portal.controller;
42 import java.util.ArrayList;
43 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
49 import org.springframework.context.annotation.EnableAspectJAutoProxy;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.GetMapping;
52 import org.springframework.web.bind.annotation.RequestMethod;
53 import org.springframework.web.bind.annotation.RestController;
54 import org.onap.music.main.MusicUtil;
55 import org.onap.portalapp.controller.EPUnRestrictedBaseController;
56 import org.onap.portalapp.portal.listener.HealthMonitor;
57 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
58 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
59 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
60 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
61 import org.onap.portalapp.portal.utils.EcompPortalUtils;
62 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
64 import com.google.gson.Gson;
67 * This controller processes requests for the health-check feature implemented
68 * in the HealthMonitor, which runs in its own thread. These requests do not
69 * require any authentication nor an active user session.
72 @org.springframework.context.annotation.Configuration
73 @EnableAspectJAutoProxy
75 public class HealthCheckController extends EPUnRestrictedBaseController {
77 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
79 private class HealthStatus {
80 public int statusCode;
81 @SuppressWarnings("unused")
84 public HealthStatus(int code, String body) {
85 this.statusCode = code;
90 private class HealthStatusInfo {
91 HealthStatusInfo(String healthCheckComponent) {
92 this.healthCheckComponent = healthCheckComponent;
93 this.healthCheckStatus = statusUp; // Default value
95 this.description = statusOk; // Default value
98 this.dbClusterStatus = "";
99 this.dbPermissions = "";
102 @SuppressWarnings("unused")
103 public String healthCheckComponent;
104 @SuppressWarnings("unused")
105 public String healthCheckStatus;
106 @SuppressWarnings("unused")
107 public String version;
108 @SuppressWarnings("unused")
109 public String description;
110 @SuppressWarnings("unused")
111 public String hostName;
112 @SuppressWarnings("unused")
113 public String ipAddress;
114 @SuppressWarnings("unused")
115 public String dbClusterStatus;
116 @SuppressWarnings("unused")
117 public String dbPermissions;
120 private final String statusUp = "UP";
121 private final String statusDown = "DOWN";
122 private final String statusOk = "OK";
124 @GetMapping(value = { "/portalApi/healthCheck" }, produces = "application/json")
125 public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
126 HealthStatus healthStatus = new HealthStatus(500, "");
128 // Return the status as 500 if it suspended due to manual fail over
129 if (HealthMonitor.isSuspended()) {
130 healthStatus.body = "Suspended";
131 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
132 MDC.put(EPCommonSystemProperties.RESPONSE_CODE,
133 Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
138 boolean overallStatus = true;
140 List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
142 HealthStatusInfo beInfo = new HealthStatusInfo("BE");
143 beInfo.hostName = EcompPortalUtils.getMyHostName();
144 beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
145 if (!HealthMonitor.isBackEndUp()) {
146 overallStatus = false;
147 beInfo.healthCheckStatus = statusDown;
148 beInfo.description = "Check the logs for more details";
149 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
151 statusCollection.add(beInfo);
153 HealthStatusInfo feInfo = new HealthStatusInfo("FE");
154 if (!HealthMonitor.isFrontEndUp()) {
155 overallStatus = false;
156 feInfo.healthCheckStatus = statusDown;
157 feInfo.description = "Check the logs for more details";
158 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
160 statusCollection.add(feInfo);
162 HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
163 if (!HealthMonitor.isDatabaseUp()) {
164 overallStatus = false;
165 dbInfo.healthCheckStatus = statusDown;
166 dbInfo.description = "Check the logs for more details";
167 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
170 // if (!HealthMonitor.isClusterStatusOk()) {
171 // dbInfo.dbClusterStatus = "Problem, check the logs for more details";
172 // EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
174 // dbInfo.dbClusterStatus = statusOk;
177 if (!HealthMonitor.isDbPermissionsOk()) {
178 dbInfo.dbPermissions = "Problem, check the logs for more details";
179 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
181 dbInfo.dbPermissions = statusOk;
183 statusCollection.add(dbInfo);
185 if(org.onap.portalapp.music.util.MusicUtil.isMusicEnable()){
186 HealthStatusInfo CassandraStatusInfo = new HealthStatusInfo("Music-Cassandra");
187 //CassandraStatusInfo.hostName = EcompPortalUtils.getMyHostName();
188 CassandraStatusInfo.ipAddress = MusicUtil.getMyCassaHost();
190 if (!HealthMonitor.isCassandraStatusOk()) {
191 overallStatus = false;
192 CassandraStatusInfo.healthCheckStatus = statusDown;
193 CassandraStatusInfo.description = "Check the logs for more details";
194 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
196 statusCollection.add(CassandraStatusInfo);
199 * HealthStatusInfo zookeeperStatusInfo = new
200 * HealthStatusInfo("Music-zookeeper"); --zookeeperStatusInfo.hostName =
201 * EcompPortalUtils.getMyHostName(); zookeeperStatusInfo.ipAddress =
202 * MusicUtil.getMyZkHost(); if (!HealthMonitor.isZookeeperStatusOk()) {
203 * overallStatus = false; zookeeperStatusInfo.healthCheckStatus = statusDown;
204 * zookeeperStatusInfo.description = "Check the logs for more details";
205 * EPLogUtil.logEcompError(logger,
206 * EPAppMessagesEnum.MusicHealthCheckZookeeperError); }
207 * statusCollection.add(zookeeperStatusInfo);
213 json = new Gson().toJson(statusCollection);
214 } catch (Exception e) {
215 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
217 logger.info(EELFLoggerDelegate.debugLogger, json);
220 healthStatus = new HealthStatus(200, json);
222 healthStatus = new HealthStatus(500, json);
223 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
225 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
226 } catch (Exception e) {
227 logger.error(EELFLoggerDelegate.errorLogger, "healthCheck failed", e);
230 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
235 @GetMapping(value = {
236 "/portalApi/healthCheckSuspend" }, produces = "application/json")
237 public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
238 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
240 HealthMonitor.setSuspended(true);
241 healthStatus.statusCode = 200;
243 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
244 response.getStatus());
249 @GetMapping(value = {
250 "/portalApi/healthCheckResume" }, produces = "application/json")
251 public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
252 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
254 HealthMonitor.setSuspended(false);
255 healthStatus.statusCode = 200;
256 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
257 response.getStatus());
261 @GetMapping(value = { "/portalApi/ping" }, produces = "application/json")
262 public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
263 HealthStatus healthStatus = new HealthStatus(200, "OK");
264 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());