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


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/e091f431
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/e091f431
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/e091f431

Branch: refs/heads/internallb
Commit: e091f43182cf1602a2dda0990a0aa95e5cfa219e
Parents: b16ccc9
Author: Likitha Shetty <[email protected]>
Authored: Thu Apr 11 23:04:41 2013 +0530
Committer: Likitha Shetty <[email protected]>
Committed: Wed May 1 14:29:50 2013 +0530

----------------------------------------------------------------------
 .../configuration/ConfigurationManagerImpl.java    |   35 +++++++++++++-
 .../src/com/cloud/network/NetworkManagerImpl.java  |   24 +++++++---
 2 files changed, 49 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e091f431/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java 
b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
index 4092e74..37ca793 100755
--- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
@@ -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;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e091f431/server/src/com/cloud/network/NetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java 
b/server/src/com/cloud/network/NetworkManagerImpl.java
index 4a8b38f..5179e87 100755
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@ -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);

Reply via email to