Moti Asayag has uploaded a new change for review.

Change subject: engine: Add ModifiedNetworkAttachmentValidator
......................................................................

engine: Add ModifiedNetworkAttachmentValidator

The validator is used to validate modified network attachments,
i.e. when a network attachment is updated or removed so the
change done to the subject attachment is valid.

Change-Id: Ie64dff04dbf43b8507b0559d82dc56a24e170e7d
Signed-off-by: Moti Asayag <[email protected]>
---
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java
1 file changed, 75 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/67/34967/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java
new file mode 100644
index 0000000..eaf56e4
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/ModifiedNetworkAttachmentValidator.java
@@ -0,0 +1,75 @@
+package org.ovirt.engine.core.bll.validator;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.ovirt.engine.core.bll.ValidationResult;
+import org.ovirt.engine.core.bll.network.VmInterfaceManager;
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.network.Network;
+import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment;
+import org.ovirt.engine.core.common.errors.VdcBllMessages;
+import org.ovirt.engine.core.utils.NetworkUtils;
+import org.ovirt.engine.core.utils.ReplacementUtils;
+
+public class ModifiedNetworkAttachmentValidator extends 
NetworkAttachmentValidator {
+
+    private NetworkAttachment oldAttachment;
+    private Network oldNetwork;
+
+    public ModifiedNetworkAttachmentValidator(NetworkAttachment attachment, 
VDS host) {
+        super(attachment, host);
+    }
+
+    public ValidationResult networkAttachmentExists() {
+        NetworkAttachment attachmentFromDb = getOldNetworkAttachment();
+        return 
ValidationResult.failWith(VdcBllMessages.NETWORK_ATTACHMENT_NOT_EXISTS).when(attachmentFromDb
 == null);
+    }
+
+    public ValidationResult notRemovingManagementNetwork() {
+        return 
ValidationResult.failWith(VdcBllMessages.NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK,
+                getNetworkNameReplacement())
+                .when(NetworkUtils.isManagementNetwork(getOldNetwork()));
+    }
+
+    public ValidationResult networkNotUsedByVms(String networkName) {
+        List<String> vmNames =
+                new 
VmInterfaceManager().findActiveVmsUsingNetworks(host.getId(),
+                        Collections.singleton(networkName));
+
+        if (vmNames.isEmpty()) {
+            return ValidationResult.VALID;
+        } else {
+            Collection<String> replacements =
+                    ReplacementUtils.replaceWith("ENTITIES_USING_NETWORK", new 
ArrayList<Object>(vmNames));
+            replacements.add(VdcBllMessages.VAR__ENTITIES__VMS.name());
+            return new 
ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_IN_USE, 
replacements);
+        }
+    }
+
+    public ValidationResult networkNotUsedByVms() {
+        return networkNotUsedByVms(getNetwork().getName());
+    }
+
+    private String getNetworkNameReplacement() {
+        return String.format("$NetworkName %s", getOldNetwork().getName());
+    }
+
+    private NetworkAttachment getOldNetworkAttachment() {
+        if (oldAttachment == null) {
+            oldAttachment = 
getDbFacade().getNetworkAttachmentDao().get(attachment.getId());
+        }
+
+        return oldAttachment;
+    }
+
+    private Network getOldNetwork() {
+        if (oldNetwork == null) {
+            oldNetwork = 
getDbFacade().getNetworkDao().get(getOldNetworkAttachment().getNetworkId());
+        }
+
+        return oldNetwork;
+    }
+}


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

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

Reply via email to