cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject [2/3] Refactoring for the Load Balancing feature -
Date Tue, 02 Apr 2013 19:12:13 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java b/server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
index 45a8068..43cd537 100644
--- a/server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
+++ b/server/src/com/cloud/network/dao/FirewallRulesDaoImpl.java
@@ -31,7 +31,6 @@ import com.cloud.network.rules.FirewallRule.TrafficType;
 import com.cloud.network.rules.FirewallRuleVO;
 import com.cloud.server.ResourceTag.TaggedResourceType;
 import com.cloud.tags.dao.ResourceTagDao;
-import com.cloud.tags.dao.ResourceTagsDaoImpl;
 import com.cloud.utils.db.DB;
 import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.GenericSearchBuilder;
@@ -111,18 +110,6 @@ public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> i
     }
 
     @Override
-    public boolean releasePorts(long ipId, String protocol, FirewallRule.Purpose purpose, int[] ports) {
-        SearchCriteria<FirewallRuleVO> sc = ReleaseSearch.create();
-        sc.setParameters("protocol", protocol);
-        sc.setParameters("ipId", ipId);
-        sc.setParameters("purpose", purpose);
-        sc.setParameters("ports", ports);
-
-        int results = remove(sc);
-        return results == ports.length;
-    }
-
-    @Override
     public List<FirewallRuleVO> listByIpAndPurpose(long ipId, FirewallRule.Purpose purpose) {
         SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
         sc.setParameters("ipId", ipId);
@@ -192,12 +179,6 @@ public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> i
     }
 
     @Override
-    public boolean revoke(FirewallRuleVO rule) {
-        rule.setState(State.Revoke);
-        return update(rule.getId(), rule);
-    }
-
-    @Override
     public List<FirewallRuleVO> listStaticNatByVmId(long vmId) {
         if (VmSearch == null) {
             SearchBuilder<IPAddressVO> IpSearch = _ipDao.createSearchBuilder();
@@ -313,9 +294,7 @@ public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> i
         txn.start();
         FirewallRuleVO entry = findById(id);
         if (entry != null) {
-            if (entry.getPurpose() == Purpose.LoadBalancing) {
-                _tagsDao.removeByIdAndType(id, TaggedResourceType.LoadBalancer);
-            } else if (entry.getPurpose() == Purpose.PortForwarding) {
+            if (entry.getPurpose() == Purpose.PortForwarding) {
                 _tagsDao.removeByIdAndType(id, TaggedResourceType.PortForwardingRule);
             } else if (entry.getPurpose() == Purpose.Firewall) {
                 _tagsDao.removeByIdAndType(id, TaggedResourceType.FirewallRule);
@@ -329,22 +308,6 @@ public class FirewallRulesDaoImpl extends GenericDaoBase<FirewallRuleVO, Long> i
     }
 
     @Override
-    public List<FirewallRuleVO> listByIpAndPurposeWithState(Long ipId, Purpose purpose, State state) {
-        SearchCriteria<FirewallRuleVO> sc = AllFieldsSearch.create();
-        sc.setParameters("ipId", ipId);
-
-        if (state != null) {
-            sc.setParameters("state", state);
-        }
-
-        if (purpose != null) {
-            sc.setParameters("purpose", purpose);
-        }
-
-        return listBy(sc);
-    }
-
-    @Override
     public void loadSourceCidrs(FirewallRuleVO rule) {
         List<String> sourceCidrs = _firewallRulesCidrsDao.getSourceCidrs(rule.getId());
         rule.setSourceCidrList(sourceCidrs);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/dao/LoadBalancerDao.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/dao/LoadBalancerDao.java b/server/src/com/cloud/network/dao/LoadBalancerDao.java
index 611282e..82ada2b 100644
--- a/server/src/com/cloud/network/dao/LoadBalancerDao.java
+++ b/server/src/com/cloud/network/dao/LoadBalancerDao.java
@@ -18,19 +18,23 @@ package com.cloud.network.dao;
 
 import java.util.List;
 
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.utils.db.GenericDao;
 
 public interface LoadBalancerDao extends GenericDao<LoadBalancerVO, Long> {
-    List<Long> listInstancesByLoadBalancer(long loadBalancerId);
-
+    
     List<LoadBalancerVO> listByIpAddress(long ipAddressId);
 
-    LoadBalancerVO findByIpAddressAndPublicPort(long ipAddressId, String publicPort);
-
-    LoadBalancerVO findByAccountAndName(Long accountId, String name);
-
     List<LoadBalancerVO> listByNetworkId(long networkId);
 
     List<LoadBalancerVO> listInTransitionStateByNetworkId(long networkId);
+    
+    boolean setStateToAdd(LoadBalancerVO rule);
+    
+    List<LoadBalancerVO> listByIpAndNotRevoked(long ipAddressId);
+    
+    List<LoadBalancerVO> listByNetworkNotRevoked(long networkId);
+    
+    List<LoadBalancerVO> listByIpAndState(Long addressId, LoadBalancer.State state);
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/dao/LoadBalancerDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/dao/LoadBalancerDaoImpl.java b/server/src/com/cloud/network/dao/LoadBalancerDaoImpl.java
index f211a7f..49a2e63 100644
--- a/server/src/com/cloud/network/dao/LoadBalancerDaoImpl.java
+++ b/server/src/com/cloud/network/dao/LoadBalancerDaoImpl.java
@@ -16,18 +16,18 @@
 // under the License.
 package com.cloud.network.dao;
 
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.ArrayList;
 import java.util.List;
 
 import javax.ejb.Local;
 import javax.inject.Inject;
 
-import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
 import com.cloud.network.rules.FirewallRule.State;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.server.ResourceTag.TaggedResourceType;
+import com.cloud.tags.dao.ResourceTagDao;
+import com.cloud.utils.db.DB;
 import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
@@ -37,37 +37,33 @@ import com.cloud.utils.db.Transaction;
 @Component
 @Local(value = { LoadBalancerDao.class })
 public class LoadBalancerDaoImpl extends GenericDaoBase<LoadBalancerVO, Long> implements LoadBalancerDao {
-    private static final Logger s_logger = Logger.getLogger(LoadBalancerDaoImpl.class);
-    private static final String LIST_INSTANCES_BY_LOAD_BALANCER = "SELECT vm.id " +
-            "    FROM vm_instance vm, load_balancer lb, ip_forwarding fwd, user_ip_address ip " +
-            "    WHERE lb.id = ? AND " +
-            "          fwd.group_id = lb.id AND " +
-            "          fwd.forwarding = 0 AND " +
-            "          fwd.private_ip_address = vm.private_ip_address AND " +
-            "          lb.ip_address = ip.public_ip_address AND " +
-            "          ip.data_center_id = vm.data_center_id ";
     private final SearchBuilder<LoadBalancerVO> ListByIp;
-    private final SearchBuilder<LoadBalancerVO> IpAndPublicPortSearch;
-    private final SearchBuilder<LoadBalancerVO> AccountAndNameSearch;
     protected final SearchBuilder<LoadBalancerVO> TransitionStateSearch;
+    protected final SearchBuilder<LoadBalancerVO> AllFieldsSearch;
+    protected final SearchBuilder<LoadBalancerVO> NotRevokedSearch;
+    @Inject ResourceTagDao _tagsDao;
 
     @Inject protected FirewallRulesCidrsDao _portForwardingRulesCidrsDao;
 
     protected LoadBalancerDaoImpl() {
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("ipAddressId", AllFieldsSearch.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.done();
+        
+        NotRevokedSearch = createSearchBuilder();
+        NotRevokedSearch.and("ipId", NotRevokedSearch.entity().getSourceIpAddressId(), Op.EQ);
+        NotRevokedSearch.and("networkId", NotRevokedSearch.entity().getNetworkId(), Op.EQ);
+        NotRevokedSearch.and("state", NotRevokedSearch.entity().getState(), Op.NEQ);
+        NotRevokedSearch.done();
+        
+        
         ListByIp = createSearchBuilder();
         ListByIp.and("ipAddressId", ListByIp.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ);
         ListByIp.and("networkId", ListByIp.entity().getNetworkId(), SearchCriteria.Op.EQ);
         ListByIp.done();
-
-        IpAndPublicPortSearch = createSearchBuilder();
-        IpAndPublicPortSearch.and("ipAddressId", IpAndPublicPortSearch.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ);
-        IpAndPublicPortSearch.and("publicPort", IpAndPublicPortSearch.entity().getSourcePortStart(), SearchCriteria.Op.EQ);
-        IpAndPublicPortSearch.done();
-
-        AccountAndNameSearch = createSearchBuilder();
-        AccountAndNameSearch.and("accountId", AccountAndNameSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
-        AccountAndNameSearch.and("name", AccountAndNameSearch.entity().getName(), SearchCriteria.Op.EQ);
-        AccountAndNameSearch.done();
+        
 
         TransitionStateSearch = createSearchBuilder();
         TransitionStateSearch.and("networkId", TransitionStateSearch.entity().getNetworkId(), Op.EQ);
@@ -75,26 +71,6 @@ public class LoadBalancerDaoImpl extends GenericDaoBase<LoadBalancerVO, Long> im
         TransitionStateSearch.done();
     }
 
-    @Override
-    public List<Long> listInstancesByLoadBalancer(long loadBalancerId) {
-        Transaction txn = Transaction.currentTxn();
-        String sql = LIST_INSTANCES_BY_LOAD_BALANCER;
-        PreparedStatement pstmt = null;
-        List<Long> instanceList = new ArrayList<Long>();
-        try {
-            pstmt = txn.prepareAutoCloseStatement(sql);
-            pstmt.setLong(1, loadBalancerId);
-
-            ResultSet rs = pstmt.executeQuery();
-            while (rs.next()) {
-                Long vmId = rs.getLong(1);
-                instanceList.add(vmId);
-            }
-        } catch (Exception ex) {
-            s_logger.error("error getting recent usage network stats", ex);
-        }
-        return instanceList;
-    }
 
     @Override
     public List<LoadBalancerVO> listByIpAddress(long ipAddressId) {
@@ -109,28 +85,66 @@ public class LoadBalancerDaoImpl extends GenericDaoBase<LoadBalancerVO, Long> im
         sc.setParameters("networkId", networkId);
         return listBy(sc);
     }
+    
 
     @Override
-    public LoadBalancerVO findByIpAddressAndPublicPort(long ipAddressId, String publicPort) {
-        SearchCriteria<LoadBalancerVO> sc = IpAndPublicPortSearch.create();
-        sc.setParameters("ipAddressId", ipAddressId);
-        sc.setParameters("publicPort", publicPort);
-        return findOneBy(sc);
+    public List<LoadBalancerVO> listInTransitionStateByNetworkId(long networkId) {
+        SearchCriteria<LoadBalancerVO> sc = TransitionStateSearch.create();
+        sc.setParameters("networkId", networkId);
+        sc.setParameters("state", State.Add.toString(), State.Revoke.toString());
+        return listBy(sc);
     }
-
+    
     @Override
-    public LoadBalancerVO findByAccountAndName(Long accountId, String name) {
-        SearchCriteria<LoadBalancerVO> sc = AccountAndNameSearch.create();
-        sc.setParameters("accountId", accountId);
-        sc.setParameters("name", name);
-        return findOneBy(sc);
+    public boolean setStateToAdd(LoadBalancerVO rule) {
+        SearchCriteria<LoadBalancerVO> sc = AllFieldsSearch.create();
+        sc.setParameters("id", rule.getId());
+        sc.setParameters("state", LoadBalancer.State.Staged);
+
+        rule.setState(LoadBalancer.State.Add);
+
+        return update(rule, sc) > 0;
     }
+    
+    
+    @Override
+    public List<LoadBalancerVO> listByIpAndNotRevoked(long ipId) {
+        SearchCriteria<LoadBalancerVO> sc = NotRevokedSearch.create();
+        sc.setParameters("ipId", ipId);
+        sc.setParameters("state", State.Revoke);
 
+        return listBy(sc);
+    }
+    
     @Override
-    public List<LoadBalancerVO> listInTransitionStateByNetworkId(long networkId) {
-        SearchCriteria<LoadBalancerVO> sc = TransitionStateSearch.create();
+    @DB
+    public boolean remove(Long id) {
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        _tagsDao.removeByIdAndType(id, TaggedResourceType.LoadBalancer);
+        boolean result = super.remove(id);
+        txn.commit();
+        return result;
+    }
+    
+    @Override
+    public List<LoadBalancerVO> listByNetworkNotRevoked(long networkId) {
+        SearchCriteria<LoadBalancerVO> sc = NotRevokedSearch.create();
         sc.setParameters("networkId", networkId);
-        sc.setParameters("state", State.Add.toString(), State.Revoke.toString());
+        sc.setParameters("state", State.Revoke);
+
+        return listBy(sc);
+    }
+
+    @Override
+    public List<LoadBalancerVO> listByIpAndState(Long addressId, LoadBalancer.State state) {
+        SearchCriteria<LoadBalancerVO> sc = AllFieldsSearch.create();
+        sc.setParameters("ipId", addressId);
+
+        if (state != null) {
+            sc.setParameters("state", state);
+        }
+
         return listBy(sc);
     }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/dao/LoadBalancerVO.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/dao/LoadBalancerVO.java b/server/src/com/cloud/network/dao/LoadBalancerVO.java
index 5422f41..65b3b64 100644
--- a/server/src/com/cloud/network/dao/LoadBalancerVO.java
+++ b/server/src/com/cloud/network/dao/LoadBalancerVO.java
@@ -16,21 +16,33 @@
 // under the License.
 package com.cloud.network.dao;
 
+import java.util.Date;
+import java.util.UUID;
+
 import javax.persistence.Column;
-import javax.persistence.DiscriminatorValue;
 import javax.persistence.Entity;
-import javax.persistence.PrimaryKeyJoinColumn;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
 import javax.persistence.Table;
 
-import com.cloud.network.rules.FirewallRuleVO;
 import com.cloud.network.rules.LoadBalancer;
+import com.cloud.utils.db.GenericDao;
 import com.cloud.utils.net.NetUtils;
 
 @Entity
 @Table(name=("load_balancing_rules"))
-@DiscriminatorValue(value="LoadBalancing")
-@PrimaryKeyJoinColumn(name="id")
-public class LoadBalancerVO extends FirewallRuleVO implements LoadBalancer {
+public class LoadBalancerVO implements LoadBalancer {
+    
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    @Column(name="id")
+    long id;
+    
+    @Column(name="uuid")
+    String uuid;
     
     @Column(name="name")
     private String name;
@@ -46,19 +58,81 @@ public class LoadBalancerVO extends FirewallRuleVO implements LoadBalancer {
     
     @Column(name="default_port_end")
     private int defaultPortEnd;
+    
+    @Column(name="start_port", updatable=false)
+    Integer sourcePortStart;
+
+    @Column(name="end_port", updatable=false)
+    Integer sourcePortEnd;
+    
+    @Column(name="network_id")
+    Long networkId;
+    
+    @Enumerated(value=EnumType.STRING)
+    @Column(name="state")
+    LoadBalancer.State state;
+    
+    @Column(name="ip_address_id", updatable=false)
+    Long sourceIpAddressId;
+    
+    @Column(name="domain_id", updatable=false)
+    long domainId;
+
+    @Column(name="account_id", updatable=false)
+    long accountId;
+    
+    @Column(name="protocol", updatable=false)
+    String protocol = NetUtils.TCP_PROTO;
+    
+    @Column(name=GenericDao.CREATED_COLUMN)
+    Date created;
+
+
+    public Integer getSourcePortStart() {
+        return sourcePortStart;
+    }
+
+    public Integer getSourcePortEnd() {
+        return sourcePortEnd;
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public long getDomainId() {
+        return domainId;
+    }
+
+    public long getAccountId() {
+        return accountId;
+    }
 
     public LoadBalancerVO() { 
     }
 
-    public LoadBalancerVO(String xId, String name, String description, long srcIpId, int srcPort, int dstPort, String algorithm, long networkId, long accountId, long domainId) {
-        super(xId, srcIpId, srcPort, NetUtils.TCP_PROTO, networkId, accountId, domainId, Purpose.LoadBalancing, null, null, null, null);
+    public LoadBalancerVO(String name, String description, long srcIpId, int srcPort, int dstPort, String algorithm,
+            long networkId, long accountId, long domainId) {
         this.name = name;
         this.description = description;
         this.algorithm = algorithm;
         this.defaultPortStart = dstPort;
         this.defaultPortEnd = dstPort;
+        this.sourcePortStart = srcPort;
+        this.sourcePortEnd = srcPort;
+        this.state = State.Staged;
+        this.networkId = networkId;
+        this.accountId = accountId;
+        this.domainId = domainId;
+        this.uuid = UUID.randomUUID().toString();
+        this.sourceIpAddressId = srcIpId;
+
     }
     
+    public void setState(LoadBalancer.State state) {
+        this.state = state;
+    }
+
     @Override
     public String getName() {
         return name;
@@ -95,4 +169,29 @@ public class LoadBalancerVO extends FirewallRuleVO implements LoadBalancer {
     public void setDescription(String description) {
         this.description = description;
     }  
+    
+    @Override
+    public String getUuid() {
+        return this.uuid;
+    }
+    
+    @Override
+    public long getNetworkId() {
+        return networkId;
+    }
+    
+    @Override
+    public String getProtocol() {
+        return protocol;
+    }
+    
+    @Override
+    public Long getSourceIpAddressId() {
+        return sourceIpAddressId;
+    }
+    
+    @Override
+    public State getState() {
+        return state;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/element/VirtualRouterElement.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/element/VirtualRouterElement.java b/server/src/com/cloud/network/element/VirtualRouterElement.java
index 169db32..130fc77 100755
--- a/server/src/com/cloud/network/element/VirtualRouterElement.java
+++ b/server/src/com/cloud/network/element/VirtualRouterElement.java
@@ -25,7 +25,6 @@ import java.util.Set;
 import javax.ejb.Local;
 import javax.inject.Inject;
 
-import com.cloud.utils.PropertiesUtil;
 import org.apache.cloudstack.api.command.admin.router.ConfigureVirtualRouterElementCmd;
 import org.apache.cloudstack.api.command.admin.router.CreateVirtualRouterElementCmd;
 import org.apache.cloudstack.api.command.admin.router.ListVirtualRouterElementsCmd;
@@ -358,7 +357,7 @@ public class VirtualRouterElement extends AdapterBase implements VirtualRouterEl
                 return true;
             }
 
-            if (!_routerMgr.applyFirewallRules(network, rules, routers)) {
+            if (!_routerMgr.applyLbRules(network, rules, routers)) {
                 throw new CloudRuntimeException("Failed to apply firewall rules in network " + network.getId());
             } else {
                 return true;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
index 080f7b0..bd525e1 100644
--- a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
+++ b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
@@ -27,17 +27,11 @@ import javax.ejb.Local;
 import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
+import org.apache.cloudstack.api.command.user.firewall.ListEgressFirewallRulesCmd;
 import org.apache.cloudstack.api.command.user.firewall.ListFirewallRulesCmd;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
-import com.mysql.jdbc.ConnectionPropertiesImpl;
-import org.apache.log4j.Logger;
-
-import org.apache.cloudstack.api.BaseListCmd;
-import org.apache.cloudstack.api.command.user.firewall.ListEgressFirewallRulesCmd;
-import org.apache.cloudstack.api.command.user.firewall.ListFirewallRulesCmd;
-import com.cloud.configuration.Config;
 import com.cloud.configuration.dao.ConfigurationDao;
 import com.cloud.domain.dao.DomainDao;
 import com.cloud.event.ActionEvent;
@@ -52,7 +46,6 @@ import com.cloud.network.IpAddress;
 import com.cloud.network.Network;
 import com.cloud.network.Network.Capability;
 import com.cloud.network.Network.Service;
-import com.cloud.network.Networks.TrafficType;
 import com.cloud.network.NetworkManager;
 import com.cloud.network.NetworkModel;
 import com.cloud.network.NetworkRuleApplier;
@@ -64,10 +57,15 @@ import com.cloud.network.element.FirewallServiceProvider;
 import com.cloud.network.element.NetworkACLServiceProvider;
 import com.cloud.network.element.PortForwardingServiceProvider;
 import com.cloud.network.element.StaticNatServiceProvider;
-import com.cloud.network.rules.*;
+import com.cloud.network.rules.FirewallManager;
+import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.FirewallRule.FirewallRuleType;
 import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.FirewallRule.State;
+import com.cloud.network.rules.FirewallRuleVO;
+import com.cloud.network.rules.PortForwardingRule;
+import com.cloud.network.rules.PortForwardingRuleVO;
+import com.cloud.network.rules.StaticNat;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
 import com.cloud.network.vpc.VpcManager;
 import com.cloud.projects.Project.ListProjectResourcesCriteria;
@@ -86,8 +84,8 @@ import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
-import com.cloud.utils.db.*;
 import com.cloud.utils.db.SearchCriteria.Op;
+import com.cloud.utils.db.Transaction;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.net.NetUtils;
 import com.cloud.vm.UserVmVO;
@@ -136,13 +134,9 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
 
     @Inject List<NetworkACLServiceProvider> _networkAclElements;
 
-    private boolean _elbEnabled = false;
-
     @Override
     public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
         _name = name;
-        String elbEnabledString = _configDao.getValue(Config.ElasticLoadBalancerEnabled.key());
-        _elbEnabled = Boolean.parseBoolean(elbEnabledString);
         s_logger.info("Firewall provider list is " + _firewallElements.iterator().next());
         return true;
     }
@@ -441,8 +435,8 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
             networkId = ipAddress.getAssociatedWithNetworkId();
         }
 
-            // Validate ip address
-            _accountMgr.checkAccess(caller, null, true, ipAddress);
+        // Validate ip address
+        _accountMgr.checkAccess(caller, null, true, ipAddress);
 
         Network network = _networkModel.getNetwork(networkId);
         assert network != null : "Can't create port forwarding rule as network associated with public ip address is null?";
@@ -454,11 +448,7 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
         // Verify that the network guru supports the protocol specified
         Map<Network.Capability, String> caps = null;
 
-        if (purpose == Purpose.LoadBalancing) {
-            if (!_elbEnabled) {
-                caps = _networkModel.getNetworkServiceCapabilities(network.getId(), Service.Lb);
-            }
-        } else if (purpose == Purpose.PortForwarding) {
+        if (purpose == Purpose.PortForwarding) {
             caps = _networkModel.getNetworkServiceCapabilities(network.getId(), Service.PortForwarding);
             }else if (purpose == Purpose.Firewall){
                 caps = _networkModel.getNetworkServiceCapabilities(network.getId(),Service.Firewall);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/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 d98872a..0d2b454 100644
--- a/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java
+++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManager.java
@@ -16,19 +16,18 @@
 // under the License.
 package com.cloud.network.lb;
 
+import java.util.List;
+
+import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
+
 import com.cloud.exception.NetworkRuleConflictException;
 import com.cloud.exception.ResourceUnavailableException;
-import com.cloud.network.Network;
 import com.cloud.network.lb.LoadBalancingRule.LbDestination;
 import com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy;
 import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy;
-import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.LbStickinessMethod;
 import com.cloud.network.rules.LoadBalancer;
 import com.cloud.user.Account;
-import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
-
-import java.util.List;
 
 public interface LoadBalancingRulesManager extends LoadBalancingRulesService {
 
@@ -47,7 +46,6 @@ public interface LoadBalancingRulesManager extends LoadBalancingRulesService {
      * @return true if removal is successful
      */
     boolean removeVmFromLoadBalancers(long vmId);
-    boolean applyRules(Network network, FirewallRule.Purpose purpose, List<? extends FirewallRule> rules) throws ResourceUnavailableException ;
     boolean applyLoadBalancersForNetwork(long networkId) throws ResourceUnavailableException;
     String getLBCapability(long networkid, String capabilityName);
     boolean configureLbAutoScaleVmGroup(long vmGroupid, String currentState) throws ResourceUnavailableException;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/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 7ad1070..cb85ce1 100755
--- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
+++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
@@ -16,6 +16,34 @@
 // under the License.
 package com.cloud.network.lb;
 
+import java.security.InvalidParameterException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.command.user.loadbalancer.CreateLBHealthCheckPolicyCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.CreateLBStickinessPolicyCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.CreateLoadBalancerRuleCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.ListLBHealthCheckPoliciesCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.ListLBStickinessPoliciesCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.ListLoadBalancerRuleInstancesCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.ListLoadBalancerRulesCmd;
+import org.apache.cloudstack.api.command.user.loadbalancer.UpdateLoadBalancerRuleCmd;
+import org.apache.cloudstack.api.response.ServiceResponse;
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
 import com.cloud.agent.api.to.LoadBalancerTO;
 import com.cloud.configuration.Config;
 import com.cloud.configuration.ConfigurationManager;
@@ -30,21 +58,65 @@ import com.cloud.event.EventTypes;
 import com.cloud.event.UsageEventUtils;
 import com.cloud.event.dao.EventDao;
 import com.cloud.event.dao.UsageEventDao;
-import com.cloud.exception.*;
-import com.cloud.network.*;
+import com.cloud.exception.InsufficientAddressCapacityException;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.PermissionDeniedException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.ExternalLoadBalancerUsageManager;
+import com.cloud.network.IpAddress;
+import com.cloud.network.LBHealthCheckPolicyVO;
+import com.cloud.network.Network;
 import com.cloud.network.Network.Capability;
 import com.cloud.network.Network.Provider;
 import com.cloud.network.Network.Service;
-import com.cloud.network.as.*;
+import com.cloud.network.NetworkManager;
+import com.cloud.network.NetworkModel;
+import com.cloud.network.addr.PublicIp;
+import com.cloud.network.as.AutoScalePolicy;
+import com.cloud.network.as.AutoScalePolicyConditionMapVO;
+import com.cloud.network.as.AutoScaleVmGroup;
+import com.cloud.network.as.AutoScaleVmGroupPolicyMapVO;
+import com.cloud.network.as.AutoScaleVmGroupVO;
+import com.cloud.network.as.AutoScaleVmProfile;
 import com.cloud.network.as.Condition;
-import com.cloud.network.as.dao.*;
-import com.cloud.network.dao.*;
+import com.cloud.network.as.Counter;
+import com.cloud.network.as.dao.AutoScalePolicyConditionMapDao;
+import com.cloud.network.as.dao.AutoScalePolicyDao;
+import com.cloud.network.as.dao.AutoScaleVmGroupDao;
+import com.cloud.network.as.dao.AutoScaleVmGroupPolicyMapDao;
+import com.cloud.network.as.dao.AutoScaleVmProfileDao;
+import com.cloud.network.as.dao.ConditionDao;
+import com.cloud.network.as.dao.CounterDao;
+import com.cloud.network.dao.FirewallRulesDao;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.network.dao.IPAddressVO;
+import com.cloud.network.dao.LBHealthCheckPolicyDao;
+import com.cloud.network.dao.LBStickinessPolicyDao;
+import com.cloud.network.dao.LBStickinessPolicyVO;
+import com.cloud.network.dao.LoadBalancerDao;
+import com.cloud.network.dao.LoadBalancerVMMapDao;
+import com.cloud.network.dao.LoadBalancerVMMapVO;
+import com.cloud.network.dao.LoadBalancerVO;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkServiceMapDao;
+import com.cloud.network.dao.NetworkVO;
 import com.cloud.network.element.LoadBalancingServiceProvider;
-import com.cloud.network.lb.LoadBalancingRule.*;
-import com.cloud.network.rules.*;
-import com.cloud.network.rules.FirewallRule.FirewallRuleType;
-import com.cloud.network.rules.FirewallRule.Purpose;
+import com.cloud.network.lb.LoadBalancingRule.LbAutoScalePolicy;
+import com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmGroup;
+import com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmProfile;
+import com.cloud.network.lb.LoadBalancingRule.LbCondition;
+import com.cloud.network.lb.LoadBalancingRule.LbDestination;
+import com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy;
+import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy;
+import com.cloud.network.rules.FirewallManager;
+import com.cloud.network.rules.FirewallRuleVO;
+import com.cloud.network.rules.HealthCheckPolicy;
+import com.cloud.network.rules.LbStickinessMethod;
 import com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.network.rules.RulesManager;
+import com.cloud.network.rules.StickinessPolicy;
 import com.cloud.network.vpc.VpcManager;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.projects.Project.ListProjectResourcesCriteria;
@@ -53,38 +125,38 @@ import com.cloud.service.dao.ServiceOfferingDao;
 import com.cloud.storage.dao.VMTemplateDao;
 import com.cloud.tags.ResourceTagVO;
 import com.cloud.tags.dao.ResourceTagDao;
-import com.cloud.user.*;
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import com.cloud.user.DomainService;
+import com.cloud.user.User;
+import com.cloud.user.UserContext;
 import com.cloud.user.dao.AccountDao;
 import com.cloud.user.dao.UserDao;
 import com.cloud.uservm.UserVm;
 import com.cloud.utils.Pair;
 import com.cloud.utils.Ternary;
 import com.cloud.utils.component.ManagerBase;
-import com.cloud.utils.db.*;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.net.NetUtils;
 import com.cloud.vm.Nic;
 import com.cloud.vm.UserVmVO;
+import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VirtualMachine.State;
 import com.cloud.vm.dao.NicDao;
 import com.cloud.vm.dao.UserVmDao;
 import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
-import org.apache.cloudstack.api.ApiConstants;
-import org.apache.cloudstack.api.command.user.loadbalancer.*;
-import org.apache.cloudstack.api.response.ServiceResponse;
-import org.apache.log4j.Logger;
-import org.springframework.stereotype.Component;
-
-import javax.ejb.Local;
-import javax.inject.Inject;
-import java.security.InvalidParameterException;
-import java.util.*;
 
 @Component
 @Local(value = { LoadBalancingRulesManager.class, LoadBalancingRulesService.class })
 public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements LoadBalancingRulesManager,
-        LoadBalancingRulesService, NetworkRuleApplier {
+        LoadBalancingRulesService {
     private static final Logger s_logger = Logger.getLogger(LoadBalancingRulesManagerImpl.class);
 
     @Inject
@@ -120,8 +192,6 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
     @Inject
     UsageEventDao _usageEventDao;
     @Inject
-    FirewallRulesCidrsDao _firewallCidrsDao;
-    @Inject
     FirewallManager _firewallMgr;
     @Inject
     NetworkDao _networkDao;
@@ -131,7 +201,6 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
     DomainService _domainMgr;
     @Inject
     ConfigurationManager _configMgr;
-
     @Inject
     ExternalLoadBalancerUsageManager _externalLBUsageMgr;
     @Inject
@@ -166,6 +235,8 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
     UserDao _userDao;
     @Inject
     List<LoadBalancingServiceProvider> _lbProviders;
+    private boolean _elbEnabled = false;
+
 
     // Will return a string. For LB Stickiness this will be a json, for
     // autoscale this will be "," separated values
@@ -273,7 +344,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
         List<LoadBalancingRule> rules = Arrays.asList(rule);
 
-        if (!_networkMgr.applyRules(rules, FirewallRule.Purpose.LoadBalancing, this, false)) {
+        if (!applyRules(rules, false)) {
             s_logger.debug("LB rules' autoscale config are not completely applied");
             return false;
         }
@@ -289,14 +360,14 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
         LoadBalancerVO loadBalancer = _lbDao.findById(vmGroup.getLoadBalancerId());
 
-        FirewallRule.State backupState = loadBalancer.getState();
+        LoadBalancer.State backupState = loadBalancer.getState();
 
         if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) {
-            loadBalancer.setState(FirewallRule.State.Add);
+            loadBalancer.setState(LoadBalancer.State.Add);
             _lbDao.persist(loadBalancer);
-        } else if (loadBalancer.getState() == FirewallRule.State.Active
+        } else if (loadBalancer.getState() == LoadBalancer.State.Active
                 && vmGroup.getState().equals(AutoScaleVmGroup.State_Revoke)) {
-            loadBalancer.setState(FirewallRule.State.Add);
+            loadBalancer.setState(LoadBalancer.State.Add);
             _lbDao.persist(loadBalancer);
         }
 
@@ -321,7 +392,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         if (success) {
             if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) {
                 Transaction.currentTxn().start();
-                loadBalancer.setState(FirewallRule.State.Active);
+                loadBalancer.setState(LoadBalancer.State.Active);
                 s_logger.debug("LB rule " + loadBalancer.getId() + " state is set to Active");
                 _lbDao.persist(loadBalancer);
                 vmGroup.setState(AutoScaleVmGroup.State_Enabled);
@@ -435,7 +506,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         }
 
         _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer);
-        if (loadBalancer.getState() == FirewallRule.State.Revoke) {
+        if (loadBalancer.getState() == LoadBalancer.State.Revoke) {
             throw new InvalidParameterValueException("Failed:  LB rule id: " + cmd.getLbRuleId()
                     + " is in deleting state: ");
         }
@@ -495,7 +566,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
         _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer);
 
-        if (loadBalancer.getState() == FirewallRule.State.Revoke) {
+        if (loadBalancer.getState() == LoadBalancer.State.Revoke) {
             throw new InvalidParameterValueException("Failed:  LB rule id: " + cmd.getLbRuleId()
                     + " is in deleting state: ");
         }
@@ -541,11 +612,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
     private boolean validateRule(LoadBalancingRule lbRule) {
         Network network = _networkDao.findById(lbRule.getNetworkId());
-        Purpose purpose = lbRule.getPurpose();
-        if (purpose != Purpose.LoadBalancing) {
-            s_logger.debug("Unable to validate network rules for purpose: " + purpose.toString());
-            return false;
-        }
+
         for (LoadBalancingServiceProvider ne : _lbProviders) {
             boolean validated = ne.validateLBRule(network, lbRule);
             if (!validated)
@@ -564,8 +631,8 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         if (loadBalancer == null) {
             throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId());
         }
-        FirewallRule.State backupState = loadBalancer.getState();
-        loadBalancer.setState(FirewallRule.State.Add);
+        LoadBalancer.State backupState = loadBalancer.getState();
+        loadBalancer.setState(LoadBalancer.State.Add);
         _lbDao.persist(loadBalancer);
         try {
             applyLoadBalancerConfig(cmd.getLbRuleId());
@@ -595,8 +662,8 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         if (loadBalancer == null) {
             throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId());
         }
-        FirewallRule.State backupState = loadBalancer.getState();
-        loadBalancer.setState(FirewallRule.State.Add);
+        LoadBalancer.State backupState = loadBalancer.getState();
+        loadBalancer.setState(LoadBalancer.State.Add);
         _lbDao.persist(loadBalancer);
         try {
             applyLoadBalancerConfig(cmd.getLbRuleId());
@@ -632,12 +699,12 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
                     + " for Stickiness policy id: " + stickinessPolicyId);
         }
         long loadBalancerId = loadBalancer.getId();
-        FirewallRule.State backupState = loadBalancer.getState();
+        LoadBalancer.State backupState = loadBalancer.getState();
         _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer);
 
         if (apply) {
-            if (loadBalancer.getState() == FirewallRule.State.Active) {
-                loadBalancer.setState(FirewallRule.State.Add);
+            if (loadBalancer.getState() == LoadBalancer.State.Active) {
+                loadBalancer.setState(LoadBalancer.State.Add);
                 _lbDao.persist(loadBalancer);
             }
 
@@ -690,12 +757,12 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
                     + " for HealthCheck policy id: " + healthCheckPolicyId);
         }
         long loadBalancerId = loadBalancer.getId();
-        FirewallRule.State backupState = loadBalancer.getState();
+        LoadBalancer.State backupState = loadBalancer.getState();
         _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer);
 
         if (apply) {
-            if (loadBalancer.getState() == FirewallRule.State.Active) {
-                loadBalancer.setState(FirewallRule.State.Add);
+            if (loadBalancer.getState() == LoadBalancer.State.Active) {
+                loadBalancer.setState(LoadBalancer.State.Add);
                 _lbDao.persist(loadBalancer);
             }
 
@@ -858,7 +925,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
                 throw ex;
             }
 
-            _rulesMgr.checkRuleAndUserVm(loadBalancer, vm, caller);
+            checkLbRuleAndUserVm(loadBalancer, vm, caller);
 
             if (vm.getAccountId() != loadBalancer.getAccountId()) {
                 throw new PermissionDeniedException("Cannot add virtual machines that do not belong to the same owner.");
@@ -902,9 +969,9 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
             return true;
         }
         boolean success = false;
-        FirewallRule.State backupState = loadBalancer.getState();
+        LoadBalancer.State backupState = loadBalancer.getState();
         try {
-            loadBalancer.setState(FirewallRule.State.Add);
+            loadBalancer.setState(LoadBalancer.State.Add);
             _lbDao.persist(loadBalancer);
             applyLoadBalancerConfig(loadBalancerId);
             success = true;
@@ -957,9 +1024,9 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         _accountMgr.checkAccess(caller.getCaller(), null, true, loadBalancer);
 
         boolean success = false;
-        FirewallRule.State backupState = loadBalancer.getState();
+        LoadBalancer.State backupState = loadBalancer.getState();
         try {
-            loadBalancer.setState(FirewallRule.State.Add);
+            loadBalancer.setState(LoadBalancer.State.Add);
             _lbDao.persist(loadBalancer);
 
             for (long instanceId : instanceIds) {
@@ -1075,16 +1142,16 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         Transaction txn = Transaction.currentTxn();
         boolean generateUsageEvent = false;
         boolean success = true;
-        FirewallRule.State backupState = lb.getState();
+        LoadBalancer.State backupState = lb.getState();
 
         txn.start();
-        if (lb.getState() == FirewallRule.State.Staged) {
+        if (lb.getState() == LoadBalancer.State.Staged) {
             if (s_logger.isDebugEnabled()) {
                 s_logger.debug("Found a rule that is still in stage state so just removing it: " + lb);
             }
             generateUsageEvent = true;
-        } else if (lb.getState() == FirewallRule.State.Add || lb.getState() == FirewallRule.State.Active) {
-            lb.setState(FirewallRule.State.Revoke);
+        } else if (lb.getState() == LoadBalancer.State.Add || lb.getState() == LoadBalancer.State.Active) {
+            lb.setState(LoadBalancer.State.Revoke);
             _lbDao.persist(lb);
             generateUsageEvent = true;
         }
@@ -1147,11 +1214,11 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
         FirewallRuleVO relatedRule = _firewallDao.findByRelatedId(lb.getId());
         if (relatedRule != null) {
-            s_logger.warn("Unable to remove firewall rule id=" + lb.getId() + " as it has related firewall rule id="
+            s_logger.warn("Unable to remove lb rule id=" + lb.getId() + " as it has related firewall rule id="
                     + relatedRule.getId() + "; leaving it in Revoke state");
             success = false;
         } else {
-            _firewallMgr.removeRule(lb);
+            _lbDao.remove(lb.getId());
         }
 
         // FIXME: breaking the dependency on ELB manager. This breaks
@@ -1302,8 +1369,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
             throw ex;
         }
 
-        _firewallMgr.validateFirewallRule(caller.getCaller(), ipAddr, srcPortStart, srcPortEnd, lb.getProtocol(),
-                Purpose.LoadBalancing, FirewallRuleType.User, networkId, null);
+        validateLbRule(caller.getCaller(), ipAddr, srcPortStart, srcPortEnd, lb.getProtocol(), networkId);
         NetworkVO network = _networkDao.findById(networkId);
         _accountMgr.checkAccess(caller.getCaller(), null, true, ipAddr);
 
@@ -1318,9 +1384,9 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         Transaction txn = Transaction.currentTxn();
         txn.start();
 
-        LoadBalancerVO newRule = new LoadBalancerVO(lb.getXid(), lb.getName(), lb.getDescription(),
-                lb.getSourceIpAddressId(), lb.getSourcePortEnd(), lb.getDefaultPortStart(), lb.getAlgorithm(),
-                network.getId(), ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId());
+        LoadBalancerVO newRule = new LoadBalancerVO(lb.getName(), lb.getDescription(), lb.getSourceIpAddressId(),
+                lb.getSourcePortEnd(), lb.getDefaultPortStart(), lb.getAlgorithm(), network.getId(),
+                ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId());
 
         // verify rule is supported by Lb provider of the network
         LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList<LbDestination>(),
@@ -1339,8 +1405,8 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         boolean success = true;
 
         try {
-            _firewallMgr.detectRulesConflict(newRule);
-            if (!_firewallDao.setStateToAdd(newRule)) {
+            //_firewallMgr.detectRulesConflict(newRule);
+            if (!_lbDao.setStateToAdd(newRule)) {
                 throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
             }
             s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port "
@@ -1390,7 +1456,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         List<LoadBalancerVO> lbs = _lbDao.listByNetworkId(networkId);
         if (lbs != null) {
             for(LoadBalancerVO lb : lbs) { // called during restart, not persisting state in db
-                lb.setState(FirewallRule.State.Revoke);
+                lb.setState(LoadBalancer.State.Revoke);
             }
             return applyLoadBalancerRules(lbs, false); // called during restart, not persisting state in db
         } else {
@@ -1410,10 +1476,8 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         }
     }
 
-    @Override
-    public boolean applyRules(Network network, Purpose purpose, List<? extends FirewallRule> rules)
+    protected boolean applyLbRules(Network network, List<? extends LoadBalancer> rules)
             throws ResourceUnavailableException {
-        assert (purpose == Purpose.LoadBalancing) : "LB Manager asked to handle non-LB rules";
         boolean handled = false;
         for (LoadBalancingServiceProvider lbElement : _lbProviders) {
             Provider provider = lbElement.getProvider();
@@ -1458,7 +1522,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
             rules.add(getLoadBalancerRuleToApply(lb));
         }
 
-        if (!_networkMgr.applyRules(rules, FirewallRule.Purpose.LoadBalancing, this, false)) {
+        if (!applyRules(rules, false)) {
             s_logger.debug("LB rules are not completely applied");
             return false;
         }
@@ -1467,12 +1531,12 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
             for (LoadBalancerVO lb : lbs) {
                 boolean checkForReleaseElasticIp = false;
                 txn.start();
-                if (lb.getState() == FirewallRule.State.Revoke) {
+                if (lb.getState() == LoadBalancer.State.Revoke) {
                     removeLBRule(lb);
                     s_logger.debug("LB " + lb.getId() + " is successfully removed");
                     checkForReleaseElasticIp = true;
-                } else if (lb.getState() == FirewallRule.State.Add) {
-                    lb.setState(FirewallRule.State.Active);
+                } else if (lb.getState() == LoadBalancer.State.Add) {
+                    lb.setState(LoadBalancer.State.Active);
                     s_logger.debug("LB rule " + lb.getId() + " state is set to Active");
                     _lbDao.persist(lb);
                 }
@@ -1491,7 +1555,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
                 }
 
                 if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) {
-                    lb.setState(FirewallRule.State.Add);
+                    lb.setState(LoadBalancer.State.Add);
                     _lbDao.persist(lb);
                     s_logger.debug("LB rule " + lb.getId()
                             + " state is set to Add as there are no more active LB-VM mappings");
@@ -1562,10 +1626,10 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
     @Override
     public boolean removeAllLoadBalanacersForIp(long ipId, Account caller, long callerUserId) {
-        List<FirewallRuleVO> rules = _firewallDao.listByIpAndPurposeAndNotRevoked(ipId, Purpose.LoadBalancing);
+        List<LoadBalancerVO> rules = _lbDao.listByIpAndNotRevoked(ipId);
         if (rules != null)
             s_logger.debug("Found " + rules.size() + " lb rules to cleanup");
-        for (FirewallRule rule : rules) {
+        for (LoadBalancerVO rule : rules) {
             boolean result = deleteLoadBalancerRule(rule.getId(), true, caller, callerUserId, false);
             if (result == false) {
                 s_logger.warn("Unable to remove load balancer rule " + rule.getId());
@@ -1577,11 +1641,10 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
 
     @Override
     public boolean removeAllLoadBalanacersForNetwork(long networkId, Account caller, long callerUserId) {
-        List<FirewallRuleVO> rules = _firewallDao
-                .listByNetworkAndPurposeAndNotRevoked(networkId, Purpose.LoadBalancing);
+        List<LoadBalancerVO> rules = _lbDao.listByNetworkNotRevoked(networkId);
         if (rules != null)
             s_logger.debug("Found " + rules.size() + " lb rules to cleanup");
-        for (FirewallRule rule : rules) {
+        for (LoadBalancerVO rule : rules) {
             boolean result = deleteLoadBalancerRule(rule.getId(), true, caller, callerUserId, false);
             if (result == false) {
                 s_logger.warn("Unable to remove load balancer rule " + rule.getId());
@@ -1672,7 +1735,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         // If algorithm is changed, have to reapply the lb config
         if (algorithm != null) {
             try {
-                lb.setState(FirewallRule.State.Add);
+                lb.setState(LoadBalancer.State.Add);
                 _lbDao.persist(lb);
                 applyLoadBalancerConfig(lbRuleId);
             } catch (ResourceUnavailableException e) {
@@ -1930,4 +1993,116 @@ public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase implements
         // remove the rule
         _lbDao.remove(rule.getId());
     }
+    
+    private void checkLbRuleAndUserVm(LoadBalancer rule, UserVm userVm, Account caller) {
+        if (userVm == null || rule == null) {
+            return;
+        }
+
+        _accountMgr.checkAccess(caller, null, true, rule, userVm);
+
+        if (userVm.getState() == VirtualMachine.State.Destroyed || userVm.getState() == VirtualMachine.State.Expunging) {
+            throw new InvalidParameterValueException("Invalid user vm: " + userVm.getId());
+        }
+
+        if (rule.getAccountId() != userVm.getAccountId()) {
+            throw new InvalidParameterValueException("New lb rule " + rule + " and vm id=" + userVm.getId() + " belong to different accounts");
+        }
+    }
+    
+    
+    protected boolean applyRules(List<? extends LoadBalancer> rules, boolean continueOnError) throws ResourceUnavailableException {
+        if (rules == null || rules.size() == 0) {
+            s_logger.debug("There are no lb rules to forward to the network elements");
+            return true;
+        }
+
+        boolean success = true;
+        Network network = _networkDao.findById(rules.get(0).getNetworkId());
+        List<PublicIp> publicIps = new ArrayList<PublicIp>();
+
+        // get the list of public ip's owned by the network
+        List<IPAddressVO> userIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null);
+        if (userIps != null && !userIps.isEmpty()) {
+            for (IPAddressVO userIp : userIps) {
+                PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
+                publicIps.add(publicIp);
+            }
+        }
+
+        // rules can not programmed unless IP is associated with network
+        // service provider, so run IP assoication for
+        // the network so as to ensure IP is associated before applying
+        // rules (in add state)
+        _networkMgr.applyIpAssociations(network, false, continueOnError, publicIps);
+
+        try {
+            applyLbRules(network, rules);
+        } catch (ResourceUnavailableException e) {
+            if (!continueOnError) {
+                throw e;
+            }
+            s_logger.warn("Problems with applying lb rules but pushing on", e);
+            success = false;
+        }
+
+        // if all the rules configured on public IP are revoked then
+        // dis-associate IP with network service provider
+        _networkMgr.applyIpAssociations(network, true, continueOnError, publicIps);
+        
+
+        return success;
+    }
+    
+    
+    protected void validateLbRule(Account caller, IPAddressVO ipAddress, Integer portStart, Integer portEnd, String proto, Long networkId) {
+        if (portStart != null && !NetUtils.isValidPort(portStart)) {
+            throw new InvalidParameterValueException("Public port is an invalid value: " + portStart);
+        }
+        if (portEnd != null && !NetUtils.isValidPort(portEnd)) {
+            throw new InvalidParameterValueException("Public port range is an invalid value: " + portEnd);
+        }
+
+        // start port can't be bigger than end port
+        if (portStart != null && portEnd != null && portStart > portEnd) {
+            throw new InvalidParameterValueException("Start port can't be bigger than end port");
+        }
+
+        if (ipAddress != null){
+            if (ipAddress.getAssociatedWithNetworkId() == null) {
+                    throw new InvalidParameterValueException("Unable to create lb rule ; ip with specified id is not associated with any network");
+            } else {
+                networkId = ipAddress.getAssociatedWithNetworkId();
+            }
+    
+            // Validate ip address
+            _accountMgr.checkAccess(caller, null, true, ipAddress);
+    
+            Network network = _networkModel.getNetwork(networkId);
+            assert network != null : "Can't create port forwarding rule as network associated with public ip address is null?";
+    
+                
+            // Verify that the network guru supports the protocol specified
+            Map<Network.Capability, String> caps = null;
+            if (!_elbEnabled) {
+                 caps = _networkModel.getNetworkServiceCapabilities(network.getId(), Service.Lb);
+            }
+
+            if (caps != null) {
+                String supportedProtocols = caps.get(Capability.SupportedProtocols).toLowerCase();
+                
+                if (!supportedProtocols.contains(proto.toLowerCase())) {
+                    throw new InvalidParameterValueException("Protocol " + proto + " is not supported in zone " + network.getDataCenterId());
+                }
+            }
+        }
+    }
+    
+    @Override
+    public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
+        _name = name;
+        String elbEnabledString = _configDao.getValue(Config.ElasticLoadBalancerEnabled.key());
+        _elbEnabled = Boolean.parseBoolean(elbEnabledString);
+        return true;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java
index f49ab79..4049e03 100644
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java
@@ -29,6 +29,7 @@ import com.cloud.network.RemoteAccessVpn;
 import com.cloud.network.VirtualNetworkApplianceService;
 import com.cloud.network.VpnUser;
 import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.rules.StaticNat;
 import com.cloud.user.Account;
 import com.cloud.user.User;
@@ -103,4 +104,6 @@ public interface VirtualNetworkApplianceManager extends Manager, VirtualNetworkA
 	
 	boolean applyUserData(Network config, NicProfile nic, VirtualMachineProfile<UserVm> vm, DeployDestination dest, 
 	        List<DomainRouterVO> routers) throws ResourceUnavailableException;
+
+    boolean applyLbRules(Network network, List<? extends LoadBalancer> rules, List<? extends VirtualRouter> routers) throws ResourceUnavailableException;
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index afdbbca..5227434 100755
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -172,6 +172,7 @@ import com.cloud.network.router.VirtualRouter.RedundantState;
 import com.cloud.network.router.VirtualRouter.Role;
 import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.FirewallRule.Purpose;
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.rules.PortForwardingRule;
 import com.cloud.network.rules.RulesManager;
 import com.cloud.network.rules.StaticNat;
@@ -3299,19 +3300,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
         return applyRules(network, routers, "firewall rules", false, null, false, new RuleApplier() {
             @Override
             public boolean execute(Network network, VirtualRouter router) throws ResourceUnavailableException {
-                if (rules.get(0).getPurpose() == Purpose.LoadBalancing) {
-                    // for load balancer we have to resend all lb rules for the network
-                    List<LoadBalancerVO> lbs = _loadBalancerDao.listByNetworkId(network.getId());
-                    List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>();
-                    for (LoadBalancerVO lb : lbs) {
-                        List<LbDestination> dstList = _lbMgr.getExistingDestinations(lb.getId());
-                        List<LbStickinessPolicy> policyList = _lbMgr.getStickinessPolicies(lb.getId());
-                        List<LbHealthCheckPolicy> hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId() );
-                        LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList);
-                        lbRules.add(loadBalancing);
-                    }
-                    return sendLBRules(router, lbRules, network.getId());
-                } else if (rules.get(0).getPurpose() == Purpose.PortForwarding) {
+                if (rules.get(0).getPurpose() == Purpose.PortForwarding) {
                     return sendPortForwardingRules(router, (List<PortForwardingRule>) rules, network.getId());
                 } else if (rules.get(0).getPurpose() == Purpose.StaticNat) {
                     return sendStaticNatRules(router, (List<StaticNatRule>) rules, network.getId());
@@ -3324,6 +3313,30 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
             }
         });
     }
+    
+    @Override
+    public boolean applyLbRules(Network network, final List<? extends LoadBalancer> rules, List<? extends VirtualRouter> routers) throws ResourceUnavailableException {
+        if (rules == null || rules.isEmpty()) {
+            s_logger.debug("No lb rules to be applied for network " + network.getId());
+            return true;
+        }
+        return applyRules(network, routers, "lb rules", false, null, false, new RuleApplier() {
+            @Override
+            public boolean execute(Network network, VirtualRouter router) throws ResourceUnavailableException {
+                // for load balancer we have to resend all lb rules for the network
+                List<LoadBalancerVO> lbs = _loadBalancerDao.listByNetworkId(network.getId());
+                List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>();
+                for (LoadBalancerVO lb : lbs) {
+                    List<LbDestination> dstList = _lbMgr.getExistingDestinations(lb.getId());
+                    List<LbStickinessPolicy> policyList = _lbMgr.getStickinessPolicies(lb.getId());
+                    List<LbHealthCheckPolicy> hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId() );
+                    LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList);
+                    lbRules.add(loadBalancing);
+                }
+                return sendLBRules(router, lbRules, network.getId());
+            }
+        });
+    }
 
     protected boolean sendLBRules(VirtualRouter router, List<LoadBalancingRule> rules, long guestNetworkId) throws ResourceUnavailableException {
         Commands cmds = new Commands(OnError.Continue);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/rules/FirewallRuleVO.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/rules/FirewallRuleVO.java b/server/src/com/cloud/network/rules/FirewallRuleVO.java
index a761520..9f73029 100644
--- a/server/src/com/cloud/network/rules/FirewallRuleVO.java
+++ b/server/src/com/cloud/network/rules/FirewallRuleVO.java
@@ -20,7 +20,6 @@ import java.util.Date;
 import java.util.List;
 import java.util.UUID;
 
-import javax.inject.Inject;
 import javax.persistence.Column;
 import javax.persistence.DiscriminatorColumn;
 import javax.persistence.DiscriminatorType;
@@ -35,7 +34,6 @@ import javax.persistence.InheritanceType;
 import javax.persistence.Table;
 import javax.persistence.Transient;
 
-import com.cloud.network.dao.FirewallRulesCidrsDao;
 import com.cloud.utils.db.GenericDao;
 import com.cloud.utils.net.NetUtils;
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/rules/RulesManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/rules/RulesManager.java b/server/src/com/cloud/network/rules/RulesManager.java
index 4b83e04..8587e4a 100644
--- a/server/src/com/cloud/network/rules/RulesManager.java
+++ b/server/src/com/cloud/network/rules/RulesManager.java
@@ -41,8 +41,6 @@ public interface RulesManager extends RulesService {
 
     void checkIpAndUserVm(IpAddress ipAddress, UserVm userVm, Account caller);
 
-    void checkRuleAndUserVm(FirewallRule rule, UserVm userVm, Account caller);
-
     boolean revokeAllPFAndStaticNatRulesForIp(long ipId, long userId, Account caller) throws ResourceUnavailableException;
 
     boolean revokeAllPFStaticNatRulesForNetwork(long networkId, long userId, Account caller) throws ResourceUnavailableException;
@@ -66,8 +64,6 @@ public interface RulesManager extends RulesService {
 
     FirewallRule[] reservePorts(IpAddress ip, String protocol, FirewallRule.Purpose purpose, boolean openFirewall, Account caller, int... ports) throws NetworkRuleConflictException;
 
-    boolean releasePorts(long ipId, String protocol, FirewallRule.Purpose purpose, int... ports);
-
     List<PortForwardingRuleVO> listByNetworkId(long networkId);
 
     boolean applyStaticNatForIp(long sourceIpId, boolean continueOnError, Account caller, boolean forRevoke);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/src/com/cloud/network/rules/RulesManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java
index 29ed5f3..8b8f0b6 100755
--- a/server/src/com/cloud/network/rules/RulesManagerImpl.java
+++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java
@@ -24,7 +24,6 @@ import java.util.Set;
 
 import javax.ejb.Local;
 import javax.inject.Inject;
-import javax.naming.ConfigurationException;
 
 import org.apache.cloudstack.api.command.user.firewall.ListPortForwardingRulesCmd;
 import org.apache.log4j.Logger;
@@ -50,6 +49,8 @@ import com.cloud.network.dao.FirewallRulesCidrsDao;
 import com.cloud.network.dao.FirewallRulesDao;
 import com.cloud.network.dao.IPAddressDao;
 import com.cloud.network.dao.IPAddressVO;
+import com.cloud.network.dao.LoadBalancerDao;
+import com.cloud.network.dao.LoadBalancerVO;
 import com.cloud.network.rules.FirewallRule.FirewallRuleType;
 import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
@@ -66,7 +67,6 @@ import com.cloud.user.UserContext;
 import com.cloud.uservm.UserVm;
 import com.cloud.utils.Pair;
 import com.cloud.utils.Ternary;
-import com.cloud.utils.component.Manager;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.utils.db.DB;
 import com.cloud.utils.db.Filter;
@@ -128,6 +128,8 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules
     VpcManager _vpcMgr;
     @Inject
     NicSecondaryIpDao _nicSecondaryDao;
+    @Inject
+    LoadBalancerDao _lbDao;
 
     @Override
     public void checkIpAndUserVm(IpAddress ipAddress, UserVm userVm, Account caller) {
@@ -157,8 +159,7 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules
 
     }
 
-    @Override
-    public void checkRuleAndUserVm(FirewallRule rule, UserVm userVm, Account caller) {
+    private void checkRuleAndUserVm(FirewallRule rule, UserVm userVm, Account caller) {
         if (userVm == null || rule == null) {
             return;
         }
@@ -558,7 +559,7 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules
                 throw new NetworkRuleConflictException("Failed to enable static nat for the ip address " + ipAddress + " as it already has PortForwarding rules assigned");
             }
 
-            List<FirewallRuleVO> loadBalancingRules = _firewallDao.listByIpAndPurposeAndNotRevoked(ipAddress.getId(), Purpose.LoadBalancing);
+            List<LoadBalancerVO> loadBalancingRules = _lbDao.listByIpAndNotRevoked(ipAddress.getId());
             if (loadBalancingRules != null && !loadBalancingRules.isEmpty()) {
                 throw new NetworkRuleConflictException("Failed to enable static nat for the ip address " + ipAddress + " as it already has LoadBalancing rules assigned");
             }
@@ -571,7 +572,7 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules
 
         if (oldIP != null) {
             // If elasticIP functionality is supported in the network, we always have to disable static nat on the old
-// ip in order to re-enable it on the new one
+            // ip in order to re-enable it on the new one
             Long networkId = oldIP.getAssociatedWithNetworkId();
             boolean reassignStaticNat = false;
             if (networkId != null) {
@@ -1120,11 +1121,6 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules
     }
 
     @Override
-    public boolean releasePorts(long ipId, String protocol, FirewallRule.Purpose purpose, int... ports) {
-        return _firewallDao.releasePorts(ipId, protocol, purpose, ports);
-    }
-
-    @Override
     @DB
     public FirewallRuleVO[] reservePorts(IpAddress ip, String protocol, FirewallRule.Purpose purpose, 
             boolean openFirewall, Account caller, int... ports) throws NetworkRuleConflictException {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/test/com/cloud/network/MockNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/network/MockNetworkManagerImpl.java b/server/test/com/cloud/network/MockNetworkManagerImpl.java
index 6da48ec..d903042 100755
--- a/server/test/com/cloud/network/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/network/MockNetworkManagerImpl.java
@@ -28,8 +28,8 @@ import org.apache.cloudstack.api.command.admin.usage.ListTrafficTypeImplementors
 import org.apache.cloudstack.api.command.user.network.CreateNetworkCmd;
 import org.apache.cloudstack.api.command.user.network.ListNetworksCmd;
 import org.apache.cloudstack.api.command.user.network.RestartNetworkCmd;
-import org.springframework.stereotype.Component;
 import org.apache.cloudstack.api.command.user.vm.ListNicsCmd;
+import org.springframework.stereotype.Component;
 
 import com.cloud.dc.DataCenter;
 import com.cloud.dc.Pod;
@@ -55,24 +55,21 @@ import com.cloud.network.element.UserDataServiceProvider;
 import com.cloud.network.guru.NetworkGuru;
 import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.FirewallRule.Purpose;
-import com.cloud.network.rules.FirewallRule.State;
+import com.cloud.network.rules.LoadBalancer.State;
 import com.cloud.network.rules.StaticNat;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.offerings.NetworkOfferingVO;
 import com.cloud.user.Account;
 import com.cloud.user.User;
 import com.cloud.utils.Pair;
-import com.cloud.utils.component.Manager;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.vm.Nic;
 import com.cloud.vm.NicProfile;
-import com.cloud.vm.NicSecondaryIp;
 import com.cloud.vm.NicVO;
 import com.cloud.vm.ReservationContext;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VirtualMachineProfile;
-import com.cloud.vm.VirtualMachineProfileImpl;
 
 @Component
 @Local(value = { NetworkManager.class, NetworkService.class })
@@ -808,12 +805,6 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
     }
 
     @Override
-    public int getRuleCountForIp(Long addressId, Purpose purpose, State state) {
-        // TODO Auto-generated method stub
-        return 0;
-    }
-
-    @Override
     public LoadBalancingServiceProvider getLoadBalancingProviderForNetwork(Network network) {
         // TODO Auto-generated method stub
         return null;
@@ -888,4 +879,10 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
         // TODO Auto-generated method stub
         return null;
     }
+
+    @Override
+    public int getLbRuleCountForIp(Long addressId, State state) {
+        // TODO Auto-generated method stub
+        return 0;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/test/com/cloud/network/MockRulesManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/network/MockRulesManagerImpl.java b/server/test/com/cloud/network/MockRulesManagerImpl.java
index e5a6894..d4961ce 100644
--- a/server/test/com/cloud/network/MockRulesManagerImpl.java
+++ b/server/test/com/cloud/network/MockRulesManagerImpl.java
@@ -28,16 +28,15 @@ import com.cloud.exception.InsufficientAddressCapacityException;
 import com.cloud.exception.NetworkRuleConflictException;
 import com.cloud.exception.ResourceUnavailableException;
 import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.PortForwardingRule;
 import com.cloud.network.rules.PortForwardingRuleVO;
 import com.cloud.network.rules.RulesManager;
 import com.cloud.network.rules.RulesService;
 import com.cloud.network.rules.StaticNatRule;
-import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.user.Account;
 import com.cloud.uservm.UserVm;
 import com.cloud.utils.Pair;
-import com.cloud.utils.component.Manager;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.utils.net.Ip;
 import com.cloud.vm.VirtualMachine;
@@ -170,13 +169,6 @@ public class MockRulesManagerImpl extends ManagerBase implements RulesManager, R
 	}
 
 	@Override
-	public void checkRuleAndUserVm(FirewallRule rule, UserVm userVm,
-			Account caller) {
-		// TODO Auto-generated method stub
-
-	}
-
-	@Override
 	public boolean revokeAllPFAndStaticNatRulesForIp(long ipId, long userId,
 			Account caller) throws ResourceUnavailableException {
 		// TODO Auto-generated method stub
@@ -231,13 +223,6 @@ public class MockRulesManagerImpl extends ManagerBase implements RulesManager, R
 	}
 
 	@Override
-	public boolean releasePorts(long ipId, String protocol, Purpose purpose,
-			int... ports) {
-		// TODO Auto-generated method stub
-		return false;
-	}
-
-	@Override
 	public List<PortForwardingRuleVO> listByNetworkId(long networkId) {
 		// TODO Auto-generated method stub
 		return null;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
index ead0051..4cb7444 100644
--- a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
@@ -58,7 +58,6 @@ import com.cloud.network.PhysicalNetwork;
 import com.cloud.network.PhysicalNetworkServiceProvider;
 import com.cloud.network.PhysicalNetworkTrafficType;
 import com.cloud.network.PublicIpAddress;
-import com.cloud.network.UserIpv6Address;
 import com.cloud.network.addr.PublicIp;
 import com.cloud.network.dao.IPAddressVO;
 import com.cloud.network.dao.NetworkServiceMapDao;
@@ -70,7 +69,7 @@ import com.cloud.network.element.UserDataServiceProvider;
 import com.cloud.network.guru.NetworkGuru;
 import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.FirewallRule.Purpose;
-import com.cloud.network.rules.FirewallRule.State;
+import com.cloud.network.rules.LoadBalancer.State;
 import com.cloud.network.rules.StaticNat;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.offerings.NetworkOfferingVO;
@@ -78,17 +77,14 @@ import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
 import com.cloud.user.Account;
 import com.cloud.user.User;
 import com.cloud.utils.Pair;
-import com.cloud.utils.component.Manager;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.vm.Nic;
 import com.cloud.vm.NicProfile;
-import com.cloud.vm.NicSecondaryIp;
 import com.cloud.vm.NicVO;
 import com.cloud.vm.ReservationContext;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VirtualMachineProfile;
-import com.cloud.vm.VirtualMachineProfileImpl;
 
 @Component
 @Local(value = { NetworkManager.class, NetworkService.class })
@@ -1301,17 +1297,6 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
         return null;
     }
 
-    @Override
-    public int getRuleCountForIp(Long addressId, Purpose purpose, State state) {
-        // TODO Auto-generated method stub
-        return 0;
-    }
-
-    @Override
-    public LoadBalancingServiceProvider getLoadBalancingProviderForNetwork(Network network) {
-        // TODO Auto-generated method stub
-        return null;
-    }
 
     /* (non-Javadoc)
      * @see com.cloud.network.NetworkService#getNetwork(java.lang.String)
@@ -1424,4 +1409,24 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
     }
 
 
+
+
+
+    @Override
+    public int getLbRuleCountForIp(Long addressId, State state) {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+
+
+
+
+    @Override
+    public LoadBalancingServiceProvider getLoadBalancingProviderForNetwork(Network network) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java b/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java
index ef5478b..c6844b7 100644
--- a/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java
+++ b/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java
@@ -39,6 +39,7 @@ import com.cloud.network.VpnUser;
 import com.cloud.network.router.VirtualRouter;
 import com.cloud.network.router.VpcVirtualNetworkApplianceManager;
 import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.rules.StaticNat;
 import com.cloud.network.vpc.PrivateGateway;
 import com.cloud.network.vpc.StaticRouteProfile;
@@ -402,4 +403,10 @@ VpcVirtualNetworkApplianceService {
         return null;
     }
 
+    @Override
+    public boolean applyLbRules(Network network, List<? extends LoadBalancer> rules, List<? extends VirtualRouter> routers) throws ResourceUnavailableException {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java
----------------------------------------------------------------------
diff --git a/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java b/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java
index 700fe8f..30c7886 100644
--- a/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java
+++ b/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java
@@ -15,21 +15,19 @@
 
 package org.apache.cloudstack.region.gslb;
 
-import com.cloud.agent.AgentManager;
-import com.cloud.configuration.dao.ConfigurationDao;
-import com.cloud.exception.InvalidParameterValueException;
-import com.cloud.network.dao.*;
-import com.cloud.network.rules.FirewallRule;
-import com.cloud.network.rules.RulesManager;
-import com.cloud.region.ha.GlobalLoadBalancerRule;
-import com.cloud.user.Account;
-import com.cloud.user.AccountManager;
-import com.cloud.user.AccountVO;
-import com.cloud.user.UserContext;
-import com.cloud.utils.db.Transaction;
-import com.cloud.utils.net.Ip;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Mockito.when;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
 import junit.framework.Assert;
 import junit.framework.TestCase;
+
 import org.apache.cloudstack.api.command.user.region.ha.gslb.AssignToGlobalLoadBalancerRuleCmd;
 import org.apache.cloudstack.api.command.user.region.ha.gslb.CreateGlobalLoadBalancerRuleCmd;
 import org.apache.cloudstack.api.command.user.region.ha.gslb.DeleteGlobalLoadBalancerRuleCmd;
@@ -40,13 +38,25 @@ import org.apache.log4j.Logger;
 import org.junit.Test;
 import org.mockito.Mockito;
 
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
-import static org.mockito.Matchers.*;
-import static org.mockito.Mockito.when;
+import com.cloud.agent.AgentManager;
+import com.cloud.configuration.dao.ConfigurationDao;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.network.dao.IPAddressVO;
+import com.cloud.network.dao.LoadBalancerDao;
+import com.cloud.network.dao.LoadBalancerVO;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkVO;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.network.rules.RulesManager;
+import com.cloud.region.ha.GlobalLoadBalancerRule;
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import com.cloud.user.AccountVO;
+import com.cloud.user.UserContext;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.net.Ip;
 
 public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
 
@@ -510,11 +520,11 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
         when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
 
         LoadBalancerVO lbRule = new LoadBalancerVO();
-        lbRule.setState(FirewallRule.State.Active);
-        Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
+        lbRule.setState(LoadBalancer.State.Active);
+        Field networkIdField = LoadBalancerVO.class.getDeclaredField("networkId");
         networkIdField.setAccessible(true);
         networkIdField.set(lbRule, new Long(1));
-        Field sourceIpAddressId = LoadBalancerVO.class.getSuperclass().getDeclaredField("sourceIpAddressId");
+        Field sourceIpAddressId = LoadBalancerVO.class.getDeclaredField("sourceIpAddressId");
         sourceIpAddressId.setAccessible(true);
         sourceIpAddressId.set(lbRule, new Long(1));
 
@@ -575,20 +585,20 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
         when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
 
         LoadBalancerVO lbRule1 = new LoadBalancerVO();
-        lbRule1.setState(FirewallRule.State.Active);
-        Field networkIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
+        lbRule1.setState(LoadBalancer.State.Active);
+        Field networkIdField1 = LoadBalancerVO.class.getDeclaredField("networkId");
         networkIdField1.setAccessible(true);
         networkIdField1.set(lbRule1, new Long(1));
-        Field idField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
+        Field idField1 = LoadBalancerVO.class.getDeclaredField("id");
         idField1.setAccessible(true);
         idField1.set(lbRule1, new Long(1));
 
         LoadBalancerVO lbRule2 = new LoadBalancerVO();
-        lbRule2.setState(FirewallRule.State.Active);
-        Field networkIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
+        lbRule2.setState(LoadBalancer.State.Active);
+        Field networkIdField2 = LoadBalancerVO.class.getDeclaredField("networkId");
         networkIdField2.setAccessible(true);
         networkIdField2.set(lbRule2, new Long(1));
-        Field idField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
+        Field idField2 = LoadBalancerVO.class.getDeclaredField("id");
         idField2.setAccessible(true);
         idField2.set(lbRule2, new Long(2));
 
@@ -649,8 +659,8 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
         when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
 
         LoadBalancerVO lbRule = new LoadBalancerVO();
-        lbRule.setState(FirewallRule.State.Active);
-        Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
+        lbRule.setState(LoadBalancer.State.Active);
+        Field networkIdField = LoadBalancerVO.class.getDeclaredField("networkId");
         networkIdField.setAccessible(true);
         networkIdField.set(lbRule, new Long(1));
 
@@ -708,14 +718,14 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
         when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
 
         LoadBalancerVO lbRule = new LoadBalancerVO();
-        lbRule.setState(FirewallRule.State.Active);
-        Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
+        lbRule.setState(LoadBalancer.State.Active);
+        Field networkIdField = LoadBalancerVO.class.getDeclaredField("networkId");
         networkIdField.setAccessible(true);
         networkIdField.set(lbRule, new Long(1));
-        Field idField = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
+        Field idField = LoadBalancerVO.class.getDeclaredField("id");
         idField.setAccessible(true);
         idField.set(lbRule, new Long(1));
-        Field sourceIpAddressId = LoadBalancerVO.class.getSuperclass().getDeclaredField("sourceIpAddressId");
+        Field sourceIpAddressId = LoadBalancerVO.class.getDeclaredField("sourceIpAddressId");
         sourceIpAddressId.setAccessible(true);
         sourceIpAddressId.set(lbRule, new Long(1));
 
@@ -778,11 +788,11 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase {
         when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
 
         LoadBalancerVO lbRule = new LoadBalancerVO();
-        lbRule.setState(FirewallRule.State.Active);
-        Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
+        lbRule.setState(LoadBalancer.State.Active);
+        Field networkIdField = LoadBalancerVO.class.getDeclaredField("networkId");
         networkIdField.setAccessible(true);
         networkIdField.set(lbRule, new Long(1));
-        Field idField = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
+        Field idField = LoadBalancerVO.class.getDeclaredField("id");
         idField.setAccessible(true);
         idField.set(lbRule, new Long(1));
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b429b960/setup/db/db/schema-410to420-cleanup.sql
----------------------------------------------------------------------
diff --git a/setup/db/db/schema-410to420-cleanup.sql b/setup/db/db/schema-410to420-cleanup.sql
index b65717f..c60c01b 100644
--- a/setup/db/db/schema-410to420-cleanup.sql
+++ b/setup/db/db/schema-410to420-cleanup.sql
@@ -25,4 +25,4 @@ ALTER TABLE `cloud`.`remote_access_vpn` DROP primary key;
 ALTER TABLE `cloud`.`remote_access_vpn` ADD primary key (`id`);
 ALTER TABLE `cloud`.`remote_access_vpn` ADD CONSTRAINT `fk_remote_access_vpn__vpn_server_addr_id` FOREIGN KEY (`vpn_server_addr_id`) REFERENCES `user_ip_address` (`id`);
 
-
+ALTER TABLE `cloud`.`load_balancing_rules` DROP foreign key `fk_load_balancing_rules__id`;


Mime
View raw message