@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"); }
@Override public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) { final 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 (final 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) { final Account account = this._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 (this._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()) && (this._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) { this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account); } else { this._accountMgr.checkAccess(caller, null, true, account); } ownerType = ResourceOwnerType.Account; ownerId = accountId; } else if (domainId != null) { final Domain domain = this._entityMgr.findById(Domain.class, domainId); this._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"); } final Long parentDomainId = domain.getParent(); if (parentDomainId != null) { final DomainVO parentDomain = this._domainDao.findById(parentDomainId); final 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"); } final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType); if (limit != null) { this._resourceLimitDao.update(limit.getId(), max); return this._resourceLimitDao.findById(limit.getId()); } else { return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType)); } }
ResourceLimitManagerImpl extends ManagerBase implements ResourceLimitService { @Override public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) { final 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 (final 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) { final Account account = this._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 (this._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()) && (this._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) { this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account); } else { this._accountMgr.checkAccess(caller, null, true, account); } ownerType = ResourceOwnerType.Account; ownerId = accountId; } else if (domainId != null) { final Domain domain = this._entityMgr.findById(Domain.class, domainId); this._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"); } final Long parentDomainId = domain.getParent(); if (parentDomainId != null) { final DomainVO parentDomain = this._domainDao.findById(parentDomainId); final 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"); } final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType); if (limit != null) { this._resourceLimitDao.update(limit.getId(), max); return this._resourceLimitDao.findById(limit.getId()); } else { return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType)); } } }
ResourceLimitManagerImpl extends ManagerBase implements ResourceLimitService { @Override public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) { final 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 (final 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) { final Account account = this._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 (this._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()) && (this._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) { this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account); } else { this._accountMgr.checkAccess(caller, null, true, account); } ownerType = ResourceOwnerType.Account; ownerId = accountId; } else if (domainId != null) { final Domain domain = this._entityMgr.findById(Domain.class, domainId); this._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"); } final Long parentDomainId = domain.getParent(); if (parentDomainId != null) { final DomainVO parentDomain = this._domainDao.findById(parentDomainId); final 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"); } final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType); if (limit != null) { this._resourceLimitDao.update(limit.getId(), max); return this._resourceLimitDao.findById(limit.getId()); } else { return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType)); } }  }
ResourceLimitManagerImpl extends ManagerBase implements ResourceLimitService { @Override public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) { final 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 (final 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) { final Account account = this._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 (this._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()) && (this._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) { this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account); } else { this._accountMgr.checkAccess(caller, null, true, account); } ownerType = ResourceOwnerType.Account; ownerId = accountId; } else if (domainId != null) { final Domain domain = this._entityMgr.findById(Domain.class, domainId); this._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"); } final Long parentDomainId = domain.getParent(); if (parentDomainId != null) { final DomainVO parentDomain = this._domainDao.findById(parentDomainId); final 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"); } final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType); if (limit != null) { this._resourceLimitDao.update(limit.getId(), max); return this._resourceLimitDao.findById(limit.getId()); } else { return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType)); } }  @Override boolean configure(final String name, final Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max); @Override List<ResourceCountVO> recalculateResourceCount(final Long accountId, final Long domainId, final Integer typeId); @Override List<ResourceLimitVO> searchForLimits(final Long id, Long accountId, Long domainId, final Integer type, final Long startIndex, final Long pageSizeVal); @Override long findCorrectResourceLimitForAccount(final Account account, final ResourceType type); @Override long findCorrectResourceLimitForAccount(final long accountId, final Long limit, final ResourceType type); @Override long findCorrectResourceLimitForDomain(final Domain domain, final ResourceType type); @Override void incrementResourceCount(final long accountId, final ResourceType type, final Long... delta); @Override void decrementResourceCount(final long accountId, final ResourceType type, final Long... delta); @Override @DB void checkResourceLimit(final Account account, final ResourceType type, final long... count); @Override long getResourceCount(final Account account, final ResourceType type); @Override void checkResourceLimit(final Account account, final ResourceType type, final Boolean displayResource, final long... count); @Override void incrementResourceCount(final long accountId, final ResourceType type, final Boolean displayResource, final Long... delta); @Override void changeResourceCount(final long accountId, final ResourceType type, final Boolean displayResource, final Long... delta); @Override void decrementResourceCount(final long accountId, final ResourceType type, final Boolean displayResource, final Long... delta); long countCpusForAccount(final long accountId); long calculateMemoryForAccount(final long accountId); long calculateSecondaryStorageForAccount(final long accountId); }
ResourceLimitManagerImpl extends ManagerBase implements ResourceLimitService { @Override public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) { final 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 (final 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) { final Account account = this._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 (this._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()) && (this._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) { this._accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account); } else { this._accountMgr.checkAccess(caller, null, true, account); } ownerType = ResourceOwnerType.Account; ownerId = accountId; } else if (domainId != null) { final Domain domain = this._entityMgr.findById(Domain.class, domainId); this._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"); } final Long parentDomainId = domain.getParent(); if (parentDomainId != null) { final DomainVO parentDomain = this._domainDao.findById(parentDomainId); final 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"); } final ResourceLimitVO limit = this._resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType); if (limit != null) { this._resourceLimitDao.update(limit.getId(), max); return this._resourceLimitDao.findById(limit.getId()); } else { return this._resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType)); } }  @Override boolean configure(final String name, final Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max); @Override List<ResourceCountVO> recalculateResourceCount(final Long accountId, final Long domainId, final Integer typeId); @Override List<ResourceLimitVO> searchForLimits(final Long id, Long accountId, Long domainId, final Integer type, final Long startIndex, final Long pageSizeVal); @Override long findCorrectResourceLimitForAccount(final Account account, final ResourceType type); @Override long findCorrectResourceLimitForAccount(final long accountId, final Long limit, final ResourceType type); @Override long findCorrectResourceLimitForDomain(final Domain domain, final ResourceType type); @Override void incrementResourceCount(final long accountId, final ResourceType type, final Long... delta); @Override void decrementResourceCount(final long accountId, final ResourceType type, final Long... delta); @Override @DB void checkResourceLimit(final Account account, final ResourceType type, final long... count); @Override long getResourceCount(final Account account, final ResourceType type); @Override void checkResourceLimit(final Account account, final ResourceType type, final Boolean displayResource, final long... count); @Override void incrementResourceCount(final long accountId, final ResourceType type, final Boolean displayResource, final Long... delta); @Override void changeResourceCount(final long accountId, final ResourceType type, final Boolean displayResource, final Long... delta); @Override void decrementResourceCount(final long accountId, final ResourceType type, final Boolean displayResource, final Long... delta); long countCpusForAccount(final long accountId); long calculateMemoryForAccount(final long accountId); long calculateSecondaryStorageForAccount(final long accountId); static final Logger s_logger; }