Merge "MicroserviceParameter class DB constraints"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / listener / HealthMonitor.java
index 6f934e9..45b5323 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START==========================================
  * ONAP Portal
  * ===================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
  */
 package org.onap.portalapp.portal.listener;
 
-import java.io.IOException;
-import java.util.HashSet;
+import java.time.Instant;
 import java.util.List;
-import java.util.Set;
-import java.util.UUID;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
@@ -89,10 +86,10 @@ public class HealthMonitor {
        private static boolean uebUp;
        private static boolean frontEndUp;
        private static boolean backEndUp;
-       private static boolean dbClusterStatusOk;
        private static boolean dbPermissionsOk;
        private static boolean zookeeperStatusOk;
        private static boolean cassandraStatusOk;
+       private static String APPLICATION = "Portal";
        
        /**
         * Read directly by external classes.
@@ -108,10 +105,6 @@ public class HealthMonitor {
                return databaseUp;
        }
 
-       public static boolean isClusterStatusOk() {
-               return dbClusterStatusOk;
-       }
-
        public static boolean isDatabasePermissionsOk() {
                return dbPermissionsOk;
        }
@@ -145,6 +138,7 @@ public class HealthMonitor {
                int numIntervalsCassandraNotHealthy = 0;
 
                logger.debug(EELFLoggerDelegate.debugLogger, "monitorEPHealth thread started");
+        
 
                long sleepInterval = (Long
                                .valueOf(SystemProperties.getProperty(EPCommonSystemProperties.HEALTH_POLL_INTERVAL_SECONDS)) * 1000);
@@ -153,8 +147,10 @@ public class HealthMonitor {
                logger.debug(EELFLoggerDelegate.debugLogger,
                                "monitorEPHealth: Polling health every " + sleepInterval + " milliseconds. Alerting every "
                                                + (sleepInterval * numIntervalsBetweenAlerts) / 1000 + " seconds when component remains down.");
-
+               
                while (true) {
+                       logger.debug(EELFLoggerDelegate.debugLogger,
+                                       "monitorEPHealth: Test Connection to all");
                        //
                        // Get DB status. If down, signal alert once every X intervals.
                        //
@@ -171,18 +167,6 @@ public class HealthMonitor {
                                }
                        }
 
-                       dbClusterStatusOk = this.checkClusterStatus();
-                       if (dbClusterStatusOk == false) {
-                               if ((numIntervalsClusterNotHealthy % numIntervalsBetweenAlerts) == 0) {
-                                       logger.debug(EELFLoggerDelegate.debugLogger,
-                                                       "monitorEPHealth: cluster nodes down, logging to error log to trigger alert.");
-                                       EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckMySqlError);
-                                       numIntervalsClusterNotHealthy++;
-                               } else {
-                                       numIntervalsClusterNotHealthy = 0;
-                               }
-                       }
-
                        dbPermissionsOk = this.checkDatabasePermissions();
                        if (dbPermissionsOk == false) {
                                if ((numIntervalsDatabasePermissionsIncorrect % numIntervalsBetweenAlerts) == 0) {
@@ -194,31 +178,36 @@ public class HealthMonitor {
                                        numIntervalsDatabasePermissionsIncorrect = 0;
                                }
                        }
-                       
-                       zookeeperStatusOk = this.checkZookeeperStatus();
-                       if (zookeeperStatusOk == false) {
-                               if ((numIntervalsZookeeperNotHealthy % numIntervalsBetweenAlerts) == 0) {
-                                       logger.debug(EELFLoggerDelegate.debugLogger,
-                                                       "monitorEPHealth: cluster nodes down, logging to error log to trigger alert.");
-                                       EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckZookeeperError);
-                                       numIntervalsZookeeperNotHealthy++;
-                               } else {
-                                       numIntervalsZookeeperNotHealthy = 0;
+                       org.onap.portalapp.music.util.MusicUtil MusicUtilSDK = new org.onap.portalapp.music.util.MusicUtil();
+                       if(MusicUtilSDK.isMusicEnable()){
+
+                               zookeeperStatusOk = this.checkZookeeperStatus();
+
+                               if (zookeeperStatusOk == false) {
+                                       if ((numIntervalsZookeeperNotHealthy % numIntervalsBetweenAlerts) == 0) {
+                                               logger.debug(EELFLoggerDelegate.debugLogger,
+                                                               "monitorEPHealth: cluster nodes down, logging to error log to trigger alert.");
+                                               EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckZookeeperError);
+                                               numIntervalsZookeeperNotHealthy++;
+                                       } else {
+                                               numIntervalsZookeeperNotHealthy = 0;
+                                       }
                                }
-                       }
 
-                       cassandraStatusOk = this.checkCassandraStatus();
-                       if (cassandraStatusOk == false) {
-                               if ((numIntervalsCassandraNotHealthy % numIntervalsBetweenAlerts) == 0) {
-                                       logger.debug(EELFLoggerDelegate.debugLogger,
-                                                       "monitorEPHealth: cluster nodes down, logging to error log to trigger alert.");
-                                       EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
-                                       numIntervalsCassandraNotHealthy++;
-                               } else {
-                                       numIntervalsCassandraNotHealthy = 0;
+                               cassandraStatusOk = this.checkCassandraStatus();
+                               if (cassandraStatusOk == false) {
+                                       if ((numIntervalsCassandraNotHealthy % numIntervalsBetweenAlerts) == 0) {
+                                               logger.debug(EELFLoggerDelegate.debugLogger,
+                                                               "monitorEPHealth: cluster nodes down, logging to error log to trigger alert.");
+                                               EPLogUtil.logEcompError(logger, EPAppMessagesEnum.MusicHealthCheckCassandraError);
+                                               numIntervalsCassandraNotHealthy++;
+                                       } else {
+                                               numIntervalsCassandraNotHealthy = 0;
+                                       }
                                }
                        }
                        
+                       
                        //
                        // Get UEB status. Publish a bogus message to EP inbox, if 200 OK
                        // returned, status is Up.
@@ -278,7 +267,8 @@ public class HealthMonitor {
                                        monitorEPHealth();
                                } catch (InterruptedException e) {
                                        logger.debug(EELFLoggerDelegate.debugLogger, "healthMonitorThread interrupted", e);
-                               } catch (Exception e) {
+                               } 
+                               catch (Exception e) {
                                        logger.error(EELFLoggerDelegate.errorLogger, "healthMonitorThread failed", e);
                                }
                        }
@@ -326,36 +316,6 @@ public class HealthMonitor {
                return isUp;
        }
 
-       private boolean checkClusterStatus() {
-               boolean isUp = false;
-               Session localSession = null;
-               try {
-                       localSession = sessionFactory.openSession();
-                       if (localSession != null) {
-                               // If all nodes are unhealthy in a cluster, this will throw an
-                               // exception
-                               String sql = "select * from mysql.user";
-                               Query query = localSession.createSQLQuery(sql);
-                               @SuppressWarnings("unchecked")
-                               List<String> queryList = query.list();
-                               if (queryList != null) {
-                                       isUp = true;
-                               }
-                       }
-               } catch (Exception e) {
-                       logger.error(EELFLoggerDelegate.errorLogger, "checkClusterStatus failed", e);
-                       if ((e.getCause() != null) && (e.getCause().getMessage() != null)) {
-                               logger.error(EELFLoggerDelegate.errorLogger, "checkClusterStatus failure cause", e.getCause());
-                       }
-                       isUp = false;
-               } finally {
-                       if (localSession != null) {
-                               localSession.close();
-                       }
-               }
-               return isUp;
-       }
-
        private boolean checkZookeeperStatus() {
 
                String[] zookeeperNodes = MusicUtil.getMyZkHost().split(",");
@@ -373,7 +333,7 @@ public class HealthMonitor {
                                                        zkNodeStatistics.indexOf("Node"));
                                        logger.info(EELFLoggerDelegate.applicationLogger,
                                                        "Getting Status for zookeeper :" + zookeeperNodes[i].trim() + ":------:" + state);
-                                       if (state.contains("leader"))
+                                       if (state.contains("leader") || state.contains("follower"))
                                                return true;
                                }
                        } catch (Exception e) {
@@ -396,30 +356,22 @@ public class HealthMonitor {
        }
        
        private Boolean getAdminKeySpace() {
-               String musicKeySpace = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_KEYSPACE );
-               //deletePortalHealthcheck(musicKeySpace);
+               String musicKeySpace = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_KEYSPACE);
+               Instant creationTime = Instant.now();
                PreparedQueryObject pQuery = new PreparedQueryObject();
-               pQuery.appendQueryString("insert into  "+musicKeySpace+".healthcheck (id) values (?)");
-               pQuery.addValue(UUID.randomUUID());
+               pQuery.appendQueryString(
+                               "UPDATE " + musicKeySpace + ".health_check  SET creation_time = ? WHERE primary_id = ?");
+               pQuery.addValue(creationTime.toString());
+               pQuery.addValue(APPLICATION);
                try {
-                        MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL);
+                       MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.CRITICAL);
                } catch (MusicServiceException e) {
-                       logger.error(EELFLoggerDelegate.errorLogger, "getAdminKeySpace() failed", e.getCause());
                        return Boolean.FALSE;
                }
-                       return Boolean.TRUE;
-       }
+               return Boolean.TRUE;
 
-       
-       private void  deletePortalHealthcheck(String musicKeySpace) {
-               PreparedQueryObject pQuery = new PreparedQueryObject();
-               pQuery.appendQueryString("TRUNCATE  "+musicKeySpace+".healthcheck");
-               try {
-                       MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL);
-               } catch (MusicServiceException e) {
-                       logger.error(EELFLoggerDelegate.errorLogger, "deletePortalHealthcheck() failed", e.getCause());
-               }
        }
+
        
        private boolean checkDatabasePermissions() {
                boolean isUp = false;