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.isDatabasePermissionsOk()) {
 
 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                         HealthStatusInfo CassandraStatusInfo = new HealthStatusInfo("Music-Cassandra");
 
 183                         //CassandraStatusInfo.hostName = EcompPortalUtils.getMyHostName();
 
 184                         CassandraStatusInfo.ipAddress = MusicUtil.getMyCassaHost();
 
 186                         if (!HealthMonitor.isCassandraStatusOk()) {
 
 187                                 overallStatus = false;
 
 188                                 CassandraStatusInfo.healthCheckStatus = statusDown;
 
 189                                 CassandraStatusInfo.description = "Check the logs for more details";
 
 190                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
 
 192                         statusCollection.add(CassandraStatusInfo);
 
 194                         HealthStatusInfo zookeeperStatusInfo = new HealthStatusInfo("Music-zookeeper");
 
 195                         //zookeeperStatusInfo.hostName = EcompPortalUtils.getMyHostName();
 
 196                         zookeeperStatusInfo.ipAddress = MusicUtil.getMyZkHost();
 
 197                         if (!HealthMonitor.isZookeeperStatusOk()) {
 
 198                                 overallStatus = false;
 
 199                                 zookeeperStatusInfo.healthCheckStatus = statusDown;
 
 200                                 zookeeperStatusInfo.description = "Check the logs for more details";
 
 201                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckZookeeperError);
 
 203                         statusCollection.add(zookeeperStatusInfo);
 
 207                                 json = new Gson().toJson(statusCollection);
 
 208                         } catch (Exception e) {
 
 209                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
 
 211                         logger.info(EELFLoggerDelegate.debugLogger, json);
 
 214                                 healthStatus = new HealthStatus(200, json);
 
 216                                 healthStatus = new HealthStatus(500, json);
 
 217                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 
 219                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
 
 220                 } catch (Exception e) {
 
 221                         logger.error(EELFLoggerDelegate.errorLogger,    "healthCheck failed", e);
 
 224                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
 
 229         @RequestMapping(value = {
 
 230                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
 
 231         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
 
 232                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
 
 234                 HealthMonitor.isSuspended = true;
 
 235                 healthStatus.statusCode = 200;
 
 237                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
 
 238                                 response.getStatus());
 
 243         @RequestMapping(value = {
 
 244                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
 
 245         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
 
 246                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
 
 248                 HealthMonitor.isSuspended = false;
 
 249                 healthStatus.statusCode = 200;
 
 250                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
 
 251                                 response.getStatus());
 
 255         @RequestMapping(value = { "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
 
 256         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
 
 257                 HealthStatus healthStatus = new HealthStatus(200, "OK");
 
 258                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =", response.getStatus());