Mike Kolesnik has uploaded a new change for review.

Change subject: engine: Extract common code for network used
......................................................................

engine: Extract common code for network used

This validation is the same for hosts, VMs and templates; so extracted
it to a common method that handles the core validation logic, and the
calling methods need just to supply the entities and a replacement for
their verbal representation in the message.

Change-Id: I42fdbca64b805cc5d506b2b63add176062b93124
Signed-off-by: Mike Kolesnik <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/NetworkCommon.java
1 file changed, 12 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/68/10768/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/NetworkCommon.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/NetworkCommon.java
index 7c380ca..1e521a0 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/NetworkCommon.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/NetworkCommon.java
@@ -10,9 +10,7 @@
 import org.ovirt.engine.core.common.FeatureSupported;
 import org.ovirt.engine.core.common.VdcObjectType;
 import org.ovirt.engine.core.common.action.AddNetworkStoragePoolParameters;
-import org.ovirt.engine.core.common.businessentities.VDS;
-import org.ovirt.engine.core.common.businessentities.VM;
-import org.ovirt.engine.core.common.businessentities.VmTemplate;
+import org.ovirt.engine.core.common.businessentities.Nameable;
 import org.ovirt.engine.core.common.businessentities.network.Network;
 import org.ovirt.engine.core.common.businessentities.network.NetworkCluster;
 import org.ovirt.engine.core.compat.Guid;
@@ -114,15 +112,18 @@
                 : ValidationResult.VALID;
     }
 
-    protected ValidationResult networkNotUsedByVms(final Network network) {
-        List<VM> vms = getVmDAO().getAllForNetwork(network.getId());
-        if (vms.isEmpty()) {
+    private ValidationResult networkNotUsed(List<? extends Nameable> entities, 
VdcBllMessages entitiesReplacement) {
+        if (entities.isEmpty()) {
             return ValidationResult.VALID;
         }
 
-        Collection<String> replacements = 
ReplacementUtils.replaceWithNameable("ENTITIES_USING_NETWORK", vms);
-        replacements.add(VdcBllMessages.VAR__TYPE__VMS.name());
+        Collection<String> replacements = 
ReplacementUtils.replaceWithNameable("ENTITIES_USING_NETWORK", entities);
+        replacements.add(entitiesReplacement.name());
         return new 
ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_IN_USE, 
replacements);
+    }
+
+    protected ValidationResult networkNotUsedByVms(final Network network) {
+        return networkNotUsed(getVmDAO().getAllForNetwork(network.getId()), 
VdcBllMessages.VAR__TYPE__VMS);
     }
 
     protected List<NetworkCluster> getClusterAttachments() {
@@ -134,25 +135,11 @@
     }
 
     protected ValidationResult networkNotUsedByHosts(final Network network) {
-        List<VDS> hostsWithNetwork = 
getVdsDAO().getAllForNetwork(network.getId());
-        if (hostsWithNetwork.isEmpty()) {
-            return ValidationResult.VALID;
-        }
-
-        Collection<String> replacements =
-                ReplacementUtils.replaceWithNameable("ENTITIES_USING_NETWORK", 
hostsWithNetwork);
-        replacements.add(VdcBllMessages.VAR__TYPE__HOSTS.name());
-        return new 
ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_IN_USE, 
replacements);
+        return networkNotUsed(getVdsDAO().getAllForNetwork(network.getId()), 
VdcBllMessages.VAR__TYPE__HOSTS);
     }
 
     protected ValidationResult networkNotUsedByTemplates(final Network 
network) {
-        List<VmTemplate> templates = 
getVmTemplateDAO().getAllForNetwork(network.getId());
-        if (templates.isEmpty()) {
-            return ValidationResult.VALID;
-        }
-
-        Collection<String> replacements = 
ReplacementUtils.replaceWithNameable("ENTITIES_USING_NETWORK", templates);
-        replacements.add(VdcBllMessages.VAR__TYPE__VM_TEMPLATES.name());
-        return new 
ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_IN_USE, 
replacements);
+        return 
networkNotUsed(getVmTemplateDAO().getAllForNetwork(network.getId()),
+                VdcBllMessages.VAR__TYPE__VM_TEMPLATES);
     }
 }


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I42fdbca64b805cc5d506b2b63add176062b93124
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Mike Kolesnik <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to