VPC: CS-15505 - allocate guest nics when new VR starts in VPC

This commit is contained in:
Alena Prokharchyk 2012-07-09 18:07:23 -07:00
parent 525ba472e8
commit c2134b431f
5 changed files with 61 additions and 72 deletions

View File

@ -1285,11 +1285,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
new Pair<Boolean, PublicIp>(publicNetwork, sourceNatIp)); new Pair<Boolean, PublicIp>(publicNetwork, sourceNatIp));
DomainRouterVO router = deployRouter(owner, dest, plan, params, isRedundant, vrProvider, offeringId, DomainRouterVO router = deployRouter(owner, dest, plan, params, isRedundant, vrProvider, offeringId,
null, networks); null, networks);
//add router to router network map
if (!_routerDao.isRouterPartOfGuestNetwork(router.getId(), network.getId())) { _routerDao.addRouterToGuestNetwork(router, network);
DomainRouterVO routerVO = _routerDao.findById(router.getId());
_routerDao.addRouterToGuestNetwork(routerVO, network);
}
routers.add(router); routers.add(router);
} }
} finally { } finally {
@ -1443,6 +1441,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
} else { } else {
gatewayNic.setDefaultNic(true); gatewayNic.setDefaultNic(true);
} }
networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) guestNetwork, gatewayNic)); networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) guestNetwork, gatewayNic));
hasGuestNetwork = true; hasGuestNetwork = true;
} }

View File

@ -18,7 +18,6 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.ejb.Local; import javax.ejb.Local;
@ -221,11 +220,8 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
//Add router to the Guest network //Add router to the Guest network
boolean result = true; boolean result = true;
try { try {
if (!_routerDao.isRouterPartOfGuestNetwork(router.getId(), network.getId())) { _routerDao.addRouterToGuestNetwork(router, network);
DomainRouterVO routerVO = _routerDao.findById(router.getId());
_routerDao.addRouterToGuestNetwork(routerVO, network);
}
NicProfile guestNic = _itMgr.addVmToNetwork(router, network, null); NicProfile guestNic = _itMgr.addVmToNetwork(router, network, null);
//setup guest network //setup guest network
if (guestNic != null) { if (guestNic != null) {
@ -275,11 +271,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
if (result) { if (result) {
if (result) { if (result) {
//check if router is already part of network _routerDao.removeRouterFromGuestNetwork(router.getId(), network.getId());
if (_routerDao.isRouterPartOfGuestNetwork(router.getId(), network.getId())) {
s_logger.debug("Removing router " + router + " from network" + network);
_routerDao.removeRouterFromNetwork(router.getId(), network.getId());
}
} }
} }
return result; return result;
@ -795,10 +787,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
} }
} }
//3) PREPARE PLUG NIC COMMANDS
//4) PREPARE PLUG NIC COMMANDS
try { try {
//add VPC router to public networks //add VPC router to public networks
List<PublicIp> sourceNat = new ArrayList<PublicIp>(1); List<PublicIp> sourceNat = new ArrayList<PublicIp>(1);
@ -860,7 +849,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
return false; return false;
} }
//5) RE-APPLY ALL STATIC ROUTE RULES //4) RE-APPLY ALL STATIC ROUTE RULES
List<? extends StaticRoute> routes = _staticRouteDao.listByVpcId(router.getVpcId()); List<? extends StaticRoute> routes = _staticRouteDao.listByVpcId(router.getVpcId());
List<StaticRouteProfile> staticRouteProfiles = new ArrayList<StaticRouteProfile>(routes.size()); List<StaticRouteProfile> staticRouteProfiles = new ArrayList<StaticRouteProfile>(routes.size());
Map<Long, VpcGateway> gatewayMap = new HashMap<Long, VpcGateway>(); Map<Long, VpcGateway> gatewayMap = new HashMap<Long, VpcGateway>();
@ -879,9 +868,9 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
createStaticRouteCommands(staticRouteProfiles, router, cmds); createStaticRouteCommands(staticRouteProfiles, router, cmds);
} }
//6) REISSUE VPN CONNECTION //5) REISSUE VPN CONNECTION
//7) REPROGRAM GUEST NETWORK //6) REPROGRAM GUEST NETWORK
boolean reprogramGuestNtwks = true; boolean reprogramGuestNtwks = true;
if (profile.getParameter(Param.ReProgramGuestNetworks) != null if (profile.getParameter(Param.ReProgramGuestNetworks) != null
&& (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) { && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) {
@ -931,7 +920,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
boolean result = true; boolean result = true;
try { try {
Network network = _networkMgr.getNetwork(gateway.getNetworkId()); Network network = _networkMgr.getNetwork(gateway.getNetworkId());
NicProfile requested = createPrivateNicProfile(gateway); NicProfile requested = createPrivateNicProfileForGateway(gateway);
NicProfile guestNic = _itMgr.addVmToNetwork(router, network, requested); NicProfile guestNic = _itMgr.addVmToNetwork(router, network, requested);
@ -1177,19 +1166,26 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(4); List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(4);
networks = super.createRouterNetworks(owner, isRedundant, plan, null, publicNetwork); networks = super.createRouterNetworks(owner, isRedundant, plan, null, publicNetwork);
//allocate nic for private gateway if needed //1) allocate nic for private gateway if needed
VpcGateway privateGateway = _vpcMgr.getPrivateGatewayForVpc(vpcId); VpcGateway privateGateway = _vpcMgr.getPrivateGatewayForVpc(vpcId);
if (privateGateway != null) { if (privateGateway != null) {
NicProfile privateNic = createPrivateNicProfile(privateGateway); NicProfile privateNic = createPrivateNicProfileForGateway(privateGateway);
Network privateNetwork = _networkMgr.getNetwork(privateGateway.getNetworkId()); Network privateNetwork = _networkMgr.getNetwork(privateGateway.getNetworkId());
networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) privateNetwork, privateNic)); networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) privateNetwork, privateNic));
} }
//2) allocate nic for guest gateway if needed
List<? extends Network> guestNetworks = _vpcMgr.getVpcNetworks(vpcId);
for (Network guestNetwork : guestNetworks) {
NicProfile guestNic = createGuestNicProfileForVpcRouter(guestNetwork);
networks.add(new Pair<NetworkVO, NicProfile>((NetworkVO) guestNetwork, guestNic));
}
return networks; return networks;
} }
@DB @DB
protected NicProfile createPrivateNicProfile(VpcGateway privateGateway) { protected NicProfile createPrivateNicProfileForGateway(VpcGateway privateGateway) {
Network network = _networkMgr.getNetwork(privateGateway.getNetworkId()); Network network = _networkMgr.getNetwork(privateGateway.getNetworkId());
PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(network.getDataCenterId(), network.getId(), privateGateway.getIp4Address()); PrivateIpVO ipVO = _privateIpDao.allocateIpAddress(network.getDataCenterId(), network.getId(), privateGateway.getIp4Address());
@ -1210,4 +1206,16 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
return privateNic; return privateNic;
} }
protected NicProfile createGuestNicProfileForVpcRouter(Network guestNetwork) {
NicProfile guestNic = new NicProfile();
guestNic.setIp4Address(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));
return guestNic;
}
} }

View File

@ -94,13 +94,6 @@ public interface VpcManager extends VpcService{
*/ */
List<DomainRouterVO> getVpcRouters(long vpcId); List<DomainRouterVO> getVpcRouters(long vpcId);
/**
* @param zoneId
* @return
*/
boolean vpcProviderEnabledInZone(long zoneId);
/** /**
* @param vpcId * @param vpcId
* @return * @return

View File

@ -19,6 +19,7 @@ package com.cloud.vm.dao;
import java.util.List; import java.util.List;
import com.cloud.network.Network; import com.cloud.network.Network;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.router.VirtualRouter.Role; import com.cloud.network.router.VirtualRouter.Role;
import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.GenericDao;
import com.cloud.vm.DomainRouterVO; import com.cloud.vm.DomainRouterVO;
@ -120,19 +121,12 @@ public interface DomainRouterDao extends GenericDao<DomainRouterVO, Long> {
* @param routerId * @param routerId
* @param guestNetwork * @param guestNetwork
*/ */
void addRouterToGuestNetwork(DomainRouterVO router, Network guestNetwork); void addRouterToGuestNetwork(VirtualRouter router, Network guestNetwork);
/** /**
* @param routerId * @param routerId
* @param guestNetworkId * @param guestNetworkId
*/ */
void removeRouterFromNetwork(long routerId, long guestNetworkId); void removeRouterFromGuestNetwork(long routerId, long guestNetworkId);
/**
* @param routerId
* @param guestNetworkId
* @return
*/
boolean isRouterPartOfGuestNetwork(long routerId, long guestNetworkId);
} }

View File

@ -25,6 +25,7 @@ import com.cloud.host.dao.HostDaoImpl;
import com.cloud.network.Network; import com.cloud.network.Network;
import com.cloud.network.RouterNetworkDaoImpl; import com.cloud.network.RouterNetworkDaoImpl;
import com.cloud.network.RouterNetworkVO; import com.cloud.network.RouterNetworkVO;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.router.VirtualRouter.Role; import com.cloud.network.router.VirtualRouter.Role;
import com.cloud.offering.NetworkOffering; import com.cloud.offering.NetworkOffering;
import com.cloud.user.UserStatisticsVO; import com.cloud.user.UserStatisticsVO;
@ -269,12 +270,7 @@ public class DomainRouterDaoImpl extends GenericDaoBase<DomainRouterVO, Long> im
if (guestNetworks != null && !guestNetworks.isEmpty()) { if (guestNetworks != null && !guestNetworks.isEmpty()) {
// 2) add router to the network // 2) add router to the network
for (Network guestNetwork : guestNetworks) { for (Network guestNetwork : guestNetworks) {
if (!isRouterPartOfGuestNetwork(router.getId(), guestNetwork.getId())) { addRouterToGuestNetwork(router, guestNetwork);
//add only when network is not private network
if (!(guestNetwork.getName() != NetworkOffering.SystemPrivateGatewayNetworkOffering)) {
addRouterToGuestNetwork(router, guestNetwork);
}
}
} }
} }
@ -284,27 +280,32 @@ public class DomainRouterDaoImpl extends GenericDaoBase<DomainRouterVO, Long> im
@Override @Override
@DB @DB
public void addRouterToGuestNetwork(DomainRouterVO router, Network guestNetwork) { public void addRouterToGuestNetwork(VirtualRouter router, Network guestNetwork) {
Transaction txn = Transaction.currentTxn(); if (_routerNetworkDao.findByRouterAndNetwork(router.getId(), guestNetwork.getId()) == null &&
txn.start(); guestNetwork.getName() != NetworkOffering.SystemPrivateGatewayNetworkOffering) {
//1) add router to network Transaction txn = Transaction.currentTxn();
RouterNetworkVO routerNtwkMap = new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType()); txn.start();
_routerNetworkDao.persist(routerNtwkMap); //1) add router to network
//2) create user stats entry for the network RouterNetworkVO routerNtwkMap = new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType());
UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(), _routerNetworkDao.persist(routerNtwkMap);
guestNetwork.getId(), null, router.getId(), router.getType().toString()); //2) create user stats entry for the network
if (stats == null) { UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(),
stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterIdToDeployIn(), null, router.getId(), guestNetwork.getId(), null, router.getId(), router.getType().toString());
router.getType().toString(), guestNetwork.getId()); if (stats == null) {
_userStatsDao.persist(stats); stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterIdToDeployIn(), null, router.getId(),
} router.getType().toString(), guestNetwork.getId());
txn.commit(); _userStatsDao.persist(stats);
}
txn.commit();
}
} }
@Override @Override
public void removeRouterFromNetwork(long routerId, long guestNetworkId) { public void removeRouterFromGuestNetwork(long routerId, long guestNetworkId) {
RouterNetworkVO routerNtwkMap = _routerNetworkDao.findByRouterAndNetwork(routerId, guestNetworkId); RouterNetworkVO routerNtwkMap = _routerNetworkDao.findByRouterAndNetwork(routerId, guestNetworkId);
_routerNetworkDao.remove(routerNtwkMap.getId()); if (routerNtwkMap != null) {
_routerNetworkDao.remove(routerNtwkMap.getId());
}
} }
@Override @Override
@ -319,11 +320,5 @@ public class DomainRouterDaoImpl extends GenericDaoBase<DomainRouterVO, Long> im
sc.setParameters("role", Role.VIRTUAL_ROUTER); sc.setParameters("role", Role.VIRTUAL_ROUTER);
return listBy(sc); return listBy(sc);
} }
@Override
public boolean isRouterPartOfGuestNetwork(long routerId, long guestNetworkId) {
RouterNetworkVO routerNtwkMap = _routerNetworkDao.findByRouterAndNetwork(routerId, guestNetworkId);
return routerNtwkMap != null;
}
} }