Skip to content

Commit f893aa8

Browse files
author
Likitha Shetty
committed
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
1 parent 6d68875 commit f893aa8

5 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎engine/schema/src/com/cloud/dc/dao/VlanDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ public interface VlanDao extends GenericDao<VlanVO, Long> {
5454
List<VlanVO> listZoneWideNonDedicatedVlans(long zoneId);
5555

5656
List<VlanVO> listVlansByNetworkIdAndGateway(long networkid, String gateway);
57+
58+
List<VlanVO> listDedicatedVlans(long accountId);
5759
}

‎engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> implements VlanDao
5858
protected SearchBuilder<VlanVO> PhysicalNetworkVlanSearch;
5959
protected SearchBuilder<VlanVO> ZoneWideNonDedicatedVlanSearch;
6060
protected SearchBuilder<VlanVO> VlanGatewaysearch;
61+
protected SearchBuilder<VlanVO> DedicatedVlanSearch;
6162

6263
protected SearchBuilder<AccountVlanMapVO> AccountVlanMapSearch;
6364

@@ -213,6 +214,13 @@ public boolean configure(String name, Map<String, Object> params)
213214
ZoneWideNonDedicatedVlanSearch.done();
214215
AccountVlanMapSearch.done();
215216

217+
DedicatedVlanSearch = createSearchBuilder();
218+
AccountVlanMapSearch = _accountVlanMapDao.createSearchBuilder();
219+
AccountVlanMapSearch.and("accountId", AccountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
220+
DedicatedVlanSearch.join("AccountVlanMapSearch", AccountVlanMapSearch, DedicatedVlanSearch.entity().getId(), AccountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.LEFTOUTER);
221+
DedicatedVlanSearch.done();
222+
AccountVlanMapSearch.done();
223+
216224
return result;
217225
}
218226

@@ -343,4 +351,11 @@ public List<VlanVO> listZoneWideNonDedicatedVlans(long zoneId) {
343351
return listBy(sc);
344352
}
345353

354+
@Override
355+
public List<VlanVO> listDedicatedVlans(long accountId) {
356+
SearchCriteria<VlanVO> sc = DedicatedVlanSearch.create();
357+
sc.setJoinParameters("AccountVlanMapSearch", "accountId", accountId);
358+
return listBy(sc);
359+
}
360+
346361
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,8 @@ public Vlan createVlanAndPublicIpRange(long zoneId, long networkId, long physica
28532853
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
28542854
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
28552855
}
2856+
// increment resource count for dedicated public ip's
2857+
_resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size()));
28562858
} else if (podId != null) {
28572859
// This VLAN is pod-wide, so create a PodVlanMapVO entry
28582860
PodVlanMapVO podVlanMapVO = new PodVlanMapVO(podId, vlan.getId());
@@ -3124,6 +3126,10 @@ public Vlan dedicatePublicIpRange(DedicatePublicIpRangeCmd cmd) throws ResourceA
31243126
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
31253127
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
31263128
}
3129+
3130+
// increment resource count for dedicated public ip's
3131+
_resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size()));
3132+
31273133
return vlan;
31283134
}
31293135

@@ -3187,10 +3193,12 @@ public boolean releasePublicIpRange(long vlanDbId, long userId, Account caller)
31873193
if (_accountVlanMapDao.remove(acctVln.get(0).getId())) {
31883194
// generate usage events to remove dedication for every ip in the range
31893195
for (IPAddressVO ip : ips) {
3190-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getId(),
3196+
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getAccountId(),
31913197
ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan.getVlanType().toString(),
31923198
ip.getSystem(), ip.getClass().getName(), ip.getUuid());
31933199
}
3200+
// decrement resource count for dedicated public ip's
3201+
_resourceLimitMgr.decrementResourceCount(acctVln.get(0).getAccountId(), ResourceType.public_ip, new Long(ips.size()));
31943202
return true;
31953203
} else {
31963204
return false;

‎server/src/com/cloud/network/NetworkManagerImpl.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ public void markPublicIpAsAllocated(IPAddressVO addr) {
394394
addr.getDataCenterId(), addr.getId(), addr.getAddress().toString(), addr.isSourceNat(), guestType,
395395
addr.getSystem(), addr.getClass().getName(), addr.getUuid());
396396
}
397-
// don't increment resource count for direct ip addresses
398-
if (addr.getAssociatedWithNetworkId() != null) {
397+
// don't increment resource count for direct and dedicated ip addresses
398+
if (addr.getAssociatedWithNetworkId() != null && !isIpDedicated(addr)) {
399399
_resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip);
400400
}
401401
}
@@ -640,10 +640,6 @@ public IpAddress allocateIp(Account ipOwner, boolean isSystem, Account caller, l
640640
s_logger.debug("Associate IP address lock acquired");
641641
}
642642

643-
// Check that the maximum number of public IPs for the given
644-
// accountId will not be exceeded
645-
_resourceLimitMgr.checkResourceLimit(accountToLock, ResourceType.public_ip);
646-
647643
txn.start();
648644

649645
// If account has dedicated Public IP ranges, allocate IP from the dedicated range
@@ -668,6 +664,10 @@ public IpAddress allocateIp(Account ipOwner, boolean isSystem, Account caller, l
668664
}
669665

670666
if (!allocateFromDedicatedRange) {
667+
// Check that the maximum number of public IPs for the given
668+
// accountId will not be exceeded
669+
_resourceLimitMgr.checkResourceLimit(accountToLock, ResourceType.public_ip);
670+
671671
List<VlanVO> nonDedicatedVlans = _vlanDao.listZoneWideNonDedicatedVlans(zone.getId());
672672
for (VlanVO nonDedicatedVlan : nonDedicatedVlans) {
673673
nonDedicatedVlanDbIds.add(nonDedicatedVlan.getId());
@@ -2964,8 +2964,8 @@ public IPAddressVO markIpAsUnavailable(long addrId) {
29642964
if (ip.getState() != State.Releasing) {
29652965
txn.start();
29662966

2967-
// don't decrement resource count for direct ips
2968-
if (ip.getAssociatedWithNetworkId() != null) {
2967+
// don't decrement resource count for direct and dedicated ips
2968+
if (ip.getAssociatedWithNetworkId() != null && !isIpDedicated(ip)) {
29692969
_resourceLimitMgr.decrementResourceCount(_ipAddressDao.findById(addrId).getAllocatedToAccountId(), ResourceType.public_ip);
29702970
}
29712971

‎server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@
4646
import com.cloud.configuration.dao.ResourceCountDao;
4747
import com.cloud.configuration.dao.ResourceLimitDao;
4848
import com.cloud.dao.EntityManager;
49+
import com.cloud.dc.VlanVO;
50+
import com.cloud.dc.dao.VlanDao;
4951
import com.cloud.domain.Domain;
5052
import com.cloud.domain.DomainVO;
5153
import com.cloud.domain.dao.DomainDao;
5254
import com.cloud.exception.InvalidParameterValueException;
5355
import com.cloud.exception.PermissionDeniedException;
5456
import com.cloud.exception.ResourceAllocationException;
5557
import com.cloud.network.dao.IPAddressDao;
58+
import com.cloud.network.dao.IPAddressVO;
5659
import com.cloud.network.dao.NetworkDao;
5760
import com.cloud.network.vpc.dao.VpcDao;
5861
import com.cloud.projects.Project;
@@ -141,6 +144,8 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
141144
private ServiceOfferingDao _serviceOfferingDao;
142145
@Inject
143146
private VMTemplateHostDao _vmTemplateHostDao;
147+
@Inject
148+
private VlanDao _vlanDao;
144149

145150
protected GenericSearchBuilder<VMTemplateHostVO, SumCount> templateSizeSearch;
146151

@@ -814,7 +819,7 @@ protected long recalculateAccountResourceCount(long accountId, ResourceType type
814819
} else if (type == Resource.ResourceType.snapshot) {
815820
newCount = _snapshotDao.countSnapshotsForAccount(accountId);
816821
} else if (type == Resource.ResourceType.public_ip) {
817-
newCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
822+
newCount = calculatePublicIpForAccount(accountId);
818823
} else if (type == Resource.ResourceType.template) {
819824
newCount = _vmTemplateDao.countTemplatesForAccount(accountId);
820825
} else if (type == Resource.ResourceType.project) {
@@ -906,6 +911,22 @@ public long calculateSecondaryStorageForAccount(long accountId) {
906911
return totalVolumesSize + totalSnapshotsSize + totalTemplatesSize;
907912
}
908913

914+
private long calculatePublicIpForAccount(long accountId) {
915+
Long dedicatedCount = 0L;
916+
Long allocatedCount = 0L;
917+
918+
List<VlanVO> dedicatedVlans = _vlanDao.listDedicatedVlans(accountId);
919+
for (VlanVO dedicatedVlan : dedicatedVlans) {
920+
List<IPAddressVO> ips = _ipAddressDao.listByVlanId(dedicatedVlan.getId());
921+
dedicatedCount += new Long(ips.size());
922+
}
923+
allocatedCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
924+
if (dedicatedCount > allocatedCount)
925+
return dedicatedCount;
926+
else
927+
return allocatedCount;
928+
}
929+
909930
@Override
910931
public long getResourceCount(Account account, ResourceType type) {
911932
return _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type);

0 commit comments

Comments
 (0)