2  * ================================================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ================================================================================
 
  20 package org.openecomp.portalapp.portal.controller;
 
  22 import java.util.ArrayList;
 
  23 import java.util.List;
 
  25 import javax.servlet.http.HttpServletRequest;
 
  26 import javax.servlet.http.HttpServletResponse;
 
  29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
  30 import org.springframework.web.bind.annotation.RequestMapping;
 
  31 import org.springframework.web.bind.annotation.RequestMethod;
 
  32 import org.springframework.web.bind.annotation.RestController;
 
  34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  35 import org.openecomp.portalapp.controller.EPUnRestrictedBaseController;
 
  36 import org.openecomp.portalapp.portal.listener.HealthMonitor;
 
  37 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
 
  38 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
 
  39 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
 
  40 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
 
  41 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
 
  42 import com.google.gson.Gson;
 
  45  * This controller checks the health status and returns JSON response.
 
  46  * It does not require authentication or session.
 
  49 @org.springframework.context.annotation.Configuration
 
  50 @EnableAspectJAutoProxy
 
  52 public class HealthCheckController extends EPUnRestrictedBaseController {
 
  54         private class HealthStatus {
 
  55                 public int statusCode;
 
  56                 @SuppressWarnings("unused")
 
  59                 public HealthStatus(int code, String body) {
 
  60                         this.statusCode = code;
 
  65         private class HealthStatusInfo {
 
  66                 HealthStatusInfo(String healthCheckComponent) {
 
  67                         this.healthCheckComponent = healthCheckComponent;
 
  68                         this.healthCheckStatus = statusUp; // Default value
 
  70                         this.description = statusOk; // Default value
 
  73                         this.dbClusterStatus = "";
 
  74                         this.dbPermissions = "";
 
  77                 @SuppressWarnings("unused")
 
  78                 public String healthCheckComponent;
 
  79                 @SuppressWarnings("unused")
 
  80                 public String healthCheckStatus;
 
  81                 @SuppressWarnings("unused")
 
  82                 public String version;
 
  83                 @SuppressWarnings("unused")
 
  84                 public String description;
 
  85                 @SuppressWarnings("unused")
 
  86                 public String hostName;
 
  87                 @SuppressWarnings("unused")
 
  88                 public String ipAddress;
 
  89                 @SuppressWarnings("unused")
 
  90                 public String dbClusterStatus;
 
  91                 @SuppressWarnings("unused")
 
  92                 public String dbPermissions;
 
  95         private final String statusUp = "UP";
 
  96         private final String statusDown = "DOWN";
 
  97         private final String statusOk = "OK";
 
  99         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class);
 
 101         @RequestMapping(value = { "/portalApi/healthCheck" }, method = RequestMethod.GET, produces = "application/json")
 
 102         public HealthStatus healthCheck(HttpServletRequest request, HttpServletResponse response) {
 
 103                 HealthStatus healthStatus = new HealthStatus(500, "");
 
 105                 // Return the status as 500 if it suspended due to manual fail over
 
 106                 if (HealthMonitor.isSuspended) {
 
 107                         healthStatus.body = "Suspended";
 
 108                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 
 109                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
 
 114                         boolean overallStatus = true;
 
 116                         List<HealthStatusInfo> statusCollection = new ArrayList<HealthStatusInfo>();
 
 118                         HealthStatusInfo beInfo = new HealthStatusInfo("BE");
 
 119                         beInfo.hostName = EcompPortalUtils.getMyHostName();
 
 120                         beInfo.ipAddress = EcompPortalUtils.getMyIpAdddress();
 
 121                         if (!HealthMonitor.isBackEndUp()) {
 
 122                                 overallStatus = false;
 
 123                                 beInfo.healthCheckStatus = statusDown;
 
 124                                 beInfo.description = "Check the logs for more details";
 
 125                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckError);
 
 127                         statusCollection.add(beInfo);
 
 129                         HealthStatusInfo feInfo = new HealthStatusInfo("FE");
 
 130                         if (!HealthMonitor.isFrontEndUp()) {
 
 131                                 overallStatus = false;
 
 132                                 feInfo.healthCheckStatus = statusDown;
 
 133                                 feInfo.description = "Check the logs for more details";
 
 134                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.FeHealthCheckError);
 
 136                         statusCollection.add(feInfo);
 
 138                         HealthStatusInfo dbInfo = new HealthStatusInfo("DB");
 
 139                         if (!HealthMonitor.isDatabaseUp()) {
 
 140                                 overallStatus = false;
 
 141                                 dbInfo.healthCheckStatus = statusDown;
 
 142                                 dbInfo.description = "Check the logs for more details";
 
 143                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
 
 146                         if (!HealthMonitor.isClusterStatusOk()) {
 
 147                                 dbInfo.dbClusterStatus = "Problem, check the logs for more details";
 
 148                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
 
 151                                 dbInfo.dbClusterStatus = statusOk;
 
 154                         if (!HealthMonitor.isDatabasePermissionsOk()) {
 
 155                                 dbInfo.dbPermissions = "Problem, check the logs for more details";
 
 156                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
 
 159                                 dbInfo.dbPermissions = statusOk;
 
 161                         statusCollection.add(dbInfo);
 
 163                         HealthStatusInfo uebInfo = new HealthStatusInfo("UEB");
 
 164                         if (!HealthMonitor.isUebUp()) {
 
 165                                 //As per test case review meeting, UEB is considered as critical as DB. Hence commenting
 
 166                                 //overallStatus = false;
 
 167                                 uebInfo.healthCheckStatus = statusDown;
 
 168                                 uebInfo.description = "Check the logs for more details";
 
 169                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError);
 
 171                         statusCollection.add(uebInfo);
 
 175                                 json = new Gson().toJson(statusCollection);
 
 176                         } catch (Exception e) {
 
 177                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput);
 
 179                         logger.info(EELFLoggerDelegate.debugLogger, json);
 
 182                                 healthStatus = new HealthStatus(200, json);
 
 184                                 healthStatus = new HealthStatus(500, json);
 
 185                                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 
 187                         MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(healthStatus.statusCode));
 
 188                 } catch (Exception e) {
 
 189                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the healthcheck. Details: "
 
 190                                         + EcompPortalUtils.getStackTrace(e));
 
 193                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheck", "GET result =", response.getStatus());
 
 198         @RequestMapping(value = {
 
 199                         "/portalApi/healthCheckSuspend" }, method = RequestMethod.GET, produces = "application/json")
 
 200         public HealthStatus healthCheckSuspend(HttpServletRequest request, HttpServletResponse response) {
 
 201                 HealthStatus healthStatus = new HealthStatus(500, "Suspended for manual failover mechanism");
 
 203                 HealthMonitor.isSuspended = true;
 
 204                 healthStatus.statusCode = 200;
 
 206                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckSuspend", "GET result =",
 
 207                                                                                                                                                 response.getStatus());
 
 212         @RequestMapping(value = {
 
 213                         "/portalApi/healthCheckResume" }, method = RequestMethod.GET, produces = "application/json")
 
 214         public HealthStatus healthCheckResume(HttpServletRequest request, HttpServletResponse response) {
 
 215                 HealthStatus healthStatus = new HealthStatus(500, "Resumed from manual failover mechanism");
 
 217                 HealthMonitor.isSuspended = false;
 
 218                 healthStatus.statusCode = 200;
 
 219                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/healthCheckResume", "GET result =",
 
 220                                                                                                                                                         response.getStatus());
 
 226         @RequestMapping(value = {
 
 227                         "/portalApi/ping" }, method = RequestMethod.GET, produces = "application/json")
 
 228         public HealthStatus ping(HttpServletRequest request, HttpServletResponse response) {
 
 229                 HealthStatus healthStatus = new HealthStatus(200, "OK");        
 
 230                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/ping", "GET result =",
 
 231                                 response.getStatus());