refactoring monitor hierarchy and adding status to monitor's monitor methos


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/fa5a7412
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/fa5a7412
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/fa5a7412

Branch: refs/heads/4.0.0-grouping
Commit: fa5a741200d0936c9f17ce1c50eb8b1c65735bf9
Parents: 35ef227
Author: reka <[email protected]>
Authored: Fri Oct 17 16:58:35 2014 +0530
Committer: reka <[email protected]>
Committed: Fri Oct 17 16:58:35 2014 +0530

----------------------------------------------------------------------
 .../stratos/autoscaler/AutoscalerContext.java   |  91 ++--
 .../grouping/dependency/DependencyBuilder.java  |  10 +-
 .../grouping/dependency/DependencyTree.java     |  38 +-
 .../dependency/context/ApplicationContext.java  |  23 +-
 .../context/ApplicationContextFactory.java      |  31 +-
 .../dependency/context/ClusterContext.java      |  12 +-
 .../AutoscalerHealthStatEventReceiver.java      |   4 +-
 .../AutoscalerTopologyEventReceiver.java        |  57 ++-
 .../monitor/AbstractClusterMonitor.java         |   9 +-
 .../monitor/ApplicationMonitorFactory.java      | 245 ++++++++-
 .../stratos/autoscaler/monitor/Monitor.java     | 426 +---------------
 .../monitor/MonitorStatusEventBuilder.java      |   8 +-
 .../monitor/ParentComponentMonitor.java         | 499 +++++++++++++++++++
 .../monitor/application/ApplicationMonitor.java | 107 +---
 .../autoscaler/monitor/group/GroupMonitor.java  | 124 +++--
 .../status/checker/StatusChecker.java           |  85 ++--
 .../stratos/autoscaler/util/AutoscalerUtil.java | 181 -------
 17 files changed, 1085 insertions(+), 865 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/AutoscalerContext.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/AutoscalerContext.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/AutoscalerContext.java
index 1145204..ff59ecf 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/AutoscalerContext.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/AutoscalerContext.java
@@ -22,9 +22,8 @@ package org.apache.stratos.autoscaler;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.autoscaler.monitor.Monitor;
 import org.apache.stratos.autoscaler.monitor.application.ApplicationMonitor;
-import org.apache.stratos.autoscaler.monitor.cluster.ClusterMonitor;
-import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
 import org.apache.stratos.autoscaler.monitor.cluster.LbClusterMonitor;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 
@@ -37,9 +36,18 @@ import java.util.Map;
 public class AutoscalerContext {
 
     private static final Log log = LogFactory.getLog(AutoscalerContext.class);
+    // Map<ClusterId, ClusterMonitor>
+    private Map<String, Monitor> monitors;
+    // Map<ClusterId, ClusterMonitor>
+    private Map<String, StatusChecker> statusCheckers;
+    // Map<LBClusterId, LBClusterMonitor>
+    private Map<String, LbClusterMonitor> lbMonitors;
+    private Map<String, ApplicationMonitor> appMonitors;
+
+
     private AutoscalerContext() {
         try {
-            setMonitors(new HashMap<String, ClusterMonitor>());
+            setMonitors(new HashMap<String, Monitor>());
             setLbMonitors(new HashMap<String, LbClusterMonitor>());
             setAppMonitors(new HashMap<String, ApplicationMonitor>());
         } catch (Exception e) {
@@ -47,49 +55,34 @@ public class AutoscalerContext {
         }
     }
 
-    // Map<ClusterId, ClusterMonitor>
-    private Map<String, ClusterMonitor> monitors;
-
-    // Map<ClusterId, ClusterMonitor>
-    private Map<String, StatusChecker> statusCheckers;
-    // Map<LBClusterId, LBClusterMonitor>
-    private Map<String, LbClusterMonitor> lbMonitors;
-
-
-    private Map<String, ApplicationMonitor> appMonitors;
+    public static AutoscalerContext getInstance() {
+        return Holder.INSTANCE;
+    }
 
     public Map<String, ApplicationMonitor> getAppMonitors() {
         return appMonitors;
     }
 
-    public ApplicationMonitor getAppMonitor(String applicationId) {
-        return appMonitors.get(applicationId);
-    }
-
     public void setAppMonitors(Map<String, ApplicationMonitor> appMonitors) {
         this.appMonitors = appMonitors;
     }
 
+    public ApplicationMonitor getAppMonitor(String applicationId) {
+        return appMonitors.get(applicationId);
+    }
+
     public Map<String, StatusChecker> getStatusCheckers() {
         return statusCheckers;
     }
 
-    private static class Holder {
-               private static final AutoscalerContext INSTANCE = new 
AutoscalerContext();
-       }
-
-       public static AutoscalerContext getInstance() {
-               return Holder.INSTANCE;
-       }
-
-    public void addMonitor(ClusterMonitor monitor) {
-        monitors.put(monitor.getClusterId(), monitor);
+    public void addMonitor(Monitor monitor) {
+        monitors.put(monitor.getId(), monitor);
     }
 
-    public ClusterMonitor getMonitor(String clusterId) {
+    public Monitor getMonitor(String clusterId) {
         return monitors.get(clusterId);
     }
-    
+
     public boolean monitorExist(String clusterId) {
         return monitors.containsKey(clusterId);
     }
@@ -101,38 +94,38 @@ public class AutoscalerContext {
     public boolean appMonitorExist(String appId) {
         return appMonitors.containsKey(appId);
     }
-    
+
     public boolean lbMonitorExist(String clusterId) {
         return lbMonitors.containsKey(clusterId);
     }
-    
+
     public LbClusterMonitor getLBMonitor(String clusterId) {
         return lbMonitors.get(clusterId);
     }
 
-    public ClusterMonitor removeMonitor(String clusterId) {
-       if(!monitorExist(clusterId)) {
-               log.fatal("Cluster monitor not found for cluster id: 
"+clusterId);
-               return null;
-       }
-       log.info("Removed monitor [cluster id]: " + clusterId);
+    public Monitor removeMonitor(String clusterId) {
+        if (!monitorExist(clusterId)) {
+            log.fatal("Cluster monitor not found for cluster id: " + 
clusterId);
+            return null;
+        }
+        log.info("Removed monitor [cluster id]: " + clusterId);
         return monitors.remove(clusterId);
     }
+
     public LbClusterMonitor removeLbMonitor(String clusterId) {
-       if(!lbMonitorExist(clusterId)) {
-               log.fatal("LB monitor not found for cluster id: "+clusterId);
-               return null;
-       }
-       log.info("Removed LB monitor [cluster id]: " + clusterId);
+        if (!lbMonitorExist(clusterId)) {
+            log.fatal("LB monitor not found for cluster id: " + clusterId);
+            return null;
+        }
+        log.info("Removed LB monitor [cluster id]: " + clusterId);
         return lbMonitors.remove(clusterId);
     }
 
-    public Map<String, ClusterMonitor> getMonitors() {
+    public Map<String, Monitor> getMonitors() {
         return monitors;
     }
 
-
-    public void setMonitors(Map<String, ClusterMonitor> monitors) {
+    public void setMonitors(Map<String, Monitor> monitors) {
         this.monitors = monitors;
     }
 
@@ -145,12 +138,16 @@ public class AutoscalerContext {
     }
 
     public ApplicationMonitor removeAppMonitor(String appId) {
-        if(!appMonitorExist(appId)) {
-            log.fatal("LB monitor not found for App id: "+ appId);
+        if (!appMonitorExist(appId)) {
+            log.fatal("LB monitor not found for App id: " + appId);
             return null;
         }
         log.info("Removed APP monitor [App id]: " + appId);
         return appMonitors.remove(appId);
     }
 
+    private static class Holder {
+        private static final AutoscalerContext INSTANCE = new 
AutoscalerContext();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyBuilder.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyBuilder.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyBuilder.java
index 71c73a2..14fc90e 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyBuilder.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyBuilder.java
@@ -140,14 +140,16 @@ public class DependencyBuilder {
         // as they can start in parallel.
         for (Group group1 : component.getAliasToGroupMap().values()) {
             if (dependencyTree.findApplicationContextWithId(group1.getAlias()) 
== null) {
-                dependencyTree.addApplicationContext(new 
GroupContext(group1.getAlias(),
-                        dependencyTree.isKillDependent()));
+                ApplicationContext context = ApplicationContextFactory.
+                        getGroupContext(group1.getAlias(), 
dependencyTree.isKillDependent());
+                dependencyTree.addApplicationContext(context);
             }
         }
         for (ClusterDataHolder dataHolder : 
component.getClusterDataMap().values()) {
             if 
(dependencyTree.findApplicationContextWithId(dataHolder.getClusterId()) == 
null) {
-                dependencyTree.addApplicationContext(new 
ClusterContext(dataHolder.getClusterId(),
-                        dependencyTree.isKillDependent()));
+                ApplicationContext context = 
ApplicationContextFactory.getClusterContext(dataHolder,
+                                                                
dependencyTree.isKillDependent());
+                dependencyTree.addApplicationContext(context);
 
             }
         }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
index d3dded3..156f3a5 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/DependencyTree.java
@@ -46,6 +46,10 @@ public class DependencyTree {
 
     private boolean killDependent;
 
+    private boolean startupOder;
+
+    private boolean reverseStartupOrder;
+
     private String id;
 
     public DependencyTree(String id) {
@@ -88,7 +92,7 @@ public class DependencyTree {
      */
     private ApplicationContext findApplicationContextWithId(String id, 
List<ApplicationContext> contexts) {
         for (ApplicationContext context : contexts) {
-            if (context.getId().equals(id)) {
+            if (context.getId().equals(id) && context.getCurrentStatus() == 
null) {
                 return context;
             }
         }
@@ -130,23 +134,23 @@ public class DependencyTree {
      * @param id the alias/id of group/cluster in which terminated event 
received
      * @return all the kill able children dependencies
      */
-    public List<ApplicationContext> getKillDependencies(String id) {
+    public List<ApplicationContext> getTerminationDependencies(String id) {
         List<ApplicationContext> allChildrenOfAppContext = new 
ArrayList<ApplicationContext>();
+        ApplicationContext applicationContext = 
findApplicationContextWithId(id);
 
-        if (killDependent) {
+        if (this.killDependent) {
             //finding the ApplicationContext of the given id
-            ApplicationContext applicationContext = 
findApplicationContextWithId(id);
             //finding all the children of the found application context
-            
findAllChildrenOfAppContext(applicationContext.getApplicationContextList(),
-                    allChildrenOfAppContext);
-            return allChildrenOfAppContext;
-        } else if (killAll) {
+                
findAllChildrenOfAppContext(applicationContext.getApplicationContextList(),
+                        allChildrenOfAppContext);
+                return allChildrenOfAppContext;
+        } else if (this.killAll) {
             //killall will be killed by the monitor from it's list.
             findAllChildrenOfAppContext(this.applicationContextList,
                     allChildrenOfAppContext);
 
         }
-        //return empty for the kill-none case
+        //return empty for the kill-none case, what ever returns here will be 
killed in
         return allChildrenOfAppContext;
     }
 
@@ -221,4 +225,20 @@ public class DependencyTree {
     public void setId(String id) {
         this.id = id;
     }
+
+    public boolean isStartupOder() {
+        return startupOder;
+    }
+
+    public void setStartupOder(boolean startupOder) {
+        this.startupOder = startupOder;
+    }
+
+    public boolean isReverseStartupOrder() {
+        return reverseStartupOrder;
+    }
+
+    public void setReverseStartupOrder(boolean reverseStartupOrder) {
+        this.reverseStartupOrder = reverseStartupOrder;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
index f923bb0..5b98264 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContext.java
@@ -22,6 +22,7 @@ import org.apache.stratos.messaging.domain.topology.Status;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Stack;
 
 /**
  * This is to keep track of the
@@ -35,13 +36,13 @@ public abstract class ApplicationContext {
 
     private Status status;
 
-    private List<Status> statusLifeCycle;
+    private Stack<Status> statusLifeCycle;
 
     protected boolean killDependent;
 
     public ApplicationContext(String id, boolean killDependent) {
         applicationContextList = new ArrayList<ApplicationContext>();
-        statusLifeCycle = new ArrayList<Status>();
+        statusLifeCycle = new Stack<Status>();
         this.killDependent = killDependent;
         this.id = id;
     }
@@ -60,7 +61,7 @@ public abstract class ApplicationContext {
     }
 
     public void addStatusToLIfeCycle(Status status) {
-       this.statusLifeCycle.add(status);
+       this.statusLifeCycle.push(status);
     }
 
     public String getId() {
@@ -71,11 +72,11 @@ public abstract class ApplicationContext {
         this.id = id;
     }
 
-    public Status getStatus() {
+    public Status getCurrentStatus() {
         return status;
     }
 
-    public void setStatus(Status status) {
+    public void setCurrentStatus(Status status) {
         this.status = status;
     }
 
@@ -83,7 +84,15 @@ public abstract class ApplicationContext {
         return statusLifeCycle;
     }
 
-    public void setStatusLifeCycle(List<Status> statusLifeCycle) {
-        this.statusLifeCycle = statusLifeCycle;
+    public boolean hasChild() {
+        boolean hasChild;
+        if(this.applicationContextList.isEmpty()) {
+            hasChild = false;
+        } else {
+            hasChild = true;
+        }
+        return hasChild;
     }
+
+
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContextFactory.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContextFactory.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContextFactory.java
index 685f131..970d593 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContextFactory.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ApplicationContextFactory.java
@@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.autoscaler.Constants;
 import org.apache.stratos.messaging.domain.topology.ClusterDataHolder;
+import org.apache.stratos.messaging.domain.topology.Group;
 import org.apache.stratos.messaging.domain.topology.ParentComponent;
 
 /**
@@ -32,8 +33,9 @@ public class ApplicationContextFactory {
 
     /**
      * Will return the GroupContext/ClusterContext based on the type in start 
order
-     * @param startOrder reference of group/cluster in the start order
-     * @param component The component which used to build the dependency
+     *
+     * @param startOrder      reference of group/cluster in the start order
+     * @param component       The component which used to build the dependency
      * @param isKillDependent kill dependent behaviour of this component
      * @return Context
      */
@@ -45,15 +47,14 @@ public class ApplicationContextFactory {
         if (startOrder.startsWith(Constants.GROUP + ".")) {
             //getting the group alias
             id = getGroupFromStartupOrder(startOrder);
-            applicationContext = new GroupContext(id,
-                    isKillDependent);
+            applicationContext = getGroupContext(id, isKillDependent);
         } else if (startOrder.startsWith(Constants.CARTRIDGE + ".")) {
             //getting the cluster alias
             id = getClusterFromStartupOrder(startOrder);
             //getting the cluster-id from cluster alias
             ClusterDataHolder clusterDataHolder = 
component.getClusterDataMap().get(id);
-            applicationContext = new 
ClusterContext(clusterDataHolder.getClusterId(),
-                    isKillDependent);
+            applicationContext = getClusterContext(clusterDataHolder, 
isKillDependent);
+
         } else {
             log.warn("[Startup Order]: " + startOrder + " contains unknown 
reference");
         }
@@ -63,6 +64,7 @@ public class ApplicationContextFactory {
 
     /**
      * Utility method to get the group alias from the startup order Eg: 
group.mygroup
+     *
      * @param startupOrder startup order
      * @return group alias
      */
@@ -72,10 +74,27 @@ public class ApplicationContextFactory {
 
     /**
      * Utility method to get the cluster alias from startup order Eg: 
cartridge.myphp
+     *
      * @param startupOrder startup order
      * @return cluster alias
      */
     public static String getClusterFromStartupOrder(String startupOrder) {
         return startupOrder.substring(Constants.CARTRIDGE.length() + 1);
     }
+
+    public static ApplicationContext getClusterContext(ClusterDataHolder 
dataHolder,
+                                                       boolean 
isKillDependent) {
+        ApplicationContext applicationContext;
+        applicationContext = new ClusterContext(dataHolder.getClusterId(),
+                isKillDependent);
+        ((ClusterContext) 
applicationContext).setServiceName(dataHolder.getServiceType());
+        return  applicationContext;
+    }
+
+    public static ApplicationContext getGroupContext(String id, boolean 
isKillDependent) {
+        ApplicationContext applicationContext;
+        applicationContext = new GroupContext(id,
+                isKillDependent);
+        return applicationContext;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ClusterContext.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ClusterContext.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ClusterContext.java
index 23ea039..d11389d 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ClusterContext.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/grouping/dependency/context/ClusterContext.java
@@ -19,10 +19,20 @@
 package org.apache.stratos.autoscaler.grouping.dependency.context;
 
 /**
- * Created by reka on 10/1/14.
+ * ClusterContext to keep cluster level context information
  */
 public class ClusterContext extends ApplicationContext {
+    private String serviceName;
+
     public ClusterContext(String id, boolean killDependent) {
         super(id ,killDependent);
     }
+
+    public String getServiceName() {
+        return serviceName;
+    }
+
+    public void setServiceName(String serviceName) {
+        this.serviceName = serviceName;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/health/AutoscalerHealthStatEventReceiver.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/health/AutoscalerHealthStatEventReceiver.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/health/AutoscalerHealthStatEventReceiver.java
index a1213f6..b909749 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/health/AutoscalerHealthStatEventReceiver.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/health/AutoscalerHealthStatEventReceiver.java
@@ -485,7 +485,7 @@ public class AutoscalerHealthStatEventReceiver implements 
Runnable {
         AbstractClusterMonitor monitor;
 
         if(asCtx.monitorExist(clusterId)){
-            monitor = asCtx.getMonitor(clusterId);
+            monitor = (AbstractClusterMonitor) asCtx.getMonitor(clusterId);
         }else if(asCtx.lbMonitorExist(clusterId)){
             monitor = asCtx.getLBMonitor(clusterId);
         }else{
@@ -558,7 +558,7 @@ public class AutoscalerHealthStatEventReceiver implements 
Runnable {
             }
                return null;
         }
-        AbstractClusterMonitor monitor = 
AutoscalerContext.getInstance().getMonitor(member.getClusterId());
+        AbstractClusterMonitor monitor = (AbstractClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(member.getClusterId());
         if(null == monitor){
 
             monitor = 
AutoscalerContext.getInstance().getLBMonitor(member.getClusterId());

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java
index 801af0e..1e20937 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/message/receiver/topology/AutoscalerTopologyEventReceiver.java
@@ -28,12 +28,12 @@ import 
org.apache.stratos.autoscaler.exception.DependencyBuilderException;
 import org.apache.stratos.autoscaler.exception.TerminationException;
 import org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
 import org.apache.stratos.autoscaler.monitor.AbstractClusterMonitor;
+import org.apache.stratos.autoscaler.monitor.ApplicationMonitorFactory;
 import org.apache.stratos.autoscaler.monitor.application.ApplicationMonitor;
 import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
 import org.apache.stratos.autoscaler.partition.PartitionManager;
 import org.apache.stratos.autoscaler.policy.PolicyManager;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
-import org.apache.stratos.autoscaler.util.AutoscalerUtil;
 import org.apache.stratos.messaging.domain.topology.Application;
 import org.apache.stratos.messaging.domain.topology.Cluster;
 import org.apache.stratos.messaging.domain.topology.Service;
@@ -150,7 +150,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                 String appId = clusterActivatedEvent.getAppId();
                 String clusterId = clusterActivatedEvent.getClusterId();
                 AbstractClusterMonitor clusterMonitor =
-                        AutoscalerContext.getInstance().getMonitor(clusterId);
+                        (AbstractClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
 
                 //changing the status in the monitor, will notify its parent 
monitor
                 clusterMonitor.setStatus(Status.Activated);
@@ -171,7 +171,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                 String groupId = groupActivatedEvent.getGroupId();
 
                 ApplicationMonitor appMonitor = 
AutoscalerContext.getInstance().getAppMonitor(appId);
-                GroupMonitor monitor = 
appMonitor.findGroupMonitorWithId(groupId);
+                GroupMonitor monitor = (GroupMonitor) 
appMonitor.findGroupMonitorWithId(groupId);
 
                 //changing the status in the monitor, will notify its parent 
monitor
                 monitor.setStatus(Status.Activated);
@@ -209,7 +209,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
 
                 try {
                     //TODO remove monitors as well as any starting or pending 
threads
-                    ApplicationMonitor monitor = 
AutoscalerContext.getInstance().
+                    /*ApplicationMonitor monitor = 
AutoscalerContext.getInstance().
                             
getAppMonitor(applicationRemovedEvent.getApplicationId());
                     if (monitor != null) {
                         List<String> clusters = monitor.
@@ -225,7 +225,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                     } else {
                         log.warn("Application Monitor cannot be found for the 
removed [application] "
                                 + applicationRemovedEvent.getApplicationId());
-                    }
+                    }*/
 
 
                 } finally {
@@ -248,7 +248,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                     String memberId = memberReadyToShutdownEvent.getMemberId();
 
                     if (asCtx.monitorExist(clusterId)) {
-                        monitor = asCtx.getMonitor(clusterId);
+                        monitor = (AbstractClusterMonitor) 
asCtx.getMonitor(clusterId);
                     } else if (asCtx.lbMonitorExist(clusterId)) {
                         monitor = asCtx.getLBMonitor(clusterId);
                     } else {
@@ -303,13 +303,14 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
 
                     Service service = 
TopologyManager.getTopology().getService(clusterMaitenanceEvent.getServiceName());
                     Cluster cluster = 
service.getCluster(clusterMaitenanceEvent.getClusterId());
+                    AbstractClusterMonitor monitor;
                     if 
(AutoscalerContext.getInstance().monitorExist((cluster.getClusterId()))) {
-                        
AutoscalerContext.getInstance().getMonitor(clusterMaitenanceEvent.getClusterId()).
-                                                        
setStatus(clusterMaitenanceEvent.getStatus());
+                        monitor = (AbstractClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterMaitenanceEvent.getClusterId());
+                        monitor.setStatus(Status.In_Active);
                     } else if (AutoscalerContext.getInstance().
-                                        
lbMonitorExist((cluster.getClusterId()))) {
+                            lbMonitorExist((cluster.getClusterId()))) {
                         
AutoscalerContext.getInstance().getLBMonitor(clusterMaitenanceEvent.getClusterId()).
-                                                        
setStatus(clusterMaitenanceEvent.getStatus());
+                                setStatus(clusterMaitenanceEvent.getStatus());
                     } else {
                         log.error("cluster monitor not exists for the cluster: 
" + cluster.toString());
                     }
@@ -369,7 +370,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                                 .removeLbMonitor(clusterId);
 
                     } else {
-                        monitor = AutoscalerContext.getInstance()
+                        monitor = (AbstractClusterMonitor) 
AutoscalerContext.getInstance()
                                 .removeMonitor(clusterId);
                     }
 
@@ -402,7 +403,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
             @Override
             protected void onEvent(Event event) {
 
-                MemberTerminatedEvent memberTerminatedEvent =  null;
+                MemberTerminatedEvent memberTerminatedEvent = null;
                 try {
                     //TopologyManager.acquireReadLock();
 
@@ -416,7 +417,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                             memberTerminatedEvent.getClusterId());
 
                     if 
(AutoscalerContext.getInstance().monitorExist(clusterId)) {
-                        monitor = 
AutoscalerContext.getInstance().getMonitor(clusterId);
+                        monitor = (AbstractClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
                     } else {
                         //This is LB member
                         monitor = 
AutoscalerContext.getInstance().getLBMonitor(clusterId);
@@ -426,10 +427,11 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                             getNetworkPartitionCtxt(networkPartitionId);
 
                     PartitionContext partitionContext = 
networkPartitionContext.
-                                                                    
getPartitionCtxt(partitionId);
+                            getPartitionCtxt(partitionId);
                     String memberId = memberTerminatedEvent.getMemberId();
                     partitionContext.removeMemberStatsContext(memberId);
 
+
                     if 
(partitionContext.removeTerminationPendingMember(memberId)) {
                         if (log.isDebugEnabled()) {
                             log.debug(String.format("Member is removed from 
termination pending " +
@@ -452,6 +454,9 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                         log.info(String.format("Member stat context has been 
removed " +
                                 "               successfully: [member] %s", 
memberId));
                     }
+                    //Checking whether the cluster state can be changed either 
from in_active to created/terminating to terminated
+                    StatusChecker.getInstance().onMemberTermination(clusterId);
+
 //                partitionContext.decrementCurrentActiveMemberCount(1);
 
 
@@ -486,7 +491,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                     AbstractClusterMonitor monitor;
 
                     if 
(AutoscalerContext.getInstance().monitorExist(clusterId)) {
-                        monitor = 
AutoscalerContext.getInstance().getMonitor(clusterId);
+                        monitor = (AbstractClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
                     } else {
                         //This is LB member
                         monitor = 
AutoscalerContext.getInstance().getLBMonitor(clusterId);
@@ -542,7 +547,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                     AbstractClusterMonitor monitor;
 
                     if 
(AutoscalerContext.getInstance().monitorExist(clusterId)) {
-                        monitor = 
AutoscalerContext.getInstance().getMonitor(clusterId);
+                        monitor = (AbstractClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
                         partitionContext = 
monitor.getNetworkPartitionCtxt(networkPartitionId).
                                 getPartitionCtxt(partitionId);
                     } else {
@@ -584,23 +589,23 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
             th = new Thread(
                     new ApplicationMonitorAdder(applicationId));
         }
-      //  if (th != null) {
-            th.start();
+        //  if (th != null) {
+        th.start();
         //    try {
         //        th.join();
         //    } catch (InterruptedException ignore) {
         //    }
 
-            if (log.isDebugEnabled()) {
-                log.debug(String
-                        .format("Application monitor thread has been started 
successfully: " +
-                                        "[application] %s ", applicationId));
-            }
-      //  }
+        if (log.isDebugEnabled()) {
+            log.debug(String
+                    .format("Application monitor thread has been started 
successfully: " +
+                            "[application] %s ", applicationId));
+        }
+        //  }
     }
 
     private class ApplicationMonitorAdder implements Runnable {
-        private String  appId;
+        private String appId;
 
         public ApplicationMonitorAdder(String appId) {
             this.appId = appId;
@@ -621,7 +626,7 @@ public class AutoscalerTopologyEventReceiver implements 
Runnable {
                         log.debug("application monitor is going to be started 
for [application] " +
                                 appId);
                     }
-                    applicationMonitor = 
AutoscalerUtil.getApplicationMonitor(appId);
+                    applicationMonitor = 
ApplicationMonitorFactory.getApplicationMonitor(appId);
 
                     long end = System.currentTimeMillis();
                     log.info("Time taken to start app monitor: " + (end - 
start) / 1000);

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/AbstractClusterMonitor.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/AbstractClusterMonitor.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/AbstractClusterMonitor.java
index 9aa75b4..e470d7f 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/AbstractClusterMonitor.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/AbstractClusterMonitor.java
@@ -23,7 +23,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.autoscaler.NetworkPartitionContext;
 import org.apache.stratos.autoscaler.deployment.policy.DeploymentPolicy;
-import org.apache.stratos.autoscaler.monitor.events.ClusterStatusEvent;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
 import org.apache.stratos.autoscaler.policy.model.AutoscalePolicy;
 import org.apache.stratos.autoscaler.rule.AutoscalerRuleEvaluator;
@@ -44,7 +43,7 @@ import java.util.Map;
  * and perform minimum instance check and scaling check using the underlying
  * rules engine.
  */
-abstract public class AbstractClusterMonitor implements EventHandler, Runnable 
{
+abstract public class AbstractClusterMonitor extends Monitor implements 
Runnable {
 
     private static final Log log = 
LogFactory.getLog(AbstractClusterMonitor.class);
     // Map<NetworkpartitionId, Network Partition Context>
@@ -68,7 +67,7 @@ abstract public class AbstractClusterMonitor implements 
EventHandler, Runnable {
 
     protected Status status;
 
-    protected Monitor parent;
+    protected ParentComponentMonitor parent;
 
     protected AutoscalerRuleEvaluator autoscalerRuleEvaluator;
 
@@ -249,11 +248,11 @@ abstract public class AbstractClusterMonitor implements 
EventHandler, Runnable {
 
     }
 
-    public Monitor getParent() {
+    public ParentComponentMonitor getParent() {
         return parent;
     }
 
-    public void setParent(Monitor parent) {
+    public void setParent(ParentComponentMonitor parent) {
         this.parent = parent;
         this.appId = parent.getAppId();
     }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ApplicationMonitorFactory.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ApplicationMonitorFactory.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ApplicationMonitorFactory.java
index d75780e..7cdf0cf 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ApplicationMonitorFactory.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ApplicationMonitorFactory.java
@@ -20,11 +20,254 @@ package org.apache.stratos.autoscaler.monitor;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.autoscaler.MemberStatsContext;
+import org.apache.stratos.autoscaler.NetworkPartitionContext;
+import org.apache.stratos.autoscaler.PartitionContext;
+import 
org.apache.stratos.autoscaler.client.cloud.controller.CloudControllerClient;
+import org.apache.stratos.autoscaler.deployment.policy.DeploymentPolicy;
+import org.apache.stratos.autoscaler.exception.DependencyBuilderException;
+import org.apache.stratos.autoscaler.exception.PartitionValidationException;
+import org.apache.stratos.autoscaler.exception.PolicyValidationException;
+import org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
+import 
org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationContext;
+import 
org.apache.stratos.autoscaler.grouping.dependency.context.ClusterContext;
+import org.apache.stratos.autoscaler.grouping.dependency.context.GroupContext;
+import org.apache.stratos.autoscaler.monitor.application.ApplicationMonitor;
+import org.apache.stratos.autoscaler.monitor.cluster.ClusterMonitor;
+import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
+import org.apache.stratos.autoscaler.partition.PartitionGroup;
+import org.apache.stratos.autoscaler.policy.PolicyManager;
+import org.apache.stratos.autoscaler.policy.model.AutoscalePolicy;
+import org.apache.stratos.cloud.controller.stub.deployment.partition.Partition;
+import org.apache.stratos.cloud.controller.stub.pojo.MemberContext;
+import org.apache.stratos.cloud.controller.stub.pojo.Properties;
+import org.apache.stratos.cloud.controller.stub.pojo.Property;
+import org.apache.stratos.messaging.domain.topology.*;
+import org.apache.stratos.messaging.message.receiver.topology.TopologyManager;
+import org.apache.stratos.messaging.util.Constants;
+
+import java.util.Map;
 
 /**
- * Created by reka on 10/2/14.
+ * Factory class to get the Monitors.
  */
 public class ApplicationMonitorFactory {
     private static final Log log = 
LogFactory.getLog(ApplicationMonitorFactory.class);
 
+    public static Monitor getMonitor(ApplicationContext context, String appId)
+            throws TopologyInConsistentException,
+            DependencyBuilderException, PolicyValidationException, 
PartitionValidationException {
+        Monitor monitor;
+
+        if (context instanceof GroupContext) {
+            monitor = getGroupMonitor(context.getId(), appId);
+        } else if (context instanceof ClusterContext) {
+            monitor = getClusterMonitor((ClusterContext) context, appId);
+        } else {
+            monitor = getApplicationMonitor(appId);
+        }
+        return monitor;
+    }
+
+    public static Monitor getGroupMonitor(String groupId, String appId) throws 
DependencyBuilderException,
+            TopologyInConsistentException {
+        GroupMonitor groupMonitor;
+        TopologyManager.acquireReadLockForApplication(appId);
+
+        try {
+            Group group = 
TopologyManager.getTopology().getApplication(appId).getGroupRecursively(groupId);
+            groupMonitor = new GroupMonitor(group, appId);
+            groupMonitor.setAppId(appId);
+            if (group.getStatus() != groupMonitor.getStatus()) {
+                //updating the status, so that it will notify the parent
+                groupMonitor.setStatus(group.getStatus());
+            }
+        } finally {
+            TopologyManager.releaseReadLockForApplication(appId);
+
+        }
+        return groupMonitor;
+
+    }
+
+    public static ApplicationMonitor getApplicationMonitor(String appId)
+            throws DependencyBuilderException,
+            TopologyInConsistentException {
+        ApplicationMonitor applicationMonitor;
+        TopologyManager.acquireReadLockForApplication(appId);
+        try {
+            Application application = 
TopologyManager.getTopology().getApplication(appId);
+            if (application != null) {
+                applicationMonitor = new ApplicationMonitor(application);
+            } else {
+                String msg = "[Application] " + appId + " cannot be found in 
the Topology";
+                throw new TopologyInConsistentException(msg);
+            }
+        } finally {
+            TopologyManager.releaseReadLockForApplication(appId);
+        }
+
+        return applicationMonitor;
+
+    }
+
+    /**
+     * Updates ClusterContext for given cluster
+     *
+     * @param context
+     * @return ClusterMonitor - Updated ClusterContext
+     * @throws 
org.apache.stratos.autoscaler.exception.PolicyValidationException
+     * @throws 
org.apache.stratos.autoscaler.exception.PartitionValidationException
+     */
+    public static ClusterMonitor getClusterMonitor(ClusterContext context, 
String appId)
+            throws PolicyValidationException,
+            PartitionValidationException,
+            TopologyInConsistentException {
+        //Retrieving the Cluster from Topology
+        String clusterId = context.getId();
+        String serviceName = context.getServiceName();
+
+        Cluster cluster;
+        //acquire read lock for the service and cluster
+        TopologyManager.acquireReadLockForCluster(serviceName, clusterId);
+        try {
+            Topology topology = TopologyManager.getTopology();
+            if (topology.serviceExists(serviceName)) {
+                Service service = topology.getService(serviceName);
+                if (service.clusterExists(clusterId)) {
+                    cluster = service.getCluster(clusterId);
+                    if (log.isDebugEnabled()) {
+                        log.debug("Dependency check starting the [cluster]" + 
clusterId);
+                    }
+                    // startClusterMonitor(this, cluster);
+                    //context.setCurrentStatus(Status.Created);
+                } else {
+                    String msg = "[Cluster] " + clusterId + " cannot be found 
in the " +
+                            "Topology for [service] " + serviceName;
+                    throw new TopologyInConsistentException(msg);
+                }
+            } else {
+                String msg = "[Service] " + serviceName + " cannot be found in 
the Topology";
+                throw new TopologyInConsistentException(msg);
+
+            }
+        } finally {
+            //release read lock for the service and cluster
+            TopologyManager.releaseReadLockForCluster(serviceName, clusterId);
+        }
+
+        String autoscalePolicyName = cluster.getAutoscalePolicyName();
+        String deploymentPolicyName = cluster.getDeploymentPolicyName();
+
+        if (log.isDebugEnabled()) {
+            log.debug("Deployment policy name: " + deploymentPolicyName);
+            log.debug("Autoscaler policy name: " + autoscalePolicyName);
+        }
+
+        AutoscalePolicy policy =
+                PolicyManager.getInstance()
+                        .getAutoscalePolicy(autoscalePolicyName);
+        DeploymentPolicy deploymentPolicy =
+                PolicyManager.getInstance()
+                        .getDeploymentPolicy(deploymentPolicyName);
+
+        if (deploymentPolicy == null) {
+            String msg = "Deployment Policy is null. Policy name: " + 
deploymentPolicyName;
+            log.error(msg);
+            throw new PolicyValidationException(msg);
+        }
+
+        Partition[] allPartitions = deploymentPolicy.getAllPartitions();
+        if (allPartitions == null) {
+            String msg =
+                    "Deployment Policy's Partitions are null. Policy name: " +
+                            deploymentPolicyName;
+            log.error(msg);
+            throw new PolicyValidationException(msg);
+        }
+
+        
CloudControllerClient.getInstance().validateDeploymentPolicy(cluster.getServiceName(),
 deploymentPolicy);
+
+        ClusterMonitor clusterMonitor =
+                new ClusterMonitor(cluster.getClusterId(),
+                        cluster.getServiceName(),
+                        deploymentPolicy, policy);
+        clusterMonitor.setAppId(cluster.getAppId());
+
+        for (PartitionGroup partitionGroup : 
deploymentPolicy.getPartitionGroups()) {
+
+            NetworkPartitionContext networkPartitionContext = new 
NetworkPartitionContext(partitionGroup.getId(),
+                    partitionGroup.getPartitionAlgo(), 
partitionGroup.getPartitions());
+
+            for (Partition partition : partitionGroup.getPartitions()) {
+                PartitionContext partitionContext = new 
PartitionContext(partition);
+                partitionContext.setServiceName(cluster.getServiceName());
+                partitionContext.setProperties(cluster.getProperties());
+                partitionContext.setNetworkPartitionId(partitionGroup.getId());
+
+                for (Member member : cluster.getMembers()) {
+                    String memberId = member.getMemberId();
+                    if 
(member.getPartitionId().equalsIgnoreCase(partition.getId())) {
+                        MemberContext memberContext = new MemberContext();
+                        memberContext.setClusterId(member.getClusterId());
+                        memberContext.setMemberId(memberId);
+                        memberContext.setPartition(partition);
+                        
memberContext.setProperties(convertMemberPropsToMemberContextProps(member.getProperties()));
+
+                        if (MemberStatus.Activated.equals(member.getStatus())) 
{
+                            partitionContext.addActiveMember(memberContext);
+//                            
networkPartitionContext.increaseMemberCountOfPartition(partition.getNetworkPartitionId(),
 1);
+//                            
partitionContext.incrementCurrentActiveMemberCount(1);
+
+                        } else if 
(MemberStatus.Created.equals(member.getStatus()) || 
MemberStatus.Starting.equals(member.getStatus())) {
+                            partitionContext.addPendingMember(memberContext);
+
+//                            
networkPartitionContext.increaseMemberCountOfPartition(partition.getNetworkPartitionId(),
 1);
+                        } else if 
(MemberStatus.Suspended.equals(member.getStatus())) {
+//                            partitionContext.addFaultyMember(memberId);
+                        }
+                        partitionContext.addMemberStatsContext(new 
MemberStatsContext(memberId));
+                        if (log.isInfoEnabled()) {
+                            log.info(String.format("Member stat context has 
been added: [member] %s", memberId));
+                        }
+                    }
+
+                }
+                networkPartitionContext.addPartitionContext(partitionContext);
+                if (log.isInfoEnabled()) {
+                    log.info(String.format("Partition context has been added: 
[partition] %s",
+                            partitionContext.getPartitionId()));
+                }
+            }
+
+            clusterMonitor.addNetworkPartitionCtxt(networkPartitionContext);
+            //clusterMonitor.setCurrentStatus(Status.Created);
+            if (log.isInfoEnabled()) {
+                log.info(String.format("Network partition context has been 
added: [network partition] %s",
+                        networkPartitionContext.getId()));
+            }
+        }
+
+        // set hasPrimary property
+        // hasPrimary is true if there are primary members available in that 
cluster
+        if (cluster.getProperties() != null) {
+            
clusterMonitor.setHasPrimary(Boolean.parseBoolean(cluster.getProperties().getProperty(Constants.IS_PRIMARY)));
+        }
+
+        log.info("Cluster monitor created: " + clusterMonitor.toString());
+        return clusterMonitor;
+    }
+
+
+    private static Properties convertMemberPropsToMemberContextProps(
+            java.util.Properties properties) {
+        Properties props = new Properties();
+        for (Map.Entry<Object, Object> e : properties.entrySet()) {
+            Property prop = new Property();
+            prop.setName((String) e.getKey());
+            prop.setValue((String) e.getValue());
+            props.addProperties(prop);
+        }
+        return props;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
index 4f501d2..bb09811 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/Monitor.java
@@ -18,257 +18,32 @@
  */
 package org.apache.stratos.autoscaler.monitor;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.stratos.autoscaler.AutoscalerContext;
-import org.apache.stratos.autoscaler.exception.DependencyBuilderException;
-import org.apache.stratos.autoscaler.exception.PartitionValidationException;
-import org.apache.stratos.autoscaler.exception.PolicyValidationException;
-import org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
-import org.apache.stratos.autoscaler.grouping.dependency.DependencyBuilder;
-import org.apache.stratos.autoscaler.grouping.dependency.DependencyTree;
-import 
org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationContext;
-import 
org.apache.stratos.autoscaler.grouping.dependency.context.ClusterContext;
-import org.apache.stratos.autoscaler.grouping.dependency.context.GroupContext;
-import org.apache.stratos.autoscaler.monitor.cluster.ClusterMonitor;
-import org.apache.stratos.autoscaler.monitor.cluster.LbClusterMonitor;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
-import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
-import org.apache.stratos.autoscaler.util.AutoscalerUtil;
-import org.apache.stratos.messaging.domain.topology.*;
-import org.apache.stratos.messaging.message.receiver.topology.TopologyManager;
+import org.apache.stratos.messaging.domain.topology.ParentComponent;
 
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 /**
- * Monitor is to monitor it's child monitors and
- * control them according to the dependencies respectively.
+ * Abstract class for the monitoring functionality in autoscaler.
  */
 public abstract class Monitor implements EventHandler {
-    private static final Log log = LogFactory.getLog(Monitor.class);
-
-    //id of the monitor, it can be alias or the id
     protected String id;
-    //GroupMonitor map, key=GroupAlias and value=GroupMonitor
-    protected Map<String, GroupMonitor> aliasToGroupMonitorsMap;
-    //AbstractMonitor map, key=clusterId and value=AbstractMonitors
-    protected Map<String, AbstractClusterMonitor> 
clusterIdToClusterMonitorsMap;
-    //The monitors dependency tree with all the startable/killable dependencies
-    protected DependencyTree dependencyTree;
-    //status of the monitor whether it is running/in_maintainable/terminated
-    protected Status status;
-    //Application id of this particular monitor
-    protected String appId;
-
-    public Monitor(ParentComponent component) throws 
DependencyBuilderException {
-        aliasToGroupMonitorsMap = new HashMap<String, GroupMonitor>();
-        clusterIdToClusterMonitorsMap = new HashMap<String, 
AbstractClusterMonitor>();
-        this.id = component.getUniqueIdentifier();
-        this.status = component.getStatus();
-        //Building the dependency for this monitor within the immediate 
children
-        dependencyTree = 
DependencyBuilder.getInstance().buildDependency(component);
-    }
-
-    /**
-     * Will monitor the immediate children upon any event triggers from 
parent/children
-     *
-     * @param statusEvent will be sent by parent/children with the current 
status
-     */
-    protected abstract void monitor(MonitorStatusEvent statusEvent);
-
-
-    /**
-     * This will start the parallel dependencies at once from the top level.
-     * it will get invoked when the monitor starts up only.
-     * //TODO restarting the whole group
-     */
-    public void startDependency() throws TopologyInConsistentException {
-        //start the first dependency
-        List<ApplicationContext> applicationContexts = 
this.dependencyTree.getStarAbleDependencies();
-        startDependency(applicationContexts);
-
-    }
-
-    /**
-     * This will get invoked based on the activation event of its one of the 
child
-     *
-     * @param id alias/clusterId of which receive the activated event
-     */
-    public boolean startDependency(String id) throws 
TopologyInConsistentException {
-        List<ApplicationContext> applicationContexts = 
this.dependencyTree.getStarAbleDependencies(id);
-        return startDependency(applicationContexts);
-    }
-
-    /**
-     * To start the dependency of the given application contexts
-     *
-     * @param applicationContexts the found applicationContexts to be started
-     */
-    private boolean startDependency(List<ApplicationContext> 
applicationContexts)
-            throws TopologyInConsistentException {
-        if (applicationContexts != null && applicationContexts.isEmpty()) {
-            //all the groups/clusters have been started and waiting for 
activation
-            log.info("There is no child found for the [group]: " + this.id);
-            return false;
-
-        }
-        for (ApplicationContext context : applicationContexts) {
-            if (log.isDebugEnabled()) {
-                log.debug("Dependency check for the Group " + context.getId() 
+ " started");
-            }
-            if (context instanceof GroupContext) {
-                startGroupMonitor(this, context.getId());
-            } else if (context instanceof ClusterContext) {
-                String clusterId = context.getId();
-                String serviceName = null;
-                Group group = getGroupFromTopology(this.id);
-
-                for(ClusterDataHolder dataHolder : 
group.getClusterDataMap().values()) {
-                    if(dataHolder.getClusterId().equals(clusterId)) {
-                        serviceName = dataHolder.getServiceType();
-                    }
-                }
-
-                Cluster cluster;
-                //acquire read lock for the service and cluster
-                TopologyManager.acquireReadLockForCluster(serviceName, 
clusterId);
-                try {
-                    Topology topology = TopologyManager.getTopology();
-                    if (topology.serviceExists(serviceName)) {
-                        Service service = topology.getService(serviceName);
-                        if (service.clusterExists(clusterId)) {
-                            cluster = service.getCluster(clusterId);
-                            if (log.isDebugEnabled()) {
-                                log.debug("Dependency check starting the 
[cluster]" + clusterId);
-                            }
-                            startClusterMonitor(this, cluster);
-                        } else {
-                            String msg = "[Cluster] " + clusterId + " cannot 
be found in the " +
-                                    "Topology for [service] " + serviceName;
-                            throw new TopologyInConsistentException(msg);
-                        }
-                    } else {
-                        String msg = "[Service] " + serviceName + " cannot be 
found in the Topology";
-                        throw new TopologyInConsistentException(msg);
-
-                    }
-                } finally {
-                    //release read lock for the service and cluster
-                    TopologyManager.releaseReadLockForCluster(serviceName, 
clusterId);
-                }
-            }
-        }
-        return true;
 
-    }
-
-    private Group getGroupFromTopology(String groupId) throws 
TopologyInConsistentException {
-        Application application = 
TopologyManager.getTopology().getApplication(this.appId);
-        if(application != null) {
-            Group group = application.getGroupRecursively(groupId);
-            if(group != null) {
-                return group;
-            } else {
-                String msg = "[Group] " + groupId + " cannot be found in the 
Topology";
-                throw new TopologyInConsistentException(msg);
-            }
-        } else {
-            String msg = "[Application] " + this.appId + " cannot be found in 
the Topology";
-            throw new TopologyInConsistentException(msg);
-        }
-    }
-
-    protected synchronized void startClusterMonitor(Monitor parent, Cluster 
cluster) {
-        Thread th = null;
-        if (cluster.isLbCluster()
-                && 
!this.clusterIdToClusterMonitorsMap.containsKey(cluster.getClusterId())) {
-            th = new Thread(new LBClusterMonitorAdder(
-                    cluster));
-        } else if (!cluster.isLbCluster() && 
!this.clusterIdToClusterMonitorsMap.containsKey(cluster.getClusterId())) {
-            th = new Thread(
-                    new ClusterMonitorAdder(parent, cluster));
-            if (log.isDebugEnabled()) {
-                log.debug(String
-                        .format("Cluster monitor Adder has been added: 
[cluster] %s ",
-                                cluster.getClusterId()));
-            }
-        }
-        if (th != null) {
-            th.start();
-            /*try {
-                th.join();
-            } catch (InterruptedException ignore) {
-            }*/
-
-            log.info(String
-                    .format("Cluster monitor thread has been started 
successfully: [cluster] %s ",
-                            cluster.getClusterId()));
-        }
-    }
-
-    protected synchronized void startGroupMonitor(Monitor parent, String 
groupId) {
-        Thread th = null;
-        //String groupId = group.getUniqueIdentifier();
-        if (!this.aliasToGroupMonitorsMap.containsKey(groupId)) {
-            if (log.isDebugEnabled()) {
-                log.debug(String
-                        .format("Group monitor Adder has been added: [group] 
%s ",
-                                groupId));
-            }
-            th = new Thread(
-                    new GroupMonitorAdder(parent, groupId, this.appId));
-        }
-
-        if (th != null) {
-            th.start();
-            /*try {
-                th.join();
-            } catch (InterruptedException ignore) {
-            }*/
-
-            log.info(String
-                    .format("Group monitor thread has been started 
successfully: [group] %s ",
-                            groupId));
-        }
-    }
-
-    public Status getStatus() {
-        return status;
-    }
+    protected String appId;
 
-    public Map<String, GroupMonitor> getAliasToGroupMonitorsMap() {
-        return aliasToGroupMonitorsMap;
-    }
+    protected ParentComponentMonitor parent;
 
-    public void setAliasToGroupMonitorsMap(Map<String, GroupMonitor> 
aliasToGroupMonitorsMap) {
-        this.aliasToGroupMonitorsMap = aliasToGroupMonitorsMap;
-    }
+    //GroupMonitor map, key=GroupAlias and value=GroupMonitor
+    protected Map<String, Monitor> aliasToMonitorsMap;
 
     public String getId() {
-        return this.id;
+        return id;
     }
 
     public void setId(String id) {
         this.id = id;
     }
 
-    public Map<String, AbstractClusterMonitor> 
getClusterIdToClusterMonitorsMap() {
-        return clusterIdToClusterMonitorsMap;
-    }
-
-    public void setClusterIdToClusterMonitorsMap(Map<String, 
AbstractClusterMonitor> clusterIdToClusterMonitorsMap) {
-        this.clusterIdToClusterMonitorsMap = clusterIdToClusterMonitorsMap;
-    }
-
-    public void addAbstractMonitor(AbstractClusterMonitor monitor) {
-        this.clusterIdToClusterMonitorsMap.put(monitor.getClusterId(), 
monitor);
-    }
-
-    public AbstractClusterMonitor getAbstractMonitor(String clusterId) {
-        return this.clusterIdToClusterMonitorsMap.get(clusterId);
-    }
 
     public String getAppId() {
         return appId;
@@ -278,188 +53,19 @@ public abstract class Monitor implements EventHandler {
         this.appId = appId;
     }
 
-    private class ClusterMonitorAdder implements Runnable {
-        private Cluster cluster;
-        private Monitor parent;
-
-        public ClusterMonitorAdder(Monitor parent, Cluster cluster) {
-            this.parent = parent;
-            this.cluster = cluster;
-        }
-
-        public void run() {
-            ClusterMonitor monitor = null;
-            int retries = 5;
-            boolean success = false;
-            do {
-                try {
-                    Thread.sleep(5000);
-                } catch (InterruptedException e1) {
-                }
-                try {
-                    if (log.isDebugEnabled()) {
-                        log.debug("CLuster monitor is going to be started for 
[cluster] "
-                                + cluster.getClusterId());
-                    }
-                    monitor = AutoscalerUtil.getClusterMonitor(cluster);
-                    monitor.setParent(parent);
-                    //setting the status of cluster monitor w.r.t Topology 
cluster
-                    //if(cluster.getStatus() != Status.Created &&
-                    if(cluster.getStatus() != monitor.getStatus()) {
-                        //updating the status, so that it will notify the 
parent
-                        monitor.setStatus(cluster.getStatus());
-                    }
-                    //monitor.addObserver(parent);
-                    success = true;
-                    //TODO start the status checker
-                } catch (PolicyValidationException e) {
-                    String msg = "Cluster monitor creation failed for cluster: 
" + cluster.getClusterId();
-                    log.warn(msg, e);
-                    retries--;
-
-
-                } catch (PartitionValidationException e) {
-                    String msg = "Cluster monitor creation failed for cluster: 
" + cluster.getClusterId();
-                    log.warn(msg, e);
-                    retries--;
-
-                }
-
-            } while (!success && retries != 0);
-
-
-            if (monitor == null) {
-                String msg = "Cluster monitor creation failed, even after 
retrying for 5 times, "
-                        + "for cluster: " + cluster.getClusterId();
-                log.error(msg);
-                //TODO parent.notify();
-                throw new RuntimeException(msg);
-            }
-
-            Thread th = new Thread(monitor);
-            th.start();
-
-            AutoscalerContext.getInstance().addMonitor(monitor);
-            clusterIdToClusterMonitorsMap.put(cluster.getClusterId(), monitor);
-            if (log.isInfoEnabled()) {
-                log.info(String.format("Cluster monitor has been added 
successfully: [cluster] %s",
-                        cluster.getClusterId()));
-            }
-        }
+    public Map<String, Monitor> getAliasToMonitorsMap() {
+        return aliasToMonitorsMap;
     }
 
-    private class GroupMonitorAdder implements Runnable {
-        private Monitor parent;
-        private String groupId;
-        private String appId;
-
-        public GroupMonitorAdder(Monitor parent, String groupId, String appId) 
{
-            this.parent = parent;
-            this.groupId = groupId;
-            this.appId = appId;
-        }
-
-        public void run() {
-            GroupMonitor monitor = null;
-            int retries = 5;
-            boolean success = false;
-            do {
-                try {
-                    Thread.sleep(5000);
-                } catch (InterruptedException e1) {
-                }
-
-                try {
-                    if (log.isDebugEnabled()) {
-                        log.debug("Group monitor is going to be started for 
[group] "
-                                + groupId);
-                    }
-                    monitor = AutoscalerUtil.getGroupMonitor(groupId, appId);
-                    monitor.setParent(parent);
-                    //setting the status of cluster monitor w.r.t Topology 
cluster
-                    //if(group.getStatus() != Status.Created &&
-
-                    //monitor.addObserver(parent);
-                    success = true;
-                } catch (DependencyBuilderException e) {
-                    String msg = "Group monitor creation failed for group: " + 
groupId;
-                    log.warn(msg, e);
-                    retries--;
-                } catch (TopologyInConsistentException e) {
-                    String msg = "Group monitor creation failed for group: " + 
groupId;
-                    log.warn(msg, e);
-                    retries--;
-                }
-            } while (!success && retries != 0);
-
-            if (monitor == null) {
-                String msg = "Group monitor creation failed, even after 
retrying for 5 times, "
-                        + "for group: " + groupId;
-                log.error(msg);
-                //TODO parent.notify(); as it got to failed
-
-                throw new RuntimeException(msg);
-            }
-
-            aliasToGroupMonitorsMap.put(groupId, monitor);
-            //parent.addObserver(monitor);
-
-            if (log.isInfoEnabled()) {
-                log.info(String.format("Group monitor has been added 
successfully: [group] %s",
-                        groupId));
-            }
-        }
+    public void setAliasToMonitorsMap(Map<String, Monitor> aliasToMonitorsMap) 
{
+        this.aliasToMonitorsMap = aliasToMonitorsMap;
     }
 
-    private class LBClusterMonitorAdder implements Runnable {
-        private Cluster cluster;
-
-        public LBClusterMonitorAdder(Cluster cluster) {
-            this.cluster = cluster;
-        }
-
-        public void run() {
-            LbClusterMonitor monitor = null;
-            int retries = 5;
-            boolean success = false;
-            do {
-                try {
-                    Thread.sleep(5000);
-                } catch (InterruptedException e1) {
-                }
-                try {
-                    monitor = AutoscalerUtil.getLBClusterMonitor(cluster);
-                    success = true;
-
-                } catch (PolicyValidationException e) {
-                    String msg = "LB Cluster monitor creation failed for 
cluster: " + cluster.getClusterId();
-                    log.warn(msg, e);
-                    retries--;
-
-                } catch (PartitionValidationException e) {
-                    String msg = "LB Cluster monitor creation failed for 
cluster: " + cluster.getClusterId();
-                    log.warn(msg, e);
-                    retries--;
-                }
-            } while (!success && retries <= 0);
-
-            if (monitor == null) {
-                String msg = "LB Cluster monitor creation failed, even after 
retrying for 5 times, "
-                        + "for cluster: " + cluster.getClusterId();
-                log.error(msg);
-                throw new RuntimeException(msg);
-            }
-
-            Thread th = new Thread(monitor);
-            th.start();
-            AutoscalerContext.getInstance().addLbMonitor(monitor);
-            clusterIdToClusterMonitorsMap.put(cluster.getClusterId(), monitor);
-            if (log.isInfoEnabled()) {
-                log.info(String.format("LB Cluster monitor has been added 
successfully: [cluster] %s",
-                        cluster.getClusterId()));
-            }
-        }
+    public ParentComponentMonitor getParent() {
+        return parent;
     }
 
-
+    public void setParent(ParentComponentMonitor parent) {
+        this.parent = parent;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
index a3f9e8f..d8bc6d9 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/MonitorStatusEventBuilder.java
@@ -32,22 +32,22 @@ import org.apache.stratos.messaging.domain.topology.Status;
 public class MonitorStatusEventBuilder {
     private static final Log log = 
LogFactory.getLog(MonitorStatusEventBuilder.class);
 
-    public static void handleClusterStatusEvent(Monitor parent, Status status, 
String clusterId) {
+    public static void handleClusterStatusEvent(ParentComponentMonitor parent, 
Status status, String clusterId) {
         ClusterStatusEvent clusterStatusEvent = new ClusterStatusEvent(status, 
clusterId);
         notifyParent(parent, clusterStatusEvent);
     }
 
-    public static void handleGroupStatusEvent(Monitor parent, Status status, 
String groupId) {
+    public static void handleGroupStatusEvent(ParentComponentMonitor parent, 
Status status, String groupId) {
         GroupStatusEvent groupStatusEvent = new GroupStatusEvent(status, 
groupId);
         notifyParent(parent, groupStatusEvent);
     }
 
-    public static void handleApplicationStatusEvent(Monitor parent, Status 
status, String appId) {
+    public static void handleApplicationStatusEvent(ParentComponentMonitor 
parent, Status status, String appId) {
         ApplicationStatusEvent applicationStatusEvent = new 
ApplicationStatusEvent(status, appId);
         notifyParent(parent, applicationStatusEvent);
     }
 
-    private static void notifyParent(Monitor parent, MonitorStatusEvent 
statusEvent) {
+    private static void notifyParent(ParentComponentMonitor parent, 
MonitorStatusEvent statusEvent) {
        parent.onEvent(statusEvent);
     }
 

Reply via email to