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);
 
 195                                 HealthStatusInfo zookeeperStatusInfo = new HealthStatusInfo("Music-zookeeper");
 
 196                                 //zookeeperStatusInfo.hostName = EcompPortalUtils.getMyHostName();
 
 197                                 zookeeperStatusInfo.ipAddress = MusicUtil.getMyZkHost();
 
 198                                 if (!HealthMonitor.isZookeeperStatusOk()) {
 
 199                                         overallStatus = false;
 
 200                                         zookeeperStatusInfo.healthCheckStatus = statusDown;
 
 201                                         zookeeperStatusInfo.description = "Check the logs for more details";
 
 202                                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckZookeeperError);
 
 204                                 statusCollection.add(zookeeperStatusInfo);
 
 209                                 json = new Gson().toJson(statusCollection);
 
 210                         } catch (Exception e) {
 
 211                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
 
 213                         logger.info(EELFLoggerDelegate.debugLogger, json);
 
 216                                 healthStatus = new HealthStatus(200, json);
 
 218                                 healthStatus = new HealthStatus(500, json);
 
 219                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 
 221                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
 
 222                 } catch (Exception e) {
 
 223                         logger.error(EELFLoggerDelegate.errorLogger,    "healthCheck failed", e);
 
 226                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
 
 231         @RequestMapping(value = {
 
 232                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
 
 233         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
 
 234                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
 
 236                 HealthMonitor.setSuspended(true);
 
 237                 healthStatus.statusCode = 200;
 
 239                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
 
 240                                 response.getStatus());
 
 245         @RequestMapping(value = {
 
 246                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
 
 247         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
 
 248                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
 
 250                 HealthMonitor.setSuspended(false);
 
 251                 healthStatus.statusCode = 200;
 
 252                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
 
 253                                 response.getStatus());
 
 257         @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
 
 258         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
 
 259                 HealthStatus healthStatus = new HealthStatus(200, "OK");
 
 260                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());