ResourceLimitManagerImpl extends ManagerBase implements ResourceLimitService { @Override public ResourceLimitVO updateResourceLimit(Long accountId, Long domainId, Integer typeId, Long max) { Account caller = CallContext.current().getCallingAccount(); if (max == null) { max = new Long(Resource.RESOURCE_UNLIMITED); } else if (max.longValue() < Resource.RESOURCE_UNLIMITED) { throw new InvalidParameterValueException("Please specify either '-1' for an infinite limit, or a limit that is at least '0'."); } ResourceType resourceType = null; if (typeId != null) { for (ResourceType type : Resource.ResourceType.values()) { if (type.getOrdinal() == typeId.intValue()) { resourceType = type; } } if (resourceType == null) { throw new InvalidParameterValueException("Please specify valid resource type"); } } if ((resourceType == ResourceType.primary_storage || resourceType == ResourceType.secondary_storage) && max >= 0) { max = max * ResourceType.bytesToGiB; } ResourceOwnerType ownerType = null; Long ownerId = null; if (accountId != null) { Account account = _entityMgr.findById(Account.class, accountId); if (account == null) { throw new InvalidParameterValueException("Unable to find account " + accountId); } if (account.getId() == Account.ACCOUNT_ID_SYSTEM) { throw new InvalidParameterValueException("Can't update system account"); } if (_accountMgr.isRootAdmin(account.getId()) && max.shortValue() != Resource.RESOURCE_UNLIMITED) { throw new InvalidParameterValueException("Only " + Resource.RESOURCE_UNLIMITED + " limit is supported for Root Admin accounts"); } if ((caller.getAccountId() == accountId.longValue()) && (_accountMgr.isDomainAdmin(caller.getId()) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) { throw new PermissionDeniedException("Unable to update resource limit for his own account " + accountId + ", permission denied"); } if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) { _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account); } else { _accountMgr.checkAccess(caller, null, true, account); } ownerType = ResourceOwnerType.Account; ownerId = accountId; } else if (domainId != null) { Domain domain = _entityMgr.findById(Domain.class, domainId); _accountMgr.checkAccess(caller, domain); if (Domain.ROOT_DOMAIN == domainId.longValue()) { throw new PermissionDeniedException("Cannot update resource limit for ROOT domain " + domainId + ", permission denied"); } if ((caller.getDomainId() == domainId.longValue()) && caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) { throw new PermissionDeniedException("Unable to update resource limit for domain " + domainId + ", permission denied"); } Long parentDomainId = domain.getParent(); if (parentDomainId != null) { DomainVO parentDomain = _domainDao.findById(parentDomainId); long parentMaximum = findCorrectResourceLimitForDomain(parentDomain, resourceType); if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) { throw new InvalidParameterValueException("Domain " + domain.getName() + "(id: " + parentDomain.getId() + ") has maximum allowed resource limit " + parentMaximum + " for " + resourceType + ", please specify a value less that or equal to " + parentMaximum); } } ownerType = ResourceOwnerType.Domain; ownerId = domainId; } if (ownerId == null) { throw new InvalidParameterValueException("AccountId or domainId have to be specified in order to update resource limit"); } ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType); if (limit != null) { _resourceLimitDao.update(limit.getId(), max); return _resourceLimitDao.findById(limit.getId()); } else { return _resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType)); } }  @Override boolean start(); @Override boolean stop(); @Override boolean configure(final String name, final Map<String, Object> params); @Override void incrementResourceCount(long accountId, ResourceType type, Long... delta); @Override void decrementResourceCount(long accountId, ResourceType type, Long... delta); @Override long findCorrectResourceLimitForAccount(Account account, ResourceType type); @Override long findCorrectResourceLimitForAccount(long accountId, Long limit, ResourceType type); @Override long findCorrectResourceLimitForDomain(Domain domain, ResourceType type); @Override @DB void checkResourceLimit(final Account account, final ResourceType type, long... count); @Override List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domainId, Integer type, Long startIndex, Long pageSizeVal); @Override ResourceLimitVO updateResourceLimit(Long accountId, Long domainId, Integer typeId, Long max); @Override List<ResourceCountVO> recalculateResourceCount(Long accountId, Long domainId, Integer typeId); long countCpusForAccount(long accountId); long calculateMemoryForAccount(long accountId); long calculateSecondaryStorageForAccount(long accountId); @Override long getResourceCount(Account account, ResourceType type); @Override void checkResourceLimit(Account account, ResourceType type, Boolean displayResource, long... count); @Override void incrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta); @Override void decrementResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta); @Override void changeResourceCount(long accountId, ResourceType type, Boolean displayResource, Long... delta); static final Logger s_logger; }
@Test public void testInjected() throws Exception { s_logger.info("Starting test for Resource Limit manager"); updateResourceCount(); updateResourceLimit(); s_logger.info("Resource Limit Manager: TEST PASSED"); }