cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject [09/50] [abbrv] git commit: updated refs/heads/vpc-refactor to a8cb7c1
Date Fri, 03 Oct 2014 11:11:47 GMT
More refactoring and testing in the same classes

Conflicts:
	server/src/org/cloud/network/router/deployment/RouterDeploymentDefinitionBuilder.java


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

Branch: refs/heads/vpc-refactor
Commit: d0467731dee7dbd7de2581502127133ca7f91e96
Parents: 3f02162
Author: Antonio Fornie <afornie@schubergphilis.com>
Authored: Sun Jul 27 12:08:57 2014 -0500
Committer: Wilder Rodrigues <wrodrigues@schubergphilis.com>
Committed: Fri Oct 3 06:21:26 2014 +0200

----------------------------------------------------------------------
 .../network/router/NetworkGeneralHelper.java    | 179 +----
 .../VirtualNetworkApplianceManagerImpl.java     |   2 +-
 .../deployment/RouterDeploymentDefinition.java  | 415 +++++------
 .../RouterDeploymentDefinitionBuilder.java      |  77 +-
 .../VpcRouterDeploymentDefinition.java          |  88 ++-
 .../RouterDeploymentDefinitionTest.java         | 700 ++++++++++++++++++-
 6 files changed, 926 insertions(+), 535 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d0467731/server/src/com/cloud/network/router/NetworkGeneralHelper.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/NetworkGeneralHelper.java b/server/src/com/cloud/network/router/NetworkGeneralHelper.java
index 28ff08f..dd90bef 100644
--- a/server/src/com/cloud/network/router/NetworkGeneralHelper.java
+++ b/server/src/com/cloud/network/router/NetworkGeneralHelper.java
@@ -17,7 +17,6 @@
 package com.cloud.network.router;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -57,12 +56,8 @@ import com.cloud.maint.Version;
 import com.cloud.network.IpAddressManager;
 import com.cloud.network.Network;
 import com.cloud.network.NetworkModel;
-import com.cloud.network.Networks.BroadcastDomainType;
-import com.cloud.network.Networks.IsolationType;
 import com.cloud.network.Networks.TrafficType;
 import com.cloud.network.VirtualNetworkApplianceService;
-import com.cloud.network.VirtualRouterProvider;
-import com.cloud.network.addr.PublicIp;
 import com.cloud.network.dao.IPAddressDao;
 import com.cloud.network.dao.NetworkDao;
 import com.cloud.network.dao.NetworkVO;
@@ -70,7 +65,6 @@ import com.cloud.network.dao.UserIpv6AddressDao;
 import com.cloud.network.router.VirtualRouter.RedundantState;
 import com.cloud.network.router.VirtualRouter.Role;
 import com.cloud.network.vpn.Site2SiteVpnManager;
-import com.cloud.offering.NetworkOffering;
 import com.cloud.resource.ResourceManager;
 import com.cloud.service.ServiceOfferingVO;
 import com.cloud.service.dao.ServiceOfferingDao;
@@ -82,11 +76,8 @@ import com.cloud.storage.dao.VolumeDao;
 import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
 import com.cloud.user.User;
-import com.cloud.utils.Pair;
 import com.cloud.utils.exception.CloudRuntimeException;
-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.VirtualMachine.State;
@@ -413,14 +404,13 @@ public class NetworkGeneralHelper {
 
     //    @Override
     public DomainRouterVO deployRouter(final RouterDeploymentDefinition routerDeploymentDefinition,
-            final VirtualRouterProvider vrProvider, final long svcOffId,
             final LinkedHashMap<Network, List<? extends NicProfile>> networks,
             final boolean startRouter, final List<HypervisorType> supportedHypervisors)
                     throws InsufficientAddressCapacityException,
                     InsufficientServerCapacityException, InsufficientCapacityException,
                     StorageUnavailableException, ResourceUnavailableException {
 
-        final ServiceOfferingVO routerOffering = serviceOfferingDao.findById(svcOffId);
+        final ServiceOfferingVO routerOffering = serviceOfferingDao.findById(routerDeploymentDefinition.getOfferingId());
         final DeployDestination dest = routerDeploymentDefinition.getDest();
         final Account owner = routerDeploymentDefinition.getOwner();
 
@@ -475,7 +465,7 @@ public class NetworkGeneralHelper {
                 // routerDeploymentDefinition.getVpc().getId() ==> do not use VPC because it is not a VPC offering.
                 Long vpcId = routerDeploymentDefinition.getVpc() != null ? routerDeploymentDefinition.getVpc().getId() : null;
 
-                router = new DomainRouterVO(id, routerOffering.getId(), vrProvider.getId(),
+                router = new DomainRouterVO(id, routerOffering.getId(), routerDeploymentDefinition.getVirtualProvider().getId(),
                         VirtualMachineName.getRouterName(id, VirtualNwStatus.instance), template.getId(), template.getHypervisorType(),
                         template.getGuestOSId(), owner.getDomainId(), owner.getId(), routerDeploymentDefinition.isRedundant(), 0,
                         false, RedundantState.UNKNOWN, offerHA, false, vpcId);
@@ -596,169 +586,4 @@ public class NetworkGeneralHelper {
         throw new CloudRuntimeException(errMsg);
     }
 
-
-    //    @Override
-    public LinkedHashMap<Network, List<? extends NicProfile>> createRouterNetworks(
-            final RouterDeploymentDefinition routerDeploymentDefinition,
-            final Network guestNetwork, final Pair<Boolean, PublicIp> publicNetwork)
-                    throws ConcurrentOperationException,
-                    InsufficientAddressCapacityException {
-
-        boolean setupPublicNetwork = false;
-        if (publicNetwork != null) {
-            setupPublicNetwork = publicNetwork.first();
-        }
-
-        // Form networks
-        LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<Network, List<? extends NicProfile>>(
-                3);
-        // 1) Guest network
-        boolean hasGuestNetwork = false;
-        if (guestNetwork != null) {
-            s_logger.debug("Adding nic for Virtual Router in Guest network "
-                    + guestNetwork);
-            String defaultNetworkStartIp = null, defaultNetworkStartIpv6 = null;
-            if (!setupPublicNetwork) {
-                final Nic placeholder = networkModel
-                        .getPlaceholderNicForRouter(guestNetwork,
-                                routerDeploymentDefinition.getPodId());
-                if (guestNetwork.getCidr() != null) {
-                    if (placeholder != null
-                            && placeholder.getIp4Address() != null) {
-                        s_logger.debug("Requesting ipv4 address "
-                                + placeholder.getIp4Address()
-                                + " stored in placeholder nic for the network "
-                                + guestNetwork);
-                        defaultNetworkStartIp = placeholder.getIp4Address();
-                    } else {
-                        final String startIp = networkModel
-                                .getStartIpAddress(guestNetwork.getId());
-                        if (startIp != null
-                                && ipAddressDao.findByIpAndSourceNetworkId(
-                                        guestNetwork.getId(), startIp)
-                                        .getAllocatedTime() == null) {
-                            defaultNetworkStartIp = startIp;
-                        } else if (s_logger.isDebugEnabled()) {
-                            s_logger.debug("First ipv4 "
-                                    + startIp
-                                    + " in network id="
-                                    + guestNetwork.getId()
-                                    + " is already allocated, can't use it for domain router; will get random ip address from the range");
-                        }
-                    }
-                }
-
-                if (guestNetwork.getIp6Cidr() != null) {
-                    if (placeholder != null
-                            && placeholder.getIp6Address() != null) {
-                        s_logger.debug("Requesting ipv6 address "
-                                + placeholder.getIp6Address()
-                                + " stored in placeholder nic for the network "
-                                + guestNetwork);
-                        defaultNetworkStartIpv6 = placeholder.getIp6Address();
-                    } else {
-                        final String startIpv6 = networkModel
-                                .getStartIpv6Address(guestNetwork.getId());
-                        if (startIpv6 != null
-                                && ipv6Dao.findByNetworkIdAndIp(
-                                        guestNetwork.getId(), startIpv6) == null) {
-                            defaultNetworkStartIpv6 = startIpv6;
-                        } else if (s_logger.isDebugEnabled()) {
-                            s_logger.debug("First ipv6 "
-                                    + startIpv6
-                                    + " in network id="
-                                    + guestNetwork.getId()
-                                    + " is already allocated, can't use it for domain router; will get random ipv6 address from the range");
-                        }
-                    }
-                }
-            }
-
-            final NicProfile gatewayNic = new NicProfile(defaultNetworkStartIp,
-                    defaultNetworkStartIpv6);
-            if (setupPublicNetwork) {
-                if (routerDeploymentDefinition.isRedundant()) {
-                    gatewayNic.setIp4Address(ipAddrMgr.acquireGuestIpAddress(
-                            guestNetwork, null));
-                } else {
-                    gatewayNic.setIp4Address(guestNetwork.getGateway());
-                }
-                gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri());
-                gatewayNic.setBroadcastType(guestNetwork
-                        .getBroadcastDomainType());
-                gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri());
-                gatewayNic.setMode(guestNetwork.getMode());
-                final String gatewayCidr = guestNetwork.getCidr();
-                gatewayNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr));
-            } else {
-                gatewayNic.setDefaultNic(true);
-            }
-
-            networks.put(guestNetwork,
-                    new ArrayList<NicProfile>(Arrays.asList(gatewayNic)));
-            hasGuestNetwork = true;
-        }
-
-        // 2) Control network
-        s_logger.debug("Adding nic for Virtual Router in Control network ");
-        List<? extends NetworkOffering> offerings = networkModel
-                .getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork);
-        NetworkOffering controlOffering = offerings.get(0);
-        Network controlConfig = networkMgr.setupNetwork(VirtualNwStatus.account,
-                controlOffering, routerDeploymentDefinition.getPlan(), null, null, false).get(0);
-        networks.put(controlConfig, new ArrayList<NicProfile>());
-        // 3) Public network
-        if (setupPublicNetwork) {
-            final PublicIp sourceNatIp = publicNetwork.second();
-            s_logger.debug("Adding nic for Virtual Router in Public network ");
-            // if source nat service is supported by the network, get the source
-            // nat ip address
-            final NicProfile defaultNic = new NicProfile();
-            defaultNic.setDefaultNic(true);
-            defaultNic.setIp4Address(sourceNatIp.getAddress().addr());
-            defaultNic.setGateway(sourceNatIp.getGateway());
-            defaultNic.setNetmask(sourceNatIp.getNetmask());
-            defaultNic.setMacAddress(sourceNatIp.getMacAddress());
-            // get broadcast from public network
-            final Network pubNet = networkDao.findById(sourceNatIp
-                    .getNetworkId());
-            if (pubNet.getBroadcastDomainType() == BroadcastDomainType.Vxlan) {
-                defaultNic.setBroadcastType(BroadcastDomainType.Vxlan);
-                defaultNic.setBroadcastUri(BroadcastDomainType.Vxlan
-                        .toUri(sourceNatIp.getVlanTag()));
-                defaultNic.setIsolationUri(BroadcastDomainType.Vxlan
-                        .toUri(sourceNatIp.getVlanTag()));
-            } else {
-                defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
-                defaultNic.setBroadcastUri(BroadcastDomainType.Vlan
-                        .toUri(sourceNatIp.getVlanTag()));
-                defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp
-                        .getVlanTag()));
-            }
-            if (hasGuestNetwork) {
-                defaultNic.setDeviceId(2);
-            }
-            final NetworkOffering publicOffering = networkModel
-                    .getSystemAccountNetworkOfferings(
-                            NetworkOffering.SystemPublicNetwork).get(0);
-            final List<? extends Network> publicNetworks = networkMgr
-                    .setupNetwork(VirtualNwStatus.account, publicOffering, routerDeploymentDefinition.getPlan(),
-                            null, null, false);
-            final String publicIp = defaultNic.getIp4Address();
-            // We want to use the identical MAC address for RvR on public
-            // interface if possible
-            final NicVO peerNic = nicDao.findByIp4AddressAndNetworkId(
-                    publicIp, publicNetworks.get(0).getId());
-            if (peerNic != null) {
-                s_logger.info("Use same MAC as previous RvR, the MAC is "
-                        + peerNic.getMacAddress());
-                defaultNic.setMacAddress(peerNic.getMacAddress());
-            }
-            networks.put(publicNetworks.get(0), new ArrayList<NicProfile>(
-                    Arrays.asList(defaultNic)));
-        }
-
-        return networks;
-    }
-
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d0467731/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 ce00b88..e95a079 100755
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -662,7 +662,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
                 useLocalStorage, true, null, true, VirtualMachine.Type.DomainRouter, true);
         offering.setUniqueName(ServiceOffering.routerDefaultOffUniqueName);
         offering = _serviceOfferingDao.persistSystemServiceOffering(offering);
-        routerDeploymentManagerBuilder.setOffering(offering);
+        routerDeploymentManagerBuilder.setOfferingId(offering.getId());
 
         // this can sometimes happen, if DB is manually or programmatically
         // manipulated

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d0467731/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinition.java
----------------------------------------------------------------------
diff --git a/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinition.java b/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinition.java
index a5e3ec7..f93ce3c 100644
--- a/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinition.java
+++ b/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinition.java
@@ -62,10 +62,8 @@ import com.cloud.network.router.VpcVirtualNetworkHelperImpl;
 import com.cloud.network.vpc.Vpc;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.offerings.dao.NetworkOfferingDao;
-import com.cloud.service.ServiceOfferingVO;
 import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
-import com.cloud.utils.Pair;
 import com.cloud.utils.db.DB;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
@@ -100,6 +98,7 @@ public class RouterDeploymentDefinition {
     protected NicDao nicDao;
     protected UserIpv6AddressDao ipv6Dao;
     protected IPAddressDao ipAddressDao;
+    protected VirtualRouterProvider vrProvider;
 
 
     @Inject
@@ -115,9 +114,10 @@ public class RouterDeploymentDefinition {
     protected boolean isRedundant;
     protected DeploymentPlan plan;
     protected List<DomainRouterVO> routers = new ArrayList<>();
-    protected ServiceOfferingVO offering;
+    protected Long offeringId;
     protected Long tableLockId;
-
+    protected boolean publicNetwork;
+    protected PublicIp sourceNatIp;
 
     protected RouterDeploymentDefinition(final Network guestNetwork, final DeployDestination dest,
             final Account owner, final Map<Param, Object> params, final boolean isRedundant) {
@@ -129,9 +129,10 @@ public class RouterDeploymentDefinition {
         this.isRedundant = isRedundant;
     }
 
-    protected void setOffering(ServiceOfferingVO offering) {
-        this.offering = offering;
+    public Long getOfferingId() {
+        return this.offeringId;
     }
+
     public Vpc getVpc() {
         return null;
     }
@@ -166,12 +167,12 @@ public class RouterDeploymentDefinition {
         return routers;
     }
 
-    public boolean isBasic() {
-        return this.dest.getDataCenter().getNetworkType() == NetworkType.Basic;
+    public VirtualRouterProvider getVirtualProvider() {
+        return this.vrProvider;
     }
 
-    public Long getOfferingId() {
-        return offering == null ? null : offering.getId();
+    public boolean isBasic() {
+        return this.dest.getDataCenter().getNetworkType() == NetworkType.Basic;
     }
 
     protected void generateDeploymentPlan() {
@@ -190,7 +191,7 @@ public class RouterDeploymentDefinition {
             throws InsufficientCapacityException,
             ConcurrentOperationException, ResourceUnavailableException {
 
-        findOrDeployVirtualRouter();
+        this.findOrDeployVirtualRouter();
 
         return nwHelper.startRouters(this);
     }
@@ -288,225 +289,120 @@ public class RouterDeploymentDefinition {
         return destinations;
     }
 
-    protected void executeDeployment()
-            throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
-        // 2) Figure out required routers count
-        int routerCount = 1;
-        if (this.isRedundant) {
-            routerCount = 2;
-            //Check current redundant routers, if possible(all routers are stopped), reset the priority
-            if (this.routers.size() != 0) {
-                checkAndResetPriorityOfRedundantRouter(this.routers);
-            }
+    protected int getNumberOfRoutersToDeploy() {
+        // TODO Are we sure this makes sense? Somebody said 5 was too many?
+        if (this.routers.size() >= 5) {
+            logger.error("Too many redundant routers!");
         }
 
         // If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1
-        if (this.routers.size() >= routerCount) {
-            return;
+        int routersExpected = 1;
+        if (this.isRedundant) {
+            routersExpected = 2;
         }
+        return routersExpected < this.routers.size() ?
+                0 : routersExpected - this.routers.size();
+    }
 
-        if (this.routers.size() >= 5) {
-            logger.error("Too much redundant routers!");
-        }
+    /**
+     * Executes preparation and deployment of the routers. After this method ends, {@link this#routers}
+     * should have all of the deployed routers ready for start, and no more.
+     *
+     * @throws ConcurrentOperationException
+     * @throws InsufficientCapacityException
+     * @throws ResourceUnavailableException
+     */
+    protected void executeDeployment()
+            throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
 
-        // Check if providers are supported in the physical networks
-        final Type type = Type.VirtualRouter;
-        final Long physicalNetworkId = networkModel.getPhysicalNetworkId(guestNetwork);
-        final PhysicalNetworkServiceProvider provider = physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString());
-        if (provider == null) {
-            throw new CloudRuntimeException("Cannot find service provider " + type.toString() + " in physical network " + physicalNetworkId);
-        }
-        final VirtualRouterProvider vrProvider = vrProviderDao.findByNspIdAndType(provider.getId(), type);
-        if (vrProvider == null) {
-            throw new CloudRuntimeException("Cannot find virtual router provider " + type.toString() + " as service provider " + provider.getId());
-        }
+        //Check current redundant routers, if possible(all routers are stopped), reset the priority
+        // TODO Why shouldn't we call this method also for VPC
+        setupPriorityOfRedundantRouter();
 
-        if (networkModel.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) {
-            this.owner = accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
-        }
+        if (this.getNumberOfRoutersToDeploy() > 0) {
+            if (networkModel.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) {
+                this.owner = accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
+            }
 
-        // Check if public network has to be set on VR
-        boolean publicNetwork = false;
-        if (networkModel.isProviderSupportServiceInNetwork(guestNetwork.getId(), Service.SourceNat, Provider.VirtualRouter)) {
-            publicNetwork = true;
-        }
-        if (this.isRedundant && !publicNetwork) {
-            logger.error("Didn't support redundant virtual router without public network!");
-            this.routers = null;
-            return;
-        }
+            // Check if public network has to be set on VR
+            this.publicNetwork = networkModel.isProviderSupportServiceInNetwork(
+                            guestNetwork.getId(), Service.SourceNat, Provider.VirtualRouter);
 
-        Long offeringId = networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
-        if (offeringId == null) {
-            offeringId = getOfferingId();
+            if (this.isRedundant && !this.publicNetwork) {
+                // TODO Shouldn't be this throw an exception instead of log error and empty list of routers
+                logger.error("Didn't support redundant virtual router without public network!");
+                this.routers = new ArrayList<>();
+            } else {
+                this.findVirtualProvider();
+                this.findOfferingId();
+                this.findSourceNatIP();
+                this.deployAllVirtualRouters();
+            }
         }
+    }
 
-        PublicIp sourceNatIp = null;
-        if (publicNetwork) {
-            sourceNatIp = ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(
-                    this.owner, guestNetwork);
+    protected void findSourceNatIP() throws InsufficientAddressCapacityException, ConcurrentOperationException {
+        this.sourceNatIp = null;
+        if (this.publicNetwork) {
+            this.sourceNatIp = this.ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(
+                    this.owner,this.guestNetwork);
         }
+    }
 
-        // 3) deploy virtual router(s)
-        final int count = routerCount - this.routers.size();
-        for (int i = 0; i < count; i++) {
-            LinkedHashMap<Network, List<? extends NicProfile>> networks =
-                    createRouterNetworks(new Pair<Boolean, PublicIp>(
-                    publicNetwork, sourceNatIp));
-            //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
-            DomainRouterVO router = nwHelper.deployRouter(this, vrProvider, offeringId, networks, false, null);
-
-            if (router != null) {
-                routerDao.addRouterToGuestNetwork(router, guestNetwork);
-                this.routers.add(router);
-            }
+    protected void findOfferingId() {
+        Long networkOfferingId = networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
+        if (networkOfferingId != null) {
+            this.offeringId = networkOfferingId;
         }
     }
 
-    @DB
-    protected void findOrDeployVirtualRouterOLD()
-            throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
+    protected void findVirtualProvider() {
+        // Check if providers are supported in the physical networks
+        final Type type = Type.VirtualRouter;
+        final Long physicalNetworkId = networkModel.getPhysicalNetworkId(guestNetwork);
+        final PhysicalNetworkServiceProvider provider =
+                physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString());
 
-        final Network lock = networkDao.acquireInLockTable(guestNetwork.getId(), NetworkOrchestrationService.NetworkLockTimeout.value());
-        if (lock == null) {
-            throw new ConcurrentOperationException("Unable to lock network " + guestNetwork.getId());
+        if (provider == null) {
+            throw new CloudRuntimeException(
+                    String.format("Cannot find service provider %s  in physical network %s",
+                            type.toString(), physicalNetworkId));
         }
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("Lock is acquired for network id " + lock.getId() + " as a part of router startup in " + dest);
+        this.vrProvider = vrProviderDao.findByNspIdAndType(provider.getId(), type);
+        if (this.vrProvider == null) {
+            throw new CloudRuntimeException(
+                    String.format("Cannot find virtual router provider %s as service provider %s",
+                            type.toString(), 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;
-
-            // 1) Get deployment plan and find out the list of routers
-
-            // dest has pod=null, for Basic Zone findOrDeployVRs for all Pods
-            final List<DeployDestination> destinations = new ArrayList<DeployDestination>();
-
-            // for basic zone, if 'dest' has pod set to null then this is network restart scenario otherwise it is a vm deployment scenario
-            if (this.isBasic() && dest.getPod() == null) {
-                // Find all pods in the data center with running or starting user vms
-                final long dcId = dest.getDataCenter().getId();
-                final List<HostPodVO> pods = listByDataCenterIdVMTypeAndStates(dcId, VirtualMachine.Type.User, VirtualMachine.State.Starting, VirtualMachine.State.Running);
-
-                // Loop through all the pods skip those with running or starting VRs
-                for (final HostPodVO pod : pods) {
-                    // Get list of VRs in starting or running state
-                    final long podId = pod.getId();
-                    final List<DomainRouterVO> virtualRouters = routerDao.listByPodIdAndStates(podId, VirtualMachine.State.Starting, VirtualMachine.State.Running);
-
-                    assert (virtualRouters.size() <= 1) : "Pod can have utmost one VR in Basic Zone, please check!";
-
-                    // Add virtualRouters to the routers, this avoids the situation when
-                    // all routers are skipped and VirtualRouterElement throws exception
-                    this.routers.addAll(virtualRouters);
-
-                    // If List size is one, we already have a starting or running VR, skip deployment
-                    if (virtualRouters.size() == 1) {
-                        logger.debug("Skipping VR deployment: Found a running or starting VR in Pod " + pod.getName() + " id=" + podId);
-                        continue;
-                    }
-                    // Add new DeployDestination for this pod
-                    destinations.add(new DeployDestination(dest.getDataCenter(), pod, null, null));
-                }
-            } else {
-                // Else, just add the supplied dest
-                destinations.add(dest);
-            }
-
-            // Except for Basic Zone, the for loop will iterate only once
-            for (final DeployDestination destination : destinations) {
-                this.dest = destination;
-                planDeploymentRouters();
-                this.generateDeploymentPlan();
-
-                // 2) Figure out required routers count
-                int routerCount = 1;
-                if (this.isRedundant) {
-                    routerCount = 2;
-                    //Check current redundant routers, if possible(all routers are stopped), reset the priority
-                    if (this.routers.size() != 0) {
-                        checkAndResetPriorityOfRedundantRouter(this.routers);
-                    }
-                }
-
-                // If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1
-                if (this.routers.size() >= routerCount) {
-                    return;
-                }
-
-                if (this.routers.size() >= 5) {
-                    logger.error("Too much redundant routers!");
-                }
-
-                // Check if providers are supported in the physical networks
-                final Type type = Type.VirtualRouter;
-                final Long physicalNetworkId = networkModel.getPhysicalNetworkId(guestNetwork);
-                final PhysicalNetworkServiceProvider provider = physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString());
-                if (provider == null) {
-                    throw new CloudRuntimeException("Cannot find service provider " + type.toString() + " in physical network " + physicalNetworkId);
-                }
-                final VirtualRouterProvider vrProvider = vrProviderDao.findByNspIdAndType(provider.getId(), type);
-                if (vrProvider == null) {
-                    throw new CloudRuntimeException("Cannot find virtual router provider " + type.toString() + " as service provider " + provider.getId());
-                }
-
-                if (networkModel.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) {
-                    this.owner = accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
-                }
-
-                // Check if public network has to be set on VR
-                boolean publicNetwork = false;
-                if (networkModel.isProviderSupportServiceInNetwork(guestNetwork.getId(), Service.SourceNat, Provider.VirtualRouter)) {
-                    publicNetwork = true;
-                }
-                if (this.isRedundant && !publicNetwork) {
-                    logger.error("Didn't support redundant virtual router without public network!");
-                    this.routers = null;
-                    return;
-                }
-
-                Long offeringId = networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
-                if (offeringId == null) {
-                    offeringId = getOfferingId();
-                }
+    protected void deployAllVirtualRouters()
+            throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
 
-                PublicIp sourceNatIp = null;
-                if (publicNetwork) {
-                    sourceNatIp = ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(
-                            this.owner, guestNetwork);
-                }
+        int routersToDeploy = this.getNumberOfRoutersToDeploy();
+        for(int i = 0; i < routersToDeploy; i++) {
+            LinkedHashMap<Network, List<? extends NicProfile>> networks =
+                    createRouterNetworks();
+            //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
+            DomainRouterVO router = nwHelper.deployRouter(this, networks, false, null);
 
-                // 3) deploy virtual router(s)
-                final int count = routerCount - this.routers.size();
-                for (int i = 0; i < count; i++) {
-                    LinkedHashMap<Network, List<? extends NicProfile>> networks =
-                            createRouterNetworks(new Pair<Boolean, PublicIp>(
-                            publicNetwork, sourceNatIp));
-                    //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
-                    DomainRouterVO router = nwHelper.deployRouter(this, vrProvider, offeringId, networks, false, null);
-
-                    if (router != null) {
-                        routerDao.addRouterToGuestNetwork(router, guestNetwork);
-                        this.routers.add(router);
-                    }
-                }
-            }
-        } finally {
-            if (lock != null) {
-                networkDao.releaseFromLockTable(lock.getId());
-                if (logger.isDebugEnabled()) {
-                    logger.debug("Lock is released for network id " + lock.getId() + " as a part of router startup in " + dest);
-                }
+            if (router != null) {
+                this.routerDao.addRouterToGuestNetwork(router, this.guestNetwork);
+                this.routers.add(router);
             }
         }
     }
 
-
+    /**
+     * Lists all pods given a Data Center Id, a {@link VirtualMachine.Type} and a list of
+     * {@link VirtualMachine.State}
+     *
+     * @param id
+     * @param type
+     * @param states
+     * @return
+     */
     protected List<HostPodVO> listByDataCenterIdVMTypeAndStates(final long id, final VirtualMachine.Type type, final VirtualMachine.State... states) {
         final SearchBuilder<VMInstanceVO> vmInstanceSearch = vmDao.createSearchBuilder();
         vmInstanceSearch.and("type", vmInstanceSearch.entity().getType(), SearchCriteria.Op.EQ);
@@ -535,69 +431,75 @@ public class RouterDeploymentDefinition {
         }
     }
 
-    private void checkAndResetPriorityOfRedundantRouter(final List<DomainRouterVO> routers) {
-        boolean allStopped = true;
-        for (final DomainRouterVO router : routers) {
+    /**
+     * Routers need reset if at least one of the routers is not redundant or stopped.
+     *
+     * @return
+     */
+    protected boolean routersNeedReset() {
+        boolean needReset = true;
+        for (final DomainRouterVO router : this.routers) {
             if (!router.getIsRedundantRouter() || router.getState() != VirtualMachine.State.Stopped) {
-                allStopped = false;
+                needReset = false;
                 break;
             }
         }
-        if (!allStopped) {
-            return;
-        }
 
-        for (final DomainRouterVO router : routers) {
-            // getUpdatedPriority() would update the value later
-            router.setPriority(0);
-            router.setIsPriorityBumpUp(false);
-            routerDao.update(router.getId(), router);
-        }
+        return needReset;
     }
-    protected LinkedHashMap<Network, List<? extends NicProfile>> createRouterNetworks(
-            final Pair<Boolean, PublicIp> publicNetwork)
-                    throws ConcurrentOperationException, InsufficientAddressCapacityException {
 
-        final Network guestNetwork = this.guestNetwork;
-        boolean setupPublicNetwork = false;
-        if (publicNetwork != null) {
-            setupPublicNetwork = publicNetwork.first();
-        }
+    /**
+     * Only for redundant deployment and if any routers needed reset, we shall reset all
+     * routers priorities
+     */
+    protected void setupPriorityOfRedundantRouter() {
+            if (this.isRedundant && this.routersNeedReset()) {
+                for (final DomainRouterVO router : this.routers) {
+                    // getUpdatedPriority() would update the value later
+                    router.setPriority(0);
+                    router.setIsPriorityBumpUp(false);
+                    routerDao.update(router.getId(), router);
+                }
+            }
+    }
+
+    protected LinkedHashMap<Network, List<? extends NicProfile>> createRouterNetworks()
+                    throws ConcurrentOperationException, InsufficientAddressCapacityException {
 
         //Form networks
         LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<Network, List<? extends NicProfile>>(3);
         //1) Guest network
         boolean hasGuestNetwork = false;
-        if (guestNetwork != null) {
-            logger.debug("Adding nic for Virtual Router in Guest network " + guestNetwork);
+        if (this.guestNetwork != null) {
+            logger.debug("Adding nic for Virtual Router in Guest network " + this.guestNetwork);
             String defaultNetworkStartIp = null, defaultNetworkStartIpv6 = null;
-            if (!setupPublicNetwork) {
-                final Nic placeholder = networkModel.getPlaceholderNicForRouter(guestNetwork, this.getPodId());
-                if (guestNetwork.getCidr() != null) {
+            if (!this.publicNetwork) {
+                final Nic placeholder = networkModel.getPlaceholderNicForRouter(this.guestNetwork, this.getPodId());
+                if (this.guestNetwork.getCidr() != null) {
                     if (placeholder != null && placeholder.getIp4Address() != null) {
-                        logger.debug("Requesting ipv4 address " + placeholder.getIp4Address() + " stored in placeholder nic for the network " + guestNetwork);
+                        logger.debug("Requesting ipv4 address " + placeholder.getIp4Address() + " stored in placeholder nic for the network " + this.guestNetwork);
                         defaultNetworkStartIp = placeholder.getIp4Address();
                     } else {
-                        final String startIp = networkModel.getStartIpAddress(guestNetwork.getId());
-                        if (startIp != null && ipAddressDao.findByIpAndSourceNetworkId(guestNetwork.getId(), startIp).getAllocatedTime() == null) {
+                        final String startIp = networkModel.getStartIpAddress(this.guestNetwork.getId());
+                        if (startIp != null && ipAddressDao.findByIpAndSourceNetworkId(this.guestNetwork.getId(), startIp).getAllocatedTime() == null) {
                             defaultNetworkStartIp = startIp;
                         } else if (logger.isDebugEnabled()) {
-                            logger.debug("First ipv4 " + startIp + " in network id=" + guestNetwork.getId() +
+                            logger.debug("First ipv4 " + startIp + " in network id=" + this.guestNetwork.getId() +
                                     " is already allocated, can't use it for domain router; will get random ip address from the range");
                         }
                     }
                 }
 
-                if (guestNetwork.getIp6Cidr() != null) {
+                if (this.guestNetwork.getIp6Cidr() != null) {
                     if (placeholder != null && placeholder.getIp6Address() != null) {
-                        logger.debug("Requesting ipv6 address " + placeholder.getIp6Address() + " stored in placeholder nic for the network " + guestNetwork);
+                        logger.debug("Requesting ipv6 address " + placeholder.getIp6Address() + " stored in placeholder nic for the network " + this.guestNetwork);
                         defaultNetworkStartIpv6 = placeholder.getIp6Address();
                     } else {
-                        final String startIpv6 = networkModel.getStartIpv6Address(guestNetwork.getId());
-                        if (startIpv6 != null && ipv6Dao.findByNetworkIdAndIp(guestNetwork.getId(), startIpv6) == null) {
+                        final String startIpv6 = networkModel.getStartIpv6Address(this.guestNetwork.getId());
+                        if (startIpv6 != null && ipv6Dao.findByNetworkIdAndIp(this.guestNetwork.getId(), startIpv6) == null) {
                             defaultNetworkStartIpv6 = startIpv6;
                         } else if (logger.isDebugEnabled()) {
-                            logger.debug("First ipv6 " + startIpv6 + " in network id=" + guestNetwork.getId() +
+                            logger.debug("First ipv6 " + startIpv6 + " in network id=" + this.guestNetwork.getId() +
                                     " is already allocated, can't use it for domain router; will get random ipv6 address from the range");
                         }
                     }
@@ -605,23 +507,23 @@ public class RouterDeploymentDefinition {
             }
 
             final NicProfile gatewayNic = new NicProfile(defaultNetworkStartIp, defaultNetworkStartIpv6);
-            if (setupPublicNetwork) {
+            if (this.publicNetwork) {
                 if (this.isRedundant) {
-                    gatewayNic.setIp4Address(ipAddrMgr.acquireGuestIpAddress(guestNetwork, null));
+                    gatewayNic.setIp4Address(ipAddrMgr.acquireGuestIpAddress(this.guestNetwork, null));
                 } else {
-                    gatewayNic.setIp4Address(guestNetwork.getGateway());
+                    gatewayNic.setIp4Address(this.guestNetwork.getGateway());
                 }
-                gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri());
-                gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType());
-                gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri());
-                gatewayNic.setMode(guestNetwork.getMode());
-                final String gatewayCidr = guestNetwork.getCidr();
+                gatewayNic.setBroadcastUri(this.guestNetwork.getBroadcastUri());
+                gatewayNic.setBroadcastType(this.guestNetwork.getBroadcastDomainType());
+                gatewayNic.setIsolationUri(this.guestNetwork.getBroadcastUri());
+                gatewayNic.setMode(this.guestNetwork.getMode());
+                final String gatewayCidr = this.guestNetwork.getCidr();
                 gatewayNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr));
             } else {
                 gatewayNic.setDefaultNic(true);
             }
 
-            networks.put(guestNetwork, new ArrayList<NicProfile>(Arrays.asList(gatewayNic)));
+            networks.put(this.guestNetwork, new ArrayList<NicProfile>(Arrays.asList(gatewayNic)));
             hasGuestNetwork = true;
         }
 
@@ -633,26 +535,25 @@ public class RouterDeploymentDefinition {
                 null, null, false).get(0);
         networks.put(controlConfig, new ArrayList<NicProfile>());
         //3) Public network
-        if (setupPublicNetwork) {
-            final PublicIp sourceNatIp = publicNetwork.second();
+        if (publicNetwork) {
             logger.debug("Adding nic for Virtual Router in Public network ");
             //if source nat service is supported by the network, get the source nat ip address
             final NicProfile defaultNic = new NicProfile();
             defaultNic.setDefaultNic(true);
-            defaultNic.setIp4Address(sourceNatIp.getAddress().addr());
-            defaultNic.setGateway(sourceNatIp.getGateway());
-            defaultNic.setNetmask(sourceNatIp.getNetmask());
-            defaultNic.setMacAddress(sourceNatIp.getMacAddress());
+            defaultNic.setIp4Address(this.sourceNatIp.getAddress().addr());
+            defaultNic.setGateway(this.sourceNatIp.getGateway());
+            defaultNic.setNetmask(this.sourceNatIp.getNetmask());
+            defaultNic.setMacAddress(this.sourceNatIp.getMacAddress());
             // get broadcast from public network
             final Network pubNet = networkDao.findById(sourceNatIp.getNetworkId());
             if (pubNet.getBroadcastDomainType() == BroadcastDomainType.Vxlan) {
                 defaultNic.setBroadcastType(BroadcastDomainType.Vxlan);
-                defaultNic.setBroadcastUri(BroadcastDomainType.Vxlan.toUri(sourceNatIp.getVlanTag()));
-                defaultNic.setIsolationUri(BroadcastDomainType.Vxlan.toUri(sourceNatIp.getVlanTag()));
+                defaultNic.setBroadcastUri(BroadcastDomainType.Vxlan.toUri(this.sourceNatIp.getVlanTag()));
+                defaultNic.setIsolationUri(BroadcastDomainType.Vxlan.toUri(this.sourceNatIp.getVlanTag()));
             } else {
                 defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
-                defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag()));
-                defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag()));
+                defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(this.sourceNatIp.getVlanTag()));
+                defaultNic.setIsolationUri(IsolationType.Vlan.toUri(this.sourceNatIp.getVlanTag()));
             }
             if (hasGuestNetwork) {
                 defaultNic.setDeviceId(2);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d0467731/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinitionBuilder.java
----------------------------------------------------------------------
diff --git a/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinitionBuilder.java b/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinitionBuilder.java
index a5662b5..c9f2f6a 100644
--- a/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinitionBuilder.java
+++ b/server/src/org/cloud/network/router/deployment/RouterDeploymentDefinitionBuilder.java
@@ -20,12 +20,13 @@ import com.cloud.network.dao.PhysicalNetworkDao;
 import com.cloud.network.dao.PhysicalNetworkServiceProviderDao;
 import com.cloud.network.dao.UserIpv6AddressDao;
 import com.cloud.network.dao.VirtualRouterProviderDao;
+import com.cloud.network.router.NetworkGeneralHelper;
+import com.cloud.network.router.VpcVirtualNetworkHelperImpl;
 import com.cloud.network.vpc.Vpc;
 import com.cloud.network.vpc.VpcManager;
 import com.cloud.network.vpc.dao.VpcDao;
 import com.cloud.network.vpc.dao.VpcOfferingDao;
 import com.cloud.offerings.dao.NetworkOfferingDao;
-import com.cloud.service.ServiceOfferingVO;
 import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
 import com.cloud.vm.DomainRouterVO;
@@ -39,7 +40,7 @@ public class RouterDeploymentDefinitionBuilder {
     @Inject
     protected NetworkDao networkDao;
     @Inject
-    private DomainRouterDao routerDao = null;
+    private final DomainRouterDao routerDao = null;
     @Inject
     private PhysicalNetworkServiceProviderDao physicalProviderDao;
     @Inject
@@ -75,53 +76,55 @@ public class RouterDeploymentDefinitionBuilder {
     @Inject
     private VlanDao vlanDao;
 
-    protected ServiceOfferingVO offering;
+    @Inject
+    protected NetworkGeneralHelper nwHelper;
+    @Inject
+    protected VpcVirtualNetworkHelperImpl vpcHelper;
 
-    public void setOffering(ServiceOfferingVO offering) {
-        this.offering = offering;
+    protected Long offeringId;
+
+    public void setOfferingId(final Long offeringId) {
+        this.offeringId = offeringId;
     }
 
     public IntermediateStateBuilder create() {
         return new IntermediateStateBuilder(this);
     }
 
-    protected RouterDeploymentDefinition injectDependencies(
-            final RouterDeploymentDefinition routerDeploymentDefinition) {
-
-        routerDeploymentDefinition.networkDao = this.networkDao;
-        routerDeploymentDefinition.routerDao = this.routerDao;
-        routerDeploymentDefinition.physicalProviderDao = this.physicalProviderDao;
-        routerDeploymentDefinition.networkModel = this.networkModel;
-        routerDeploymentDefinition.vrProviderDao = this.vrProviderDao;
-        routerDeploymentDefinition.networkOfferingDao = this.networkOfferingDao;
-        routerDeploymentDefinition.ipAddrMgr = this.ipAddrMgr;
-        routerDeploymentDefinition.vmDao = this.vmDao;
-        routerDeploymentDefinition.podDao = this.podDao;
-        routerDeploymentDefinition.accountMgr = this.accountMgr;
-        routerDeploymentDefinition.networkMgr = this.networkMgr;
-        routerDeploymentDefinition.nicDao = this.nicDao;
-        routerDeploymentDefinition.ipv6Dao = this.ipv6Dao;
-        routerDeploymentDefinition.ipAddressDao = this.ipAddressDao;
-        routerDeploymentDefinition.offering = this.offering;
+    protected RouterDeploymentDefinition injectDependencies(final RouterDeploymentDefinition routerDeploymentDefinition) {
+
+        routerDeploymentDefinition.networkDao = networkDao;
+        routerDeploymentDefinition.routerDao = routerDao;
+        routerDeploymentDefinition.physicalProviderDao = physicalProviderDao;
+        routerDeploymentDefinition.networkModel = networkModel;
+        routerDeploymentDefinition.vrProviderDao = vrProviderDao;
+        routerDeploymentDefinition.networkOfferingDao = networkOfferingDao;
+        routerDeploymentDefinition.ipAddrMgr = ipAddrMgr;
+        routerDeploymentDefinition.vmDao = vmDao;
+        routerDeploymentDefinition.podDao = podDao;
+        routerDeploymentDefinition.accountMgr = accountMgr;
+        routerDeploymentDefinition.networkMgr = networkMgr;
+        routerDeploymentDefinition.nicDao = nicDao;
+        routerDeploymentDefinition.ipv6Dao = ipv6Dao;
+        routerDeploymentDefinition.ipAddressDao = ipAddressDao;
+        routerDeploymentDefinition.offeringId = offeringId;
 
         if (routerDeploymentDefinition instanceof VpcRouterDeploymentDefinition) {
-            this.injectVpcDependencies((VpcRouterDeploymentDefinition) routerDeploymentDefinition);
+            injectVpcDependencies((VpcRouterDeploymentDefinition) routerDeploymentDefinition);
         }
 
         return routerDeploymentDefinition;
     }
 
-    protected void injectVpcDependencies(
-            final VpcRouterDeploymentDefinition routerDeploymentDefinition) {
+    protected void injectVpcDependencies(final VpcRouterDeploymentDefinition routerDeploymentDefinition) {
 
-        routerDeploymentDefinition.vpcDao = this.vpcDao;
-        routerDeploymentDefinition.vpcOffDao = this.vpcOffDao;
-        routerDeploymentDefinition.pNtwkDao = this.pNtwkDao;
-        routerDeploymentDefinition.vpcMgr = this.vpcMgr;
-        routerDeploymentDefinition.vlanDao = this.vlanDao;
+        routerDeploymentDefinition.vpcDao = vpcDao;
+        routerDeploymentDefinition.vpcOffDao = vpcOffDao;
+        routerDeploymentDefinition.pNtwkDao = pNtwkDao;
+        routerDeploymentDefinition.vpcMgr = vpcMgr;
+        routerDeploymentDefinition.vlanDao = vlanDao;
     }
 
-
     public class IntermediateStateBuilder {
 
         RouterDeploymentDefinitionBuilder builder;
@@ -134,16 +137,16 @@ public class RouterDeploymentDefinitionBuilder {
         protected boolean isRedundant;
         protected List<DomainRouterVO> routers = new ArrayList<>();
 
-        protected IntermediateStateBuilder(RouterDeploymentDefinitionBuilder builder) {
+        protected IntermediateStateBuilder(final RouterDeploymentDefinitionBuilder builder) {
             this.builder = builder;
         }
 
         public IntermediateStateBuilder makeRedundant() {
-            this.isRedundant = true;
+            isRedundant = true;
             return this;
         }
 
-        public IntermediateStateBuilder setRedundant(boolean isRedundant) {
+        public IntermediateStateBuilder setRedundant(final boolean isRedundant) {
             this.isRedundant = isRedundant;
             return this;
         }
@@ -154,7 +157,7 @@ public class RouterDeploymentDefinitionBuilder {
         }
 
         public IntermediateStateBuilder setGuestNetwork(final Network nw) {
-            this.guestNetwork = nw;
+            guestNetwork = nw;
             return this;
         }
 
@@ -175,7 +178,7 @@ public class RouterDeploymentDefinitionBuilder {
 
         public RouterDeploymentDefinition build() {
             RouterDeploymentDefinition routerDeploymentDefinition = null;
-            if (this.vpc != null) {
+            if (vpc != null) {
                 routerDeploymentDefinition = new VpcRouterDeploymentDefinition(vpc, dest, owner, params, isRedundant);
             } else {
                 routerDeploymentDefinition = new RouterDeploymentDefinition(guestNetwork, dest, owner, params, isRedundant);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d0467731/server/src/org/cloud/network/router/deployment/VpcRouterDeploymentDefinition.java
----------------------------------------------------------------------
diff --git a/server/src/org/cloud/network/router/deployment/VpcRouterDeploymentDefinition.java b/server/src/org/cloud/network/router/deployment/VpcRouterDeploymentDefinition.java
index f6a99a1..eb08011 100644
--- a/server/src/org/cloud/network/router/deployment/VpcRouterDeploymentDefinition.java
+++ b/server/src/org/cloud/network/router/deployment/VpcRouterDeploymentDefinition.java
@@ -24,7 +24,6 @@ import com.cloud.network.Networks.BroadcastDomainType;
 import com.cloud.network.Networks.IsolationType;
 import com.cloud.network.PhysicalNetwork;
 import com.cloud.network.PhysicalNetworkServiceProvider;
-import com.cloud.network.VirtualRouterProvider;
 import com.cloud.network.VirtualRouterProvider.Type;
 import com.cloud.network.addr.PublicIp;
 import com.cloud.network.dao.IPAddressVO;
@@ -37,7 +36,6 @@ import com.cloud.network.vpc.dao.VpcDao;
 import com.cloud.network.vpc.dao.VpcOfferingDao;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.user.Account;
-import com.cloud.utils.Pair;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.vm.DomainRouterVO;
 import com.cloud.vm.NicProfile;
@@ -106,63 +104,85 @@ public class VpcRouterDeploymentDefinition extends RouterDeploymentDefinition {
     }
 
     @Override
+    protected int getNumberOfRoutersToDeploy() {
+        // TODO Should we make our changes here in order to enable Redundant Router for VPC?
+        return 1 - this.routers.size();
+    }
+
+    /**
+     * @see RouterDeploymentDefinition#executeDeployment()
+     */
+    @Override
     protected void executeDeployment()
             throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
         //2) Return routers if exist, otherwise...
-        if (this.routers.size() < 1) {
-            Long offeringId = vpcOffDao.findById(vpc.getVpcOfferingId()).getServiceOfferingId();
-            if (offeringId == null) {
-                offeringId = offering.getId();
-            }
+        if (getNumberOfRoutersToDeploy() > 0) {
+            this.findVirtualProvider();
+            this.findOfferingId();
+            this.findSourceNatIP();
+
             //3) Deploy Virtual Router
-            List<? extends PhysicalNetwork> pNtwks = pNtwkDao.listByZone(vpc.getZoneId());
+            DomainRouterVO router = deployVpcRouter(sourceNatIp);
+            this.routers.add(router);
+        }
+    }
 
-            VirtualRouterProvider vpcVrProvider = null;
+    @Override
+    protected void findSourceNatIP() throws InsufficientAddressCapacityException, ConcurrentOperationException {
+        this.sourceNatIp = vpcMgr.assignSourceNatIpAddressToVpc(this.owner, vpc);
+    }
 
-            for (PhysicalNetwork pNtwk : pNtwks) {
-                PhysicalNetworkServiceProvider provider = physicalProviderDao.findByServiceProvider(pNtwk.getId(), Type.VPCVirtualRouter.toString());
-                if (provider == null) {
-                    throw new CloudRuntimeException("Cannot find service provider " + Type.VPCVirtualRouter.toString() + " in physical network " + pNtwk.getId());
-                }
-                vpcVrProvider = vrProviderDao.findByNspIdAndType(provider.getId(), Type.VPCVirtualRouter);
-                if (vpcVrProvider != null) {
-                    break;
-                }
-            }
+    @Override
+    protected void findVirtualProvider() {
+        List<? extends PhysicalNetwork> pNtwks = pNtwkDao.listByZone(vpc.getZoneId());
 
-            PublicIp sourceNatIp = vpcMgr.assignSourceNatIpAddressToVpc(this.owner, vpc);
+        this.vrProvider = null;
 
-            DomainRouterVO router = deployVpcRouter(vpcVrProvider, offeringId, sourceNatIp);
-            this.routers.add(router);
+        for (PhysicalNetwork pNtwk : pNtwks) {
+            PhysicalNetworkServiceProvider provider = physicalProviderDao.findByServiceProvider(pNtwk.getId(), Type.VPCVirtualRouter.toString());
+            if (provider == null) {
+                throw new CloudRuntimeException("Cannot find service provider " + Type.VPCVirtualRouter.toString() + " in physical network " + pNtwk.getId());
+            }
+            this.vrProvider = vrProviderDao.findByNspIdAndType(provider.getId(), Type.VPCVirtualRouter);
+            if (this.vrProvider != null) {
+                break;
+            }
         }
     }
 
-    protected DomainRouterVO deployVpcRouter(final VirtualRouterProvider vrProvider,
-            final long svcOffId, final PublicIp sourceNatIp) throws ConcurrentOperationException, InsufficientAddressCapacityException,
+    @Override
+    protected void findOfferingId() {
+        Long vpcOfferingId = vpcOffDao.findById(vpc.getVpcOfferingId()).getServiceOfferingId();
+        if (vpcOfferingId != null) {
+            this.offeringId = vpcOfferingId;
+        }
+    }
+
+    protected DomainRouterVO deployVpcRouter(final PublicIp sourceNatIp)
+            throws ConcurrentOperationException, InsufficientAddressCapacityException,
             InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException, ResourceUnavailableException {
 
-        LinkedHashMap<Network, List<? extends NicProfile>> networks = createVpcRouterNetworks(
-                new Pair<Boolean, PublicIp>(true, sourceNatIp), this.vpc.getId());
+        LinkedHashMap<Network, List<? extends NicProfile>> networks = createRouterNetworks();
 
         DomainRouterVO router =
-                nwHelper.deployRouter(this, vrProvider, svcOffId, networks, true, vpcMgr.getSupportedVpcHypervisors());
+                nwHelper.deployRouter(this, networks, true, vpcMgr.getSupportedVpcHypervisors());
 
         return router;
     }
 
-    protected LinkedHashMap<Network, List<? extends NicProfile>> createVpcRouterNetworks(
-            final Pair<Boolean, PublicIp> sourceNatIp, final long vpcId)
+    @Override
+    protected LinkedHashMap<Network, List<? extends NicProfile>> createRouterNetworks()
                     throws ConcurrentOperationException, InsufficientAddressCapacityException {
 
         TreeSet<String> publicVlans = new TreeSet<String>();
-        publicVlans.add(sourceNatIp.second().getVlanTag());
+        publicVlans.add(this.sourceNatIp.getVlanTag());
 
         //1) allocate nic for control and source nat public ip
-        LinkedHashMap<Network, List<? extends NicProfile>> networks = nwHelper.createRouterNetworks(this, null, sourceNatIp);
+        LinkedHashMap<Network, List<? extends NicProfile>> networks = super.createRouterNetworks();
 
 
         //2) allocate nic for private gateways if needed
-        List<PrivateGateway> privateGateways = vpcMgr.getVpcPrivateGateways(vpcId);
+        List<PrivateGateway> privateGateways = vpcMgr.getVpcPrivateGateways(this.vpc.getId());
         if (privateGateways != null && !privateGateways.isEmpty()) {
             for (PrivateGateway privateGateway : privateGateways) {
                 NicProfile privateNic = vpcHelper.createPrivateNicProfileForGateway(privateGateway);
@@ -172,7 +192,7 @@ public class VpcRouterDeploymentDefinition extends RouterDeploymentDefinition {
         }
 
         //3) allocate nic for guest gateway if needed
-        List<? extends Network> guestNetworks = vpcMgr.getVpcNetworks(vpcId);
+        List<? extends Network> guestNetworks = vpcMgr.getVpcNetworks(this.vpc.getId());
         for (Network guestNetwork : guestNetworks) {
             if (networkModel.isPrivateGateway(guestNetwork.getId())) {
                 continue;
@@ -184,7 +204,7 @@ public class VpcRouterDeploymentDefinition extends RouterDeploymentDefinition {
         }
 
         //4) allocate nic for additional public network(s)
-        List<IPAddressVO> ips = ipAddressDao.listByAssociatedVpc(vpcId, false);
+        List<IPAddressVO> ips = ipAddressDao.listByAssociatedVpc(this.vpc.getId(), false);
         List<NicProfile> publicNics = new ArrayList<NicProfile>();
         Network publicNetwork = null;
         for (IPAddressVO ip : ips) {


Mime
View raw message