incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pran...@apache.org
Subject [15/50] git commit: Handling of errors during AutoScaleConfig in NetScaler.
Date Fri, 16 Nov 2012 10:00:31 GMT
Handling of errors during AutoScaleConfig in NetScaler.


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

Branch: refs/heads/master
Commit: eec892dfbc8b5db98c828f9d2850c591b2a262ec
Parents: d215589
Author: Vijay venkatachalam <vijay.venkatachalam@citrix.com>
Authored: Thu Sep 20 14:15:40 2012 +0530
Committer: Vijay Venkatachalam <vijay.venkatachalam@citrix.com>
Committed: Fri Nov 16 10:56:57 2012 +0530

----------------------------------------------------------------------
 .../cloud/network/resource/NetscalerResource.java  |  796 ++++++++-------
 1 files changed, 420 insertions(+), 376 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/eec892df/plugins/network-elements/netscaler/src/com/cloud/network/resource/NetscalerResource.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/netscaler/src/com/cloud/network/resource/NetscalerResource.java b/plugins/network-elements/netscaler/src/com/cloud/network/resource/NetscalerResource.java
index cadf391..ee6af45 100644
--- a/plugins/network-elements/netscaler/src/com/cloud/network/resource/NetscalerResource.java
+++ b/plugins/network-elements/netscaler/src/com/cloud/network/resource/NetscalerResource.java
@@ -30,8 +30,15 @@ import com.citrix.netscaler.nitro.exception.nitro_exception;
 import com.citrix.netscaler.nitro.resource.base.base_response;
 import com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile;
 import com.citrix.netscaler.nitro.resource.config.basic.server_service_binding;
+import com.citrix.netscaler.nitro.resource.config.basic.servicegroup;
+import com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable;
+import com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable_metric_binding;
+import com.citrix.netscaler.nitro.resource.config.lb.lbmonitor;
+import com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_lbmetrictable_binding;
+import com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding;
 import com.citrix.netscaler.nitro.resource.config.lb.lbvserver;
 import com.citrix.netscaler.nitro.resource.config.lb.lbvserver_service_binding;
+import com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding;
 import com.citrix.netscaler.nitro.resource.config.network.Interface;
 import com.citrix.netscaler.nitro.resource.config.network.inat;
 import com.citrix.netscaler.nitro.resource.config.network.vlan;
@@ -40,6 +47,7 @@ import com.citrix.netscaler.nitro.resource.config.network.vlan_nsip_binding;
 import com.citrix.netscaler.nitro.resource.config.ns.nsconfig;
 import com.citrix.netscaler.nitro.resource.config.ns.nshardware;
 import com.citrix.netscaler.nitro.resource.config.ns.nsip;
+import com.citrix.netscaler.nitro.resource.config.autoscale.*;
 import com.citrix.netscaler.nitro.resource.stat.lb.lbvserver_stats;
 import com.citrix.netscaler.nitro.service.nitro_service;
 import com.citrix.netscaler.nitro.util.filtervalue;
@@ -1247,6 +1255,36 @@ public class NetscalerResource implements ServerResource {
         }
     }
 
+    private boolean isServiceGroupBoundToVirtualServer(String nsVirtualServerName, String serviceGroupName) throws ExecutionException {
+
+        lbvserver_servicegroup_binding vserver_servicegroup_binding = new lbvserver_servicegroup_binding();
+
+        try {
+            lbvserver_servicegroup_binding[] result = vserver_servicegroup_binding.get_filtered(_netscalerService, nsVirtualServerName, "servicegroupname:" + serviceGroupName);
+            if(result != null && result.length > 0){
+                return true;
+            }
+        } catch (Exception e) {
+            throw new ExecutionException("Failed to verify lb vserver " + nsVirtualServerName + "and servicegrop " +  serviceGroupName + " binding exists due to " + e.getMessage());
+        }
+        return false;
+
+    }
+
+    private boolean nsServiceGroupExisits(String lbVServerName ) throws ExecutionException {
+        try {
+            return servicegroup.get(_netscalerService, lbVServerName) != null;
+        } catch (nitro_exception e) {
+            if (e.getErrorCode() == NitroError.NS_RESOURCE_NOT_EXISTS) {
+                return false; // service group does not exist
+            } else {
+                throw new ExecutionException(e.getMessage());
+            }
+        } catch (Exception e) {
+            throw new ExecutionException(e.getMessage());
+        }
+    }
+
     private void deleteServersInGuestVlan(long vlanTag, String vlanSelfIp, String vlanNetmask) throws ExecutionException {
         try {
             com.citrix.netscaler.nitro.resource.config.basic.server[] serverList = com.citrix.netscaler.nitro.resource.config.basic.server.get(_netscalerService);
@@ -1430,7 +1468,7 @@ public class NetscalerResource implements ServerResource {
         return autoScalePolicyTO.getAction().equals("scaledown");
     }
 
-    private void removeAutoScalePolicy(String timerName, String policyName) throws Exception {
+    private void removeAutoScalePolicy(String timerName, String policyName, boolean isCleanUp) throws Exception {
         // unbind timer policy
         // unbbind timer trigger lb_astimer -policyName lb_policy_scaleUp
         com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding timer_policy_binding = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding();
@@ -1440,8 +1478,8 @@ public class NetscalerResource implements ServerResource {
             timer_policy_binding.set_global("DEFAULT");
             timer_policy_binding.delete(_netscalerService, timer_policy_binding);
         } catch (Exception e) {
-            // Ignore Exception
-            throw e;
+            // Ignore Exception on cleanup
+            if(!isCleanUp) throw e;
         }
 
         // Removing Timer policy
@@ -1451,8 +1489,8 @@ public class NetscalerResource implements ServerResource {
             timerPolicy.set_name(policyName);
             timerPolicy.delete(_netscalerService, timerPolicy);
         } catch (Exception e) {
-            // Ignore Exception
-            throw e;
+            // Ignore Exception on cleanup
+            if(!isCleanUp) throw e;
         }
 
     }
@@ -1465,42 +1503,23 @@ public class NetscalerResource implements ServerResource {
         String nsVirtualServerName  = generateNSVirtualServerName(srcIp, srcPort);
         String serviceGroupName  = generateAutoScaleServiceGroupName(srcIp, srcPort);
 
-        disableAutoScaleConfig(loadBalancerTO);
+        disableAutoScaleConfig(loadBalancerTO, false);
 
-        // UnBind autoscale service group
-        // unbind lb vserver lb lb_autoscaleGroup
-        com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding vserver_servicegroup_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding();
-        try {
+        if(isServiceGroupBoundToVirtualServer(nsVirtualServerName, serviceGroupName)) {
+            // UnBind autoscale service group
+            // unbind lb vserver lb lb_autoscaleGroup
+            lbvserver_servicegroup_binding vserver_servicegroup_binding = new lbvserver_servicegroup_binding();
             vserver_servicegroup_binding.set_name(nsVirtualServerName);
             vserver_servicegroup_binding.set_servicegroupname(serviceGroupName);
             vserver_servicegroup_binding.delete(_netscalerService, vserver_servicegroup_binding);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
         }
 
-        // Remove autoscale service group
-        com.citrix.netscaler.nitro.resource.config.basic.servicegroup serviceGroup = new com.citrix.netscaler.nitro.resource.config.basic.servicegroup();
-        try {
+        if(nsServiceGroupExisits(serviceGroupName)) {
+            // Remove autoscale service group
+            com.citrix.netscaler.nitro.resource.config.basic.servicegroup serviceGroup = new com.citrix.netscaler.nitro.resource.config.basic.servicegroup();
             serviceGroup.set_servicegroupname(serviceGroupName);
             serviceGroup.delete(_netscalerService, serviceGroup);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
-
-        //      // Set min and max autoscale members to zero
-        // com.citrix.netscaler.nitro.resource.config.lb.lbvserver lbvserver = new
-        // com.citrix.netscaler.nitro.resource.config.lb.lbvserver();
-        //      try {
-        //          lbvserver.set_name(nsVirtualServerName);
-        //          lbvserver.set_minautoscalemembers(0);
-        //          lbvserver.set_maxautoscalemembers(0);
-        //          lbvserver.update(_netscalerService, lbvserver);
-        //      } catch (Exception e) {
-        //          // Ignore Exception
-        //          throw e;
-        //      }
+        }
 
         removeLBVirtualServer(nsVirtualServerName);
 
@@ -1508,7 +1527,7 @@ public class NetscalerResource implements ServerResource {
     }
 
     @SuppressWarnings("static-access")
-    private synchronized boolean disableAutoScaleConfig(LoadBalancerTO loadBalancerTO) throws Exception {
+    private synchronized boolean disableAutoScaleConfig(LoadBalancerTO loadBalancerTO, boolean isCleanUp) throws Exception {
         String srcIp = loadBalancerTO.getSrcIp();
         int srcPort = loadBalancerTO.getSrcPort();
 
@@ -1522,108 +1541,119 @@ public class NetscalerResource implements ServerResource {
         String serviceGroupName  = generateAutoScaleServiceGroupName(srcIp, srcPort);
         AutoScaleVmGroupTO vmGroupTO = loadBalancerTO.getAutoScaleVmGroupTO();
         List<AutoScalePolicyTO> policies = vmGroupTO.getPolicies();
-
-        /* Delete min/max member policies */
         String minMemberPolicyName = generateAutoScaleMinPolicyName(srcIp, srcPort);
+        String maxMemberPolicyName = generateAutoScaleMaxPolicyName(srcIp, srcPort);
 
-        removeAutoScalePolicy(timerName, minMemberPolicyName);
+        try {
 
-        String maxMemberPolicyName = generateAutoScaleMaxPolicyName(srcIp, srcPort);
-        removeAutoScalePolicy(timerName, maxMemberPolicyName);
-
-        boolean isSnmp = false;
-        /* Create Counters */
-        for (AutoScalePolicyTO autoScalePolicyTO : policies) {
-            List<ConditionTO> conditions = autoScalePolicyTO.getConditions();
-            for (ConditionTO conditionTO : conditions) {
-                CounterTO counterTO = conditionTO.getCounter();
-                if(counterTO.getSource().equals("snmp")) {
-                    isSnmp = true;
-                    break;
-                }
-            }
-            String policyId = Long.toString(autoScalePolicyTO.getId());
-            String policyName = generateAutoScalePolicyName(srcIp, srcPort,policyId);
+            /* Delete min/max member policies */
 
-            // Removing Timer policy
-            removeAutoScalePolicy(timerName, policyName);
-        }
+            removeAutoScalePolicy(timerName, minMemberPolicyName, isCleanUp);
 
-        /* Delete AutoScale Config */
-        // Delete AutoScale ScaleDown action
-        com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleDownAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
-        try {
-            scaleDownAction.set_name(scaleDownActionName);
-            scaleDownAction.delete(_netscalerService, scaleDownAction);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+            removeAutoScalePolicy(timerName, maxMemberPolicyName, isCleanUp);
 
-        // Delete AutoScale ScaleUp action
-        com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleUpAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
-        try {
-            scaleUpAction.set_name(scaleUpActionName);
-            scaleUpAction.delete(_netscalerService, scaleUpAction);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+            boolean isSnmp = false;
+            /* Create Counters */
+            for (AutoScalePolicyTO autoScalePolicyTO : policies) {
+                List<ConditionTO> conditions = autoScalePolicyTO.getConditions();
+                for (ConditionTO conditionTO : conditions) {
+                    CounterTO counterTO = conditionTO.getCounter();
+                    if(counterTO.getSource().equals("snmp")) {
+                        isSnmp = true;
+                        break;
+                    }
+                }
+                String policyId = Long.toString(autoScalePolicyTO.getId());
+                String policyName = generateAutoScalePolicyName(srcIp, srcPort,policyId);
 
-        // Delete Timer
-        com.citrix.netscaler.nitro.resource.config.timer.timertrigger timer = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger();
-        try {
-            timer.set_name(timerName);
-            timer.delete(_netscalerService, timer);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+                // Removing Timer policy
+                removeAutoScalePolicy(timerName, policyName, isCleanUp);
+            }
 
-        // Delete AutoScale Profile
-        com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile autoscaleProfile = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile();
-        try {
-            autoscaleProfile.set_name(profileName);
-            autoscaleProfile.delete(_netscalerService, autoscaleProfile);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+            /* Delete AutoScale Config */
+            // Delete AutoScale ScaleDown action
+            com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleDownAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
+            try {
+                scaleDownAction.set_name(scaleDownActionName);
+                scaleDownAction.delete(_netscalerService, scaleDownAction);
+            } catch (Exception e) {
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
+            }
 
-        if(isSnmp) {
-            com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding monitor_servicegroup_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding();
+            // Delete AutoScale ScaleUp action
+            com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleUpAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
             try {
-                monitor_servicegroup_binding.set_monitorname(monitorName);
-                monitor_servicegroup_binding.set_servicegroupname(serviceGroupName);
-                monitor_servicegroup_binding.delete(_netscalerService, monitor_servicegroup_binding);
+                scaleUpAction.set_name(scaleUpActionName);
+                scaleUpAction.delete(_netscalerService, scaleUpAction);
             } catch (Exception e) {
-                // Ignore Exception
-                throw e;
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
             }
 
-            // Delete Monitor
-            // rm lb monitor lb_metric_table_mon
-            com.citrix.netscaler.nitro.resource.config.lb.lbmonitor monitor = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor();
+            // Delete Timer
+            com.citrix.netscaler.nitro.resource.config.timer.timertrigger timer = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger();
             try {
-                monitor.set_monitorname(monitorName);
-                monitor.set_type("LOAD");
-                monitor.delete(_netscalerService, monitor);
+                timer.set_name(timerName);
+                timer.delete(_netscalerService, timer);
             } catch (Exception e) {
-                // Ignore Exception
-                throw e;
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
             }
 
-            // Delete Metric Table
-            com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable metricTable = new com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable();
+            // Delete AutoScale Profile
+            com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile autoscaleProfile = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile();
             try {
-                metricTable.set_metrictable(mtName);
-                metricTable.delete(_netscalerService, metricTable);
+                autoscaleProfile.set_name(profileName);
+                autoscaleProfile.delete(_netscalerService, autoscaleProfile);
             } catch (Exception e) {
-                // Ignore Exception
-                throw e;
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
             }
-        }
 
+            if(isSnmp) {
+                com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding monitor_servicegroup_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding();
+                try {
+                    monitor_servicegroup_binding.set_monitorname(monitorName);
+                    monitor_servicegroup_binding.set_servicegroupname(serviceGroupName);
+                    monitor_servicegroup_binding.delete(_netscalerService, monitor_servicegroup_binding);
+                } catch (Exception e) {
+                    // Ignore Exception on cleanup
+                    if(!isCleanUp) throw e;
+                }
+
+                // Delete Monitor
+                // rm lb monitor lb_metric_table_mon
+                com.citrix.netscaler.nitro.resource.config.lb.lbmonitor monitor = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor();
+                try {
+                    monitor.set_monitorname(monitorName);
+                    monitor.set_type("LOAD");
+                    monitor.delete(_netscalerService, monitor);
+                } catch (Exception e) {
+                    // Ignore Exception on cleanup
+                    if(!isCleanUp) throw e;
+                }
+
+                // Delete Metric Table
+                com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable metricTable = new com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable();
+                try {
+                    metricTable.set_metrictable(mtName);
+                    metricTable.delete(_netscalerService, metricTable);
+                } catch (Exception e) {
+                    // Ignore Exception on cleanup
+                    if(!isCleanUp) throw e;
+                }
+            }
+        } catch (Exception ex) {
+            if(!isCleanUp) {
+                // Normal course, exception has occurred
+                enableAutoScaleConfig(loadBalancerTO, true);
+                throw ex;
+            } else {
+                // Programming error
+                throw ex;
+            }
+        }
 
         return true;
     }
@@ -1637,46 +1667,48 @@ public class NetscalerResource implements ServerResource {
         String lbAlgorithm = loadBalancerTO.getAlgorithm();
         String nsVirtualServerName  = generateNSVirtualServerName(srcIp, srcPort);
         AutoScaleVmGroupTO vmGroupTO = loadBalancerTO.getAutoScaleVmGroupTO();
-        addLBVirtualServer(nsVirtualServerName, srcIp, srcPort, lbAlgorithm, lbProtocol, loadBalancerTO.getStickinessPolicies(), vmGroupTO);
         if (s_logger.isDebugEnabled()) {
             s_logger.debug("Created load balancing virtual server " + nsVirtualServerName + " on the Netscaler device");
         }
+        addLBVirtualServer(nsVirtualServerName, srcIp, srcPort, lbAlgorithm, lbProtocol, loadBalancerTO.getStickinessPolicies(), vmGroupTO);
 
         String serviceGroupName  = generateAutoScaleServiceGroupName(srcIp, srcPort);
-        // add servicegroup lb_autoscaleGroup -autoscale POLICY -memberPort 80
-        com.citrix.netscaler.nitro.resource.config.basic.servicegroup serviceGroup = new com.citrix.netscaler.nitro.resource.config.basic.servicegroup();
-        int memberPort = vmGroupTO.getMemberPort();
-        try {
-            serviceGroup.set_servicegroupname(serviceGroupName);
-            serviceGroup.set_servicetype(lbProtocol);
-            serviceGroup.set_autoscale("POLICY"); // TODO: Values not displayed in API
-            serviceGroup.set_memberport(memberPort);
-            serviceGroup.add(_netscalerService, serviceGroup);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
+        if(!nsServiceGroupExisits(serviceGroupName)) {
+            // add servicegroup lb_autoscaleGroup -autoscale POLICY -memberPort 80
+            int memberPort = vmGroupTO.getMemberPort();
+            try {
+                servicegroup serviceGroup = new servicegroup();
+                serviceGroup.set_servicegroupname(serviceGroupName);
+                serviceGroup.set_servicetype(lbProtocol);
+                serviceGroup.set_autoscale("POLICY");
+                serviceGroup.set_memberport(memberPort);
+                serviceGroup.add(_netscalerService, serviceGroup);
+            } catch (Exception e) {
+                throw e;
+            }
         }
 
-        // Bind autoscale service group
-        // bind lb vserver lb lb_autoscaleGroup
-        com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding vserver_servicegroup_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding();
+        if(!isServiceGroupBoundToVirtualServer(nsVirtualServerName, serviceGroupName)) {
+            // Bind autoscale service group
+            // bind lb vserver lb lb_autoscaleGroup
+            lbvserver_servicegroup_binding vserver_servicegroup_binding = new lbvserver_servicegroup_binding();
 
-        try {
-            vserver_servicegroup_binding.set_name(nsVirtualServerName);
-            vserver_servicegroup_binding.set_servicegroupname(serviceGroupName);
-            vserver_servicegroup_binding.add(_netscalerService, vserver_servicegroup_binding);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
+            try {
+                vserver_servicegroup_binding.set_name(nsVirtualServerName);
+                vserver_servicegroup_binding.set_servicegroupname(serviceGroupName);
+                vserver_servicegroup_binding.add(_netscalerService, vserver_servicegroup_binding);
+            } catch (Exception e) {
+                throw e;
+            }
         }
 
         // Create the autoscale config
-        enableAutoScaleConfig(loadBalancerTO);
+        enableAutoScaleConfig(loadBalancerTO, false);
         return true;
     }
 
     @SuppressWarnings("static-access")
-    private synchronized boolean enableAutoScaleConfig(LoadBalancerTO loadBalancerTO) throws Exception {
+    private synchronized boolean enableAutoScaleConfig(LoadBalancerTO loadBalancerTO, boolean isCleanUp) throws Exception {
         String srcIp = loadBalancerTO.getSrcIp();
         int srcPort = loadBalancerTO.getSrcPort();
 
@@ -1696,278 +1728,288 @@ public class NetscalerResource implements ServerResource {
         String snmpCommunity = profileTO.getSnmpCommunity();
         long cur_prirotiy = 1;
 
+        try
+        {
+            // Set min and max autoscale members;
+            // add lb vserver lb  http 10.102.31.100 80 -minAutoscaleMinMembers 3 -maxAutoscaleMembers 10
+            int minAutoScaleMembers = vmGroupTO.getMinMembers();
+            int maxAutoScaleMembers = vmGroupTO.getMaxMembers();
+            lbvserver vserver = new lbvserver();
+            try {
+                vserver.set_name(nsVirtualServerName);
+                vserver.set_minautoscalemembers(minAutoScaleMembers);
+                vserver.set_maxautoscalemembers(maxAutoScaleMembers);
+                vserver.update(_netscalerService, vserver);
+            } catch (Exception e) {
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
+            }
 
-        // Set min and max autoscale members;
-        // add lb vserver lb  http 10.102.31.100 80 -minAutoscaleMinMembers 3 -maxAutoscaleMembers 10
-        int minAutoScaleMembers = vmGroupTO.getMinMembers();
-        int maxAutoScaleMembers = vmGroupTO.getMaxMembers();
-        com.citrix.netscaler.nitro.resource.config.lb.lbvserver lbvserver = new com.citrix.netscaler.nitro.resource.config.lb.lbvserver();
-        try {
-            lbvserver.set_name(nsVirtualServerName);
-            lbvserver.set_minautoscalemembers(minAutoScaleMembers);
-            lbvserver.set_maxautoscalemembers(maxAutoScaleMembers);
-            lbvserver.update(_netscalerService, lbvserver);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
-
-        /* AutoScale Config */
-        // Add AutoScale Profile
-        // add autoscale profile lb_asprofile CLOUDSTACK -url -http:// 10.102.31.34:8080/client/api- -apiKey abcdef
-        // -sharedSecret xyzabc
-        String apiKey = profileTO.getAutoScaleUserApiKey();
-        String secretKey = profileTO.getAutoScaleUserSecretKey();
-        String url = profileTO.getCloudStackApiUrl();
-
-        com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile autoscaleProfile = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile();
-        try {
-            autoscaleProfile.set_name(profileName);
-            autoscaleProfile.set_type("CLOUDSTACK");
-            autoscaleProfile.set_apikey(apiKey);
-            autoscaleProfile.set_sharedsecret(secretKey);
-            autoscaleProfile.set_url(url);
-            autoscaleProfile.add(_netscalerService, autoscaleProfile);
-        } catch (Exception e) {
-            // Ignore Exception
-
-            throw e;
-        }
+            /* AutoScale Config */
+            // Add AutoScale Profile
+            // add autoscale profile lb_asprofile CLOUDSTACK -url -http:// 10.102.31.34:8080/client/api- -apiKey abcdef
+            // -sharedSecret xyzabc
+            String apiKey = profileTO.getAutoScaleUserApiKey();
+            String secretKey = profileTO.getAutoScaleUserSecretKey();
+            String url = profileTO.getCloudStackApiUrl();
 
-        // Add Timer
-        com.citrix.netscaler.nitro.resource.config.timer.timertrigger timer = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger();
-        try {
-            timer.set_name(timerName);
-            timer.set_interval(interval);
-            timer.add(_netscalerService, timer);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+            com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile autoscaleProfile = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleprofile();
+            try {
+                autoscaleProfile.set_name(profileName);
+                autoscaleProfile.set_type("CLOUDSTACK");
+                autoscaleProfile.set_apikey(apiKey);
+                autoscaleProfile.set_sharedsecret(secretKey);
+                autoscaleProfile.set_url(url);
+                autoscaleProfile.add(_netscalerService, autoscaleProfile);
+            } catch (Exception e) {
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
+            }
 
-        // AutoScale Actions
-        Integer scaleUpQuietTime = null;
-        Integer scaleDownQuietTime = null;
-        for (AutoScalePolicyTO autoScalePolicyTO : policies) {
-            if(scaleUpQuietTime == null) {
-                if(isScaleUpPolicy(autoScalePolicyTO)) {
-                    scaleUpQuietTime = autoScalePolicyTO.getQuietTime();
-                    if(scaleDownQuietTime != null) {
-                        break;
+            // Add Timer
+            com.citrix.netscaler.nitro.resource.config.timer.timertrigger timer = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger();
+            try {
+                timer.set_name(timerName);
+                timer.set_interval(interval);
+                timer.add(_netscalerService, timer);
+            } catch (Exception e) {
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
+            }
+
+            // AutoScale Actions
+            Integer scaleUpQuietTime = null;
+            Integer scaleDownQuietTime = null;
+            for (AutoScalePolicyTO autoScalePolicyTO : policies) {
+                if(scaleUpQuietTime == null) {
+                    if(isScaleUpPolicy(autoScalePolicyTO)) {
+                        scaleUpQuietTime = autoScalePolicyTO.getQuietTime();
+                        if(scaleDownQuietTime != null) {
+                            break;
+                        }
                     }
                 }
-            }
-            if(scaleDownQuietTime == null) {
-                if(isScaleDownPolicy(autoScalePolicyTO)) {
-                    scaleDownQuietTime = autoScalePolicyTO.getQuietTime();
-                    if(scaleUpQuietTime != null) {
-                        break;
+                if(scaleDownQuietTime == null) {
+                    if(isScaleDownPolicy(autoScalePolicyTO)) {
+                        scaleDownQuietTime = autoScalePolicyTO.getQuietTime();
+                        if(scaleUpQuietTime != null) {
+                            break;
+                        }
                     }
                 }
             }
-        }
 
-        // Add AutoScale ScaleUp action
-        // add autoscale action lb_scaleUpAction provision -vserver lb -profilename lb_asprofile -params
-        // -lbruleid=1234&command=deployvm&zoneid=10&templateid=5&serviceofferingid=3- -quiettime 300
-        com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleUpAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
-        try {
-            scaleUpAction.set_name(scaleUpActionName);
-            scaleUpAction.set_type("SCALE_UP"); // TODO: will this be called provision?
-            scaleUpAction.set_vserver(nsVirtualServerName); // Actions Vserver, the one that is autoscaled, with CS
-            // now both are same. Not exposed in API.
-            scaleUpAction.set_profilename(profileName);
-            scaleUpAction.set_quiettime(scaleUpQuietTime);
-            String scaleUpParameters = "command=deployVirtualMachine" + "&" +
-            ApiConstants.ZONE_ID + "=" + profileTO.getZoneId()+ "&" +
-            ApiConstants.SERVICE_OFFERING_ID + "=" + profileTO.getServiceOfferingId()+ "&" +
-            ApiConstants.TEMPLATE_ID + "=" + profileTO.getTemplateId()+ "&" +
-            ((profileTO.getOtherDeployParams() == null)? "" : (profileTO.getOtherDeployParams() + "&")) +
-            "lbruleid=" + loadBalancerTO.getId();
-            scaleUpAction.set_parameters(scaleUpParameters);
-            scaleUpAction.add(_netscalerService, scaleUpAction);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+            // Add AutoScale ScaleUp action
+            // add autoscale action lb_scaleUpAction provision -vserver lb -profilename lb_asprofile -params
+            // -lbruleid=1234&command=deployvm&zoneid=10&templateid=5&serviceofferingid=3- -quiettime 300
+            com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleUpAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
+            try {
+                scaleUpAction.set_name(scaleUpActionName);
+                scaleUpAction.set_type("SCALE_UP"); // TODO: will this be called provision?
+                scaleUpAction.set_vserver(nsVirtualServerName); // Actions Vserver, the one that is autoscaled, with CS
+                // now both are same. Not exposed in API.
+                scaleUpAction.set_profilename(profileName);
+                scaleUpAction.set_quiettime(scaleUpQuietTime);
+                String scaleUpParameters = "command=deployVirtualMachine" + "&" +
+                ApiConstants.ZONE_ID + "=" + profileTO.getZoneId()+ "&" +
+                ApiConstants.SERVICE_OFFERING_ID + "=" + profileTO.getServiceOfferingId()+ "&" +
+                ApiConstants.TEMPLATE_ID + "=" + profileTO.getTemplateId()+ "&" +
+                ((profileTO.getOtherDeployParams() == null)? "" : (profileTO.getOtherDeployParams() + "&")) +
+                "lbruleid=" + loadBalancerTO.getId();
+                scaleUpAction.set_parameters(scaleUpParameters);
+                scaleUpAction.add(_netscalerService, scaleUpAction);
+            } catch (Exception e) {
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
+            }
 
-        com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleDownAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
-        Integer destroyVmGracePeriod = profileTO.getDestroyVmGraceperiod();
-        try {
-            scaleDownAction.set_name(scaleDownActionName);
-            scaleDownAction.set_type("SCALE_DOWN"); // TODO: will this be called de-provision?
-            scaleDownAction.set_vserver(nsVirtualServerName); // TODO: no global option as of now through Nitro.
-            // Testing cannot be done.
-            scaleDownAction.set_profilename(profileName);
-            scaleDownAction.set_quiettime(scaleDownQuietTime);
-            String scaleDownParameters = "command=destroyVirtualMachine" + "&" +
-            "lbruleid=" + loadBalancerTO.getId();
-            scaleDownAction.set_parameters(scaleDownParameters);
-            scaleDownAction.set_vmdestroygraceperiod(destroyVmGracePeriod);
-            scaleDownAction.add(_netscalerService, scaleDownAction);
-        } catch (Exception e) {
-            // Ignore Exception
-            throw e;
-        }
+            com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleDownAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction();
+            Integer destroyVmGracePeriod = profileTO.getDestroyVmGraceperiod();
+            try {
+                scaleDownAction.set_name(scaleDownActionName);
+                scaleDownAction.set_type("SCALE_DOWN"); // TODO: will this be called de-provision?
+                scaleDownAction.set_vserver(nsVirtualServerName); // TODO: no global option as of now through Nitro.
+                // Testing cannot be done.
+                scaleDownAction.set_profilename(profileName);
+                scaleDownAction.set_quiettime(scaleDownQuietTime);
+                String scaleDownParameters = "command=destroyVirtualMachine" + "&" +
+                "lbruleid=" + loadBalancerTO.getId();
+                scaleDownAction.set_parameters(scaleDownParameters);
+                scaleDownAction.set_vmdestroygraceperiod(destroyVmGracePeriod);
+                scaleDownAction.add(_netscalerService, scaleDownAction);
+            } catch (Exception e) {
+                // Ignore Exception on cleanup
+                if(!isCleanUp) throw e;
+            }
+
+            /* Create min member policy */
+            String minMemberPolicyName = generateAutoScaleMinPolicyName(srcIp, srcPort);
+            String minMemberPolicyExp = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.LT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MINAUTOSCALEMEMBERS)";
+            addAutoScalePolicy(timerName, minMemberPolicyName, cur_prirotiy++, minMemberPolicyExp, scaleUpActionName,
+                    interval, interval, isCleanUp);
+
+            /* Create max member policy */
+            String maxMemberPolicyName = generateAutoScaleMaxPolicyName(srcIp, srcPort);
+            String maxMemberPolicyExp = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.GT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MAXAUTOSCALEMEMBERS)";
+            addAutoScalePolicy(timerName, maxMemberPolicyName, cur_prirotiy++, maxMemberPolicyExp, scaleDownActionName,
+                    interval, interval, isCleanUp);
+
+            /* Create Counters */
+            HashMap<String, Integer> snmpMetrics = new HashMap<String, Integer>();
+            for (AutoScalePolicyTO autoScalePolicyTO : policies) {
+                List<ConditionTO> conditions = autoScalePolicyTO.getConditions();
+                String policyExpression = "";
+                int snmpCounterNumber = 0;
+                for (ConditionTO conditionTO : conditions) {
+                    CounterTO counterTO = conditionTO.getCounter();
+                    String counterName = counterTO.getName();
+                    String operator = conditionTO.getRelationalOperator();
+                    long threshold = conditionTO.getThreshold();
+
+                    StringBuilder conditionExpression = new StringBuilder();
+                    Formatter formatter = new Formatter(conditionExpression, Locale.US);
+
+                    if(counterTO.getSource().equals("snmp"))
+                    {
+                        counterName = generateSnmpMetricName(counterName);
+                        if(snmpMetrics.size() == 0) {
+                            // Create Metric Table
+                            //add lb metricTable lb_metric_table
+                            lbmetrictable metricTable = new lbmetrictable();
+                            try {
+                                metricTable.set_metrictable(mtName);
+                                metricTable.add(_netscalerService, metricTable);
+                            } catch (Exception e) {
+                                // Ignore Exception on cleanup
+                                if(!isCleanUp) throw e;
+                            }
 
-        /* Create min member policy */
-        String minMemberPolicyName = generateAutoScaleMinPolicyName(srcIp, srcPort);
-        String minMemberPolicyExp = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.LT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MINAUTOSCALEMEMBERS)";
-        addAutoScalePolicy(timerName, minMemberPolicyName, cur_prirotiy++, minMemberPolicyExp, scaleUpActionName,
-                interval, interval);
+                            // Create Monitor
+                            // add lb monitor lb_metric_table_mon LOAD -destPort 161 -snmpCommunity public -metricTable
+                            // lb_metric_table -interval <policy_interval == 80% >
+                            lbmonitor monitor = new lbmonitor();
+                            try {
+                                monitor.set_monitorname(monitorName);
+                                monitor.set_type("LOAD");
+                                monitor.set_destport(snmpPort);
+                                monitor.set_snmpcommunity(snmpCommunity);
+                                monitor.set_metrictable(mtName);
+                                monitor.set_interval((int)(interval * 0.8));
+                                monitor.add(_netscalerService, monitor);
+                            } catch (Exception e) {
+                                // Ignore Exception on cleanup
+                                if(!isCleanUp) throw e;
+                            }
 
-        /* Create max member policy */
-        String maxMemberPolicyName = generateAutoScaleMaxPolicyName(srcIp, srcPort);
-        String maxMemberPolicyExp = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.GT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MAXAUTOSCALEMEMBERS)";
-        addAutoScalePolicy(timerName, maxMemberPolicyName, cur_prirotiy++, maxMemberPolicyExp, scaleDownActionName,
-                interval, interval);
-
-        /* Create Counters */
-        HashMap<String, Integer> snmpMetrics = new HashMap<String, Integer>();
-        for (AutoScalePolicyTO autoScalePolicyTO : policies) {
-            List<ConditionTO> conditions = autoScalePolicyTO.getConditions();
-            String policyExpression = "";
-            int snmpCounterNumber = 0;
-            for (ConditionTO conditionTO : conditions) {
-                CounterTO counterTO = conditionTO.getCounter();
-                String counterName = counterTO.getName();
-                String operator = conditionTO.getRelationalOperator();
-                long threshold = conditionTO.getThreshold();
-
-                StringBuilder conditionExpression = new StringBuilder();
-                Formatter formatter = new Formatter(conditionExpression, Locale.US);
-
-                if(counterTO.getSource().equals("snmp"))
-                {
-                    counterName = generateSnmpMetricName(counterName);
-                    if(snmpMetrics.size() == 0) {
-                        // Create Metric Table
-                        //add lb metricTable lb_metric_table
-                        com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable metricTable = new com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable();
-                        try {
-                            metricTable.set_metrictable(mtName);
-                            metricTable.add(_netscalerService, metricTable);
-                        } catch (Exception e) {
-                            // Ignore Exception
-                            throw e;
+                            // Bind servicegroup to monitor. TODO: This will change later to bind Monitor to ServiceGroup.
+                            // bind lb monitor lb_metric_table_mon lb_autoscaleGroup -passive
+                            lbmonitor_servicegroup_binding monitor_servicegroup_binding = new lbmonitor_servicegroup_binding();
+                            try {
+                                monitor_servicegroup_binding.set_monitorname(monitorName);
+                                monitor_servicegroup_binding.set_servicegroupname(serviceGroupName);
+                                monitor_servicegroup_binding.set_passive(true); // Mark the monitor to do only collect
+                                // metrics, basically use it for autoscaling purpose only.
+                                monitor_servicegroup_binding.add(_netscalerService, monitor_servicegroup_binding);
+                            } catch (Exception e) {
+                                // Ignore Exception on cleanup
+                                if(!isCleanUp) throw e;
+                            }
                         }
 
-                        // Create Monitor
-                        // add lb monitor lb_metric_table_mon LOAD -destPort 161 -snmpCommunity public -metricTable
-                        // lb_metric_table -interval <policy_interval == 80% >
-                        com.citrix.netscaler.nitro.resource.config.lb.lbmonitor monitor = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor();
-                        try {
-                            monitor.set_monitorname(monitorName);
-                            monitor.set_type("LOAD");
-                            monitor.set_destport(snmpPort);
-                            monitor.set_snmpcommunity(snmpCommunity);
-                            monitor.set_metrictable(mtName);
-                            monitor.set_interval((int)(interval * 0.8));
-                            monitor.add(_netscalerService, monitor);
-                        } catch (Exception e) {
-                            // Ignore Exception
-                            throw e;
+                        boolean newMetric = !snmpMetrics.containsKey(counterName);
+                        if(newMetric) {
+                            snmpMetrics.put(counterName, snmpCounterNumber++);
                         }
 
-                        // Bind servicegroup to monitor. TODO: This will change later to bind Monitor to ServiceGroup.
-                        // bind lb monitor lb_metric_table_mon lb_autoscaleGroup -passive
-                        com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding monitor_servicegroup_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_servicegroup_binding();
-                        try {
-                            monitor_servicegroup_binding.set_monitorname(monitorName);
-                            monitor_servicegroup_binding.set_servicegroupname(serviceGroupName);
-                            monitor_servicegroup_binding.set_passive(true); // Mark the monitor to do only collect
-                            // metrics, basically use it for autoscaling purpose only.
-                            monitor_servicegroup_binding.add(_netscalerService, monitor_servicegroup_binding);
-                        } catch (Exception e) {
-                            // Ignore Exception
-                            throw e;
-                        }
-                    }
+                        if(newMetric)
+                        {
+                            // bind lb metricTable lb_metric_table mem 1.3.6.1.4.1.2021.11.9.0
+                            String counterOid = counterTO.getValue();
+                            lbmetrictable_metric_binding metrictable_metric_binding = new lbmetrictable_metric_binding();
+                            try {
+                                metrictable_metric_binding.set_metrictable(mtName);
+                                metrictable_metric_binding.set_metric(counterName);
+                                metrictable_metric_binding.set_Snmpoid(counterOid);
+                                metrictable_metric_binding.add(_netscalerService, metrictable_metric_binding);
+                            } catch (Exception e) {
+                                // Ignore Exception on cleanup
+                                if(!isCleanUp) throw e;
+                            }
 
-                    boolean newMetric = !snmpMetrics.containsKey(counterName);
-                    if(newMetric) {
-                        snmpMetrics.put(counterName, snmpCounterNumber++);
+                            // bind lb monitor lb_metric_table_mon -metric cpu -metricThreshold 1
+                            lbmonitor_lbmetrictable_binding monitor_metrictable_binding = new lbmonitor_lbmetrictable_binding();
+                            try {
+                                monitor_metrictable_binding.set_monitorname(monitorName);
+                                monitor_metrictable_binding.set_metric(counterName);
+                                monitor_metrictable_binding.set_metricthreshold(1); // 1 is a dummy threshold
+                                monitor_metrictable_binding.add(_netscalerService, monitor_metrictable_binding);
+                            } catch (Exception e) {
+                                // Ignore Exception on cleanup
+                                if(!isCleanUp) throw e;
+                            }
+                        }
+                        // SYS.VSERVER("abcd").SNMP_TABLE(0).AVERAGE_VALUE.GT(80)
+                        int counterIndex = snmpMetrics.get(counterName); // TODO: temporary fix. later on counter name will be added as a param to SNMP_TABLE.
+                        formatter.format("SYS.VSERVER(\"%s\").SNMP_TABLE(%d).AVERAGE_VALUE.%s(%d)",nsVirtualServerName, counterIndex, operator, threshold);
                     }
-
-                    if(newMetric)
+                    else if (counterTO.getSource().equals("netscaler"))
                     {
-                        // bind lb metricTable lb_metric_table mem 1.3.6.1.4.1.2021.11.9.0
-                        String counterOid = counterTO.getValue();
-                        com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable_metric_binding metrictable_metric_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbmetrictable_metric_binding();
-                        try {
-                            metrictable_metric_binding.set_metrictable(mtName);
-                            metrictable_metric_binding.set_metric(counterName);
-                            metrictable_metric_binding.set_Snmpoid(counterOid);
-                            metrictable_metric_binding.add(_netscalerService, metrictable_metric_binding);
-                        } catch (Exception e) {
-                            // Ignore Exception
-                            throw e;
-                        }
-
-                        // bind lb monitor lb_metric_table_mon -metric cpu -metricThreshold 1
-                        com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_lbmetrictable_binding monitor_metrictable_binding = new com.citrix.netscaler.nitro.resource.config.lb.lbmonitor_lbmetrictable_binding();
-                        try {
-                            monitor_metrictable_binding.set_monitorname(monitorName);
-                            monitor_metrictable_binding.set_metric(counterName);
-                            monitor_metrictable_binding.set_metricthreshold(1); // 1 is a dummy threshold
-                            monitor_metrictable_binding.add(_netscalerService, monitor_metrictable_binding);
-                        } catch (Exception e) {
-                            // Ignore Exception
-                            throw e;
-                        }
+                        //SYS.VSERVER("abcd").RESPTIME.GT(10)
+                        formatter.format("SYS.VSERVER(\"%s\").%s.%s(%d)",nsVirtualServerName, counterTO.getValue(), operator, threshold);
                     }
-                    // SYS.VSERVER("abcd").SNMP_TABLE(0).AVERAGE_VALUE.GT(80)
-                    int counterIndex = snmpMetrics.get(counterName); // TODO: temporary fix. later on counter name will be added as a param to SNMP_TABLE.
-                    formatter.format("SYS.VSERVER(\"%s\").SNMP_TABLE(%d).AVERAGE_VALUE.%s(%d)",nsVirtualServerName, counterIndex, operator, threshold);
-                }
-                else if (counterTO.getSource().equals("netscaler"))
-                {
-                    //SYS.VSERVER("abcd").RESPTIME.GT(10)
-                    formatter.format("SYS.VSERVER(\"%s\").%s.%s(%d)",nsVirtualServerName, counterTO.getValue(), operator, threshold);
+                    if(policyExpression.length() != 0) {
+                        policyExpression += " && ";
+                    }
+                    policyExpression += conditionExpression;
                 }
-                if(policyExpression.length() != 0) {
-                    policyExpression += " && ";
+                policyExpression = "(" + policyExpression + ")";
+
+                String policyId = Long.toString(autoScalePolicyTO.getId());
+                String policyName = generateAutoScalePolicyName(srcIp, srcPort, policyId);
+                String action = null;
+                if(isScaleUpPolicy(autoScalePolicyTO)) {
+                    action = scaleUpActionName;
+                    String scaleUpCondition = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.LT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MAXAUTOSCALEMEMBERS)";
+                    policyExpression = scaleUpCondition + " && " + policyExpression;
+                } else {
+                    action = scaleDownActionName;
+                    String scaleDownCondition = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.GT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MINAUTOSCALEMEMBERS)";
+                    policyExpression = scaleDownCondition + " && " + policyExpression;
                 }
-                policyExpression += conditionExpression;
+
+                addAutoScalePolicy(timerName, policyName, cur_prirotiy++, policyExpression, action,
+                        autoScalePolicyTO.getDuration(), interval, isCleanUp);
+
             }
-            policyExpression = "(" + policyExpression + ")";
+        } catch (Exception ex) {
+            if(!isCleanUp) {
+                // Normal course, exception has occurred
+                disableAutoScaleConfig(loadBalancerTO, true);
+                throw ex;
 
-            String policyId = Long.toString(autoScalePolicyTO.getId());
-            String policyName = generateAutoScalePolicyName(srcIp, srcPort, policyId);
-            String action = null;
-            if(isScaleUpPolicy(autoScalePolicyTO)) {
-                action = scaleUpActionName;
-                String scaleUpCondition = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.LT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MAXAUTOSCALEMEMBERS)";
-                policyExpression = scaleUpCondition + " && " + policyExpression;
             } else {
-                action = scaleDownActionName;
-                String scaleDownCondition = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.GT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MINAUTOSCALEMEMBERS)";
-                policyExpression = scaleDownCondition + " && " + policyExpression;
+                // Programming error. Exception should never be thrown afterall.
+                throw ex;
             }
-
-            addAutoScalePolicy(timerName, policyName, cur_prirotiy++, policyExpression, action,
-                    autoScalePolicyTO.getDuration(), interval);
-
         }
 
         return true;
     }
 
-
     private synchronized void addAutoScalePolicy(String timerName,String policyName,  long priority, String policyExpression, String action,
-            int duration, int interval) throws Exception {
+            int duration, int interval, boolean isCleanUp) throws Exception {
         // Adding a autoscale policy
         // add timer policy lb_policy_scaleUp_cpu_mem -rule - (SYS.CUR_VSERVER.METRIC_TABLE(cpu).AVG_VAL.GT(80)-
         // -action lb_scaleUpAction
-        com.citrix.netscaler.nitro.resource.config.timer.timerpolicy timerPolicy = new com.citrix.netscaler.nitro.resource.config.timer.timerpolicy();
+        timerpolicy timerPolicy = new timerpolicy();
         try {
             timerPolicy.set_name(policyName);
             timerPolicy.set_action(action);
             timerPolicy.set_rule(policyExpression);
             timerPolicy.add(_netscalerService, timerPolicy);
         } catch (Exception e) {
-            // Ignore Exception
-            throw e;
+            // Ignore Exception on cleanup
+            if(!isCleanUp) throw e;
         }
 
         // bind timer policy
@@ -1975,7 +2017,7 @@ public class NetscalerResource implements ServerResource {
         // bind timer trigger lb_astimer -policyName lb_policy_scaleUp -vserver lb -priority 1 -samplesize 5
         // TODO: later bind to lbvserver. bind timer trigger lb_astimer -policyName lb_policy_scaleUp -vserver lb -priority 1 -samplesize 5
         // -thresholdsize 5
-        com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding timer_policy_binding = new com.citrix.netscaler.nitro.resource.config.timer.timertrigger_timerpolicy_binding();
+        timertrigger_timerpolicy_binding timer_policy_binding = new timertrigger_timerpolicy_binding();
         int sampleSize = duration/interval;
         try {
             timer_policy_binding.set_name(timerName);
@@ -1988,10 +2030,11 @@ public class NetscalerResource implements ServerResource {
             timer_policy_binding.set_priority(priority);
             timer_policy_binding.add(_netscalerService, timer_policy_binding);
         } catch (Exception e) {
-            // Ignore Exception
-            throw e;
+            // Ignore Exception on cleanup
+            if(!isCleanUp) throw e;
         }
     }
+
     public synchronized void applyAutoScaleConfig(LoadBalancerTO loadBalancer) throws Exception, ExecutionException {
 
         AutoScaleVmGroupTO vmGroupTO = loadBalancer.getAutoScaleVmGroupTO();
@@ -2007,11 +2050,11 @@ public class NetscalerResource implements ServerResource {
         }
         else if(vmGroupTO.getState().equals("enabled")) {
             assert !loadBalancer.isRevoked();
-            enableAutoScaleConfig(loadBalancer);
+            enableAutoScaleConfig(loadBalancer, false);
         }
         else if(vmGroupTO.getState().equals("disabled")) {
             assert !loadBalancer.isRevoked();
-            disableAutoScaleConfig(loadBalancer);
+            disableAutoScaleConfig(loadBalancer, false);
         } else {
             ///// This should never happen
             throw new ExecutionException("Unknown vmGroup State :" + vmGroupTO.getState());
@@ -2115,6 +2158,7 @@ public class NetscalerResource implements ServerResource {
     private String generateNSServiceName(String ip, long port) {
         return genObjectName("Cloud-Service", ip, port);
     }
+
     private String generateAutoScaleServiceGroupName(String srcIp, long srcPort) {
         return genObjectName("Cloud-AutoScaleServiceGroup", srcIp, srcPort);
     }


Mime
View raw message