CLOUDSTACK-2049. Generate usage events for usage type Public IP addresses when IP is dedicated to and released from an account

1. Publish usage event EVENT_NET_IP_ASSIGN when an ip is dedicated to an account or a non-dedicated ip is acquired by an account
2. Publish usage event EVENT_NET_IP_RELEASE when an ip is released from an account or deleted
This commit is contained in:
Likitha Shetty 2013-04-11 23:04:41 +05:30
parent b16ccc9fa6
commit e091f43182
2 changed files with 49 additions and 10 deletions

View File

@ -42,6 +42,7 @@ import javax.naming.directory.InitialDirContext;
import com.cloud.dc.*;
import com.cloud.dc.dao.*;
import com.cloud.user.*;
import com.cloud.event.UsageEventUtils;
import org.apache.cloudstack.acl.SecurityChecker;
import org.apache.cloudstack.api.ApiConstants.LDAPParams;
import org.apache.cloudstack.api.command.admin.config.UpdateCfgCmd;
@ -2706,6 +2707,14 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
// This VLAN is account-specific, so create an AccountVlanMapVO entry
AccountVlanMapVO accountVlanMapVO = new AccountVlanMapVO(vlanOwner.getId(), vlan.getId());
_accountVlanMapDao.persist(accountVlanMapVO);
// generate usage event for dedication of every ip address in the range
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlan.getId());
for (IPAddressVO ip : ips) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(),
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
}
} else if (podId != null) {
// This VLAN is pod-wide, so create a PodVlanMapVO entry
PodVlanMapVO podVlanMapVO = new PodVlanMapVO(podId, vlan.getId());
@ -2734,6 +2743,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
// Check if the VLAN has any allocated public IPs
long allocIpCount = _publicIpAddressDao.countIPs(vlan.getDataCenterId(), vlanDbId, true);
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
boolean success = true;
if (allocIpCount > 0) {
if (isAccountSpecific) {
@ -2747,8 +2757,6 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
s_logger.debug("lock vlan " + vlanDbId + " is acquired");
}
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
for (IPAddressVO ip : ips) {
if (ip.isOneToOneNat()) {
throw new InvalidParameterValueException("Can't delete account specific vlan " + vlanDbId +
@ -2785,6 +2793,15 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
return false;
}
// if ip range is dedicated to an account generate usage events for release of every ip in the range
if(isAccountSpecific) {
for (IPAddressVO ip : ips) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getId(),
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
}
}
// Delete the VLAN
return _vlanDao.expunge(vlanDbId);
} else {
@ -2869,6 +2886,12 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
txn.commit();
// generate usage event for dedication of every ip address in the range
for (IPAddressVO ip : ips) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(),
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
}
return vlan;
}
@ -2898,6 +2921,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
// Check if range has any allocated public IPs
long allocIpCount = _publicIpAddressDao.countIPs(vlan.getDataCenterId(), vlanDbId, true);
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
boolean success = true;
if (allocIpCount > 0) {
try {
@ -2908,7 +2932,6 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
if (s_logger.isDebugEnabled()) {
s_logger.debug("lock vlan " + vlanDbId + " is acquired");
}
List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlanDbId);
for (IPAddressVO ip : ips) {
// Disassociate allocated IP's that are not in use
if ( !ip.isOneToOneNat() && !ip.isSourceNat() && !(_firewallDao.countRulesByIpId(ip.getId()) > 0) ) {
@ -2930,6 +2953,12 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
// A Public IP range can only be dedicated to one account at a time
if (_accountVlanMapDao.remove(acctVln.get(0).getId())) {
// generate usage events to remove dedication for every ip in the range
for (IPAddressVO ip : ips) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getId(),
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
}
return true;
} else {
return false;

View File

@ -369,9 +369,12 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
VlanVO vlan = _vlanDao.findById(addr.getVlanId());
String guestType = vlan.getVlanType().toString();
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(),
addr.getDataCenterId(), addr.getId(), addr.getAddress().toString(), addr.isSourceNat(), guestType,
addr.getSystem(), addr.getClass().getName(), addr.getUuid());
if (!isIpDedicated(addr)) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(),
addr.getDataCenterId(), addr.getId(), addr.getAddress().toString(), addr.isSourceNat(), guestType,
addr.getSystem(), addr.getClass().getName(), addr.getUuid());
}
// don't increment resource count for direct ip addresses
if (addr.getAssociatedWithNetworkId() != null) {
_resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip);
@ -381,6 +384,12 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
txn.commit();
}
private boolean isIpDedicated(IPAddressVO addr) {
List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByVlan(addr.getVlanId());
if (maps != null && !maps.isEmpty())
return true;
return false;
}
@Override
public PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork)
@ -2890,10 +2899,11 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
VlanVO vlan = _vlanDao.findById(ip.getVlanId());
String guestType = vlan.getVlanType().toString();
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE,
ip.getAllocatedToAccountId(), ip.getDataCenterId(), addrId, ip.getAddress().addr(),
ip.isSourceNat(), guestType, ip.getSystem(), ip.getClass().getName(), ip.getUuid());
if (!isIpDedicated(ip)) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE,
ip.getAllocatedToAccountId(), ip.getDataCenterId(), addrId, ip.getAddress().addr(),
ip.isSourceNat(), guestType, ip.getSystem(), ip.getClass().getName(), ip.getUuid());
}
}
ip = _ipAddressDao.markAsUnavailable(addrId);