mirror of https://github.com/apache/cloudstack.git
Cloudstack-2621 [Multiple_IP_Ranges] Failed to delete guest IP range from a new subnet
Signed-off-by: Abhinandan Prateek <aprateek@apache.org>
This commit is contained in:
parent
cd89ff7241
commit
d512368f45
|
|
@ -2906,6 +2906,13 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
|
|||
return vlan;
|
||||
}
|
||||
|
||||
public boolean removeFromDb (long vlanDbId){
|
||||
if (!deletePublicIPRange(vlanDbId)) {
|
||||
return false;
|
||||
}
|
||||
return _vlanDao.expunge(vlanDbId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@DB
|
||||
public boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId, Account caller) {
|
||||
|
|
@ -2979,49 +2986,60 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
|
|||
Network network = _networkDao.findById(vlanRange.getNetworkId());
|
||||
DhcpServiceProvider dhcpServiceProvider = _networkMgr.getDhcpServiceProvider(network);
|
||||
if (!dhcpServiceProvider.getProvider().getName().equalsIgnoreCase(Provider.VirtualRouter.getName())) {
|
||||
if (!deletePublicIPRange(vlanDbId)) {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
if (!removeFromDb(vlanDbId)) {
|
||||
txn.rollback();
|
||||
return false;
|
||||
}
|
||||
_vlanDao.expunge(vlanDbId);
|
||||
return true;
|
||||
}
|
||||
//search if the vlan has any allocated ips.
|
||||
boolean aliasIpBelongsToThisVlan = false;
|
||||
long freeIpsInsubnet = 0;
|
||||
NicIpAliasVO ipAlias = null;
|
||||
allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
|
||||
if (allocIpCount > 1) {
|
||||
throw new InvalidParameterValueException ("cannot delete this range as some of the vlans are in use.");
|
||||
}
|
||||
if (allocIpCount == 0){
|
||||
//remove the vlan range.
|
||||
if (!deletePublicIPRange(vlanDbId)) {
|
||||
return false;
|
||||
else {
|
||||
txn.commit();
|
||||
}
|
||||
_vlanDao.expunge(vlanDbId);
|
||||
return true;
|
||||
txn.close();
|
||||
}
|
||||
//check if this allocated ip is being used as an ipAlias on the router.
|
||||
else {
|
||||
return handleIpAliasDeletion(vlanRange, vlanDbId, dhcpServiceProvider, network);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleIpAliasDeletion(VlanVO vlanRange, long vlanDbId, DhcpServiceProvider dhcpServiceProvider, Network network) {
|
||||
boolean result_final = false;
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
IPAddressVO ip = null;
|
||||
NicIpAliasVO ipAlias = null;
|
||||
try{
|
||||
Integer allocIpCount=0;
|
||||
//search if the vlan has any allocated ips.
|
||||
allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
|
||||
if (allocIpCount > 1) {
|
||||
throw new InvalidParameterValueException ("cannot delete this range as some of the vlans are in use.");
|
||||
}
|
||||
if (allocIpCount == 0){
|
||||
result_final=true;
|
||||
}
|
||||
else {
|
||||
ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(vlanRange.getVlanGateway(), vlanRange.getNetworkId(), NicIpAlias.state.active);
|
||||
ipAlias.setState(NicIpAlias.state.revoked);
|
||||
_nicIpAliasDao.update(ipAlias.getId(), ipAlias);
|
||||
//check if this ip belongs to this vlan and is allocated.
|
||||
IPAddressVO ip = _publicIpAddressDao.findByIpAndVlanId(ipAlias.getIp4Address(), vlanDbId);
|
||||
ip = _publicIpAddressDao.findByIpAndVlanId(ipAlias.getIp4Address(), vlanDbId);
|
||||
if (ip != null && ip.getState() == IpAddress.State.Allocated) {
|
||||
aliasIpBelongsToThisVlan =true;
|
||||
//check if there any other vlan ranges in the same subnet having free ips
|
||||
List<VlanVO> vlanRanges = _vlanDao.listVlansByNetworkIdAndGateway(vlanRange.getNetworkId(), vlanRange.getVlanGateway());
|
||||
//if there is no other vlanrage in this subnet. free the ip and delete the vlan.
|
||||
if (vlanRanges.size() == 1){
|
||||
boolean result = dhcpServiceProvider.removeDhcpSupportForSubnet(network);
|
||||
if (result == false) {
|
||||
result_final = false;
|
||||
s_logger.debug("Failed to delete the vlan range as we could not free the ip used to provide the dhcp service.");
|
||||
}
|
||||
else {
|
||||
_publicIpAddressDao.unassignIpAddress(ip.getId());
|
||||
if (!deletePublicIPRange(vlanDbId)) {
|
||||
return false;
|
||||
}
|
||||
_vlanDao.expunge(vlanDbId);
|
||||
_nicIpAliasDao.expunge(ipAlias.getId());
|
||||
result_final = true;
|
||||
}
|
||||
} else {
|
||||
// if there are more vlans in the subnet check if there are free ips.
|
||||
|
|
@ -3034,62 +3052,50 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
|
|||
s_logger.info("vlan Range"+vlanRange.getId()+" id being deleted, one of the Ips in this range is used to provide the dhcp service, trying to free this ip and allocate a new one.");
|
||||
for (VlanVO vlanrange : vlanRanges) {
|
||||
if (vlanrange.getId() != vlanDbId) {
|
||||
freeIpsInsubnet = _publicIpAddressDao.countFreeIpsInVlan(vlanrange.getId());
|
||||
long freeIpsInsubnet = _publicIpAddressDao.countFreeIpsInVlan(vlanrange.getId());
|
||||
if (freeIpsInsubnet > 0){
|
||||
//assign one free ip to the router for creating ip Alias.
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
//changing the state to revoked so that removeDhcpSupport for subnet sses it.
|
||||
ipAlias.setState(NicIpAlias.state.revoked);
|
||||
_nicIpAliasDao.update(ipAlias.getId(), ipAlias);
|
||||
//assign one free ip to the router for creating ip Alias. The ipalias is system managed ip so we are using the system account to allocate the ip not the caller.
|
||||
boolean result = false;
|
||||
try {
|
||||
PublicIp routerPublicIP = _networkMgr.assignPublicIpAddressFromVlans(network.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, network.getId(), null, false);
|
||||
s_logger.info("creating a db entry for the new ip alias.");
|
||||
NicIpAliasVO newipAlias = new NicIpAliasVO(ipAlias.getNicId(), routerPublicIP.getAddress().addr(), ipAlias.getVmId(), ipAlias.getAccountId(), network.getDomainId(), network.getId(), ipAlias.getGateway(), ipAlias.getNetmask());
|
||||
newipAlias.setAliasCount(routerPublicIP.getIpMacAddress());
|
||||
_nicIpAliasDao.persist(newipAlias);
|
||||
//we revoke all the rules and apply all the rules as a part of the removedhcp config. so the new ip will get configured when we delete the old ip.
|
||||
|
||||
}
|
||||
catch (InsufficientAddressCapacityException e) {
|
||||
txn.rollback();
|
||||
txn.close();
|
||||
throw new InvalidParameterValueException("cannot delete vlan range"+ vlanRange.getId()+"one of the ips in this range is benig used to provide dhcp service. Cannot use some other ip as there are no free ips in this subnet");
|
||||
}
|
||||
PublicIp routerPublicIP = _networkMgr.assignPublicIpAddressFromVlans(network.getDataCenterId(), null, _accountDao.findById(Account.ACCOUNT_ID_SYSTEM), Vlan.VlanType.DirectAttached, vlanDbIdList, network.getId(), null, false);
|
||||
s_logger.info("creating a db entry for the new ip alias.");
|
||||
NicIpAliasVO newipAlias = new NicIpAliasVO(ipAlias.getNicId(), routerPublicIP.getAddress().addr(), ipAlias.getVmId(), ipAlias.getAccountId(), network.getDomainId(), network.getId(), ipAlias.getGateway(), ipAlias.getNetmask());
|
||||
newipAlias.setAliasCount(routerPublicIP.getIpMacAddress());
|
||||
_nicIpAliasDao.persist(newipAlias);
|
||||
//we revoke all the rules and apply all the rules as a part of the removedhcp config. so the new ip will get configured when we delete the old ip.
|
||||
s_logger.info("removing the old ip alias on router");
|
||||
result = dhcpServiceProvider.removeDhcpSupportForSubnet(network);
|
||||
if (result == false) {
|
||||
s_logger.debug("could't delete the ip alias on the router");
|
||||
txn.rollback();
|
||||
txn.close();
|
||||
return false;
|
||||
result_final = false;
|
||||
}
|
||||
_publicIpAddressDao.unassignIpAddress(ip.getId());
|
||||
if (!deletePublicIPRange(vlanDbId)) {
|
||||
return false;
|
||||
else {
|
||||
_publicIpAddressDao.unassignIpAddress(ip.getId());
|
||||
result_final=true;
|
||||
}
|
||||
_vlanDao.expunge(vlanDbId);
|
||||
txn.commit();
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// when there is no dhcp support in the network.
|
||||
if (!deletePublicIPRange(vlanDbId)) {
|
||||
return false;
|
||||
} catch (InsufficientAddressCapacityException e) {
|
||||
throw new InvalidParameterValueException("cannot delete vlan range"+ vlanRange.getId()+"one of the ips in this range is benig used to provide dhcp service. Cannot use some other ip as there are no free ips in this subnet");
|
||||
}
|
||||
finally {
|
||||
if (result_final) {
|
||||
if (!removeFromDb(vlanDbId)) {
|
||||
txn.rollback();
|
||||
}
|
||||
_vlanDao.expunge(vlanDbId);
|
||||
return true;
|
||||
else {
|
||||
txn.commit();
|
||||
}
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return result_final;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DB
|
||||
@ActionEvent(eventType = EventTypes.EVENT_VLAN_IP_RANGE_DEDICATE, eventDescription = "dedicating vlan ip range", async = false)
|
||||
|
|
@ -3307,6 +3313,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
|
|||
stmt.setLong(1, vlanDbId);
|
||||
stmt.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
s_logger.error(ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
txn.commit();
|
||||
|
|
|
|||
|
|
@ -2765,7 +2765,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
|
|||
//Asuming we have only one router per network For Now.
|
||||
DomainRouterVO router = routers.get(0);
|
||||
if (router.getState() != State.Running) {
|
||||
s_logger.warn("Failed to add/remove VPN users: router not in running state");
|
||||
s_logger.warn("Failed to configure dhcp: router not in running state");
|
||||
throw new ResourceUnavailableException("Unable to assign ip addresses, domR is not in right state " +
|
||||
router.getState(), DataCenter.class, network.getDataCenterId());
|
||||
}
|
||||
|
|
@ -2875,6 +2875,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
|
|||
if (result) {
|
||||
for (NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) {
|
||||
_nicIpAliasDao.expunge(revokedAliasVO.getId());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue