use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.
the class QuotaManager method validateAndSetClusterQuota.
private boolean validateAndSetClusterQuota(QuotaConsumptionParametersWrapper parameters, Pair<AuditLogType, AuditLogableBase> auditLogPair) {
boolean result = true;
List<QuotaClusterConsumptionParameter> executed = new ArrayList<>();
for (QuotaConsumptionParameter parameter : parameters.getParameters()) {
QuotaClusterConsumptionParameter clusterConsumptionParameter;
if (parameter.getParameterType() != QuotaConsumptionParameter.ParameterType.CLUSTER) {
continue;
} else {
clusterConsumptionParameter = (QuotaClusterConsumptionParameter) parameter;
}
Quota quota = parameter.getQuota();
QuotaCluster quotaCluster = null;
if (quota.getGlobalQuotaCluster() != null) {
// global cluster quota
quotaCluster = quota.getGlobalQuotaCluster();
} else {
for (QuotaCluster cluster : quota.getQuotaClusters()) {
if (cluster.getClusterId().equals(clusterConsumptionParameter.getClusterId())) {
quotaCluster = cluster;
break;
}
}
}
if (quotaCluster == null) {
parameters.getValidationMessages().add(EngineMessage.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
result = false;
break;
}
long requestedMemory = clusterConsumptionParameter.getQuotaAction() == QuotaConsumptionParameter.QuotaAction.CONSUME ? clusterConsumptionParameter.getRequestedMemory() : -clusterConsumptionParameter.getRequestedMemory();
int requestedCpu = clusterConsumptionParameter.getQuotaAction() == QuotaConsumptionParameter.QuotaAction.CONSUME ? clusterConsumptionParameter.getRequestedCpu() : -clusterConsumptionParameter.getRequestedCpu();
if (checkQuotaClusterLimits(parameters.getAuditLogable().getStoragePool().getQuotaEnforcementType(), quota, quotaCluster, requestedMemory, requestedCpu, parameters.getValidationMessages(), auditLogPair)) {
executed.add(clusterConsumptionParameter);
} else {
result = false;
break;
}
}
// if result is false (one or more parameters did not pass) - roll back the parameters that did pass
if (!result) {
rollBackClusterConsumptionParameters(executed);
}
return result;
}
use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.
the class QuotaManager method generatePerUserUsageReport.
/**
* Return a list of QuotaUsagePerUser representing the status of all the quotas available for a specific user
*
* @param quotaIdsList
* - quotas available for user
* @return - list of QuotaUsagePerUser
*/
public Map<Guid, QuotaUsagePerUser> generatePerUserUsageReport(List<Quota> quotaIdsList) {
Map<Guid, QuotaUsagePerUser> quotaPerUserUsageEntityMap = new HashMap<>();
List<Quota> needToCache = new ArrayList<>();
if (quotaIdsList != null) {
lock.readLock().lock();
try {
for (Quota quotaExternal : quotaIdsList) {
// look for the quota in the cache
Map<Guid, Quota> quotaMap = storagePoolQuotaMap.get(quotaExternal.getStoragePoolId());
Quota quota = null;
if (quotaMap != null) {
quota = quotaMap.get(quotaExternal.getId());
}
// if quota not in cache look for it in DB and add it to cache
if (quota == null) {
needToCache.add(quotaExternal);
} else {
QuotaUsagePerUser usagePerUser = addQuotaEntry(quota);
if (usagePerUser != null) {
quotaPerUserUsageEntityMap.put(quota.getId(), usagePerUser);
}
}
}
} finally {
lock.readLock().unlock();
}
if (!needToCache.isEmpty()) {
lock.writeLock().lock();
try {
for (Quota quotaExternal : needToCache) {
// look for the quota in the cache again (it may have been added by now)
addStoragePoolToCache(quotaExternal.getStoragePoolId());
Quota quota = fetchQuotaFromCache(quotaExternal.getId(), quotaExternal.getStoragePoolId());
QuotaUsagePerUser usagePerUser = addQuotaEntry(quota);
if (usagePerUser != null) {
quotaPerUserUsageEntityMap.put(quota.getId(), usagePerUser);
}
}
} finally {
lock.writeLock().unlock();
}
}
}
return quotaPerUserUsageEntityMap;
}
use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.
the class QuotaManager method checkStoragePoolMatchQuota.
// In case this param is a QuotaStorageConsumptionParameter - check that it has a valid
// storage domain id which is handled by this quota
private boolean checkStoragePoolMatchQuota(QuotaConsumptionParametersWrapper parameters, QuotaStorageConsumptionParameter param) {
Quota quota = param.getQuota();
if (param.getStorageDomainId() == null) {
parameters.getValidationMessages().add(EngineMessage.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
log.error("Quota storage parameters from command '{}' are missing storage domain id", parameters.getAuditLogable().getClass().getName());
return false;
}
boolean storageDomainInQuota = false;
if (quota.getGlobalQuotaStorage() != null) {
storageDomainInQuota = true;
} else {
for (QuotaStorage quotaStorage : quota.getQuotaStorages()) {
if (quotaStorage.getStorageId().equals(param.getStorageDomainId())) {
storageDomainInQuota = true;
break;
}
}
}
if (!storageDomainInQuota) {
parameters.getValidationMessages().add(EngineMessage.ACTION_TYPE_FAILED_NO_QUOTA_SET_FOR_DOMAIN.toString());
log.error("Quota storage parameters from command '{}'. Storage domain does not match quota", parameters.getAuditLogable().getClass().getName());
return false;
}
return true;
}
use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.
the class QuotaManager method fetchQuotaFromCache.
/**
* Get Quota by Id. If in cache - get from cache. else get from Dao and add to cache.
*
* @param quotaId - quota id
* @param storagePoolId - storage pool containing this quota
* @return - found quota. null if not found.
*/
private Quota fetchQuotaFromCache(Guid quotaId, Guid storagePoolId) throws InvalidQuotaParametersException {
Map<Guid, Quota> quotaMap = storagePoolQuotaMap.get(storagePoolId);
Quota quota = quotaMap.get(quotaId);
// if quota was not found in cache - look for it in DB
if (quota == null) {
quota = getQuotaDao().getById(quotaId);
if (quota != null) {
// cache in quota map
if (storagePoolId.equals(quota.getStoragePoolId())) {
quotaMap.put(quotaId, quota);
} else {
throw new InvalidQuotaParametersException(String.format("Quota %s does not match storage pool %s", quotaId.toString(), storagePoolId.toString()));
}
}
}
return quota;
}
use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.
the class UpdateQuotaCommandTest method testExecuteCommand.
@Test
public void testExecuteCommand() {
// Execute the command
command.executeCommand();
Quota parameterQuota = command.getParameters().getQuota();
Guid quotaId = parameterQuota.getId();
for (QuotaStorage quotaStorage : parameterQuota.getQuotaStorages()) {
assertNotNull("Quota Storage should have been assigned an ID", quotaStorage.getQuotaStorageId());
assertEquals("Wrong Qutoa ID on Quota Storage", quotaId, quotaStorage.getQuotaId());
}
for (QuotaCluster quotaCluster : parameterQuota.getQuotaClusters()) {
assertNotNull("Quota Cluster should have been assigned an ID", quotaCluster.getQuotaClusterId());
assertEquals("Wrong Qutoa ID on Quota Cluster", quotaId, quotaCluster.getQuotaId());
}
// Verify the quota was updated in the database
verify(quotaDao).update(parameterQuota);
// Assert the return value
assertTrue("Execution should be successful", command.getReturnValue().getSucceeded());
}
Aggregations