Repository: stratos
Updated Branches:
  refs/heads/4.0.0-grouping 35ef22793 -> fa5a74120


http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
new file mode 100644
index 0000000..1168883
--- /dev/null
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/ParentComponentMonitor.java
@@ -0,0 +1,499 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+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.monitor.events.MonitorStatusEvent;
+import org.apache.stratos.messaging.domain.topology.ParentComponent;
+import org.apache.stratos.messaging.domain.topology.Status;
+
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Monitor is to monitor it's child monitors and
+ * control them according to the dependencies respectively.
+ */
+public abstract class ParentComponentMonitor extends Monitor {
+    private static final Log log = 
LogFactory.getLog(ParentComponentMonitor.class);
+
+    //id of the monitor, it can be alias or the id
+    protected String id;
+
+    //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 ParentComponentMonitor(ParentComponent component) throws 
DependencyBuilderException {
+        aliasToMonitorsMap = new HashMap<String, Monitor>();
+        //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");
+            }
+            startMonitor(this, context);
+            //context.setCurrentStatus(Status.Created);
+        }
+
+        return true;
+
+    }
+
+    public Status getStatus() {
+        return status;
+    }
+
+    public String getId() {
+        return this.id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getAppId() {
+        return appId;
+    }
+
+    public void setAppId(String appId) {
+        this.appId = appId;
+    }
+
+    protected synchronized void startMonitor(ParentComponentMonitor parent, 
ApplicationContext context) {
+        Thread th = null;
+        if (!this.aliasToMonitorsMap.containsKey(context.getId())) {
+            th = new Thread(
+                    new MonitorAdder(parent, context, this.appId));
+            if (log.isDebugEnabled()) {
+                log.debug(String
+                        .format("Monitor Adder has been added: [cluster] %s ",
+                                context.getId()));
+            }
+        }
+        if (th != null) {
+            th.start();
+            log.info(String
+                    .format("Monitor thread has been started successfully: 
[cluster] %s ",
+                            context.getId()));
+        }
+    }
+
+    private class MonitorAdder implements Runnable {
+        private ApplicationContext context;
+        private ParentComponentMonitor parent;
+        private String appId;
+
+        public MonitorAdder(ParentComponentMonitor parent, ApplicationContext 
context, String appId) {
+            this.parent = parent;
+            this.context = context;
+            this.appId = appId;
+        }
+
+        public void run() {
+            Monitor monitor = null;
+            int retries = 5;
+            boolean success = false;
+            do {
+                try {
+                    Thread.sleep(5000);
+                } catch (InterruptedException e1) {
+                }
+
+                if (log.isDebugEnabled()) {
+                    log.debug("Monitor is going to be started for 
[group/cluster] "
+                            + context.getId());
+                }
+                try {
+                    monitor = ApplicationMonitorFactory.getMonitor(context, 
appId);
+                    monitor.setParent(parent);
+                    //TODO start the status checker
+
+                } catch (DependencyBuilderException e) {
+                    String msg = "Monitor creation failed for: " + 
context.getId();
+                    log.warn(msg, e);
+                    retries--;
+                } catch (TopologyInConsistentException e) {
+                    String msg = "Monitor creation failed for: " + 
context.getId();
+                    log.warn(msg, e);
+                    retries--;
+                } catch (PolicyValidationException e) {
+                    String msg = "Monitor creation failed for: " + 
context.getId();
+                    log.warn(msg, e);
+                    retries--;
+                } catch (PartitionValidationException e) {
+                    String msg = "Monitor creation failed for: " + 
context.getId();
+                    log.warn(msg, e);
+                    retries--;
+
+                }
+                success = true;
+
+            } while (!success && retries != 0);
+
+
+            if (monitor == null) {
+                String msg = "Monitor creation failed, even after retrying for 
5 times, "
+                        + "for : " + context.getId();
+                log.error(msg);
+                //TODO parent.notify();
+                throw new RuntimeException(msg);
+            }
+
+            AutoscalerContext.getInstance().addMonitor(monitor);
+            aliasToMonitorsMap.put(context.getId(), monitor);
+            if (log.isInfoEnabled()) {
+                log.info(String.format("Monitor has been added successfully 
for: %s",
+                        context.getId()));
+            }
+        }
+    }
+
+
+    /*protected synchronized void startGroupMonitor(ParentComponentMonitor 
parent, GroupContext groupContext) {
+        Thread th = null;
+        //String groupId = group.getUniqueIdentifier();
+        if (!this.aliasToMonitorsMap.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));
+        }
+    }
+*/
+
+    /*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(ParentComponentMonitor 
parent, ApplicationContext clusterContext) {
+        Thread th = null;
+        if (!this.aliasToMonitorsMap.containsKey(clusterContext.getId())) {
+            th = new Thread(
+                    new ClusterMonitorAdder(parent, clusterContext));
+            if (log.isDebugEnabled()) {
+                log.debug(String
+                        .format("Cluster monitor Adder has been added: 
[cluster] %s ",
+                                clusterContext.getClusterId()));
+            }
+        }
+        if (th != null) {
+            th.start();
+            log.info(String
+                    .format("Cluster monitor thread has been started 
successfully: [cluster] %s ",
+                            clusterContext.getClusterId()));
+        }
+    }*/
+
+
+    /*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);
+    }
+*/
+
+
+    /*private class ClusterMonitorAdder implements Runnable {
+        private Cluster cluster;
+        private ParentComponentMonitor parent;
+
+        public ClusterMonitorAdder(ParentComponentMonitor 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);
+            aliasToMonitorsMap.put(cluster.getClusterId(), monitor);
+            if (log.isInfoEnabled()) {
+                log.info(String.format("Cluster monitor has been added 
successfully: [cluster] %s",
+                        cluster.getClusterId()));
+            }
+        }
+    }
+*/
+
+
+    /*private class GroupMonitorAdder implements Runnable {
+        private ParentComponentMonitor parent;
+        private String groupId;
+        private String appId;
+
+        public GroupMonitorAdder(ParentComponentMonitor 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);
+                    //setting the parent monitor
+                    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);
+            }
+
+            aliasToMonitorsMap.put(groupId, monitor);
+            //parent.addObserver(monitor);
+
+            if (log.isInfoEnabled()) {
+                log.info(String.format("Group monitor has been added 
successfully: [group] %s",
+                        groupId));
+            }
+        }
+    }
+
+    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);
+            aliasToMonitorsMap.put(cluster.getClusterId(), monitor);
+            if (log.isInfoEnabled()) {
+                log.info(String.format("LB Cluster monitor has been added 
successfully: [cluster] %s",
+                        cluster.getClusterId()));
+            }
+        }
+    }*/
+
+
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
index 5b6598a..92bf6fa 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/application/ApplicationMonitor.java
@@ -25,11 +25,10 @@ import 
org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
 import 
org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationContext;
 import org.apache.stratos.autoscaler.monitor.AbstractClusterMonitor;
 import org.apache.stratos.autoscaler.monitor.Monitor;
+import org.apache.stratos.autoscaler.monitor.ParentComponentMonitor;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
-import org.apache.stratos.autoscaler.monitor.group.GroupMonitor;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 import org.apache.stratos.messaging.domain.topology.Application;
-import org.apache.stratos.messaging.domain.topology.ParentComponent;
 import org.apache.stratos.messaging.domain.topology.Status;
 
 import java.util.ArrayList;
@@ -39,7 +38,7 @@ import java.util.List;
 /**
  * ApplicationMonitor is to control the child monitors
  */
-public class ApplicationMonitor extends Monitor {
+public class ApplicationMonitor extends ParentComponentMonitor {
     private static final Log log = LogFactory.getLog(ApplicationMonitor.class);
 
     public ApplicationMonitor(Application application) throws 
DependencyBuilderException,
@@ -60,10 +59,10 @@ public class ApplicationMonitor extends Monitor {
     public List<String> findClustersOfApplication(String appId) {
         List<String> clusters = new ArrayList<String>();
         //considering only one level
-        for (AbstractClusterMonitor monitor : 
this.clusterIdToClusterMonitorsMap.values()) {
+        /*for (AbstractClusterMonitor monitor : 
this.clusterIdToClusterMonitorsMap.values()) {
             clusters.add(monitor.getClusterId());
-        }
-        //TODO rest
+        }*/
+        //TODO restart and read from Topology
         return clusters;
 
     }
@@ -75,8 +74,9 @@ public class ApplicationMonitor extends Monitor {
      * @return the found cluster monitor
      */
     public AbstractClusterMonitor findClusterMonitorWithId(String clusterId) {
-        return findClusterMonitor(clusterId, 
clusterIdToClusterMonitorsMap.values(),
-                aliasToGroupMonitorsMap.values());
+        /*return findClusterMonitor(clusterId, 
clusterIdToClusterMonitorsMap.values(),
+                aliasToMonitorsMap.values());*/
+        return null;
 
     }
 
@@ -88,9 +88,9 @@ public class ApplicationMonitor extends Monitor {
      * @param groupMonitors   group monitors found in the app monitor
      * @return the found cluster monitor
      */
-    private AbstractClusterMonitor findClusterMonitor(String clusterId,
+    /*private AbstractClusterMonitor findClusterMonitor(String clusterId,
                                                       
Collection<AbstractClusterMonitor> clusterMonitors,
-                                                      Collection<GroupMonitor> 
groupMonitors) {
+                                                      Collection<Monitor> 
groupMonitors) {
         for (AbstractClusterMonitor monitor : clusterMonitors) {
             // check if alias is equal, if so, return
             if (monitor.equals(clusterId)) {
@@ -98,7 +98,7 @@ public class ApplicationMonitor extends Monitor {
             }
         }
 
-        for (GroupMonitor groupMonitor : groupMonitors) {
+        for (Monitor groupMonitor : groupMonitors) {
             return findClusterMonitor(clusterId,
                     groupMonitor.getClusterIdToClusterMonitorsMap().values(),
                     groupMonitor.getAliasToGroupMonitorsMap().values());
@@ -106,15 +106,15 @@ public class ApplicationMonitor extends Monitor {
         return null;
 
     }
-
+*/
     /**
      * Find the group monitor by traversing recursively in the hierarchical 
monitors.
      *
      * @param groupId the unique alias of the Group
      * @return the found GroupMonitor
      */
-    public GroupMonitor findGroupMonitorWithId(String groupId) {
-        return findGroupMonitor(groupId, aliasToGroupMonitorsMap.values());
+    public Monitor findGroupMonitorWithId(String groupId) {
+        return findGroupMonitor(groupId, aliasToMonitorsMap.values());
 
     }
 
@@ -126,90 +126,21 @@ public class ApplicationMonitor extends Monitor {
      * @param monitors the group monitors found in the app monitor
      * @return the found GroupMonitor
      */
-    private GroupMonitor findGroupMonitor(String id, Collection<GroupMonitor> 
monitors) {
-        for (GroupMonitor monitor : monitors) {
+    private Monitor findGroupMonitor(String id, Collection<Monitor> monitors) {
+        for (Monitor monitor : monitors) {
             // check if alias is equal, if so, return
             if (monitor.getId().equals(id)) {
                 return monitor;
             } else {
                 // check if this Group has nested sub Groups. If so, traverse 
them as well
-                if (monitor.getAliasToGroupMonitorsMap() != null) {
-                    return findGroupMonitor(id, 
monitor.getAliasToGroupMonitorsMap().values());
+                if (monitor.getAliasToMonitorsMap() != null) {
+                    return findGroupMonitor(id, 
monitor.getAliasToMonitorsMap().values());
                 }
             }
         }
         return null;
     }
 
-
-    /**
-     * To find the parent monitor of a group's associate monitor
-     *
-     * @param groupId the id of the group
-     * @return the found parent monitor of the group
-     */
-    public Monitor findParentMonitorOfGroup(String groupId) {
-        return findParentMonitorForGroup(groupId, this);
-    }
-
-    /**
-     * Find the parent monitor of the given group in the app monitor
-     *
-     * @param groupId the id of the group
-     * @param monitor the app monitor
-     * @return the found parent monitor of the group
-     */
-    private Monitor findParentMonitorForGroup(String groupId, Monitor monitor) 
{
-        //if this monitor has the group, return it as the parent
-        if (monitor.getAliasToGroupMonitorsMap().containsKey(groupId)) {
-            return monitor;
-        } else {
-            if (monitor.getAliasToGroupMonitorsMap() != null) {
-                //check whether the children has the group and find its parent
-                for (GroupMonitor groupMonitor : 
monitor.getAliasToGroupMonitorsMap().values()) {
-                    return findParentMonitorForGroup(groupId, groupMonitor);
-
-                }
-            }
-        }
-        return null;
-
-    }
-
-    /**
-     * Find the parent monitor of the given cluster in the app monitor
-     *
-     * @param clusterId the id of the cluster
-     * @return the found parent monitor of the cluster
-     */
-    public Monitor findParentMonitorOfCluster(String clusterId) {
-        return findParentMonitorForCluster(clusterId, this);
-    }
-
-    /**
-     * Find the parent monitor of the given cluster in the app monitor
-     *
-     * @param clusterId the id of the cluster
-     * @param monitor   the app monitor
-     * @return the found parent monitor of the cluster
-     */
-    private Monitor findParentMonitorForCluster(String clusterId, Monitor 
monitor) {
-        //if this monitor has the group, return it as the parent
-        if (monitor.getClusterIdToClusterMonitorsMap().containsKey(clusterId)) 
{
-            return monitor;
-        } else {
-            if (monitor.getAliasToGroupMonitorsMap() != null) {
-                //check whether the children has the group and find its parent
-                for (GroupMonitor groupMonitor : 
monitor.getAliasToGroupMonitorsMap().values()) {
-                    return findParentMonitorForCluster(clusterId, 
groupMonitor);
-
-                }
-            }
-        }
-        return null;
-
-    }
-
     /**
      * To set the status of the application monitor
      *
@@ -261,7 +192,7 @@ public class ApplicationMonitor extends Monitor {
 
                 }
                 //updating the life cycle and current status
-                context.setStatus(statusEvent.getStatus());
+                context.setCurrentStatus(statusEvent.getStatus());
                 context.addStatusToLIfeCycle(statusEvent.getStatus());
                 if(!startDep) {
                     //Checking in the children whether all are active,

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
index 0bdad16..619d13e 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/monitor/group/GroupMonitor.java
@@ -24,28 +24,25 @@ import 
org.apache.stratos.autoscaler.exception.DependencyBuilderException;
 import org.apache.stratos.autoscaler.exception.TopologyInConsistentException;
 import 
org.apache.stratos.autoscaler.grouping.dependency.context.ApplicationContext;
 import org.apache.stratos.autoscaler.monitor.EventHandler;
-import org.apache.stratos.autoscaler.monitor.Monitor;
+import org.apache.stratos.autoscaler.monitor.ParentComponentMonitor;
 import org.apache.stratos.autoscaler.monitor.MonitorStatusEventBuilder;
-import org.apache.stratos.autoscaler.monitor.events.ClusterStatusEvent;
-import org.apache.stratos.autoscaler.monitor.events.GroupStatusEvent;
 import org.apache.stratos.autoscaler.monitor.events.MonitorStatusEvent;
 import org.apache.stratos.autoscaler.status.checker.StatusChecker;
 import org.apache.stratos.messaging.domain.topology.Group;
 import org.apache.stratos.messaging.domain.topology.Status;
-import org.apache.stratos.messaging.event.application.status.StatusEvent;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * This is GroupMonitor to monitor the group which consists of
  * groups and clusters
  */
-public class GroupMonitor extends Monitor implements EventHandler {
+public class GroupMonitor extends ParentComponentMonitor implements 
EventHandler {
     private static final Log log = LogFactory.getLog(GroupMonitor.class);
 
     //Parent monitor of this monitor
-    private Monitor parent;
-    //Application id of this particular monitor
-    protected String appId;
-
+    private ParentComponentMonitor parent;
 
     /**
      * Constructor of GroupMonitor
@@ -53,9 +50,10 @@ public class GroupMonitor extends Monitor implements 
EventHandler {
      * @throws DependencyBuilderException throws when couldn't build the 
Topology
      * @throws TopologyInConsistentException throws when topology is 
inconsistent
      */
-    public GroupMonitor(Group group) throws DependencyBuilderException,
+    public GroupMonitor(Group group, String appId) throws 
DependencyBuilderException,
                                             TopologyInConsistentException {
         super(group);
+        this.appId = appId;
         startDependency();
     }
 
@@ -79,39 +77,87 @@ public class GroupMonitor extends Monitor implements 
EventHandler {
     @Override
     protected void monitor(MonitorStatusEvent statusEvent) {
         String id = statusEvent.getId();
-        ApplicationContext context = this.dependencyTree.
-                findApplicationContextWithId(id);
-        if(context.getStatusLifeCycle().isEmpty()) {
-            try {
-                //if life cycle is empty, need to start the monitor
-                boolean startDep = startDependency(statusEvent.getId());
-                if(log.isDebugEnabled()) {
-                    log.debug("started a child: " + startDep + " by the 
group/cluster: " + id);
+        Status status1 = statusEvent.getStatus();
+        ApplicationContext context = 
this.dependencyTree.findApplicationContextWithId(id);
+        //Events coming from parent are In_Active(in faulty detection), 
Scaling events, termination
+        //TODO if statusEvent is for active, then start the next one if any 
available
+        if(!isParent(id)) {
+            if(status1 == Status.Activated) {
+                try {
+                    //if life cycle is empty, need to start the monitor
+                    boolean startDep = startDependency(statusEvent.getId());
+                    if (log.isDebugEnabled()) {
+                        log.debug("started a child: " + startDep + " by the 
group/cluster: " + id);
+
+                    }
+                    //updating the life cycle and current status
+                    if (startDep) {
+                        context.setCurrentStatus(Status.Created);
+                        context.addStatusToLIfeCycle(Status.Created);
+                    } else {
+                        StatusChecker.getInstance().onChildStatusChange(id, 
this.id, this.appId);
+                    }
 
+                } catch (TopologyInConsistentException e) {
+                    //TODO revert the siblings and notify parent, change a 
flag for reverting/un-subscription
+                    log.error(e);
                 }
-                //updating the life cycle and current status
-                context.setStatus(statusEvent.getStatus());
-                context.addStatusToLIfeCycle(statusEvent.getStatus());
-                if(!startDep) {
-                    //Checking in the children whether all are active,
-                    // since no dependency found to be started.
-                    StatusChecker.getInstance().onChildStatusChange(id, 
this.id, this.appId);
+            } else if(status1 == Status.In_Active) {
+                //TODO if C1 depends on C2, then if C2 is in_active, then by 
getting killdepend as C1 and
+                //TODO need to send in_active for c1. When C1 in_active 
receives, get dependent and
+                //TODO check whether dependent in_active. Then kill c1.
+                //evaluate termination behavior and take action based on that.
+
+                List<ApplicationContext> terminationList = new 
ArrayList<ApplicationContext>();
+                terminationList = 
this.dependencyTree.getTerminationDependencies(id);
+
+                //check whether all the children are in_active state
+                for(ApplicationContext terminationContext : terminationList) {
+                   //terminationContext
                 }
-            } catch (TopologyInConsistentException e) {
-                //TODO revert the siblings and notify parent, change a flag 
for reverting/un-subscription
-                log.error(e);
+
+                /*if(terminationList != null && !terminationList.isEmpty()) {
+                    for(ApplicationContext context1 : terminationList) {
+                        if(context1 instanceof ClusterContext) {
+                            AbstractClusterMonitor monitor = 
this.clusterIdToClusterMonitorsMap.
+                                    get(context1.getId());
+                            //Whether life cycle change to Created
+                            if(monitor.getStatus() == Status.Created) {
+                                canTerminate = true;
+                            } else {
+                                //TODO sending group in_active event to 
dependent cluster/group
+                                
StatusEventPublisher.sendGroupActivatedEvent(this.appId, this.id);
+                                //not all dependent clusters are in created 
state.
+                                canTerminate = false;
+                            }
+                        }
+                    }
+                    if(canTerminate) {
+                       //
+                    }*/
+                } else {
+                    //TODO get dependents
+                    List<ApplicationContext> dependents = 
this.dependencyTree.getTerminationDependencies(id);
+                }
+
+
+
+
+
+            } else if(status1 == Status.Created) {
+                //the dependent goes to be created state, so terminate the 
dependents
             }
-        } else {
-            //TODO act based on life cycle events
         }
 
-    }
 
-    public Monitor getParent() {
+
+
+
+    public ParentComponentMonitor getParent() {
         return parent;
     }
 
-    public void setParent(Monitor parent) {
+    public void setParent(ParentComponentMonitor parent) {
         this.parent = parent;
     }
 
@@ -123,4 +169,16 @@ public class GroupMonitor extends Monitor implements 
EventHandler {
         this.appId = appId;
     }
 
+    private boolean isParent(String id) {
+        if(this.parent.getId().equals(id)) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+
+
+
+
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
index ee04fff..04ed325 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/status/checker/StatusChecker.java
@@ -54,7 +54,7 @@ public class StatusChecker {
      * @param clusterId id of the cluster
      */
     public void onMemberStatusChange(String clusterId) {
-        ClusterMonitor monitor = 
AutoscalerContext.getInstance().getMonitor(clusterId);
+        ClusterMonitor monitor = (ClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
         boolean clusterActive = clusterActive(monitor);
         // if active then notify upper layer
         if (clusterActive) {
@@ -64,6 +64,13 @@ public class StatusChecker {
         }
     }
 
+    public void onMemberTermination(String clusterId) {
+        ClusterMonitor monitor = (ClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
+        //TODO get Topology status
+        // if cluster not having any members and if the cluster was in_active 
then send created Events
+        // if cluster was in terminating, then send terminated event.
+    }
+
     private boolean clusterActive(AbstractClusterMonitor monitor) {
         boolean clusterActive = false;
         for (NetworkPartitionContext networkPartitionContext : 
monitor.getNetworkPartitionCtxts().values()) {
@@ -84,6 +91,42 @@ public class StatusChecker {
     }
 
     /**
+     * @param clusterId
+     * @param appId
+     * @param partitionContext is to decide in which partition has less 
members while others have active members
+     */
+    public void onMemberFaultEvent(final String clusterId, final String appId, 
final PartitionContext partitionContext) {
+        ClusterMonitor monitor = (ClusterMonitor) 
AutoscalerContext.getInstance().getMonitor(clusterId);
+        boolean clusterInActive = getClusterInActive(monitor, 
partitionContext);
+        if (clusterInActive) {
+            //TODO evaluate life cycle
+            //send cluster In-Active event to cluster status topic
+
+        } else {
+            boolean clusterActive = clusterActive(monitor);
+            if(clusterActive) {
+                //TODO evaluate life cycle
+                //send clusterActive event to cluster status topic
+            }
+        }
+    }
+
+    private boolean getClusterInActive(AbstractClusterMonitor monitor, 
PartitionContext partitionContext) {
+        boolean clusterInActive = false;
+        for (NetworkPartitionContext networkPartitionContext : 
monitor.getNetworkPartitionCtxts().values()) {
+            for (PartitionContext partition : 
networkPartitionContext.getPartitionCtxts().values()) {
+                if 
(partitionContext.getPartitionId().equals(partition.getPartitionId()) &&
+                        partition.getActiveMemberCount() < 
partition.getMinimumMemberCount()) {
+                    clusterInActive = true;
+                    return clusterInActive;
+                }
+            }
+
+        }
+        return clusterInActive;
+    }
+
+    /**
      *
      * @param idOfChild
      * @param groupId
@@ -149,47 +192,7 @@ public class StatusChecker {
     }
 
 
-    /**
-     * @param clusterId
-     * @param appId
-     * @param partitionContext is to decide in which partition has less 
members while others have active members
-     */
-    public void onMemberFaultEvent(final String clusterId, final String appId, 
final PartitionContext partitionContext) {
-        Runnable memberFault = new Runnable() {
-            public void run() {
-                ClusterMonitor monitor = 
AutoscalerContext.getInstance().getMonitor(clusterId);
-                boolean clusterActive = false;
-                boolean clusterInMaintenance = false;
-                for (NetworkPartitionContext networkPartitionContext : 
monitor.getNetworkPartitionCtxts().values()) {
-                    for (PartitionContext partition : 
networkPartitionContext.getPartitionCtxts().values()) {
-                        if 
(partitionContext.getPartitionId().equals(partition.getPartitionId()) &&
-                                partition.getActiveMemberCount() < 
partition.getMinimumMemberCount()) {
-                            clusterInMaintenance = true;
-                        } else {
-                            log.info(String.format("Hence the [partition] %s, 
in [networkpartition], " +
-                                            "%s has exceeded the [minimum], %d 
with current active " +
-                                            "[members], %d the [cluster], %s 
is still in active mode."
-                                    , partition.getPartitionId(), 
partition.getNetworkPartitionId(),
-                                    partition.getMinimumMemberCount(), 
partition.getActiveMemberCount(), clusterId));
-                        }
-                        if (partitionContext.getMinimumMemberCount() >= 
partitionContext.getActiveMemberCount()) {
-                            clusterActive = true;
-                        }
-                        clusterActive = false;
-                    }
-
-                }
-                // if in maintenance then notify upper layer
-                if (clusterActive && clusterInMaintenance) {
-                    //send clusterInmaintenance event to cluster status topic
-
-                }
 
-            }
-        };
-        Thread faultHandlingThread = new Thread(memberFault);
-        faultHandlingThread.start();
-    }
 
     /**
      * This will use to calculate whether  all children of a particular 
component is active by travesing Top

http://git-wip-us.apache.org/repos/asf/stratos/blob/fa5a7412/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
index cb03158..050a388 100644
--- 
a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
+++ 
b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
@@ -63,149 +63,8 @@ public class AutoscalerUtil {
     }
 
 
-    /**
-     * Updates ClusterContext for given cluster
-     *
-     * @param cluster
-     * @return ClusterMonitor - Updated ClusterContext
-     * @throws PolicyValidationException
-     * @throws PartitionValidationException
-     */
-    public static ClusterMonitor getClusterMonitor(Cluster cluster) throws 
PolicyValidationException, PartitionValidationException {
-        // FIXME fix the following code to correctly update
-        // AutoscalerContext context = AutoscalerContext.getInstance();
-        if (null == cluster) {
-            return null;
-        }
 
-        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.setStatus(Status.Created);
-            if(log.isInfoEnabled()){
-                log.info(String.format("Network partition context has been 
added: [network partition] %s",
-                            networkPartitionContext.getId()));
-            }
-        }
-        
-        
-        // find lb reference type
-        /*java.util.Properties props = cluster.getProperties();
-        
-        if(props.containsKey(Constants.LOAD_BALANCER_REF)) {
-            String value = props.getProperty(Constants.LOAD_BALANCER_REF);
-            clusterMonitor.setLbReferenceType(value);
-            if(log.isDebugEnabled()) {
-                log.debug("Set the lb reference type: "+value);
-            }
-        }*/
-
-        //register a status Checker
-
-        
-        // 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;
-       }
 
 
        public static LbClusterMonitor getLBClusterMonitor(Cluster cluster) 
throws PolicyValidationException, PartitionValidationException {
@@ -324,47 +183,7 @@ public class AutoscalerUtil {
     }
 
     //TODO moving it into factory class
-    public static GroupMonitor 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);
-            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;
-
-    }
 
 
 

Reply via email to