CLOUDSTACK-2116
Public IP addresses resource count of an account - number of ip addresses 
dedicated to an account
plus the number of ip addresses belonging to the system that have been 
allocated to the account


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

Branch: refs/heads/ui-vpc-redesign
Commit: f893aa8a786a071ebf108bad0368d148755ce233
Parents: 6d68875
Author: Likitha Shetty <[email protected]>
Authored: Tue May 14 14:24:47 2013 +0530
Committer: Likitha Shetty <[email protected]>
Committed: Wed May 15 18:05:01 2013 +0530

----------------------------------------------------------------------
 engine/schema/src/com/cloud/dc/dao/VlanDao.java    |    2 +
 .../schema/src/com/cloud/dc/dao/VlanDaoImpl.java   |   15 +++++++++
 .../configuration/ConfigurationManagerImpl.java    |   10 ++++++-
 .../src/com/cloud/network/NetworkManagerImpl.java  |   16 +++++-----
 .../resourcelimit/ResourceLimitManagerImpl.java    |   23 ++++++++++++++-
 5 files changed, 56 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f893aa8a/engine/schema/src/com/cloud/dc/dao/VlanDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDao.java 
b/engine/schema/src/com/cloud/dc/dao/VlanDao.java
index 605fb20..39fa818 100755
--- a/engine/schema/src/com/cloud/dc/dao/VlanDao.java
+++ b/engine/schema/src/com/cloud/dc/dao/VlanDao.java
@@ -54,4 +54,6 @@ public interface VlanDao extends GenericDao<VlanVO, Long> {
         List<VlanVO> listZoneWideNonDedicatedVlans(long zoneId);
 
     List<VlanVO> listVlansByNetworkIdAndGateway(long networkid, String 
gateway);
+
+    List<VlanVO> listDedicatedVlans(long accountId);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f893aa8a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java 
b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
index e8c68b1..eb3bde9 100755
--- a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
+++ b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java
@@ -58,6 +58,7 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> 
implements VlanDao
     protected SearchBuilder<VlanVO> PhysicalNetworkVlanSearch;
     protected SearchBuilder<VlanVO> ZoneWideNonDedicatedVlanSearch;
     protected SearchBuilder<VlanVO> VlanGatewaysearch;
+    protected SearchBuilder<VlanVO> DedicatedVlanSearch;
 
     protected SearchBuilder<AccountVlanMapVO> AccountVlanMapSearch;
 
@@ -213,6 +214,13 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, 
Long> implements VlanDao
         ZoneWideNonDedicatedVlanSearch.done();
         AccountVlanMapSearch.done();
 
+        DedicatedVlanSearch = createSearchBuilder();
+        AccountVlanMapSearch = _accountVlanMapDao.createSearchBuilder();
+        AccountVlanMapSearch.and("accountId", 
AccountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
+        DedicatedVlanSearch.join("AccountVlanMapSearch", AccountVlanMapSearch, 
DedicatedVlanSearch.entity().getId(), 
AccountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.LEFTOUTER);
+        DedicatedVlanSearch.done();
+        AccountVlanMapSearch.done();
+
         return result;
     }
 
@@ -343,4 +351,11 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, 
Long> implements VlanDao
         return listBy(sc);
     }
 
+    @Override
+    public List<VlanVO> listDedicatedVlans(long accountId) {
+        SearchCriteria<VlanVO> sc = DedicatedVlanSearch.create();
+        sc.setJoinParameters("AccountVlanMapSearch", "accountId", accountId);
+        return listBy(sc);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f893aa8a/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 55e7b7e..e1aaa50 100755
--- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
@@ -2853,6 +2853,8 @@ public class ConfigurationManagerImpl extends ManagerBase 
implements Configurati
                         ip.getDataCenterId(), ip.getId(), 
ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
                         ip.getSystem(), ip.getClass().getName(), ip.getUuid());
             }
+            // increment resource count for dedicated public ip's
+            _resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), 
ResourceType.public_ip, new Long(ips.size()));
         } else if (podId != null) {
             // This VLAN is pod-wide, so create a PodVlanMapVO entry
             PodVlanMapVO podVlanMapVO = new PodVlanMapVO(podId, vlan.getId());
@@ -3124,6 +3126,10 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
                     ip.getDataCenterId(), ip.getId(), 
ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
                     ip.getSystem(), ip.getClass().getName(), ip.getUuid());
         }
+
+        // increment resource count for dedicated public ip's
+        _resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), 
ResourceType.public_ip, new Long(ips.size()));
+
         return vlan;
     }
 
@@ -3187,10 +3193,12 @@ public class ConfigurationManagerImpl extends 
ManagerBase implements Configurati
         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(),
+                
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, 
acctVln.get(0).getAccountId(),
                         ip.getDataCenterId(), ip.getId(), 
ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
                         ip.getSystem(), ip.getClass().getName(), ip.getUuid());
             }
+            // decrement resource count for dedicated public ip's
+            
_resourceLimitMgr.decrementResourceCount(acctVln.get(0).getAccountId(), 
ResourceType.public_ip, new Long(ips.size()));
             return true;
         } else {
             return false;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f893aa8a/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 4ce5440..9440286 100755
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@ -394,8 +394,8 @@ public class NetworkManagerImpl extends ManagerBase 
implements NetworkManager, L
                         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) {
+            // don't increment resource count for direct and dedicated ip 
addresses
+            if (addr.getAssociatedWithNetworkId() != null && 
!isIpDedicated(addr)) {
                 _resourceLimitMgr.incrementResourceCount(owner.getId(), 
ResourceType.public_ip);
             }
         }
@@ -640,10 +640,6 @@ public class NetworkManagerImpl extends ManagerBase 
implements NetworkManager, L
                 s_logger.debug("Associate IP address lock acquired");
             }
 
-            // Check that the maximum number of public IPs for the given
-            // accountId will not be exceeded
-            _resourceLimitMgr.checkResourceLimit(accountToLock, 
ResourceType.public_ip);
-
             txn.start();
 
             // If account has dedicated Public IP ranges, allocate IP from the 
dedicated range
@@ -668,6 +664,10 @@ public class NetworkManagerImpl extends ManagerBase 
implements NetworkManager, L
             }
 
             if (!allocateFromDedicatedRange) {
+                // Check that the maximum number of public IPs for the given
+                // accountId will not be exceeded
+                _resourceLimitMgr.checkResourceLimit(accountToLock, 
ResourceType.public_ip);
+
                 List<VlanVO> nonDedicatedVlans = 
_vlanDao.listZoneWideNonDedicatedVlans(zone.getId());
                 for (VlanVO nonDedicatedVlan : nonDedicatedVlans) {
                     nonDedicatedVlanDbIds.add(nonDedicatedVlan.getId());
@@ -2964,8 +2964,8 @@ public class NetworkManagerImpl extends ManagerBase 
implements NetworkManager, L
         if (ip.getState() != State.Releasing) {
             txn.start();
 
-            // don't decrement resource count for direct ips
-            if (ip.getAssociatedWithNetworkId() != null) {
+            // don't decrement resource count for direct and dedicated ips
+            if (ip.getAssociatedWithNetworkId() != null && !isIpDedicated(ip)) 
{
                 
_resourceLimitMgr.decrementResourceCount(_ipAddressDao.findById(addrId).getAllocatedToAccountId(),
 ResourceType.public_ip);
             }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f893aa8a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java 
b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
index 5bb7708..6d929c6 100755
--- a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
+++ b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
@@ -46,6 +46,8 @@ import com.cloud.configuration.dao.ConfigurationDao;
 import com.cloud.configuration.dao.ResourceCountDao;
 import com.cloud.configuration.dao.ResourceLimitDao;
 import com.cloud.dao.EntityManager;
+import com.cloud.dc.VlanVO;
+import com.cloud.dc.dao.VlanDao;
 import com.cloud.domain.Domain;
 import com.cloud.domain.DomainVO;
 import com.cloud.domain.dao.DomainDao;
@@ -53,6 +55,7 @@ import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.PermissionDeniedException;
 import com.cloud.exception.ResourceAllocationException;
 import com.cloud.network.dao.IPAddressDao;
+import com.cloud.network.dao.IPAddressVO;
 import com.cloud.network.dao.NetworkDao;
 import com.cloud.network.vpc.dao.VpcDao;
 import com.cloud.projects.Project;
@@ -141,6 +144,8 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
     private ServiceOfferingDao _serviceOfferingDao;
     @Inject
     private VMTemplateHostDao _vmTemplateHostDao;
+    @Inject
+    private VlanDao _vlanDao;
 
     protected GenericSearchBuilder<VMTemplateHostVO, SumCount> 
templateSizeSearch;
 
@@ -814,7 +819,7 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
         } else if (type == Resource.ResourceType.snapshot) {
             newCount = _snapshotDao.countSnapshotsForAccount(accountId);
         } else if (type == Resource.ResourceType.public_ip) {
-            newCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
+            newCount = calculatePublicIpForAccount(accountId);
         } else if (type == Resource.ResourceType.template) {
             newCount = _vmTemplateDao.countTemplatesForAccount(accountId);
         } else if (type == Resource.ResourceType.project) {
@@ -906,6 +911,22 @@ public class ResourceLimitManagerImpl extends ManagerBase 
implements ResourceLim
         return totalVolumesSize + totalSnapshotsSize + totalTemplatesSize;
     }
 
+    private long calculatePublicIpForAccount(long accountId) {
+        Long dedicatedCount = 0L;
+        Long allocatedCount = 0L;
+
+        List<VlanVO> dedicatedVlans = _vlanDao.listDedicatedVlans(accountId);
+        for (VlanVO dedicatedVlan : dedicatedVlans) {
+            List<IPAddressVO> ips = 
_ipAddressDao.listByVlanId(dedicatedVlan.getId());
+            dedicatedCount += new Long(ips.size());
+        }
+        allocatedCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
+        if (dedicatedCount > allocatedCount)
+            return dedicatedCount;
+        else
+            return allocatedCount;
+    }
+
     @Override
     public long getResourceCount(Account account, ResourceType type) {
         return _resourceCountDao.getResourceCount(account.getId(), 
ResourceOwnerType.Account, type);

Reply via email to