mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
Fixing Site2Site VPN and private gateway on VpcVirtualRouterElement
Fixing Finalize methods in the VpcVirtualNetworkApplianceManager Making use ot eh RouterControlHelper instead of duplicating methods in the VirtualNetworkApplianceManager [witp] - redundant vpc virtual routers have the same mac address, have to fix that.
This commit is contained in:
parent
381c0e1c69
commit
66d8b1f13d
@ -152,6 +152,7 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
if (routers == null || routers.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean result = true;
|
||||
for (final DomainRouterVO router : routers) {
|
||||
result = result && _routerMgr.destroyRouter(router.getId(), context.getAccount(), context.getCaller().getId()) != null;
|
||||
@ -549,12 +550,16 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
}
|
||||
|
||||
final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(ip.getVpcId());
|
||||
if (routers == null || routers.size() != 1) {
|
||||
if (routers == null) {
|
||||
throw new ResourceUnavailableException("Cannot enable site-to-site VPN on the backend; virtual router doesn't exist in the vpc " + ip.getVpcId(), DataCenter.class,
|
||||
vpc.getZoneId());
|
||||
}
|
||||
|
||||
return _vpcRouterMgr.startSite2SiteVpn(conn, routers.get(0));
|
||||
boolean result = true;
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
result = result && _vpcRouterMgr.startSite2SiteVpn(conn, domainRouterVO);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -576,12 +581,17 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
}
|
||||
|
||||
final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(ip.getVpcId());
|
||||
if (routers == null || routers.size() != 1) {
|
||||
if (routers == null) {
|
||||
throw new ResourceUnavailableException("Cannot enable site-to-site VPN on the backend; virtual router doesn't exist in the vpc " + ip.getVpcId(), DataCenter.class,
|
||||
vpc.getZoneId());
|
||||
}
|
||||
|
||||
return _vpcRouterMgr.stopSite2SiteVpn(conn, routers.get(0));
|
||||
boolean result = true;
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
result = result && _vpcRouterMgr.stopSite2SiteVpn(conn, domainRouterVO);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -591,7 +601,7 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
}
|
||||
|
||||
final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(vpn.getVpcId());
|
||||
if (routers == null || routers.size() != 1) {
|
||||
if (routers == null) {
|
||||
s_logger.debug("Cannot apply vpn users on the backend; virtual router doesn't exist in the network " + vpn.getVpcId());
|
||||
return null;
|
||||
}
|
||||
@ -600,7 +610,11 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
final DataCenterVO dcVO = _dcDao.findById(vpc.getZoneId());
|
||||
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
|
||||
|
||||
return networkTopology.applyVpnUsers(vpn, users, routers.get(0));
|
||||
String[] result = null;
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
result = networkTopology.applyVpnUsers(vpn, users, domainRouterVO);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -610,11 +624,16 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
}
|
||||
|
||||
final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(vpn.getVpcId());
|
||||
if (routers == null || routers.size() != 1) {
|
||||
if (routers == null) {
|
||||
s_logger.debug("Cannot apply vpn users on the backend; virtual router doesn't exist in the network " + vpn.getVpcId());
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return _vpcRouterMgr.startRemoteAccessVpn(vpn, routers.get(0));
|
||||
|
||||
boolean result = true;
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
result = result && _vpcRouterMgr.startRemoteAccessVpn(vpn, domainRouterVO);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -624,11 +643,15 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc
|
||||
}
|
||||
|
||||
final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(vpn.getVpcId());
|
||||
if (routers == null || routers.size() != 1) {
|
||||
if (routers == null) {
|
||||
s_logger.debug("Cannot apply vpn users on the backend; virtual router doesn't exist in the network " + vpn.getVpcId());
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return _vpcRouterMgr.stopRemoteAccessVpn(vpn, routers.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
boolean result = true;
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
result = result && _vpcRouterMgr.stopRemoteAccessVpn(vpn, domainRouterVO);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -367,6 +367,9 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
@Qualifier("networkHelper")
|
||||
protected NetworkHelper _nwHelper;
|
||||
|
||||
@Inject
|
||||
protected RouterControlHelper _routerControlHelper;
|
||||
|
||||
@Inject
|
||||
protected CommandSetupHelper _commandSetupHelper;
|
||||
@Inject
|
||||
@ -924,7 +927,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
continue;
|
||||
} else if (privateIP != null) {
|
||||
final CheckS2SVpnConnectionsCommand command = new CheckS2SVpnConnectionsCommand(ipList);
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, getRouterControlIp(router.getId()));
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
|
||||
command.setWait(30);
|
||||
final Answer origAnswer = _agentMgr.easySend(router.getHostId(), command);
|
||||
@ -972,7 +975,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
}
|
||||
|
||||
protected void updateRoutersRedundantState(final List<DomainRouterVO> routers) {
|
||||
boolean updated = false;
|
||||
boolean updated;
|
||||
for (final DomainRouterVO router : routers) {
|
||||
updated = false;
|
||||
if (!router.getIsRedundantRouter()) {
|
||||
@ -991,7 +994,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
updated = true;
|
||||
} else if (privateIP != null) {
|
||||
final CheckRouterCommand command = new CheckRouterCommand();
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, getRouterControlIp(router.getId()));
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
|
||||
command.setWait(30);
|
||||
final Answer origAnswer = _agentMgr.easySend(router.getHostId(), command);
|
||||
@ -1055,9 +1058,6 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
|
||||
protected class RvRStatusUpdateTask extends ManagedContextRunnable {
|
||||
|
||||
public RvRStatusUpdateTask() {
|
||||
}
|
||||
|
||||
/*
|
||||
* In order to make fail-over works well at any time, we have to ensure:
|
||||
* 1. Backup router's priority = Master's priority - DELTA + 1 2. Backup
|
||||
@ -1662,7 +1662,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
|
||||
final List<Long> routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId());
|
||||
for (final Long guestNetworkId : routerGuestNtwkIds) {
|
||||
final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, router.getInstanceName(), controlNic.getIp4Address(), getRouterIpInNetwork(
|
||||
final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, router.getInstanceName(), controlNic.getIp4Address(), _routerControlHelper.getRouterIpInNetwork(
|
||||
guestNetworkId, router.getId()));
|
||||
cmds.addCommand(startCmd);
|
||||
|
||||
@ -1686,7 +1686,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
|
||||
finalizeUserDataAndDhcpOnStart(cmds, router, provider, guestNetworkId);
|
||||
|
||||
final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, router.getInstanceName(), controlNic.getIp4Address(), getRouterIpInNetwork(
|
||||
final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, router.getInstanceName(), controlNic.getIp4Address(), _routerControlHelper.getRouterIpInNetwork(
|
||||
guestNetworkId, router.getId()));
|
||||
cmds.addCommand(finishCmd);
|
||||
}
|
||||
@ -1738,7 +1738,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
}
|
||||
final SetMonitorServiceCommand command = new SetMonitorServiceCommand(servicesTO);
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_IP, controlNic.getIp4Address());
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, getRouterIpInNetwork(networkId, router.getId()));
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, _routerControlHelper.getRouterIpInNetwork(networkId, router.getId()));
|
||||
command.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
|
||||
|
||||
if (!add) {
|
||||
@ -2373,31 +2373,6 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected String getRouterControlIp(final long routerId) {
|
||||
String routerControlIpAddress = null;
|
||||
final List<NicVO> nics = _nicDao.listByVmId(routerId);
|
||||
for (final NicVO n : nics) {
|
||||
final NetworkVO nc = _networkDao.findById(n.getNetworkId());
|
||||
if (nc != null && nc.getTrafficType() == TrafficType.Control) {
|
||||
routerControlIpAddress = n.getIp4Address();
|
||||
// router will have only one control ip
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (routerControlIpAddress == null) {
|
||||
s_logger.warn("Unable to find router's control ip in its attached NICs!. routerId: " + routerId);
|
||||
final DomainRouterVO router = _routerDao.findById(routerId);
|
||||
return router.getPrivateIpAddress();
|
||||
}
|
||||
|
||||
return routerControlIpAddress;
|
||||
}
|
||||
|
||||
protected String getRouterIpInNetwork(final long networkId, final long instanceId) {
|
||||
return _nicDao.getIpAddress(networkId, instanceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareStop(final VirtualMachineProfile profile) {
|
||||
// Collect network usage before stopping Vm
|
||||
@ -2657,7 +2632,7 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
protected boolean aggregationExecution(final AggregationControlCommand.Action action, final Network network, final List<DomainRouterVO> routers)
|
||||
throws AgentUnavailableException, ResourceUnavailableException {
|
||||
for (final DomainRouterVO router : routers) {
|
||||
final AggregationControlCommand cmd = new AggregationControlCommand(action, router.getInstanceName(), getRouterControlIp(router.getId()), getRouterIpInNetwork(
|
||||
final AggregationControlCommand cmd = new AggregationControlCommand(action, router.getInstanceName(), _routerControlHelper.getRouterControlIp(router.getId()), _routerControlHelper.getRouterIpInNetwork(
|
||||
network.getId(), router.getId()));
|
||||
final Commands cmds = new Commands(cmd);
|
||||
if (!_nwHelper.sendCommandsToRouter(router, cmds)) {
|
||||
@ -2676,4 +2651,4 @@ Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||
public boolean completeAggregatedExecution(final Network network, final List<DomainRouterVO> routers) throws AgentUnavailableException, ResourceUnavailableException {
|
||||
return aggregationExecution(Action.Finish, network, routers);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -237,32 +237,38 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
||||
final DomainRouterVO vr = _routerDao.findById(profile.getId());
|
||||
|
||||
if (vr.getVpcId() != null) {
|
||||
String defaultDns1 = null;
|
||||
String defaultDns2 = null;
|
||||
// remove public and guest nics as we will plug them later
|
||||
final Iterator<NicProfile> it = profile.getNics().iterator();
|
||||
while (it.hasNext()) {
|
||||
final NicProfile nic = it.next();
|
||||
if (nic.getTrafficType() == TrafficType.Public || nic.getTrafficType() == TrafficType.Guest) {
|
||||
// save dns information
|
||||
if (nic.getTrafficType() == TrafficType.Public) {
|
||||
defaultDns1 = nic.getDns1();
|
||||
defaultDns2 = nic.getDns2();
|
||||
final List<DomainRouterVO> routers = getVpcRouters(vr.getVpcId());
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
|
||||
if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
|
||||
String defaultDns1 = null;
|
||||
String defaultDns2 = null;
|
||||
// remove public and guest nics as we will plug them later
|
||||
final Iterator<NicProfile> it = profile.getNics().iterator();
|
||||
while (it.hasNext()) {
|
||||
final NicProfile nic = it.next();
|
||||
if (nic.getTrafficType() == TrafficType.Public || nic.getTrafficType() == TrafficType.Guest) {
|
||||
// save dns information
|
||||
if (nic.getTrafficType() == TrafficType.Public) {
|
||||
defaultDns1 = nic.getDns1();
|
||||
defaultDns2 = nic.getDns2();
|
||||
}
|
||||
s_logger.debug("Removing nic " + nic + " of type " + nic.getTrafficType() + " from the nics passed on vm start. " + "The nic will be plugged later");
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// add vpc cidr/dns/networkdomain to the boot load args
|
||||
final StringBuilder buf = profile.getBootArgsBuilder();
|
||||
final Vpc vpc = _entityMgr.findById(Vpc.class, domainRouterVO.getVpcId());
|
||||
buf.append(" vpccidr=" + vpc.getCidr() + " domain=" + vpc.getNetworkDomain());
|
||||
|
||||
buf.append(" dns1=").append(defaultDns1);
|
||||
if (defaultDns2 != null) {
|
||||
buf.append(" dns2=").append(defaultDns2);
|
||||
}
|
||||
s_logger.debug("Removing nic " + nic + " of type " + nic.getTrafficType() + " from the nics passed on vm start. " + "The nic will be plugged later");
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// add vpc cidr/dns/networkdomain to the boot load args
|
||||
final StringBuilder buf = profile.getBootArgsBuilder();
|
||||
final Vpc vpc = _entityMgr.findById(Vpc.class, vr.getVpcId());
|
||||
buf.append(" vpccidr=" + vpc.getCidr() + " domain=" + vpc.getNetworkDomain());
|
||||
|
||||
buf.append(" dns1=").append(defaultDns1);
|
||||
if (defaultDns2 != null) {
|
||||
buf.append(" dns2=").append(defaultDns2);
|
||||
}
|
||||
}
|
||||
|
||||
return super.finalizeVirtualMachineProfile(profile, dest, context);
|
||||
@ -277,177 +283,182 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
||||
return super.finalizeCommandsOnStart(cmds, profile);
|
||||
}
|
||||
|
||||
// 1) FORM SSH CHECK COMMAND
|
||||
final NicProfile controlNic = getControlNic(profile);
|
||||
if (controlNic == null) {
|
||||
s_logger.error("Control network doesn't exist for the router " + router);
|
||||
return false;
|
||||
}
|
||||
final List<DomainRouterVO> routers = getVpcRouters(router.getVpcId());
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
|
||||
finalizeSshAndVersionAndNetworkUsageOnStart(cmds, profile, router, controlNic);
|
||||
if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
|
||||
// 1) FORM SSH CHECK COMMAND
|
||||
final NicProfile controlNic = getControlNic(profile);
|
||||
if (controlNic == null) {
|
||||
s_logger.error("Control network doesn't exist for the router " + domainRouterVO);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2) FORM PLUG NIC COMMANDS
|
||||
final List<Pair<Nic, Network>> guestNics = new ArrayList<Pair<Nic, Network>>();
|
||||
final List<Pair<Nic, Network>> publicNics = new ArrayList<Pair<Nic, Network>>();
|
||||
final Map<String, String> vlanMacAddress = new HashMap<String, String>();
|
||||
finalizeSshAndVersionAndNetworkUsageOnStart(cmds, profile, domainRouterVO, controlNic);
|
||||
|
||||
final List<? extends Nic> routerNics = _nicDao.listByVmId(profile.getId());
|
||||
for (final Nic routerNic : routerNics) {
|
||||
final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
|
||||
if (network.getTrafficType() == TrafficType.Guest) {
|
||||
final Pair<Nic, Network> guestNic = new Pair<Nic, Network>(routerNic, network);
|
||||
guestNics.add(guestNic);
|
||||
} else if (network.getTrafficType() == TrafficType.Public) {
|
||||
final Pair<Nic, Network> publicNic = new Pair<Nic, Network>(routerNic, network);
|
||||
publicNics.add(publicNic);
|
||||
final String vlanTag = BroadcastDomainType.getValue(routerNic.getBroadcastUri());
|
||||
vlanMacAddress.put(vlanTag, routerNic.getMacAddress());
|
||||
}
|
||||
}
|
||||
// 2) FORM PLUG NIC COMMANDS
|
||||
final List<Pair<Nic, Network>> guestNics = new ArrayList<Pair<Nic, Network>>();
|
||||
final List<Pair<Nic, Network>> publicNics = new ArrayList<Pair<Nic, Network>>();
|
||||
final Map<String, String> vlanMacAddress = new HashMap<String, String>();
|
||||
|
||||
final List<Command> usageCmds = new ArrayList<Command>();
|
||||
|
||||
// 3) PREPARE PLUG NIC COMMANDS
|
||||
try {
|
||||
// add VPC router to public networks
|
||||
final List<PublicIp> sourceNat = new ArrayList<PublicIp>(1);
|
||||
for (final Pair<Nic, Network> nicNtwk : publicNics) {
|
||||
final Nic publicNic = nicNtwk.first();
|
||||
final Network publicNtwk = nicNtwk.second();
|
||||
final IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(publicNtwk.getId(), publicNic.getIp4Address());
|
||||
|
||||
if (userIp.isSourceNat()) {
|
||||
final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
|
||||
sourceNat.add(publicIp);
|
||||
|
||||
if (router.getPublicIpAddress() == null) {
|
||||
final DomainRouterVO routerVO = _routerDao.findById(router.getId());
|
||||
routerVO.setPublicIpAddress(publicNic.getIp4Address());
|
||||
routerVO.setPublicNetmask(publicNic.getNetmask());
|
||||
routerVO.setPublicMacAddress(publicNic.getMacAddress());
|
||||
_routerDao.update(routerVO.getId(), routerVO);
|
||||
final List<? extends Nic> routerNics = _nicDao.listByVmId(profile.getId());
|
||||
for (final Nic routerNic : routerNics) {
|
||||
final Network network = _networkModel.getNetwork(routerNic.getNetworkId());
|
||||
if (network.getTrafficType() == TrafficType.Guest) {
|
||||
final Pair<Nic, Network> guestNic = new Pair<Nic, Network>(routerNic, network);
|
||||
guestNics.add(guestNic);
|
||||
} else if (network.getTrafficType() == TrafficType.Public) {
|
||||
final Pair<Nic, Network> publicNic = new Pair<Nic, Network>(routerNic, network);
|
||||
publicNics.add(publicNic);
|
||||
final String vlanTag = BroadcastDomainType.getValue(routerNic.getBroadcastUri());
|
||||
vlanMacAddress.put(vlanTag, routerNic.getMacAddress());
|
||||
}
|
||||
}
|
||||
final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(router, publicNic.getNetworkId(), publicNic.getBroadcastUri().toString()),
|
||||
router.getInstanceName(), router.getType());
|
||||
cmds.addCommand(plugNicCmd);
|
||||
final VpcVO vpc = _vpcDao.findById(router.getVpcId());
|
||||
final NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(router.getPrivateIpAddress(), router.getInstanceName(), true, publicNic.getIp4Address(), vpc.getCidr());
|
||||
usageCmds.add(netUsageCmd);
|
||||
UserStatisticsVO stats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterId(), publicNtwk.getId(), publicNic.getIp4Address(), router.getId(),
|
||||
router.getType().toString());
|
||||
if (stats == null) {
|
||||
stats = new UserStatisticsVO(router.getAccountId(), router.getDataCenterId(), publicNic.getIp4Address(), router.getId(), router.getType().toString(),
|
||||
publicNtwk.getId());
|
||||
_userStatsDao.persist(stats);
|
||||
}
|
||||
}
|
||||
|
||||
// create ip assoc for source nat
|
||||
if (!sourceNat.isEmpty()) {
|
||||
_commandSetupHelper.createVpcAssociatePublicIPCommands(router, sourceNat, cmds, vlanMacAddress);
|
||||
}
|
||||
final List<Command> usageCmds = new ArrayList<Command>();
|
||||
|
||||
// add VPC router to guest networks
|
||||
for (final Pair<Nic, Network> nicNtwk : guestNics) {
|
||||
final Nic guestNic = nicNtwk.first();
|
||||
// plug guest nic
|
||||
final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(router, guestNic.getNetworkId(), null), router.getInstanceName(), router.getType());
|
||||
cmds.addCommand(plugNicCmd);
|
||||
if (!_networkModel.isPrivateGateway(guestNic.getNetworkId())) {
|
||||
// set guest network
|
||||
final VirtualMachine vm = _vmDao.findById(router.getId());
|
||||
final NicProfile nicProfile = _networkModel.getNicProfile(vm, guestNic.getNetworkId(), null);
|
||||
final SetupGuestNetworkCommand setupCmd = _commandSetupHelper.createSetupGuestNetworkCommand(router, true, nicProfile);
|
||||
cmds.addCommand(setupCmd);
|
||||
} else {
|
||||
// 3) PREPARE PLUG NIC COMMANDS
|
||||
try {
|
||||
// add VPC router to public networks
|
||||
final List<PublicIp> sourceNat = new ArrayList<PublicIp>(1);
|
||||
for (final Pair<Nic, Network> nicNtwk : publicNics) {
|
||||
final Nic publicNic = nicNtwk.first();
|
||||
final Network publicNtwk = nicNtwk.second();
|
||||
final IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(publicNtwk.getId(), publicNic.getIp4Address());
|
||||
|
||||
// set private network
|
||||
final PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(guestNic.getNetworkId(), guestNic.getIp4Address());
|
||||
final Network network = _networkDao.findById(guestNic.getNetworkId());
|
||||
BroadcastDomainType.getValue(network.getBroadcastUri());
|
||||
final String netmask = NetUtils.getCidrNetmask(network.getCidr());
|
||||
final PrivateIpAddress ip = new PrivateIpAddress(ipVO, network.getBroadcastUri().toString(), network.getGateway(), netmask, guestNic.getMacAddress());
|
||||
if (userIp.isSourceNat()) {
|
||||
final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
|
||||
sourceNat.add(publicIp);
|
||||
|
||||
final List<PrivateIpAddress> privateIps = new ArrayList<PrivateIpAddress>(1);
|
||||
privateIps.add(ip);
|
||||
_commandSetupHelper.createVpcAssociatePrivateIPCommands(router, privateIps, cmds, true);
|
||||
|
||||
final Long privateGwAclId = _vpcGatewayDao.getNetworkAclIdForPrivateIp(ipVO.getVpcId(), ipVO.getNetworkId(), ipVO.getIpAddress());
|
||||
|
||||
if (privateGwAclId != null) {
|
||||
// set network acl on private gateway
|
||||
final List<NetworkACLItemVO> networkACLs = _networkACLItemDao.listByACL(privateGwAclId);
|
||||
s_logger.debug("Found " + networkACLs.size() + " network ACLs to apply as a part of VPC VR " + router + " start for private gateway ip = "
|
||||
+ ipVO.getIpAddress());
|
||||
|
||||
_commandSetupHelper.createNetworkACLsCommands(networkACLs, router, cmds, ipVO.getNetworkId(), true);
|
||||
if (domainRouterVO.getPublicIpAddress() == null) {
|
||||
final DomainRouterVO routerVO = _routerDao.findById(domainRouterVO.getId());
|
||||
routerVO.setPublicIpAddress(publicNic.getIp4Address());
|
||||
routerVO.setPublicNetmask(publicNic.getNetmask());
|
||||
routerVO.setPublicMacAddress(publicNic.getMacAddress());
|
||||
_routerDao.update(routerVO.getId(), routerVO);
|
||||
}
|
||||
}
|
||||
final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, publicNic.getNetworkId(), publicNic.getBroadcastUri().toString()),
|
||||
domainRouterVO.getInstanceName(), domainRouterVO.getType());
|
||||
cmds.addCommand(plugNicCmd);
|
||||
final VpcVO vpc = _vpcDao.findById(domainRouterVO.getVpcId());
|
||||
final NetworkUsageCommand netUsageCmd = new NetworkUsageCommand(domainRouterVO.getPrivateIpAddress(), domainRouterVO.getInstanceName(), true, publicNic.getIp4Address(), vpc.getCidr());
|
||||
usageCmds.add(netUsageCmd);
|
||||
UserStatisticsVO stats = _userStatsDao.findBy(domainRouterVO.getAccountId(), domainRouterVO.getDataCenterId(), publicNtwk.getId(), publicNic.getIp4Address(), domainRouterVO.getId(),
|
||||
domainRouterVO.getType().toString());
|
||||
if (stats == null) {
|
||||
stats = new UserStatisticsVO(domainRouterVO.getAccountId(), domainRouterVO.getDataCenterId(), publicNic.getIp4Address(), domainRouterVO.getId(), domainRouterVO.getType().toString(),
|
||||
publicNtwk.getId());
|
||||
_userStatsDao.persist(stats);
|
||||
}
|
||||
}
|
||||
|
||||
// create ip assoc for source nat
|
||||
if (!sourceNat.isEmpty()) {
|
||||
_commandSetupHelper.createVpcAssociatePublicIPCommands(domainRouterVO, sourceNat, cmds, vlanMacAddress);
|
||||
}
|
||||
|
||||
// add VPC router to guest networks
|
||||
for (final Pair<Nic, Network> nicNtwk : guestNics) {
|
||||
final Nic guestNic = nicNtwk.first();
|
||||
// plug guest nic
|
||||
final PlugNicCommand plugNicCmd = new PlugNicCommand(_nwHelper.getNicTO(domainRouterVO, guestNic.getNetworkId(), null), domainRouterVO.getInstanceName(), domainRouterVO.getType());
|
||||
cmds.addCommand(plugNicCmd);
|
||||
if (!_networkModel.isPrivateGateway(guestNic.getNetworkId())) {
|
||||
// set guest network
|
||||
final VirtualMachine vm = _vmDao.findById(domainRouterVO.getId());
|
||||
final NicProfile nicProfile = _networkModel.getNicProfile(vm, guestNic.getNetworkId(), null);
|
||||
final SetupGuestNetworkCommand setupCmd = _commandSetupHelper.createSetupGuestNetworkCommand(domainRouterVO, true, nicProfile);
|
||||
cmds.addCommand(setupCmd);
|
||||
} else {
|
||||
|
||||
// set private network
|
||||
final PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(guestNic.getNetworkId(), guestNic.getIp4Address());
|
||||
final Network network = _networkDao.findById(guestNic.getNetworkId());
|
||||
BroadcastDomainType.getValue(network.getBroadcastUri());
|
||||
final String netmask = NetUtils.getCidrNetmask(network.getCidr());
|
||||
final PrivateIpAddress ip = new PrivateIpAddress(ipVO, network.getBroadcastUri().toString(), network.getGateway(), netmask, guestNic.getMacAddress());
|
||||
|
||||
final List<PrivateIpAddress> privateIps = new ArrayList<PrivateIpAddress>(1);
|
||||
privateIps.add(ip);
|
||||
_commandSetupHelper.createVpcAssociatePrivateIPCommands(domainRouterVO, privateIps, cmds, true);
|
||||
|
||||
final Long privateGwAclId = _vpcGatewayDao.getNetworkAclIdForPrivateIp(ipVO.getVpcId(), ipVO.getNetworkId(), ipVO.getIpAddress());
|
||||
|
||||
if (privateGwAclId != null) {
|
||||
// set network acl on private gateway
|
||||
final List<NetworkACLItemVO> networkACLs = _networkACLItemDao.listByACL(privateGwAclId);
|
||||
s_logger.debug("Found " + networkACLs.size() + " network ACLs to apply as a part of VPC VR " + domainRouterVO + " start for private gateway ip = "
|
||||
+ ipVO.getIpAddress());
|
||||
|
||||
_commandSetupHelper.createNetworkACLsCommands(networkACLs, domainRouterVO, cmds, ipVO.getNetworkId(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
s_logger.warn("Failed to add router " + domainRouterVO + " to network due to exception ", ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4) RE-APPLY ALL STATIC ROUTE RULES
|
||||
final List<? extends StaticRoute> routes = _staticRouteDao.listByVpcId(domainRouterVO.getVpcId());
|
||||
final List<StaticRouteProfile> staticRouteProfiles = new ArrayList<StaticRouteProfile>(routes.size());
|
||||
final Map<Long, VpcGateway> gatewayMap = new HashMap<Long, VpcGateway>();
|
||||
for (final StaticRoute route : routes) {
|
||||
VpcGateway gateway = gatewayMap.get(route.getVpcGatewayId());
|
||||
if (gateway == null) {
|
||||
gateway = _entityMgr.findById(VpcGateway.class, route.getVpcGatewayId());
|
||||
gatewayMap.put(gateway.getId(), gateway);
|
||||
}
|
||||
staticRouteProfiles.add(new StaticRouteProfile(route, gateway));
|
||||
}
|
||||
|
||||
s_logger.debug("Found " + staticRouteProfiles.size() + " static routes to apply as a part of vpc route " + domainRouterVO + " start");
|
||||
if (!staticRouteProfiles.isEmpty()) {
|
||||
_commandSetupHelper.createStaticRouteCommands(staticRouteProfiles, domainRouterVO, cmds);
|
||||
}
|
||||
|
||||
// 5) RE-APPLY ALL REMOTE ACCESS VPNs
|
||||
final RemoteAccessVpnVO vpn = _vpnDao.findByAccountAndVpc(domainRouterVO.getAccountId(), domainRouterVO.getVpcId());
|
||||
if (vpn != null) {
|
||||
_commandSetupHelper.createApplyVpnCommands(true, vpn, domainRouterVO, cmds);
|
||||
}
|
||||
|
||||
// 6) REPROGRAM GUEST NETWORK
|
||||
boolean reprogramGuestNtwks = true;
|
||||
if (profile.getParameter(Param.ReProgramGuestNetworks) != null && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) {
|
||||
reprogramGuestNtwks = false;
|
||||
}
|
||||
|
||||
final VirtualRouterProvider vrProvider = _vrProviderDao.findById(domainRouterVO.getElementId());
|
||||
if (vrProvider == null) {
|
||||
throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + domainRouterVO.getHostName());
|
||||
}
|
||||
final Provider provider = Network.Provider.getProvider(vrProvider.getType().toString());
|
||||
if (provider == null) {
|
||||
throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
|
||||
}
|
||||
|
||||
for (final Pair<Nic, Network> nicNtwk : guestNics) {
|
||||
final Nic guestNic = nicNtwk.first();
|
||||
final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, domainRouterVO.getInstanceName(), controlNic.getIp4Address(), _routerControlHelper.getRouterIpInNetwork(
|
||||
guestNic.getNetworkId(), domainRouterVO.getId()));
|
||||
cmds.addCommand(startCmd);
|
||||
if (reprogramGuestNtwks) {
|
||||
finalizeIpAssocForNetwork(cmds, domainRouterVO, provider, guestNic.getNetworkId(), vlanMacAddress);
|
||||
finalizeNetworkRulesForNetwork(cmds, domainRouterVO, provider, guestNic.getNetworkId());
|
||||
}
|
||||
|
||||
finalizeUserDataAndDhcpOnStart(cmds, domainRouterVO, provider, guestNic.getNetworkId());
|
||||
final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, domainRouterVO.getInstanceName(), controlNic.getIp4Address(), _routerControlHelper.getRouterIpInNetwork(
|
||||
guestNic.getNetworkId(), domainRouterVO.getId()));
|
||||
cmds.addCommand(finishCmd);
|
||||
}
|
||||
|
||||
// Add network usage commands
|
||||
cmds.addCommands(usageCmds);
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
s_logger.warn("Failed to add router " + router + " to network due to exception ", ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4) RE-APPLY ALL STATIC ROUTE RULES
|
||||
final List<? extends StaticRoute> routes = _staticRouteDao.listByVpcId(router.getVpcId());
|
||||
final List<StaticRouteProfile> staticRouteProfiles = new ArrayList<StaticRouteProfile>(routes.size());
|
||||
final Map<Long, VpcGateway> gatewayMap = new HashMap<Long, VpcGateway>();
|
||||
for (final StaticRoute route : routes) {
|
||||
VpcGateway gateway = gatewayMap.get(route.getVpcGatewayId());
|
||||
if (gateway == null) {
|
||||
gateway = _entityMgr.findById(VpcGateway.class, route.getVpcGatewayId());
|
||||
gatewayMap.put(gateway.getId(), gateway);
|
||||
}
|
||||
staticRouteProfiles.add(new StaticRouteProfile(route, gateway));
|
||||
}
|
||||
|
||||
s_logger.debug("Found " + staticRouteProfiles.size() + " static routes to apply as a part of vpc route " + router + " start");
|
||||
if (!staticRouteProfiles.isEmpty()) {
|
||||
_commandSetupHelper.createStaticRouteCommands(staticRouteProfiles, router, cmds);
|
||||
}
|
||||
|
||||
// 5) RE-APPLY ALL REMOTE ACCESS VPNs
|
||||
final RemoteAccessVpnVO vpn = _vpnDao.findByAccountAndVpc(router.getAccountId(), router.getVpcId());
|
||||
if (vpn != null) {
|
||||
_commandSetupHelper.createApplyVpnCommands(true, vpn, router, cmds);
|
||||
}
|
||||
|
||||
// 6) REPROGRAM GUEST NETWORK
|
||||
boolean reprogramGuestNtwks = true;
|
||||
if (profile.getParameter(Param.ReProgramGuestNetworks) != null && (Boolean) profile.getParameter(Param.ReProgramGuestNetworks) == false) {
|
||||
reprogramGuestNtwks = false;
|
||||
}
|
||||
|
||||
final VirtualRouterProvider vrProvider = _vrProviderDao.findById(router.getElementId());
|
||||
if (vrProvider == null) {
|
||||
throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + router.getHostName());
|
||||
}
|
||||
final Provider provider = Network.Provider.getProvider(vrProvider.getType().toString());
|
||||
if (provider == null) {
|
||||
throw new CloudRuntimeException("Cannot find related provider of virtual router provider: " + vrProvider.getType().toString());
|
||||
}
|
||||
|
||||
for (final Pair<Nic, Network> nicNtwk : guestNics) {
|
||||
final Nic guestNic = nicNtwk.first();
|
||||
final AggregationControlCommand startCmd = new AggregationControlCommand(Action.Start, router.getInstanceName(), controlNic.getIp4Address(), getRouterIpInNetwork(
|
||||
guestNic.getNetworkId(), router.getId()));
|
||||
cmds.addCommand(startCmd);
|
||||
if (reprogramGuestNtwks) {
|
||||
finalizeIpAssocForNetwork(cmds, router, provider, guestNic.getNetworkId(), vlanMacAddress);
|
||||
finalizeNetworkRulesForNetwork(cmds, router, provider, guestNic.getNetworkId());
|
||||
}
|
||||
|
||||
finalizeUserDataAndDhcpOnStart(cmds, router, provider, guestNic.getNetworkId());
|
||||
final AggregationControlCommand finishCmd = new AggregationControlCommand(Action.Finish, router.getInstanceName(), controlNic.getIp4Address(), getRouterIpInNetwork(
|
||||
guestNic.getNetworkId(), router.getId()));
|
||||
cmds.addCommand(finishCmd);
|
||||
}
|
||||
|
||||
// Add network usage commands
|
||||
cmds.addCommands(usageCmds);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -457,11 +468,17 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
||||
super.finalizeNetworkRulesForNetwork(cmds, router, provider, guestNetworkId);
|
||||
|
||||
if (router.getVpcId() != null) {
|
||||
if (_networkModel.isProviderSupportServiceInNetwork(guestNetworkId, Service.NetworkACL, Provider.VPCVirtualRouter)) {
|
||||
final List<NetworkACLItemVO> networkACLs = _networkACLMgr.listNetworkACLItems(guestNetworkId);
|
||||
if (networkACLs != null && !networkACLs.isEmpty()) {
|
||||
s_logger.debug("Found " + networkACLs.size() + " network ACLs to apply as a part of VPC VR " + router + " start for guest network id=" + guestNetworkId);
|
||||
_commandSetupHelper.createNetworkACLsCommands(networkACLs, router, cmds, guestNetworkId, false);
|
||||
final List<DomainRouterVO> routers = getVpcRouters(router.getVpcId());
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
|
||||
if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
|
||||
if (_networkModel.isProviderSupportServiceInNetwork(guestNetworkId, Service.NetworkACL, Provider.VPCVirtualRouter)) {
|
||||
final List<NetworkACLItemVO> networkACLs = _networkACLMgr.listNetworkACLItems(guestNetworkId);
|
||||
if (networkACLs != null && !networkACLs.isEmpty()) {
|
||||
s_logger.debug("Found " + networkACLs.size() + " network ACLs to apply as a part of VPC VR " + domainRouterVO + " start for guest network id=" + guestNetworkId);
|
||||
_commandSetupHelper.createNetworkACLsCommands(networkACLs, domainRouterVO, cmds, guestNetworkId, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -565,12 +582,18 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
|
||||
return;
|
||||
}
|
||||
|
||||
final ArrayList<? extends PublicIpAddress> publicIps = getPublicIpsToApply(router, provider, guestNetworkId, IpAddress.State.Releasing);
|
||||
final List<DomainRouterVO> routers = getVpcRouters(router.getVpcId());
|
||||
for (final DomainRouterVO domainRouterVO : routers) {
|
||||
|
||||
if (publicIps != null && !publicIps.isEmpty()) {
|
||||
s_logger.debug("Found " + publicIps.size() + " ip(s) to apply as a part of domR " + router + " start.");
|
||||
// Re-apply public ip addresses - should come before PF/LB/VPN
|
||||
_commandSetupHelper.createVpcAssociatePublicIPCommands(router, publicIps, cmds, vlanMacAddress);
|
||||
if (domainRouterVO.getState() == State.Starting || domainRouterVO.getState() == State.Running) {
|
||||
final ArrayList<? extends PublicIpAddress> publicIps = getPublicIpsToApply(domainRouterVO, provider, guestNetworkId, IpAddress.State.Releasing);
|
||||
|
||||
if (publicIps != null && !publicIps.isEmpty()) {
|
||||
s_logger.debug("Found " + publicIps.size() + " ip(s) to apply as a part of domR " + domainRouterVO + " start.");
|
||||
// Re-apply public ip addresses - should come before PF/LB/VPN
|
||||
_commandSetupHelper.createVpcAssociatePublicIPCommands(domainRouterVO, publicIps, cmds, vlanMacAddress);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user