ofri masad has uploaded a new change for review.

Change subject: core: Quota refactor - QuotaManager step 2
......................................................................

core: Quota refactor - QuotaManager step 2

Added validation and completion of the parameters

Added caching of Quota

Change-Id: I15314d6269d1f5f43de17c36a6ae91ba874c8a58
Signed-off-by: Ofri Masad <oma...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaManager.java
1 file changed, 109 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/77/8777/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaManager.java
index 8bd8f85..b088be3 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaManager.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/quota/QuotaManager.java
@@ -31,8 +31,10 @@
     private final static ReentrantReadWriteLock lock = new 
ReentrantReadWriteLock();
     private final static Log log = LogFactory.getLog(QuotaManager.class);
     private final static DecimalFormat percentageFormatter = new 
DecimalFormat("#.##");
+    private final static QuotaManagerAuditLogger AUDIT_LOGGER = 
QuotaManagerAuditLogger.getInstance();
     private final ConcurrentHashMap<Guid, Map<Guid, Quota>> 
storagePoolQuotaMap =
             new ConcurrentHashMap<Guid, Map<Guid, Quota>>();
+    private final ConcurrentHashMap<Guid, Quota> directQuotaMap = new 
ConcurrentHashMap<Guid, Quota>();
 
     public static QuotaManager getInstance() {
         return INSTANCE;
@@ -747,8 +749,114 @@
      *            - Quota consumption parameters
      */
     private boolean validateAndCompleteParameters(QuotaConsumptionParameters 
parameters) {
-        // TODO
+        // if storage_pool is null
+        if (parameters.getMetaData() == null || parameters.getStorage_pool() 
== null){
+            log.debug("Null storage pool was passed to 
'QuotaManager.validateAndSetStorageQuota()'");
+            // if no quota was passed
+            if (parameters.getParameters().isEmpty() || 
parameters.getParameters().get(0).getQuotaGuid() == null) {
+                
parameters.getCanDoActionMessages().add(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
+                return false;
+            }
+            // try to get storage pool from quota
+            storage_pool storage_pool = 
getStoragePoolByQuota(parameters.getParameters().get(0).getQuotaGuid());
+            if (null != storage_pool) {
+                parameters.setStorage_pool(storage_pool);
+            } else {
+                parameters.getCanDoActionMessages()
+                        
.add(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
+                return false;
+            }
+        }
+
+        if 
(QuotaEnforcementTypeEnum.DISABLED.equals(parameters.getStorage_pool().getQuotaEnforcementType()))
 {
+            return true;
+        }
+
+        boolean hardEnforcement =
+                
QuotaEnforcementTypeEnum.HARD_ENFORCEMENT.equals(parameters.getStorage_pool().getQuotaEnforcementType());
+
+        // if one of the main objects of the parameters is null
+        if (null == parameters.getCanDoActionMessages()
+                || null == parameters.getAuditLogable()
+                || null == parameters.getParameters()){
+            // if in hard enforcement - return false
+            if (hardEnforcement) {
+                
parameters.getCanDoActionMessages().add(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
+                return false;
+            } else { // otherwise write to auditLog and return true
+                AUDIT_LOGGER.auditLog(parameters.getAuditLogable(), 
AuditLogType.MISSING_QUOTA_STORAGE_PARAMETERS_PERMISSIVE_MODE);
+                return true;
+            }
+        }
+
+        // for each parameter - check and complete
+        for (QuotaConsumptionParameter param : parameters.getParameters()) {
+            boolean emptyID = param.getQuotaGuid() == null || 
Guid.Empty.equals(param.getQuotaGuid());
+            boolean storageDomainMissing =
+                    param instanceof QuotaStorageConsumptionParameter
+                            && (((QuotaStorageConsumptionParameter) 
param).getStorageDomainId() == null
+                            || 
Guid.Empty.equals(((QuotaStorageConsumptionParameter) 
param).getStorageDomainId()));
+            boolean clusterMissing =
+                    param instanceof QuotaVdsConsumptionParameter
+                            && (((QuotaVdsConsumptionParameter) 
param).getVdsGroupId() == null
+                            || param.getQuotaGuid() == null
+                            || 
Guid.Empty.equals(((QuotaVdsConsumptionParameter) param).getVdsGroupId()));
+
+            // if empty quota id or missing SD or missing cluster
+            if (emptyID || storageDomainMissing || clusterMissing) {
+                // if in hard enforcement - return false
+                if (hardEnforcement) {
+                    
parameters.getCanDoActionMessages().add(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
+                    return false;
+                } else { // otherwise write to auditLog and return true
+                    AUDIT_LOGGER.auditLog(parameters.getAuditLogable(), 
AuditLogType.MISSING_QUOTA_STORAGE_PARAMETERS_PERMISSIVE_MODE);
+                    return true;
+                }
+            }
+            // get quota and add it to the params.
+            Quota quota = fetchQuotaFromDB(param.getQuotaGuid());
+            if (quota == null) {
+                
parameters.getCanDoActionMessages().add(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_IS_NO_LONGER_AVAILABLE_IN_SYSTEM.toString());
+                log.errorFormat("The quota id {0} is not found in backend and 
DB.", param.getQuotaGuid().toString());
+                return false;
+            } else {
+                param.setQuota(quota);
+            }
+        }
         return true;
     }
 
+    /**
+     * Get Quota by Id. If in cache - get from cache. else get from DAO and 
add to cache.
+     * @param quotaId - quota id
+     * @return - found quota. null if not found.
+     */
+    private Quota fetchQuotaFromDB(Guid quotaId) {
+        // if the id is valid
+        if (quotaId != null && quotaId != Guid.Empty) {
+            if (!directQuotaMap.contains(quotaId)) {
+                // cache in direct quota map
+                Quota quota = getQuotaDAO().getById(quotaId);
+                directQuotaMap.put(quotaId, quota);
+                // cache in storage-pool->quota map
+                storagePoolQuotaMap.putIfAbsent(quota.getStoragePoolId(), new 
HashMap<Guid, Quota>());
+                storagePoolQuotaMap.get(quota.getStoragePoolId()).put(quotaId, 
quota);
+            }
+            return directQuotaMap.get(quotaId);
+        }
+        return null;
+    }
+
+    private storage_pool getStoragePoolByQuota(Guid quotaId) {
+        Quota quota = fetchQuotaFromDB(quotaId);
+        if (null != quota) {
+            return getStoragePoolById(quota.getStoragePoolId());
+        }
+        return null;
+    }
+
+    private storage_pool getStoragePoolById(Guid storagePoolId) {
+        return DbFacade.getInstance().getStoragePoolDao().get(storagePoolId);
+    }
+
 }


--
To view, visit http://gerrit.ovirt.org/8777
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I15314d6269d1f5f43de17c36a6ae91ba874c8a58
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: ofri masad <oma...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to