Skip to content

Commit 7e6ec2c

Browse files
DamodarJayapal
authored andcommitted
CLOUDSTACK-7877: The NET.IPRELEASE events are not added to usage_event on IP range deletion from Physical Networks.
Signed-off-by: Jayapal <jayapal@apache.org>
1 parent 0350590 commit 7e6ec2c

1 file changed

Lines changed: 52 additions & 55 deletions

File tree

server/src/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,69 +3159,66 @@ public boolean deleteVlanAndPublicIpRange(long userId, final long vlanDbId, Acco
31593159
}
31603160

31613161
// Check if the VLAN has any allocated public IPs
3162-
long allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
31633162
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
3164-
boolean success = true;
3165-
if (allocIpCount > 0) {
3166-
if (isAccountSpecific) {
3167-
try {
3168-
vlanRange = _vlanDao.acquireInLockTable(vlanDbId, 30);
3169-
if (vlanRange == null) {
3170-
throw new CloudRuntimeException("Unable to acquire vlan configuration: " + vlanDbId);
3171-
}
3172-
3173-
if (s_logger.isDebugEnabled()) {
3174-
s_logger.debug("lock vlan " + vlanDbId + " is acquired");
3175-
}
3176-
for (IPAddressVO ip : ips) {
3177-
if (ip.isOneToOneNat()) {
3178-
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip
3179-
+ " belonging to the range is used for static nat purposes. Cleanup the rules first");
3180-
}
3181-
3182-
if (ip.isSourceNat()) {
3183-
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip
3184-
+ " belonging to the range is a source nat ip for the network id=" + ip.getSourceNetworkId()
3185-
+ ". IP range with the source nat ip address can be removed either as a part of Network, or account removal");
3186-
}
3163+
if (isAccountSpecific) {
3164+
try {
3165+
vlanRange = _vlanDao.acquireInLockTable(vlanDbId, 30);
3166+
if (vlanRange == null) {
3167+
throw new CloudRuntimeException("Unable to acquire vlan configuration: " + vlanDbId);
3168+
}
31873169

3188-
if (_firewallDao.countRulesByIpId(ip.getId()) > 0) {
3189-
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip
3190-
+ " belonging to the range has firewall rules applied. Cleanup the rules first");
3191-
}
3192-
// release public ip address here
3193-
success = success && _ipAddrMgr.disassociatePublicIpAddress(ip.getId(), userId, caller);
3194-
}
3195-
if (!success) {
3196-
s_logger.warn("Some ip addresses failed to be released as a part of vlan " + vlanDbId + " removal");
3197-
} else {
3198-
for (IPAddressVO ip : ips) {
3199-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getId(), ip.getDataCenterId(), ip.getId(),
3200-
ip.getAddress().toString(), ip.isSourceNat(), vlanRange.getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid());
3201-
}
3202-
}
3203-
} finally {
3204-
_vlanDao.releaseFromLockTable(vlanDbId);
3205-
}
3206-
} else { // !isAccountSpecific
3207-
NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(vlanRange.getVlanGateway(), vlanRange.getNetworkId(), NicIpAlias.state.active);
3208-
//check if the ipalias belongs to the vlan range being deleted.
3209-
if (ipAlias != null && vlanDbId == _publicIpAddressDao.findByIpAndSourceNetworkId(vlanRange.getNetworkId(), ipAlias.getIp4Address()).getVlanId()) {
3210-
throw new InvalidParameterValueException("Cannot delete vlan range " + vlanDbId + " as " + ipAlias.getIp4Address()
3211-
+ "is being used for providing dhcp service in this subnet. Delete all VMs in this subnet and try again");
3212-
}
3213-
allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
3214-
if (allocIpCount > 0) {
3215-
throw new InvalidParameterValueException(allocIpCount + " Ips are in use. Cannot delete this vlan");
3216-
}
3170+
if (s_logger.isDebugEnabled()) {
3171+
s_logger.debug("lock vlan " + vlanDbId + " is acquired");
32173172
}
3173+
for (IPAddressVO ip : ips) {
3174+
boolean success = true;
3175+
if (ip.isOneToOneNat()) {
3176+
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip
3177+
+ " belonging to the range is used for static nat purposes. Cleanup the rules first");
3178+
}
3179+
3180+
if (ip.isSourceNat()) {
3181+
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip
3182+
+ " belonging to the range is a source nat ip for the network id=" + ip.getSourceNetworkId()
3183+
+ ". IP range with the source nat ip address can be removed either as a part of Network, or account removal");
3184+
}
3185+
3186+
if (_firewallDao.countRulesByIpId(ip.getId()) > 0) {
3187+
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId + " as ip " + ip
3188+
+ " belonging to the range has firewall rules applied. Cleanup the rules first");
3189+
}
3190+
if(ip.getAllocatedTime() != null) {// This means IP is allocated
3191+
// release public ip address here
3192+
success = _ipAddrMgr.disassociatePublicIpAddress(ip.getId(), userId, caller);
3193+
}
3194+
if (!success) {
3195+
s_logger.warn("Some ip addresses failed to be released as a part of vlan " + vlanDbId + " removal");
3196+
} else {
3197+
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getAccountId(), ip.getDataCenterId(), ip.getId(),
3198+
ip.getAddress().toString(), ip.isSourceNat(), vlanRange.getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid());
3199+
}
3200+
}
3201+
} finally {
3202+
_vlanDao.releaseFromLockTable(vlanDbId);
3203+
}
3204+
} else { // !isAccountSpecific
3205+
NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(vlanRange.getVlanGateway(), vlanRange.getNetworkId(), NicIpAlias.state.active);
3206+
//check if the ipalias belongs to the vlan range being deleted.
3207+
if (ipAlias != null && vlanDbId == _publicIpAddressDao.findByIpAndSourceNetworkId(vlanRange.getNetworkId(), ipAlias.getIp4Address()).getVlanId()) {
3208+
throw new InvalidParameterValueException("Cannot delete vlan range " + vlanDbId + " as " + ipAlias.getIp4Address()
3209+
+ "is being used for providing dhcp service in this subnet. Delete all VMs in this subnet and try again");
3210+
}
3211+
long allocIpCount = _publicIpAddressDao.countIPs(vlanRange.getDataCenterId(), vlanDbId, true);
3212+
if (allocIpCount > 0) {
3213+
throw new InvalidParameterValueException(allocIpCount + " Ips are in use. Cannot delete this vlan");
3214+
}
32183215
}
32193216

32203217
Transaction.execute(new TransactionCallbackNoReturn() {
32213218
@Override
32223219
public void doInTransactionWithoutResult(TransactionStatus status) {
3223-
_publicIpAddressDao.deletePublicIPRange(vlanDbId);
3224-
_vlanDao.remove(vlanDbId);
3220+
_publicIpAddressDao.deletePublicIPRange(vlanDbId);
3221+
_vlanDao.remove(vlanDbId);
32253222
}
32263223
});
32273224

0 commit comments

Comments
 (0)