You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by pr...@apache.org on 2012/11/16 11:00:31 UTC

[13/50] git commit: Autoscale:Changes for addressing AutoScale bugs CS-15930, CS-15931, CS-15934, CS-15935

Autoscale:Changes for addressing AutoScale bugs CS-15930, CS-15931, CS-15934, CS-15935


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

Branch: refs/heads/master
Commit: 3e32a29048b897dfa1b47d162352fd25151d5fa8
Parents: c1b4bc4
Author: Vijay venkatachalam <vi...@citrix.com>
Authored: Mon Sep 24 11:33:56 2012 +0530
Committer: Vijay Venkatachalam <vi...@citrix.com>
Committed: Fri Nov 16 10:56:58 2012 +0530

----------------------------------------------------------------------
 api/src/com/cloud/agent/api/to/LoadBalancerTO.java |   24 +-
 api/src/com/cloud/network/as/AutoScaleService.java |    3 +-
 .../com/cloud/network/lb/LoadBalancingRule.java    |   12 +-
 .../cloud/network/resource/NetscalerResource.java  |  447 ++++++++-------
 .../com/cloud/network/as/AutoScaleManagerImpl.java |   36 +-
 .../network/lb/LoadBalancingRulesManager.java      |    2 +-
 .../network/lb/LoadBalancingRulesManagerImpl.java  |   49 +-
 7 files changed, 309 insertions(+), 264 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/api/src/com/cloud/agent/api/to/LoadBalancerTO.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/agent/api/to/LoadBalancerTO.java b/api/src/com/cloud/agent/api/to/LoadBalancerTO.java
index 884c7c5..04f718e 100644
--- a/api/src/com/cloud/agent/api/to/LoadBalancerTO.java
+++ b/api/src/com/cloud/agent/api/to/LoadBalancerTO.java
@@ -49,7 +49,7 @@ public class LoadBalancerTO {
     final static int MAX_STICKINESS_POLICIES = 1;
 
     public LoadBalancerTO (Long id, String srcIp, int srcPort, String protocol, String algorithm, boolean revoked, boolean alreadyAdded, List<LbDestination> destinations) {
-        if(destinations == null) { // for autoscaleconfig destinations will be null;
+        if (destinations == null) { // for autoscaleconfig destinations will be null;
             destinations = new ArrayList<LbDestination>();
         }
         this.id = id;
@@ -70,7 +70,7 @@ public class LoadBalancerTO {
     public LoadBalancerTO (Long id, String srcIp, int srcPort, String protocol, String algorithm, boolean revoked, boolean alreadyAdded, List<LbDestination> arg_destinations, List<LbStickinessPolicy> stickinessPolicies) {
         this(id, srcIp, srcPort, protocol, algorithm, revoked, alreadyAdded, arg_destinations);
         this.stickinessPolicies = null;
-        if (stickinessPolicies != null && stickinessPolicies.size()>0) {
+        if (stickinessPolicies != null && stickinessPolicies.size() > 0) {
             this.stickinessPolicies = new StickinessPolicyTO[MAX_STICKINESS_POLICIES];
             int index = 0;
             for (LbStickinessPolicy stickinesspolicy : stickinessPolicies) {
@@ -186,7 +186,7 @@ public class LoadBalancerTO {
             return alreadyAdded;
         }
     }
-    public static class CounterTO implements Serializable{
+    public static class CounterTO implements Serializable {
         private final String name;
         private final String source;
         private final String value;
@@ -210,7 +210,7 @@ public class LoadBalancerTO {
         }
     }
 
-    public static class ConditionTO implements Serializable{
+    public static class ConditionTO implements Serializable {
         private final long threshold;
         private final String relationalOperator;
         private final CounterTO counter;
@@ -235,7 +235,7 @@ public class LoadBalancerTO {
         }
     }
 
-    public static class AutoScalePolicyTO implements Serializable{
+    public static class AutoScalePolicyTO implements Serializable {
         private final long id;
         private final int duration;
         private final int quietTime;
@@ -277,7 +277,7 @@ public class LoadBalancerTO {
         }
     }
 
-    public static class AutoScaleVmProfileTO implements Serializable{
+    public static class AutoScaleVmProfileTO implements Serializable {
         private final Long zoneId;
         private final Long domainId;
         private final Long serviceOfferingId;
@@ -350,7 +350,7 @@ public class LoadBalancerTO {
         }
     }
 
-    public static class AutoScaleVmGroupTO implements Serializable{
+    public static class AutoScaleVmGroupTO implements Serializable {
         private final int minMembers;
         private final int maxMembers;
         private final int memberPort;
@@ -358,8 +358,9 @@ public class LoadBalancerTO {
         private final List<AutoScalePolicyTO> policies;
         private final AutoScaleVmProfileTO profile;
         private final String state;
+        private final String currentState;
 
-        AutoScaleVmGroupTO(int minMembers, int maxMembers, int memberPort, int interval, List<AutoScalePolicyTO> policies, AutoScaleVmProfileTO profile, String state)
+        AutoScaleVmGroupTO(int minMembers, int maxMembers, int memberPort, int interval, List<AutoScalePolicyTO> policies, AutoScaleVmProfileTO profile, String state, String currentState)
         {
             this.minMembers = minMembers;
             this.maxMembers = maxMembers;
@@ -368,6 +369,7 @@ public class LoadBalancerTO {
             this.policies = policies;
             this.profile = profile;
             this.state = state;
+            this.currentState = currentState;
         }
 
         public int getMinMembers() {
@@ -397,6 +399,10 @@ public class LoadBalancerTO {
         public String getState() {
             return state;
         }
+
+        public String getCurrentState() {
+            return currentState;
+        }
     }
 
     public void setAutoScaleVmGroup(LbAutoScaleVmGroup lbAutoScaleVmGroup)
@@ -428,7 +434,7 @@ public class LoadBalancerTO {
 
         AutoScaleVmGroup autoScaleVmGroup = lbAutoScaleVmGroup.getVmGroup();
         autoScaleVmGroupTO = new AutoScaleVmGroupTO(autoScaleVmGroup.getMinMembers(), autoScaleVmGroup.getMaxMembers(), autoScaleVmGroup.getMemberPort(),
-                autoScaleVmGroup.getInterval(), autoScalePolicyTOs, autoScaleVmProfileTO, autoScaleVmGroup.getState());
+                autoScaleVmGroup.getInterval(), autoScalePolicyTOs, autoScaleVmProfileTO, autoScaleVmGroup.getState(), lbAutoScaleVmGroup.getCurrentState());
     }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/api/src/com/cloud/network/as/AutoScaleService.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/network/as/AutoScaleService.java b/api/src/com/cloud/network/as/AutoScaleService.java
index ef99512..c1ca804 100644
--- a/api/src/com/cloud/network/as/AutoScaleService.java
+++ b/api/src/com/cloud/network/as/AutoScaleService.java
@@ -32,6 +32,7 @@ import com.cloud.api.commands.UpdateAutoScalePolicyCmd;
 import com.cloud.api.commands.UpdateAutoScaleVmGroupCmd;
 import com.cloud.api.commands.UpdateAutoScaleVmProfileCmd;
 import com.cloud.exception.ResourceInUseException;
+import com.cloud.exception.ResourceUnavailableException;
 import com.cloud.network.as.AutoScalePolicy;
 import com.cloud.network.as.AutoScaleVmGroup;
 import com.cloud.network.as.AutoScaleVmProfile;
@@ -58,7 +59,7 @@ public interface AutoScaleService {
 
     AutoScaleVmGroup createAutoScaleVmGroup(CreateAutoScaleVmGroupCmd cmd);
 
-    boolean configureAutoScaleVmGroup(CreateAutoScaleVmGroupCmd cmd);
+    boolean configureAutoScaleVmGroup(CreateAutoScaleVmGroupCmd cmd) throws ResourceUnavailableException;
 
     boolean deleteAutoScaleVmGroup(long vmGroupId);
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/api/src/com/cloud/network/lb/LoadBalancingRule.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/network/lb/LoadBalancingRule.java b/api/src/com/cloud/network/lb/LoadBalancingRule.java
index 7775f89..ffa6cbc 100644
--- a/api/src/com/cloud/network/lb/LoadBalancingRule.java
+++ b/api/src/com/cloud/network/lb/LoadBalancingRule.java
@@ -27,7 +27,7 @@ import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.LoadBalancer;
 import com.cloud.utils.Pair;
 
-public class LoadBalancingRule implements FirewallRule, LoadBalancer{
+public class LoadBalancingRule implements FirewallRule, LoadBalancer {
     private LoadBalancer lb;
     private List<LbDestination> destinations;
     private List<LbStickinessPolicy> stickinessPolicies;
@@ -319,17 +319,19 @@ public class LoadBalancingRule implements FirewallRule, LoadBalancer{
         public String getCsUrl() {
             return csUrl;
         }
-    }
+        }
 
     public static class LbAutoScaleVmGroup {
         AutoScaleVmGroup vmGroup;
         private final List<LbAutoScalePolicy> policies;
         private final LbAutoScaleVmProfile profile;
+        private final String currentState;
 
-        public LbAutoScaleVmGroup(AutoScaleVmGroup vmGroup, List<LbAutoScalePolicy> policies, LbAutoScaleVmProfile profile) {
+        public LbAutoScaleVmGroup(AutoScaleVmGroup vmGroup, List<LbAutoScalePolicy> policies, LbAutoScaleVmProfile profile, String currentState) {
             this.vmGroup = vmGroup;
             this.policies = policies;
             this.profile = profile;
+            this.currentState = currentState;
         }
 
         public AutoScaleVmGroup getVmGroup() {
@@ -343,5 +345,9 @@ public class LoadBalancingRule implements FirewallRule, LoadBalancer{
         public LbAutoScaleVmProfile getProfile() {
             return profile;
         }
+
+        public String getCurrentState() {
+            return currentState;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/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 dbf7116..21486e6 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
@@ -237,8 +237,26 @@ public class NetscalerResource implements ServerResource {
         }
     }
 
+    private void logout() throws ExecutionException {
+        try {
+            if (!_isSdx) {
+                if (_netscalerService != null) {
+                    _netscalerService.logout();
+                }
+            } else {
+                if (_netscalerSdxService != null) {
+                    _netscalerSdxService.logout();
+                }
+            }
+        } catch (Exception e) {
+            // Ignore logout exceptions
+        }
+    }
+
     private void login() throws ExecutionException {
         try {
+            // If a previous session was open, log it out.
+            logout();
             if (!_isSdx) {
                 _netscalerService = new nitro_service(_ip, "https");
                 _netscalerService.set_credential(_username, _password);
@@ -252,7 +270,7 @@ public class NetscalerResource implements ServerResource {
                 _netscalerSdxService.set_credential(_username, _password);
                 com.citrix.sdx.nitro.resource.base.login login  = _netscalerSdxService.login();
                 if (login == null) {
-                    throw new ExecutionException ("Failed to log in to Netscaler device at " + _ip + " due to error " + apiCallResult.errorcode + " and message " + apiCallResult.message);
+                    throw new ExecutionException ("Failed to log in to Netscaler SDX device at " + _ip + " due to error " + apiCallResult.errorcode + " and message " + apiCallResult.message);
                 }
             }
         } catch (nitro_exception e) {
@@ -1416,9 +1434,9 @@ public class NetscalerResource implements ServerResource {
                 // set session persistence timeout
                 vserver.set_timeout(timeout);
             } else {
-                    // delete the LB stickyness policy
-                    vserver.set_persistencetype("NONE");
-                }
+                // delete the LB stickyness policy
+                vserver.set_persistencetype("NONE");
+            }
 
             if (vserverExisis) {
                 apiCallResult = lbvserver.update(_netscalerService,vserver);
@@ -1482,7 +1500,12 @@ public class NetscalerResource implements ServerResource {
             disableAutoScaleConfig(loadBalancer, false);
         } else {
             ///// This should never happen
-            throw new ExecutionException("Unknown vmGroup State :" + vmGroupTO.getState());
+            throw new ExecutionException("Unknown AutoScale Vm Group State :" + vmGroupTO.getState());
+        }
+        // AutoScale APIs are successful executed, now save the configuration.
+        saveConfiguration();
+        if (s_logger.isInfoEnabled()) {
+            s_logger.info("Successfully executed resource AutoScaleConfig");
         }
     }
 
@@ -1497,23 +1520,23 @@ public class NetscalerResource implements ServerResource {
         AutoScaleVmGroupTO vmGroupTO = loadBalancerTO.getAutoScaleVmGroupTO();
         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);
         if(!nsServiceGroupExists(serviceGroupName)) {
             // add servicegroup lb_autoscaleGroup -autoscale POLICY -memberPort 80
             int memberPort = vmGroupTO.getMemberPort();
-        try {
+            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;
-        }
+            } catch (Exception e) {
+                throw e;
+            }
         }
 
         if(!isServiceGroupBoundToVirtualServer(nsVirtualServerName, serviceGroupName)) {
@@ -1521,13 +1544,13 @@ public class NetscalerResource implements ServerResource {
             // bind lb vserver lb lb_autoscaleGroup
             lbvserver_servicegroup_binding vserver_servicegroup_binding = new lbvserver_servicegroup_binding();
 
-        try {
+            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;
-        }
+            } catch (Exception e) {
+                throw e;
+            }
         }
 
         // Create the autoscale config
@@ -1543,32 +1566,32 @@ public class NetscalerResource implements ServerResource {
         String nsVirtualServerName  = generateNSVirtualServerName(srcIp, srcPort);
         String serviceGroupName  = generateAutoScaleServiceGroupName(srcIp, srcPort);
 
-        if (loadBalancerTO.getAutoScaleVmGroupTO().getState().equals("enabled")) {
-        disableAutoScaleConfig(loadBalancerTO, false);
+        if (loadBalancerTO.getAutoScaleVmGroupTO().getCurrentState().equals("enabled")) {
+            disableAutoScaleConfig(loadBalancerTO, false);
         }
 
-        if(isServiceGroupBoundToVirtualServer(nsVirtualServerName, serviceGroupName)) {
-        // UnBind autoscale service group
-        // unbind lb vserver lb lb_autoscaleGroup
+        if (isServiceGroupBoundToVirtualServer(nsVirtualServerName, serviceGroupName)) {
+            // UnBind autoscale service group
+            // unbind 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.delete(_netscalerService, vserver_servicegroup_binding);
-        } catch (Exception e) {
-            throw e;
-        }
+            try {
+                vserver_servicegroup_binding.set_name(nsVirtualServerName);
+                vserver_servicegroup_binding.set_servicegroupname(serviceGroupName);
+                vserver_servicegroup_binding.delete(_netscalerService, vserver_servicegroup_binding);
+            } catch (Exception e) {
+                throw e;
+            }
         }
 
-        if(nsServiceGroupExists(serviceGroupName)) {
-        // Remove autoscale service group
-        com.citrix.netscaler.nitro.resource.config.basic.servicegroup serviceGroup = new com.citrix.netscaler.nitro.resource.config.basic.servicegroup();
-        try {
-            serviceGroup.set_servicegroupname(serviceGroupName);
-            serviceGroup.delete(_netscalerService, serviceGroup);
-        } catch (Exception e) {
-            throw e;
-        }
+        if (nsServiceGroupExists(serviceGroupName)) {
+            // Remove autoscale service group
+            com.citrix.netscaler.nitro.resource.config.basic.servicegroup serviceGroup = new com.citrix.netscaler.nitro.resource.config.basic.servicegroup();
+            try {
+                serviceGroup.set_servicegroupname(serviceGroupName);
+                serviceGroup.delete(_netscalerService, serviceGroup);
+            } catch (Exception e) {
+                throw e;
+            }
         }
 
         removeLBVirtualServer(nsVirtualServerName);
@@ -1599,181 +1622,181 @@ public class NetscalerResource implements ServerResource {
 
         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();
+            // 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 {
+            try {
                 vserver.set_name(nsVirtualServerName);
                 vserver.set_minautoscalemembers(minAutoScaleMembers);
                 vserver.set_maxautoscalemembers(maxAutoScaleMembers);
                 vserver.update(_netscalerService, vserver);
-        } catch (Exception e) {
+            } catch (Exception e) {
                 // Ignore Exception on cleanup
                 if(!isCleanUp) 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();
+            /* 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) {
+            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;
-        }
+            }
 
-        // Add Timer
+            // Add Timer
             nstimer timer = new nstimer();
-        try {
-            timer.set_name(timerName);
-            timer.set_interval(interval);
-            timer.add(_netscalerService, timer);
-        } catch (Exception e) {
+            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;
+            // 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() + "&")) +
+            // 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) {
+                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" + "&" +
+            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) {
+                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 */
+            /* 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,
+            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 */
+            /* 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,
+            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
+            /* 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) {
+                            try {
+                                metricTable.set_metrictable(mtName);
+                                metricTable.add(_netscalerService, metricTable);
+                            } 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% >
+                            // 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) {
+                            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;
-                        }
+                            }
 
                             // Bind monitor to servicegroup.
-                        // bind lb monitor lb_metric_table_mon lb_autoscaleGroup -passive
+                            // bind lb monitor lb_metric_table_mon lb_autoscaleGroup -passive
                             servicegroup_lbmonitor_binding servicegroup_monitor_binding = new servicegroup_lbmonitor_binding();
-                        try {
+                            try {
                                 servicegroup_monitor_binding.set_servicegroupname(serviceGroupName);
                                 servicegroup_monitor_binding.set_monitor_name(monitorName);
 
@@ -1782,35 +1805,35 @@ public class NetscalerResource implements ServerResource {
                                 servicegroup_monitor_binding.set_passive(true);
 
                                 servicegroup_lbmonitor_binding.add(_netscalerService, servicegroup_monitor_binding);
-                        } catch (Exception e) {
+                            } catch (Exception e) {
                                 // Ignore Exception on cleanup
                                 if(!isCleanUp) throw e;
+                            }
                         }
-                    }
 
-                    boolean newMetric = !snmpMetrics.containsKey(counterName);
-                    if(newMetric) {
-                        snmpMetrics.put(counterName, snmpCounterNumber++);
-                    }
+                        boolean newMetric = !snmpMetrics.containsKey(counterName);
+                        if(newMetric) {
+                            snmpMetrics.put(counterName, snmpCounterNumber++);
+                        }
 
-                    if(newMetric)
-                    {
-                        // bind lb metricTable lb_metric_table mem 1.3.6.1.4.1.2021.11.9.0
-                        String counterOid = counterTO.getValue();
+                        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) {
+                            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;
-                        }
+                            }
 
-                        // bind lb monitor lb_metric_table_mon -metric cpu -metricThreshold 1
+                            // bind lb monitor lb_metric_table_mon -metric cpu -metricThreshold 1
                             lbmonitor_metric_binding monitor_metric_binding = new lbmonitor_metric_binding();;
-                        try {
+                            try {
                                 monitor_metric_binding.set_monitorname(monitorName);
                                 monitor_metric_binding.set_metric(counterName);
                                 /*
@@ -1820,41 +1843,41 @@ public class NetscalerResource implements ServerResource {
                                  */
                                 monitor_metric_binding.set_metricthreshold(Integer.MAX_VALUE);
                                 monitor_metric_binding.add(_netscalerService, monitor_metric_binding);
-                        } catch (Exception e) {
+                            } catch (Exception e) {
                                 // Ignore Exception on cleanup
                                 if(!isCleanUp) throw e;
+                            }
                         }
-                    }
-                    // SYS.VSERVER("abcd").SNMP_TABLE(0).AVERAGE_VALUE.GT(80)
+                        // 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 += " && ";
+                        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;
                 }
-                policyExpression += conditionExpression;
-            }
-            policyExpression = "(" + policyExpression + ")";
+                policyExpression = "(" + policyExpression + ")";
 
-            String policyId = Long.toString(autoScalePolicyTO.getId());
+                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;
-            }
+                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;
+                }
 
-            addAutoScalePolicy(timerName, policyName, cur_prirotiy++, policyExpression, action,
+                addAutoScalePolicy(timerName, policyName, cur_prirotiy++, policyExpression, action,
                         autoScalePolicyTO.getDuration(), interval, isCleanUp);
 
             }
@@ -1968,7 +1991,7 @@ public class NetscalerResource implements ServerResource {
                 } catch (Exception e) {
                     // Ignore Exception on cleanup
                     if(!isCleanUp) throw e;
-        }
+                }
 
                 // Delete Monitor
                 // rm lb monitor lb_metric_table_mon
@@ -2068,7 +2091,7 @@ public class NetscalerResource implements ServerResource {
             if(!isCleanUp) throw e;
         }
 
-        }
+    }
 
 
     private boolean isAutoScaleSupportedInNetScaler() throws ExecutionException {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
index 0e5e81b..62b5e47 100644
--- a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
+++ b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java
@@ -56,6 +56,7 @@ import com.cloud.event.ActionEvent;
 import com.cloud.event.EventTypes;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.ResourceInUseException;
+import com.cloud.exception.ResourceUnavailableException;
 import com.cloud.network.LoadBalancerVO;
 import com.cloud.network.Network.Capability;
 import com.cloud.network.as.dao.AutoScalePolicyConditionMapDao;
@@ -708,20 +709,22 @@ public class AutoScaleManagerImpl<Type> implements AutoScaleManager, AutoScaleSe
     }
 
     @Override
-    public boolean configureAutoScaleVmGroup(CreateAutoScaleVmGroupCmd cmd) {
-        return configureAutoScaleVmGroup(cmd.getEntityId());
+    public boolean configureAutoScaleVmGroup(CreateAutoScaleVmGroupCmd cmd) throws ResourceUnavailableException {
+        return configureAutoScaleVmGroup(cmd.getEntityId(), AutoScaleVmGroup.State_New);
     }
 
     public boolean isLoadBalancerBasedAutoScaleVmGroup(AutoScaleVmGroup vmGroup) {
         return vmGroup.getLoadBalancerId() != null;
     }
 
-    public boolean configureAutoScaleVmGroup(long vmGroupid) {
+    private boolean configureAutoScaleVmGroup(long vmGroupid, String currentState) throws ResourceUnavailableException{
         AutoScaleVmGroup vmGroup = _autoScaleVmGroupDao.findById(vmGroupid);
 
         if (isLoadBalancerBasedAutoScaleVmGroup(vmGroup)) {
             try {
-                return _lbRulesMgr.configureLbAutoScaleVmGroup(vmGroupid);
+                return _lbRulesMgr.configureLbAutoScaleVmGroup(vmGroupid, currentState);
+            } catch (ResourceUnavailableException re) {
+                throw re;
             } catch (RuntimeException re) {
                 s_logger.warn("Exception during configureLbAutoScaleVmGrouop in lb rules manager", re);
             }
@@ -747,12 +750,14 @@ public class AutoScaleManagerImpl<Type> implements AutoScaleManager, AutoScaleSe
         boolean success = false;
 
         try {
-            success = configureAutoScaleVmGroup(id);
-        } finally {
+            success = configureAutoScaleVmGroup(id, bakupState);
+        } catch (ResourceUnavailableException e) {
+            autoScaleVmGroupVO.setState(bakupState);
+            _autoScaleVmGroupDao.persist(autoScaleVmGroupVO);
+        }
+        finally {
             if (!success) {
                 s_logger.warn("Could not delete AutoScale Vm Group id : " + id);
-                autoScaleVmGroupVO.setState(bakupState);
-                _autoScaleVmGroupDao.persist(autoScaleVmGroupVO);
                 return false;
             }
         }
@@ -942,12 +947,12 @@ public class AutoScaleManagerImpl<Type> implements AutoScaleManager, AutoScaleSe
         try {
             vmGroup.setState(AutoScaleVmGroup.State_Enabled);
             vmGroup = _autoScaleVmGroupDao.persist(vmGroup);
-            success = configureAutoScaleVmGroup(id);
-
+            success = configureAutoScaleVmGroup(id, AutoScaleVmGroup.State_Disabled);
+        } catch (ResourceUnavailableException e) {
+            vmGroup.setState(AutoScaleVmGroup.State_Disabled);
+            _autoScaleVmGroupDao.persist(vmGroup);
         } finally {
             if (!success) {
-                vmGroup.setState(AutoScaleVmGroup.State_Disabled);
-                _autoScaleVmGroupDao.persist(vmGroup);
                 s_logger.warn("Failed to enable AutoScale Vm Group id : " + id);
                 return null;
             }
@@ -969,11 +974,12 @@ public class AutoScaleManagerImpl<Type> implements AutoScaleManager, AutoScaleSe
         try {
             vmGroup.setState(AutoScaleVmGroup.State_Disabled);
             vmGroup = _autoScaleVmGroupDao.persist(vmGroup);
-            success = configureAutoScaleVmGroup(id);
+            success = configureAutoScaleVmGroup(id, AutoScaleVmGroup.State_Enabled);
+        } catch (ResourceUnavailableException e) {
+            vmGroup.setState(AutoScaleVmGroup.State_Enabled);
+            _autoScaleVmGroupDao.persist(vmGroup);
         } finally {
             if (!success) {
-                vmGroup.setState(AutoScaleVmGroup.State_Enabled);
-                _autoScaleVmGroupDao.persist(vmGroup);
                 s_logger.warn("Failed to disable AutoScale Vm Group id : " + id);
                 return null;
             }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java
index ebe4e2a..47f1b18 100644
--- a/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java
+++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java
@@ -46,5 +46,5 @@ public interface LoadBalancingRulesManager extends LoadBalancingRulesService {
 
     boolean applyLoadBalancersForNetwork(long networkId) throws ResourceUnavailableException;
     String getLBCapability(long networkid, String capabilityName);
-    boolean configureLbAutoScaleVmGroup(long vmGroupid);
+    boolean configureLbAutoScaleVmGroup(long vmGroupid, String currentState) throws ResourceUnavailableException;
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/3e32a290/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
index 9944384..df18c04 100755
--- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
+++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
@@ -243,7 +243,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
         }
         return null;
     }
-    private LbAutoScaleVmGroup getLbAutoScaleVmGroup(AutoScaleVmGroup vmGroup) {
+    private LbAutoScaleVmGroup getLbAutoScaleVmGroup(AutoScaleVmGroup vmGroup, String currentState) {
         List<AutoScaleVmGroupPolicyMapVO> vmGroupPolicyMapList = _autoScaleVmGroupPolicyMapDao.listByVmGroupId(vmGroup.getId());
         List<LbAutoScalePolicy> autoScalePolicies = new ArrayList<LbAutoScalePolicy>();
         for (AutoScaleVmGroupPolicyMapVO vmGroupPolicyMap : vmGroupPolicyMapList) {
@@ -264,23 +264,28 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
         String secretKey = user.getSecretKey();
         String csUrl = _configDao.getValue(Config.EndpointeUrl.key());
 
-        if(apiKey == null) {
+        if (apiKey == null) {
             throw new InvalidParameterValueException("apiKey for user: " + user.getUsername() + " is empty. Please generate it");
         }
 
-        if(secretKey == null) {
+        if (secretKey == null) {
             throw new InvalidParameterValueException("secretKey for user: " + user.getUsername() + " is empty. Please generate it");
         }
 
-        if(csUrl == null || csUrl.contains("localhost")) {
+        if (csUrl == null || csUrl.contains("localhost")) {
             throw new InvalidParameterValueException("Global setting endpointe.url has to be set to the Management Server's API end point");
         }
 
         LbAutoScaleVmProfile lbAutoScaleVmProfile = new LbAutoScaleVmProfile(autoScaleVmProfile, apiKey, secretKey, csUrl);
-        return new LbAutoScaleVmGroup(vmGroup, autoScalePolicies, lbAutoScaleVmProfile);
+        return new LbAutoScaleVmGroup(vmGroup, autoScalePolicies, lbAutoScaleVmProfile, currentState);
     }
 
-    private boolean applyAutoScaleConfig(LoadBalancerVO lb, LoadBalancingRule rule) throws ResourceUnavailableException {
+    private boolean applyAutoScaleConfig(LoadBalancerVO lb, AutoScaleVmGroupVO vmGroup, String currentState) throws ResourceUnavailableException {
+        LbAutoScaleVmGroup lbAutoScaleVmGroup = getLbAutoScaleVmGroup(vmGroup, currentState);
+        /* Regular config like destinations need not be packed for applying autoscale config as of today.*/
+        LoadBalancingRule rule = new LoadBalancingRule(lb, null, null);
+        rule.setAutoScaleVmGroup(lbAutoScaleVmGroup);
+
         if (!isRollBackAllowedForProvider(lb)) {
             // this is for Netscalar type of devices. if their is failure the db entries will be rollbacked.
             return false;
@@ -298,7 +303,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
 
     @Override
     @DB
-    public boolean configureLbAutoScaleVmGroup(long vmGroupid) {
+    public boolean configureLbAutoScaleVmGroup(long vmGroupid, String currentState) throws ResourceUnavailableException {
         AutoScaleVmGroupVO vmGroup = _autoScaleVmGroupDao.findById(vmGroupid);
         boolean success = false;
 
@@ -318,20 +323,18 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
 
         // LBTODO
         try {
-            LbAutoScaleVmGroup lbAutoScaleVmGroup = getLbAutoScaleVmGroup(vmGroup);
-            LoadBalancingRule rule = new LoadBalancingRule(loadBalancer, null, null);
-            rule.setAutoScaleVmGroup(lbAutoScaleVmGroup);
-            success = applyAutoScaleConfig(loadBalancer, rule);
+            success = applyAutoScaleConfig(loadBalancer, vmGroup, currentState);
         } catch (ResourceUnavailableException e) {
             s_logger.warn("Unable to configure AutoScaleVmGroup to the lb rule: " + loadBalancer.getId() + " because resource is unavaliable:", e);
+            if (isRollBackAllowedForProvider(loadBalancer)) {
+                loadBalancer.setState(backupState);
+                _lbDao.persist(loadBalancer);
+                s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating AutoscaleVmGroup");
+            }
+            throw e;
         } finally {
             if (!success) {
                 s_logger.warn("Failed to configure LB Auto Scale Vm Group with Id:" + vmGroupid);
-                if (isRollBackAllowedForProvider(loadBalancer)) {
-                    loadBalancer.setState(backupState);
-                    _lbDao.persist(loadBalancer);
-                    s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating AutoscaleVmGroup");
-                }
             }
         }
 
@@ -469,7 +472,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
 
         LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId());
         if (loadBalancer == null) {
-            throw new InvalidParameterException("Invalid Load balancer Id:"  + cmd.getLbRuleId());
+            throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId());
         }
         FirewallRule.State backupState = loadBalancer.getState();
         loadBalancer.setState(FirewallRule.State.Add);
@@ -481,7 +484,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
             if (isRollBackAllowedForProvider(loadBalancer)) {
                 loadBalancer.setState(backupState);
                 _lbDao.persist(loadBalancer);
-                s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating sticky policy" );
+                s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating sticky policy");
             }
             deleteLBStickinessPolicy(cmd.getEntityId(), false);
             success = false;
@@ -537,7 +540,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
                 s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
                 success = false;
             }
-        }else{
+        } else {
             _lb2stickinesspoliciesDao.remove(stickinessPolicy.getLoadBalancerId());
         }
 
@@ -647,7 +650,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
             s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
         }
 
-        if(!success){
+        if (!success) {
             CloudRuntimeException ex = new CloudRuntimeException("Failed to add specified loadbalancerruleid for vms " + instanceIds);
             ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId");
             // TBD: Also pack in the instanceIds in the exception using the right VO object or table name.
@@ -715,7 +718,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
             }
             s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
         }
-        if(!success){
+        if (!success) {
             CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + instanceIds);
             ex.addProxyObject(loadBalancer, loadBalancerId, "loadBalancerId");
             throw ex;
@@ -1303,7 +1306,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
                     if (lbBackup.getDescription() != null) {
                         lb.setDescription(lbBackup.getDescription());
                     }
-                    if (lbBackup.getAlgorithm() != null){
+                    if (lbBackup.getAlgorithm() != null) {
                         lb.setAlgorithm(lbBackup.getAlgorithm());
                     }
                     lb.setState(lbBackup.getState());
@@ -1455,7 +1458,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
 
         if (tags != null && !tags.isEmpty()) {
             SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
-            for (int count=0; count < tags.size(); count++) {
+            for (int count = 0; count < tags.size(); count++) {
                 tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
                 tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
                 tagSearch.cp();