cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject [2/2] git commit: updated refs/heads/internallb to 08521e4
Date Wed, 10 Apr 2013 22:35:19 GMT
Internal LB: intermediate checkin - added InternalLBAppliance manager and managerImpl


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

Branch: refs/heads/internallb
Commit: 528796d4598fa622ca7df0b6640a9b96283b006c
Parents: d8ea854
Author: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Authored: Wed Apr 10 15:17:58 2013 -0700
Committer: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Committed: Wed Apr 10 15:34:55 2013 -0700

----------------------------------------------------------------------
 .../com/cloud/network/router/VirtualRouter.java    |    2 +-
 .../element/InternalLoadBalancerElement.java       |  102 ++-
 .../network/lb/InternalLoadBalancerManager.java    |   74 ++
 .../lb/InternalLoadBalancerManagerImpl.java        |  739 ++++++++++++++-
 .../router/VirtualNetworkApplianceManager.java     |    1 +
 .../router/VirtualNetworkApplianceManagerImpl.java |    2 +-
 .../lb/dao/ApplicationLoadBalancerRuleDao.java     |    6 +-
 .../lb/dao/ApplicationLoadBalancerRuleDaoImpl.java |   25 +
 .../vpc/MockVpcVirtualNetworkApplianceManager.java |    1 -
 9 files changed, 907 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/api/src/com/cloud/network/router/VirtualRouter.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/network/router/VirtualRouter.java b/api/src/com/cloud/network/router/VirtualRouter.java
index 59410fa..2311f48 100755
--- a/api/src/com/cloud/network/router/VirtualRouter.java
+++ b/api/src/com/cloud/network/router/VirtualRouter.java
@@ -23,7 +23,7 @@ import com.cloud.vm.VirtualMachine;
  */
 public interface VirtualRouter extends VirtualMachine {
 	public enum Role {
-		VIRTUAL_ROUTER, LB, InternalLB
+		VIRTUAL_ROUTER, LB, INTERNAL_LB_VM
 	}
     Role getRole();
     boolean getIsRedundantRouter();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java
index 610e5d5..c2df4b6 100644
--- a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java
+++ b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java
@@ -29,9 +29,13 @@ import javax.inject.Inject;
 import org.apache.cloudstack.api.command.admin.internallb.ConfigureInternalLoadBalancerElementCmd;
 import org.apache.cloudstack.api.command.admin.internallb.CreateInternalLoadBalancerElementCmd;
 import org.apache.cloudstack.api.command.admin.internallb.ListInternalLoadBalancerElementsCmd;
+import org.apache.cloudstack.network.lb.InternalLoadBalancerManager;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.api.to.LoadBalancerTO;
+import com.cloud.configuration.ConfigurationManager;
+import com.cloud.dc.DataCenter;
+import com.cloud.dc.DataCenter.NetworkType;
 import com.cloud.deploy.DeployDestination;
 import com.cloud.exception.ConcurrentOperationException;
 import com.cloud.exception.InsufficientCapacityException;
@@ -59,6 +63,7 @@ import com.cloud.network.lb.LoadBalancingRule;
 import com.cloud.network.router.VirtualRouter.Role;
 import com.cloud.network.rules.LoadBalancerContainer;
 import com.cloud.offering.NetworkOffering;
+import com.cloud.user.AccountManager;
 import com.cloud.utils.component.AdapterBase;
 import com.cloud.utils.db.SearchCriteria.Op;
 import com.cloud.utils.db.SearchCriteria2;
@@ -80,8 +85,17 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala
     @Inject DomainRouterDao _routerDao;
     @Inject VirtualRouterProviderDao _vrProviderDao;
     @Inject PhysicalNetworkServiceProviderDao _pNtwkSvcProviderDao;
+    @Inject InternalLoadBalancerManager _internalLbMgr;
+    @Inject ConfigurationManager _configMgr;
+    @Inject AccountManager _accountMgr;
     
     private boolean canHandle(Network config, List<LoadBalancingRule> rules) {
+        //works in Advance zone only
+        DataCenter dc = _configMgr.getZone(config.getDataCenterId());
+        if (dc.getNetworkType() != NetworkType.Advanced) {
+            s_logger.trace("Not hanling zone of network type " + dc.getNetworkType());
+            return false;
+        }
         if (config.getGuestType() != Network.GuestType.Isolated || config.getTrafficType() != TrafficType.Guest) {
             s_logger.trace("Not handling network with Type  " + config.getGuestType() + " and traffic type " + config.getTrafficType());
             return false;
@@ -138,26 +152,70 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala
 
     @Override
     public boolean shutdown(Network network, ReservationContext context, boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException {
-        // TODO Shutdown all the internal lb elements
-        return true;
+        List<DomainRouterVO> internalLbVms = _routerDao.listByNetworkAndRole(network.getId(), Role.INTERNAL_LB_VM);
+        if (internalLbVms == null || internalLbVms.isEmpty()) {
+            return true;
+        }
+        boolean result = true;
+        for (DomainRouterVO internalLbVm : internalLbVms) {
+            result = result && _internalLbMgr.destroyInternalLbVm(internalLbVm.getId(),
+                    context.getAccount(), context.getCaller().getId()) != null;
+            if (cleanup) {
+                if (!result) {
+                    s_logger.warn("Failed to stop internal lb element " + internalLbVm + ", but would try to process clean up anyway.");
+                }
+                result = (_internalLbMgr.destroyInternalLbVm(internalLbVm.getId(),
+                        context.getAccount(), context.getCaller().getId()) != null);
+                if (!result) {
+                    s_logger.warn("Failed to clean up internal lb element " + internalLbVm);
+                }
+            }
+        }
+        return result;
     }
 
     @Override
     public boolean destroy(Network network, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
-        // TODO Shutdown all the internal lb elements
-        return true;
+        List<DomainRouterVO> internalLbVms = _routerDao.listByNetworkAndRole(network.getId(), Role.INTERNAL_LB_VM);
+        if (internalLbVms == null || internalLbVms.isEmpty()) {
+            return true;
+        }
+        boolean result = true;
+        for (DomainRouterVO internalLbVm : internalLbVms) {
+            result = result && (_internalLbMgr.destroyInternalLbVm(internalLbVm.getId(),
+                    context.getAccount(), context.getCaller().getId()) != null);
+        }
+        return result;
     }
 
     @Override
     public boolean isReady(PhysicalNetworkServiceProvider provider) {
-        // TODO Shutdown all the internal lb elements
-        return true;
+        VirtualRouterProviderVO element = _vrProviderDao.findByNspIdAndType(provider.getId(), 
+                VirtualRouterProviderType.InternalLbVm);
+        if (element == null) {
+            return false;
+        }
+        return element.isEnabled();
     }
 
     @Override
-    public boolean shutdownProviderInstances(PhysicalNetworkServiceProvider provider, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
-        // TODO Shutdown all the internal lb elements
-        return true;
+    public boolean shutdownProviderInstances(PhysicalNetworkServiceProvider provider, ReservationContext context)
+            throws ConcurrentOperationException, ResourceUnavailableException {
+        VirtualRouterProviderVO element = _vrProviderDao.findByNspIdAndType(provider.getId(), 
+                VirtualRouterProviderType.InternalLbVm);
+        if (element == null) {
+            return true;
+        }
+        long elementId = element.getId();
+        List<DomainRouterVO> internalLbVms = _routerDao.listByElementId(elementId);
+        boolean result = true;
+        for (DomainRouterVO internalLbVm : internalLbVms) {
+            result = result && (_internalLbMgr.destroyInternalLbVm(internalLbVm.getId(),
+                    context.getAccount(), context.getCaller().getId()) != null);
+        }
+        _vrProviderDao.remove(elementId);
+        
+        return result;
     }
 
     @Override
@@ -177,8 +235,25 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala
 
     @Override
     public boolean applyLBRules(Network network, List<LoadBalancingRule> rules) throws ResourceUnavailableException {
-        // TODO Auto-generated method stub
-        return true;
+        List<DomainRouterVO> routers;
+        try {
+            DeployDestination dest = new DeployDestination(_configMgr.getZone(network.getDataCenterId()), null, null, null); 
+            routers = _internalLbMgr.deployInternalLbVm(network, null, dest, _accountMgr.getAccount(network.getAccountId()), null);
+        } catch (InsufficientCapacityException e) {
+            s_logger.warn("Failed to apply lb rule(s) on the element " + this.getName() + " due to:", e);
+            return false;
+        } catch (ConcurrentOperationException e) {
+            s_logger.warn("Failed to apply lb rule(s) on the element " + this.getName() + " due to:", e);
+            return false;
+        }
+        
+        if ((routers == null) || (routers.size() == 0)) {
+            throw new ResourceUnavailableException("Can't find/deploy internal lb vm to handle LB rules",
+                    DataCenter.class, network.getDataCenterId());
+        }
+        
+        //TODO - apply the rules
+        return true;    
     }
 
     @Override
@@ -186,7 +261,7 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala
         List<LoadBalancingRule> rules = new ArrayList<LoadBalancingRule>();
         rules.add(rule);
         if (canHandle(network, rules)) {
-            List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.InternalLB);
+            List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.INTERNAL_LB_VM);
             if (routers == null || routers.isEmpty()) {
                 return true;
             }
@@ -291,7 +366,8 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala
 
     @Override
     public boolean applyIps(Network network, List<? extends PublicIpAddress> ipAddress, Set<Service> services) throws ResourceUnavailableException {
-        //do nothing here; this element just has to extend the ip deployer as the LB service implements IPDeployerRequester
+        //do nothing here; this element just has to extend the ip deployer
+        //as the LB service implements IPDeployerRequester
         return true;
     }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManager.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManager.java b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManager.java
index eab5e6a..84a5908 100644
--- a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManager.java
+++ b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManager.java
@@ -16,8 +16,82 @@
 // under the License.
 package org.apache.cloudstack.network.lb;
 
+import java.util.List;
+import java.util.Map;
+
+import com.cloud.deploy.DeployDestination;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.exception.StorageUnavailableException;
+import com.cloud.network.Network;
+import com.cloud.network.router.VirtualRouter;
+import com.cloud.user.Account;
+import com.cloud.user.User;
 import com.cloud.utils.component.Manager;
+import com.cloud.utils.net.Ip;
+import com.cloud.vm.DomainRouterVO;
+import com.cloud.vm.VirtualMachineProfile.Param;
 
 public interface InternalLoadBalancerManager extends Manager{
+    /**
+     * Destroys Internal LB vm instance
+     * @param vmId
+     * @param caller
+     * @param callerUserId
+     * @return 
+     * @throws ResourceUnavailableException
+     * @throws ConcurrentOperationException
+     */
+    VirtualRouter destroyInternalLbVm(long vmId, Account caller, Long callerUserId) 
+            throws ResourceUnavailableException, ConcurrentOperationException;
+    
+   
+    /**
+     * Stops Internal lb vm
+     * @param vmId
+     * @param forced
+     * @param caller
+     * @param callerUserId
+     * @return
+     * @throws ConcurrentOperationException
+     * @throws ResourceUnavailableException
+     */
+    VirtualRouter stopInternalLbVm(long vmId, boolean forced, Account caller, Long callerUserId) 
+            throws ConcurrentOperationException, ResourceUnavailableException;
+
+
+    /**
+     * Deploys internal lb vm
+     * @param guestNetwork
+     * @param requestedGuestIp
+     * @param dest
+     * @param owner
+     * @param params
+     * @return
+     * @throws InsufficientCapacityException
+     * @throws ConcurrentOperationException
+     * @throws ResourceUnavailableException
+     */
+    List<DomainRouterVO> deployInternalLbVm(Network guestNetwork, Ip requestedGuestIp, DeployDestination dest, Account owner,
+            Map<Param, Object> params) throws InsufficientCapacityException,
+            ConcurrentOperationException, ResourceUnavailableException;
+
+
+    /**
+     * Starts Internal lb vm
+     * @param internalLbVm
+     * @param user
+     * @param caller
+     * @param params
+     * @return
+     * @throws StorageUnavailableException
+     * @throws InsufficientCapacityException
+     * @throws ConcurrentOperationException
+     * @throws ResourceUnavailableException
+     */
+    DomainRouterVO startInternalLbVm(DomainRouterVO internalLbVm, User user, Account caller, Map<Param, Object> params) 
+            throws StorageUnavailableException, InsufficientCapacityException, ConcurrentOperationException,
+            ResourceUnavailableException;
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManagerImpl.java b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManagerImpl.java
index 0a023ae..d6d3adb 100644
--- a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManagerImpl.java
+++ b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/lb/InternalLoadBalancerManagerImpl.java
@@ -16,31 +16,103 @@
 // under the License.
 package org.apache.cloudstack.network.lb;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.ejb.Local;
 import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
+import org.apache.cloudstack.network.lb.dao.ApplicationLoadBalancerRuleDao;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.GetDomRVersionAnswer;
+import com.cloud.agent.api.GetDomRVersionCmd;
 import com.cloud.agent.api.StopAnswer;
+import com.cloud.agent.api.check.CheckSshAnswer;
+import com.cloud.agent.api.check.CheckSshCommand;
+import com.cloud.agent.api.routing.LoadBalancerConfigCommand;
+import com.cloud.agent.api.routing.NetworkElementCommand;
+import com.cloud.agent.api.to.LoadBalancerTO;
 import com.cloud.agent.api.to.NicTO;
 import com.cloud.agent.api.to.VirtualMachineTO;
 import com.cloud.agent.manager.Commands;
+import com.cloud.configuration.Config;
+import com.cloud.configuration.dao.ConfigurationDao;
+import com.cloud.dc.DataCenter;
+import com.cloud.dc.DataCenterVO;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.deploy.DataCenterDeployment;
 import com.cloud.deploy.DeployDestination;
+import com.cloud.deploy.DeploymentPlan;
 import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientAddressCapacityException;
 import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.InsufficientServerCapacityException;
+import com.cloud.exception.OperationTimedoutException;
 import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.exception.StorageUnavailableException;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
 import com.cloud.network.Network;
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.network.NetworkManager;
+import com.cloud.network.NetworkModel;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.PhysicalNetworkServiceProvider;
+import com.cloud.network.VirtualRouterProvider;
+import com.cloud.network.VirtualRouterProvider.VirtualRouterProviderType;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkVO;
+import com.cloud.network.dao.PhysicalNetworkServiceProviderDao;
+import com.cloud.network.dao.VirtualRouterProviderDao;
+import com.cloud.network.lb.LoadBalancingRule;
+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.lb.LoadBalancingRulesManager;
+import com.cloud.network.router.VirtualNetworkApplianceManager;
+import com.cloud.network.router.VirtualRouter;
+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.LoadBalancerContainer.Scheme;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.offering.ServiceOffering;
+import com.cloud.offerings.dao.NetworkOfferingDao;
+import com.cloud.resource.ResourceManager;
+import com.cloud.service.ServiceOfferingVO;
+import com.cloud.service.dao.ServiceOfferingDao;
+import com.cloud.storage.VMTemplateVO;
+import com.cloud.storage.dao.VMTemplateDao;
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import com.cloud.user.User;
+import com.cloud.utils.NumbersUtil;
+import com.cloud.utils.Pair;
 import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.net.Ip;
+import com.cloud.utils.net.NetUtils;
 import com.cloud.vm.DomainRouterVO;
+import com.cloud.vm.Nic;
+import com.cloud.vm.NicProfile;
+import com.cloud.vm.NicVO;
 import com.cloud.vm.ReservationContext;
 import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VirtualMachineGuru;
 import com.cloud.vm.VirtualMachineManager;
+import com.cloud.vm.VirtualMachineName;
 import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.VirtualMachineProfile.Param;
+import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.NicDao;
 
 @Component
 @Local(value = { InternalLoadBalancerManager.class })
@@ -48,95 +120,293 @@ public class InternalLoadBalancerManagerImpl extends ManagerBase implements
 InternalLoadBalancerManager, VirtualMachineGuru<DomainRouterVO> {
     private static final Logger s_logger = Logger
             .getLogger(InternalLoadBalancerManagerImpl.class);
+    private String _instance;
+    private String _mgmtHost;
+    private String _mgmtCidr;
+    int _internalLbVmRamSize;
+    int _internalLbVmCpuMHz;
+    private ServiceOfferingVO _internalLbVmOffering;
     
     @Inject VirtualMachineManager _itMgr;
+    @Inject DomainRouterDao _routerDao;
+    @Inject ConfigurationDao _configDao;
+    @Inject AgentManager _agentMgr;
+    @Inject DataCenterDao _dcDao;
+    @Inject VirtualRouterProviderDao _vrProviderDao;
+    @Inject ApplicationLoadBalancerRuleDao _lbDao;
+    @Inject NetworkModel _ntwkModel;
+    @Inject LoadBalancingRulesManager _lbMgr;
+    @Inject NicDao _nicDao;
+    @Inject AccountManager _accountMgr;
+    @Inject NetworkDao _networkDao;
+    @Inject NetworkManager _ntwkMgr;
+    @Inject ServiceOfferingDao _serviceOfferingDao;
+    @Inject PhysicalNetworkServiceProviderDao _physicalProviderDao;
+    @Inject NetworkOfferingDao _networkOfferingDao;
+    @Inject VMTemplateDao _templateDao;
+    @Inject ResourceManager _resourceMgr;
 
     @Override
     public DomainRouterVO findByName(String name) {
-        // TODO Auto-generated method stub
-        return null;
+        if (!VirtualMachineName.isValidRouterName(name)) {
+            return null;
+        }
+
+        return _routerDao.findById(VirtualMachineName.getRouterId(name));
     }
 
     @Override
     public DomainRouterVO findById(long id) {
-        // TODO Auto-generated method stub
-        return null;
+        return _routerDao.findById(id);
     }
 
     @Override
     public DomainRouterVO persist(DomainRouterVO vm) {
-        // TODO Auto-generated method stub
-        return null;
+        DomainRouterVO virtualRouter =  _routerDao.persist(vm);
+        return virtualRouter;
     }
 
     @Override
-    public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) {
-        // TODO Auto-generated method stub
-        return false;
+    public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<DomainRouterVO> profile,
+            DeployDestination dest, ReservationContext context) {
+
+        //1) Prepare boot loader elements related with Control network
+
+        StringBuilder buf = profile.getBootArgsBuilder();
+        buf.append(" template=domP");
+        buf.append(" name=").append(profile.getHostName());
+
+        if (Boolean.valueOf(_configDao.getValue("system.vm.random.password"))) {
+            buf.append(" vmpassword=").append(_configDao.getValue("system.vm.password"));
+        }
+        
+        NicProfile controlNic = null;
+      
+        for (NicProfile nic : profile.getNics()) {
+            int deviceId = nic.getDeviceId();
+            boolean ipv4 = false, ipv6 = false;
+            if (nic.getIp4Address() != null) {
+                ipv4 = true;
+                buf.append(" eth").append(deviceId).append("ip=").append(nic.getIp4Address());
+                buf.append(" eth").append(deviceId).append("mask=").append(nic.getNetmask());
+            }
+            if (nic.getIp6Address() != null) {
+                ipv6 = true;
+                buf.append(" eth").append(deviceId).append("ip6=").append(nic.getIp6Address());
+                buf.append(" eth").append(deviceId).append("ip6prelen=").append(NetUtils.getIp6CidrSize(nic.getIp6Cidr()));
+            }
+            
+            if (nic.isDefaultNic()) {
+                if (ipv4) {
+                    buf.append(" gateway=").append(nic.getGateway());
+                }
+                if (ipv6) {
+                    buf.append(" ip6gateway=").append(nic.getIp6Gateway());
+                }
+            }
+
+            if (nic.getTrafficType() == TrafficType.Management) {
+                buf.append(" localgw=").append(dest.getPod().getGateway());
+            } else if (nic.getTrafficType() == TrafficType.Control) {
+                controlNic = nic;
+                // Internal LB control command is sent over management server in VMware
+                if (dest.getHost().getHypervisorType() == HypervisorType.VMware) {
+                    if (s_logger.isInfoEnabled()) {
+                        s_logger.info("Check if we need to add management server explicit route to Internal LB. pod cidr: " 
+                                + dest.getPod().getCidrAddress() + "/" + dest.getPod().getCidrSize()
+                                + ", pod gateway: " + dest.getPod().getGateway() + ", management host: " + _mgmtHost);
+                    }
+
+                    if (s_logger.isInfoEnabled()) {
+                        s_logger.info("Add management server explicit route to Internal LB.");
+                    }
+                    
+               
+                    buf.append(" mgmtcidr=").append(_mgmtCidr);
+                    buf.append(" localgw=").append(dest.getPod().getGateway());
+                }
+            }
+        }
+
+        if (controlNic == null) {
+            throw new CloudRuntimeException("Didn't start a control port");
+        }
+
+        //FIXME - change if use other template for internal lb vm
+        String type = "vpcrouter";
+        buf.append(" type=" + type);
+
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("Boot Args for " + profile + ": " + buf.toString());
+        }
+
+        return true;
     }
 
     @Override
     public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException {
-        // TODO Auto-generated method stub
-        return false;
+        DomainRouterVO internalLbVm = profile.getVirtualMachine();
+
+        List<NicProfile> nics = profile.getNics();
+        for (NicProfile nic : nics) {
+            if (nic.getTrafficType() == TrafficType.Control) {
+                internalLbVm.setPrivateIpAddress(nic.getIp4Address());
+                internalLbVm.setPrivateMacAddress(nic.getMacAddress());
+            }
+        }
+        _routerDao.update(internalLbVm.getId(), internalLbVm);
+
+        finalizeCommandsOnStart(cmds, profile);
+        return true;
     }
 
     @Override
     public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long hostId, Commands cmds, ReservationContext context) {
-        // TODO Auto-generated method stub
-        return false;
+        DomainRouterVO internalLbVm = profile.getVirtualMachine();
+        
+        boolean result = true;
+
+        Answer answer = cmds.getAnswer("checkSsh");
+        if (answer != null && answer instanceof CheckSshAnswer) {
+            CheckSshAnswer sshAnswer = (CheckSshAnswer) answer;
+            if (sshAnswer == null || !sshAnswer.getResult()) {
+                s_logger.warn("Unable to ssh to the VM: " + sshAnswer.getDetails());
+                result = false;
+            }
+        } else {
+            result = false;
+        }
+        if (result == false) {
+            return result;
+        }
+        
+        //Get guest network info
+        List<Network> guestNetworks = new ArrayList<Network>();
+        List<? extends Nic> internalLbNics = _nicDao.listByVmId(profile.getId());
+        for (Nic routerNic : internalLbNics) {
+            Network network = _ntwkModel.getNetwork(routerNic.getNetworkId());
+            if (network.getTrafficType() == TrafficType.Guest) {
+                guestNetworks.add(network);
+            }
+        }
+        
+        answer = cmds.getAnswer("getDomRVersion");
+        if (answer != null && answer instanceof GetDomRVersionAnswer) {
+            GetDomRVersionAnswer versionAnswer = (GetDomRVersionAnswer)answer;
+            if (answer == null || !answer.getResult()) {
+                s_logger.warn("Unable to get the template/scripts version of router " + internalLbVm.getInstanceName() +
+                        " due to: " + versionAnswer.getDetails());
+                result = false;
+            } else {
+                internalLbVm.setTemplateVersion(versionAnswer.getTemplateVersion());
+                internalLbVm.setScriptsVersion(versionAnswer.getScriptsVersion());
+                internalLbVm = _routerDao.persist(internalLbVm, guestNetworks);
+            }
+        } else {
+            result = false;
+        }
+
+        return result;
     }
 
     @Override
     public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile<DomainRouterVO> profile) {
-        // TODO Auto-generated method stub
-        return false;
+        DomainRouterVO internalLbVm = profile.getVirtualMachine();
+        NicProfile controlNic = getNicProfileByTrafficType(profile, TrafficType.Control);
+
+        if (controlNic == null) {
+            s_logger.error("Control network doesn't exist for the internal LB vm " + internalLbVm);
+            return false;
+        }
+
+        finalizeSshAndVersionOnStart(cmds, profile, internalLbVm, controlNic);
+
+        // restart network if restartNetwork = false is not specified in profile parameters
+        boolean reprogramGuestNtwk = true;
+        if (profile.getParameter(Param.ReProgramGuestNetworks) != null 
+                && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) {
+            reprogramGuestNtwk = false;
+        }
+
+        VirtualRouterProvider lbProvider = _vrProviderDao.findById(internalLbVm.getElementId());
+        if (lbProvider == null) {
+            throw new CloudRuntimeException("Cannot find related element " + VirtualRouterProviderType.InternalLbVm + " of vm: " + internalLbVm.getHostName());
+        }
+        
+        Provider provider = Network.Provider.getProvider(lbProvider.getType().toString());
+        if (provider == null) {
+            throw new CloudRuntimeException("Cannot find related provider of provider: " + lbProvider.getType().toString());
+        }
+
+        if (reprogramGuestNtwk) {
+            NicProfile guestNic = getNicProfileByTrafficType(profile, TrafficType.Guest);
+            finalizeLbRulesForIp(cmds, internalLbVm, provider, new Ip(guestNic.getIp4Address()), guestNic.getNetworkId());
+        }
+
+        return true;
     }
 
     @Override
     public void finalizeStop(VirtualMachineProfile<DomainRouterVO> profile, StopAnswer answer) {
-        // TODO Auto-generated method stub
-        
     }
 
     @Override
     public void finalizeExpunge(DomainRouterVO vm) {
-        // TODO Auto-generated method stub
-        
     }
 
     @Override
     public Long convertToId(String vmName) {
-        // TODO Auto-generated method stub
-        return null;
+        if (!VirtualMachineName.isValidRouterName(vmName, _instance)) {
+            return null;
+        }
+
+        return VirtualMachineName.getRouterId(vmName);
     }
 
     @Override
     public boolean plugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException,
-            InsufficientCapacityException {
-        // TODO Auto-generated method stub
-        return false;
+            InsufficientCapacityException {        
+        //not supported
+        throw new UnsupportedOperationException("Plug nic is not supported for vm of type " + vm.getType());
     }
 
     @Override
     public boolean unplugNic(Network network, NicTO nic, VirtualMachineTO vm, ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException {
-        // TODO Auto-generated method stub
-        return false;
+        //not supported
+        throw new UnsupportedOperationException("Unplug nic is not supported for vm of type " + vm.getType());
     }
 
     @Override
     public void prepareStop(VirtualMachineProfile<DomainRouterVO> profile) {
-        // TODO Auto-generated method stub
-        
     }
     
     @Override
     public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
+        final Map<String, String> configs = _configDao.getConfiguration("AgentManager", params);
+        _instance = configs.get("instance.name");
+        if (_instance == null) {
+            _instance = "DEFAULT";
+        }
+        
+        _mgmtHost = configs.get("host");
+        
+        _mgmtCidr = _configDao.getValue(Config.ManagementNetwork.key());
+        
+        _internalLbVmRamSize = NumbersUtil.parseInt(configs.get("router.ram.size"), VirtualNetworkApplianceManager.DEFAULT_ROUTER_VM_RAMSIZE);
+        _internalLbVmCpuMHz = NumbersUtil.parseInt(configs.get("router.cpu.mhz"), VirtualNetworkApplianceManager.DEFAULT_ROUTER_CPU_MHZ);
+        
+        boolean useLocalStorage = Boolean.parseBoolean(configs.get(Config.SystemVMUseLocalStorage.key()));
+        _internalLbVmOffering = new ServiceOfferingVO("System Offering For Software Router", 1, _internalLbVmRamSize, _internalLbVmCpuMHz, null,
+                null, true, null, useLocalStorage, true, null, true, VirtualMachine.Type.DomainRouter, true);
+        _internalLbVmOffering.setUniqueName(ServiceOffering.routerDefaultOffUniqueName);
+        _internalLbVmOffering = _serviceOfferingDao.persistSystemServiceOffering(_internalLbVmOffering);
+        
         _itMgr.registerGuru(VirtualMachine.Type.InternalLoadBalancerVm, this);
 
         if (s_logger.isInfoEnabled()) {
             s_logger.info(getName()  +  " has been configured");
         }
+        
         return true;
     }
     
@@ -144,5 +414,418 @@ InternalLoadBalancerManager, VirtualMachineGuru<DomainRouterVO> {
     public String getName() {
         return _name;
     }
+    
+    protected NicProfile getNicProfileByTrafficType(VirtualMachineProfile<DomainRouterVO> profile, TrafficType trafficType) {
+        for (NicProfile nic : profile.getNics()) {
+            if (nic.getTrafficType() == trafficType && nic.getIp4Address() != null) {
+                return nic;
+            }
+        }
+        return null;
+     }
+    
+    protected void finalizeSshAndVersionOnStart(Commands cmds, VirtualMachineProfile<DomainRouterVO> profile, DomainRouterVO router, NicProfile controlNic) {
+        cmds.addCommand("checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922));
+
+        // Update internal lb vm template/scripts version
+        final GetDomRVersionCmd command = new GetDomRVersionCmd();
+        command.setAccessDetail(NetworkElementCommand.ROUTER_IP, controlNic.getIp4Address());
+        command.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
+        cmds.addCommand("getDomRVersion", command);
+    }
+    
+    
+    protected void finalizeLbRulesForIp(Commands cmds, DomainRouterVO internalLbVm, Provider provider, Ip sourceIp, long guestNtwkId) {
+        s_logger.debug("Resending load balancing rules as a part of start for " + internalLbVm);
+        List<ApplicationLoadBalancerRuleVO> lbs = _lbDao.listBySrcIpSrcNtwkIdAndScheme(sourceIp, guestNtwkId, Scheme.Internal);
+        List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>();
+        if (_ntwkModel.isProviderSupportServiceInNetwork(guestNtwkId, Service.Lb, provider)) {
+            // Re-apply load balancing rules
+            for (ApplicationLoadBalancerRuleVO 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, sourceIp);
+                lbRules.add(loadBalancing);
+            }
+        }
+
+        s_logger.debug("Found " + lbRules.size() + " load balancing rule(s) to apply as a part of Intenrnal LB vm" + internalLbVm + " start.");
+        if (!lbRules.isEmpty()) {
+            createApplyLoadBalancingRulesCommands(lbRules, internalLbVm, cmds, guestNtwkId);
+        }
+    }
+    
+    private void createApplyLoadBalancingRulesCommands(List<LoadBalancingRule> rules, VirtualRouter internalLbVm, Commands cmds, long guestNetworkId) {
+
+        LoadBalancerTO[] lbs = new LoadBalancerTO[rules.size()];
+        int i = 0;
+        boolean inline = false;
+        for (LoadBalancingRule rule : rules) {
+            boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke));
+            String protocol = rule.getProtocol();
+            String algorithm = rule.getAlgorithm();
+            String uuid = rule.getUuid();
+
+            String srcIp = rule.getSourceIp().addr();
+            int srcPort = rule.getSourcePortStart();
+            List<LbDestination> destinations = rule.getDestinations();
+            List<LbStickinessPolicy> stickinessPolicies = rule.getStickinessPolicies();
+            LoadBalancerTO lb = new LoadBalancerTO(uuid, srcIp, srcPort, protocol, algorithm, revoked, false, inline, destinations, stickinessPolicies);
+            lbs[i++] = lb;
+        }
+        
+        Network guestNetwork = _ntwkModel.getNetwork(guestNetworkId);
+        Nic guestNic = _nicDao.findByInstanceIdAndNetworkId(guestNetwork.getId(), internalLbVm.getId());
+        NicProfile guestNicProfile = new NicProfile(guestNic, guestNetwork, guestNic.getBroadcastUri(), guestNic.getIsolationUri(), 
+                _ntwkModel.getNetworkRate(guestNetwork.getId(), internalLbVm.getId()), 
+                _ntwkModel.isSecurityGroupSupportedInNetwork(guestNetwork), 
+                _ntwkModel.getNetworkTag(internalLbVm.getHypervisorType(), guestNetwork));
+
+        LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lbs, null, 
+                guestNic.getIp4Address(), internalLbVm.getPrivateIpAddress(), 
+                _itMgr.toNicTO(guestNicProfile, internalLbVm.getHypervisorType()), internalLbVm.getVpcId());
+
+        cmd.lbStatsVisibility = _configDao.getValue(Config.NetworkLBHaproxyStatsVisbility.key());
+        cmd.lbStatsUri = _configDao.getValue(Config.NetworkLBHaproxyStatsUri.key());
+        cmd.lbStatsAuth = _configDao.getValue(Config.NetworkLBHaproxyStatsAuth.key());
+        cmd.lbStatsPort = _configDao.getValue(Config.NetworkLBHaproxyStatsPort.key());
+
+        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, getInternalLbControlIp(internalLbVm.getId()));
+        cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, guestNic.getIp4Address());
+        cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, internalLbVm.getInstanceName());
+        DataCenterVO dcVo = _dcDao.findById(internalLbVm.getDataCenterId());
+        cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString());
+        cmds.addCommand(cmd);
+    }
+    
+    
+    protected String getInternalLbControlIp(long internalLbVmId) {
+        String controlIpAddress = null;
+        List<NicVO> nics = _nicDao.listByVmId(internalLbVmId);
+        for (NicVO nic : nics) {
+            Network ntwk = _ntwkModel.getNetwork(nic.getNetworkId());
+            if (ntwk.getTrafficType() == TrafficType.Control) {
+                controlIpAddress = nic.getIp4Address();
+            }
+        }
+        
+        if(controlIpAddress == null) {
+            s_logger.warn("Unable to find Internal LB control ip in its attached NICs!. Internal LB vm: " + internalLbVmId);
+            DomainRouterVO internalLbVm = _routerDao.findById(internalLbVmId);
+            return internalLbVm.getPrivateIpAddress();
+        }
+            
+        return controlIpAddress;
+    }
+
+    @Override
+    public VirtualRouter destroyInternalLbVm(long vmId, Account caller, Long callerUserId)
+            throws ResourceUnavailableException, ConcurrentOperationException {
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("Attempting to destroy Internal LB vm " + vmId);
+        }
+
+        DomainRouterVO internalLbVm = _routerDao.findById(vmId);
+        if (internalLbVm == null) {
+            return null;
+        }
+
+        _accountMgr.checkAccess(caller, null, true, internalLbVm);
+
+        boolean result = _itMgr.expunge(internalLbVm, _accountMgr.getActiveUser(callerUserId), caller);
+
+        if (result) {
+            return internalLbVm;
+        }
+        return null;
+    }
+
+    @Override
+    public VirtualRouter stopInternalLbVm(long vmId, boolean forced, Account caller, Long callerUserId) throws ConcurrentOperationException, ResourceUnavailableException {
+        DomainRouterVO internalLbVm = _routerDao.findById(vmId);
+        if (internalLbVm == null) {
+            return null;
+        }
+        
+        s_logger.debug("Stopping internal lb vm " + internalLbVm);
+        try {
+            if (_itMgr.advanceStop((DomainRouterVO) internalLbVm, forced, _accountMgr.getActiveUser(callerUserId), caller)) {
+                return _routerDao.findById(internalLbVm.getId());
+            } else {
+                return null;
+            }
+        } catch (OperationTimedoutException e) {
+            throw new CloudRuntimeException("Unable to stop " + internalLbVm, e);
+        }
+    }
+    
+    
+    @Override
+    public List<DomainRouterVO> deployInternalLbVm(Network guestNetwork, Ip requestedGuestIp, DeployDestination dest, 
+            Account owner, Map<Param, Object> params) throws InsufficientCapacityException,
+    ConcurrentOperationException, ResourceUnavailableException {
+
+        List<DomainRouterVO> internalLbVms = findOrDeployInternalLbVm(guestNetwork, requestedGuestIp, dest, owner, params);
+        
+        return startRouters(params, internalLbVms);
+    }
+    
+    protected List<DomainRouterVO> startRouters(Map<Param, Object> params, List<DomainRouterVO> routers) 
+            throws StorageUnavailableException, InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
+        List<DomainRouterVO> runningRouters = null;
+
+        if (routers != null) {
+            runningRouters = new ArrayList<DomainRouterVO>();
+        }
+
+        for (DomainRouterVO router : routers) {
+            router = startInternalLbVm(router, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount(), params);
+            
+            if (router != null) {
+                runningRouters.add(router);
+            }
+            
+        }
+        return runningRouters;
+    }
+    
+    
+    
+    @DB
+    protected List<DomainRouterVO> findOrDeployInternalLbVm(Network guestNetwork, Ip requestedGuestIp, DeployDestination dest, 
+            Account owner, Map<Param, Object> params) throws ConcurrentOperationException, 
+            InsufficientCapacityException, ResourceUnavailableException {
+
+        List<DomainRouterVO> internalLbs = new ArrayList<DomainRouterVO>();
+        Network lock = _networkDao.acquireInLockTable(guestNetwork.getId(), _ntwkMgr.getNetworkLockTimeout());
+        if (lock == null) {
+            throw new ConcurrentOperationException("Unable to lock network " + guestNetwork.getId());
+        }
+        
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("Lock is acquired for network id " + lock.getId() + " as a part of internal lb startup in " + dest);
+        }
+        
+        // Check if providers are supported in the physical networks
+        VirtualRouterProviderType type = VirtualRouterProviderType.InternalLbVm;
+            Long physicalNetworkId = _ntwkModel.getPhysicalNetworkId(guestNetwork);
+        PhysicalNetworkServiceProvider provider = _physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString());
+        if (provider == null) {
+            throw new CloudRuntimeException("Cannot find service provider " + type.toString() + " in physical network " + physicalNetworkId);
+        }
+        VirtualRouterProvider internalLbProvider = _vrProviderDao.findByNspIdAndType(provider.getId(), type);
+        if (internalLbProvider == null) {
+            throw new CloudRuntimeException("Cannot find provider " + type.toString() + " as service provider " + provider.getId());
+        }
+        
+        try {
+            assert guestNetwork.getState() == Network.State.Implemented || guestNetwork.getState() == Network.State.Setup ||
+                    guestNetwork.getState() == Network.State.Implementing : "Network is not yet fully implemented: "
+                    + guestNetwork;
+            assert guestNetwork.getTrafficType() == TrafficType.Guest;
+
+            Long offeringId = _networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
+            if (offeringId == null) {
+                offeringId = _internalLbVmOffering.getId();
+            }
+
+            // 3) deploy internal lb vm
+            Pair<DeploymentPlan, List<DomainRouterVO>> planAndRouters = getDeploymentPlanAndInternalLbVms(dest, guestNetwork.getId(), requestedGuestIp);
+            internalLbs = planAndRouters.second();
+            DeploymentPlan plan = planAndRouters.first();
+
+            List<Pair<NetworkVO, NicProfile>> networks = createInternalLbVmNetworks(guestNetwork, plan, null);
+            //don't start the internal lb as we are holding the network lock that needs to be released at the end of router allocation
+            DomainRouterVO internalLb = deployInternalLbVm(owner, dest, plan, params, internalLbProvider, offeringId, guestNetwork.getVpcId(),
+                networks, false);
+            if (internalLb != null) {
+                internalLbs.add(internalLb);
+            }
+        } finally {
+            if (lock != null) {
+                _networkDao.releaseFromLockTable(lock.getId());
+                if (s_logger.isDebugEnabled()) {
+                    s_logger.debug("Lock is released for network id " + lock.getId() + " as a part of router startup in " + dest);
+                }
+            }
+        }
+        return internalLbs;
+    }
+    
+    protected List<Pair<NetworkVO, NicProfile>> createInternalLbVmNetworks(Network guestNetwork, DeploymentPlan plan, Ip guestIp) throws ConcurrentOperationException,
+            InsufficientAddressCapacityException {
+
+        //Form networks
+        List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(3);
+        
+        //1) Guest network
+        if (guestNetwork != null) {
+            s_logger.debug("Adding nic for Internal LB in Guest network " + guestNetwork);
+            NicProfile guestNic = new NicProfile();
+            if (guestIp != null) {
+                guestNic.setIp4Address(guestIp.addr());  
+            } else {
+                guestNic.setIp4Address(_ntwkMgr.acquireGuestIpAddress(guestNetwork, null));
+            }
+            guestNic.setGateway(guestNetwork.getGateway());
+            guestNic.setBroadcastUri(guestNetwork.getBroadcastUri());
+            guestNic.setBroadcastType(guestNetwork.getBroadcastDomainType());
+            guestNic.setIsolationUri(guestNetwork.getBroadcastUri());
+            guestNic.setMode(guestNetwork.getMode());
+            String gatewayCidr = guestNetwork.getCidr();
+            guestNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr));
+            networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) guestNetwork, guestNic));
+        }
+
+        //2) Control network
+        s_logger.debug("Adding nic for Internal LB vm in Control network ");
+        List<? extends NetworkOffering> offerings = _ntwkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork);
+        NetworkOffering controlOffering = offerings.get(0);
+        NetworkVO controlConfig = _ntwkMgr.setupNetwork(_accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), controlOffering, plan, null, null, false).get(0);
+        networks.add(new Pair<NetworkVO, NicProfile>(controlConfig, null));
+
+        return networks;
+    }
+    
+    
+    protected Pair<DeploymentPlan, List<DomainRouterVO>> getDeploymentPlanAndInternalLbVms(DeployDestination dest, long guestNetworkId, Ip requestedGuestIp) {
+        long dcId = dest.getDataCenter().getId();
+        DeploymentPlan plan = new DataCenterDeployment(dcId);
+        List<DomainRouterVO> internalLbVms = _routerDao.listByNetworkAndRole(guestNetworkId, Role.INTERNAL_LB_VM); 
+        if (requestedGuestIp != null) {
+            Iterator<DomainRouterVO> it = internalLbVms.iterator();
+            while (it.hasNext()) {
+                DomainRouterVO vm = it.next();
+                Nic nic = _nicDao.findByInstanceIdAndNetworkId(guestNetworkId, vm.getId());
+                if (!nic.getIp4Address().equalsIgnoreCase(requestedGuestIp.addr())) {
+                    it.remove();
+                }
+            }
+        }
+
+        return new Pair<DeploymentPlan, List<DomainRouterVO>>(plan, internalLbVms);
+    }
+    
+    
+    protected DomainRouterVO deployInternalLbVm(Account owner, DeployDestination dest, DeploymentPlan plan, Map<Param, Object> params,
+            VirtualRouterProvider internalLbProvider, long svcOffId, Long vpcId,
+            List<Pair<NetworkVO, NicProfile>> networks, boolean startVm) throws ConcurrentOperationException,
+            InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException,
+            StorageUnavailableException, ResourceUnavailableException {
+        
+        long id = _routerDao.getNextInSequence(Long.class, "id");
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("Creating the internal lb " + id + " in datacenter "  + dest.getDataCenter());
+        }
+
+        ServiceOfferingVO routerOffering = _serviceOfferingDao.findById(svcOffId);
+
+        // Internal lb is the network element, we don't know the hypervisor type yet.
+        // Try to allocate the domR twice using diff hypervisors, and when failed both times, throw the exception up
+        List<HypervisorType> hypervisors = getHypervisors(dest, plan, null);
+
+        int allocateRetry = 0;
+        int startRetry = 0;
+        DomainRouterVO internalLbVm = null;
+        for (Iterator<HypervisorType> iter = hypervisors.iterator(); iter.hasNext();) {
+            HypervisorType hType = iter.next();
+            try {
+                s_logger.debug("Allocating the Internal lb with the hypervisor type " + hType);
+                VMTemplateVO template = _templateDao.findRoutingTemplate(hType);
+
+                if (template == null) {
+                    s_logger.debug(hType + " won't support system vm, skip it");
+                    continue;
+                }
+
+                internalLbVm = new DomainRouterVO(id, routerOffering.getId(), internalLbProvider.getId(), 
+                VirtualMachineName.getRouterName(id, _instance), template.getId(), template.getHypervisorType(),
+                template.getGuestOSId(), owner.getDomainId(), owner.getId(), false, 0, false, 
+                RedundantState.UNKNOWN, false, false, vpcId);
+                internalLbVm.setRole(Role.INTERNAL_LB_VM);
+                internalLbVm = _itMgr.allocate(internalLbVm, template, routerOffering, networks, plan, null, owner);
+            } catch (InsufficientCapacityException ex) {
+                if (allocateRetry < 2 && iter.hasNext()) {
+                    s_logger.debug("Failed to allocate the Internal lb vm with hypervisor type " + hType + ", retrying one more time");
+                    continue;
+                } else {
+                    throw ex;
+                }
+            } finally {
+                allocateRetry++;
+            }
+
+            if (startVm) {
+                try {
+                    internalLbVm = startInternalLbVm(internalLbVm, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount(), params);
+                    break;
+                } catch (InsufficientCapacityException ex) {
+                    if (startRetry < 2 && iter.hasNext()) {
+                        s_logger.debug("Failed to start the Internal lb vm  " + internalLbVm + " with hypervisor type " + hType + ", " +
+                                "destroying it and recreating one more time");
+                        // destroy the internal lb vm
+                        destroyInternalLbVm(internalLbVm.getId(), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM);
+                        continue;
+                    } else {
+                        throw ex;
+                    }
+                } finally {
+                    startRetry++;
+                }
+            } else {
+                //return stopped internal lb vm
+                return internalLbVm;
+            }
+        }
+        return internalLbVm;
+    }
+    
+    
+    @Override
+    public DomainRouterVO startInternalLbVm(DomainRouterVO internalLbVm, User user, Account caller, Map<Param, Object> params) 
+            throws StorageUnavailableException, InsufficientCapacityException,
+    ConcurrentOperationException, ResourceUnavailableException {
+        s_logger.debug("Starting Internal LB VM " + internalLbVm);
+        if (_itMgr.start(internalLbVm, params, user, caller, null) != null) {
+            if (internalLbVm.isStopPending()) {
+                s_logger.info("Clear the stop pending flag of Internal LB VM " + internalLbVm.getHostName() + " after start router successfully!");
+                internalLbVm.setStopPending(false);
+                internalLbVm = _routerDao.persist(internalLbVm);
+            }
+            return _routerDao.findById(internalLbVm.getId());
+        } else {
+            return null;
+        }
+    }
+    
+    
+    protected List<HypervisorType> getHypervisors(DeployDestination dest, DeploymentPlan plan, 
+            List<HypervisorType> supportedHypervisors) throws InsufficientServerCapacityException {
+        List<HypervisorType> hypervisors = new ArrayList<HypervisorType>();
+
+        HypervisorType defaults = _resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId());
+        if (defaults != HypervisorType.None) {
+            hypervisors.add(defaults);
+        } else {
+            //if there is no default hypervisor, get it from the cluster
+            hypervisors = _resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId(), true,
+                plan.getPodId());
+        }
+
+        //keep only elements defined in supported hypervisors
+        StringBuilder hTypesStr = new StringBuilder();
+        if (supportedHypervisors != null && !supportedHypervisors.isEmpty()) {
+            hypervisors.retainAll(supportedHypervisors);
+            for (HypervisorType hType : supportedHypervisors) {
+                hTypesStr.append(hType).append(" ");
+            }
+        }
 
+        if (hypervisors.isEmpty()) {
+            throw new InsufficientServerCapacityException("Unable to create internal lb vm, " +
+                    "there are no clusters in the zone ", DataCenter.class, dest.getDataCenter().getId());
+        }
+        return hypervisors;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/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 b16105d..fcf650f 100644
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManager.java
@@ -106,4 +106,5 @@ public interface VirtualNetworkApplianceManager extends Manager, VirtualNetworkA
 	        List<DomainRouterVO> routers) throws ResourceUnavailableException;
 
     boolean applyLoadBalancingRules(Network network, List<? extends LoadBalancingRule> rules, List<? extends VirtualRouter> routers) throws ResourceUnavailableException;
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/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 46d55a7..1464991 100755
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -2490,7 +2490,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
             Network network = _networkModel.getNetwork(routerNic.getNetworkId());
             if (network.getTrafficType() == TrafficType.Guest) {
                 guestNetworks.add(network);
-            } 
+            }
         }
         
         answer = cmds.getAnswer("getDomRVersion");

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDao.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDao.java b/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDao.java
index 4de18da..6209949 100644
--- a/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDao.java
+++ b/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDao.java
@@ -17,10 +17,14 @@
 
 package org.apache.cloudstack.network.lb.dao;
 
+import java.util.List;
+
 import org.apache.cloudstack.network.lb.ApplicationLoadBalancerRuleVO;
 
+import com.cloud.network.rules.LoadBalancerContainer.Scheme;
 import com.cloud.utils.db.GenericDao;
+import com.cloud.utils.net.Ip;
 
 public interface ApplicationLoadBalancerRuleDao extends GenericDao<ApplicationLoadBalancerRuleVO, Long>{
-
+    List<ApplicationLoadBalancerRuleVO> listBySrcIpSrcNtwkIdAndScheme(Ip sourceIp, long sourceNetworkId, Scheme scheme);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java b/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java
index 3467462..ff6d38b 100644
--- a/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java
+++ b/server/src/org/apache/cloudstack/network/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java
@@ -17,15 +17,40 @@
 
 package org.apache.cloudstack.network.lb.dao;
 
+import java.util.List;
+
 import javax.ejb.Local;
 
 import org.apache.cloudstack.network.lb.ApplicationLoadBalancerRuleVO;
 import org.springframework.stereotype.Component;
 
+import com.cloud.network.rules.LoadBalancerContainer.Scheme;
 import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.net.Ip;
 
 @Component
 @Local(value = { ApplicationLoadBalancerRuleDao.class })
 public class ApplicationLoadBalancerRuleDaoImpl extends GenericDaoBase<ApplicationLoadBalancerRuleVO, Long> implements ApplicationLoadBalancerRuleDao{
+    protected final SearchBuilder<ApplicationLoadBalancerRuleVO> AllFieldsSearch;
+    
+    protected ApplicationLoadBalancerRuleDaoImpl() {
+        AllFieldsSearch = createSearchBuilder();
+        AllFieldsSearch.and("sourceIp", AllFieldsSearch.entity().getSourceIp(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("sourceIpNetworkId", AllFieldsSearch.entity().getSourceIpNetworkId(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.and("scheme", AllFieldsSearch.entity().getScheme(), SearchCriteria.Op.EQ);
+        AllFieldsSearch.done();
+    }
+
+    @Override
+    public List<ApplicationLoadBalancerRuleVO> listBySrcIpSrcNtwkIdAndScheme(Ip sourceIp, long sourceNetworkId, Scheme scheme) {
+        SearchCriteria<ApplicationLoadBalancerRuleVO> sc = AllFieldsSearch.create();
+        sc.setParameters("sourceIp", sourceIp);
+        sc.setParameters("sourceIpNetworkId", sourceNetworkId);
+        sc.setParameters("scheme", scheme);
+        return listBy(sc);
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/528796d4/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 8b9d263..aee4fcb 100644
--- a/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java
+++ b/server/test/com/cloud/vpc/MockVpcVirtualNetworkApplianceManager.java
@@ -47,7 +47,6 @@ import com.cloud.network.vpc.Vpc;
 import com.cloud.user.Account;
 import com.cloud.user.User;
 import com.cloud.uservm.UserVm;
-import com.cloud.utils.component.Manager;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.vm.DomainRouterVO;
 import com.cloud.vm.NicProfile;


Mime
View raw message