Moti Asayag has uploaded a new change for review.

Change subject: engine: Add NetworkAttachmentsValidator
......................................................................

engine: Add NetworkAttachmentsValidator

The validator is used to verify constraints on the entire
destined network attachments.

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


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

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentsValidator.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentsValidator.java
new file mode 100644
index 0000000..5cc2c57
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkAttachmentsValidator.java
@@ -0,0 +1,90 @@
+package org.ovirt.engine.core.bll.validator;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.collections.bag.HashBag;
+import org.ovirt.engine.core.bll.ValidationResult;
+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.compat.Guid;
+import org.ovirt.engine.core.utils.NetworkUtils;
+
+/**
+ * The {@code NetworkAttachmentsValidator} is performs validation on the 
entire network attachments as a whole and for
+ * cross network attachments configuration. For a specific network attachment 
entity validation use
+ * {@link org.ovirt.engine.core.bll.validator.NetworkAttachmentValidator};
+ */
+public class NetworkAttachmentsValidator {
+
+    private final Collection<NetworkAttachment> attachmentsToConfigure;
+    private final Map<Guid, Network> clusterNetworks;
+
+    public NetworkAttachmentsValidator(Collection<NetworkAttachment> 
attachmentsToConfigure,
+            Map<Guid, Network> clusterNetworks) {
+        this.attachmentsToConfigure = attachmentsToConfigure;
+        this.clusterNetworks = clusterNetworks;
+    }
+
+    public ValidationResult validateNetworkExclusiveOnNics() {
+        Map<String, List<NetworkType>> nicsToNetworks = new HashMap<>();
+        for (NetworkAttachment attachment : attachmentsToConfigure) {
+            String nicName = attachment.getNicName();
+            if (!nicsToNetworks.containsKey(nicName)) {
+                nicsToNetworks.put(nicName, new ArrayList<NetworkType>());
+            }
+
+            Network networkToConfigure = 
clusterNetworks.get(attachment.getNetworkId());
+            
nicsToNetworks.get(nicName).add(determineNetworkType(networkToConfigure));
+        }
+
+        List<String> violatedNics = new ArrayList<>();
+        for (Entry<String, List<NetworkType>> nicToNetworkTypes : 
nicsToNetworks.entrySet()) {
+            if (!validateNetworkExclusiveOnNic(nicToNetworkTypes.getKey(), 
nicToNetworkTypes.getValue())) {
+                violatedNics.add(nicToNetworkTypes.getKey());
+            }
+        }
+
+        if (violatedNics.isEmpty()) {
+            return ValidationResult.VALID;
+        } else {
+            return new 
ValidationResult(VdcBllMessages.NETWORK_INTERFACES_NOT_EXCLUSIVELY_USED_BY_NETWORK,
 violatedNics);
+        }
+    }
+
+    private NetworkType determineNetworkType(Network network) {
+        return NetworkUtils.isVlan(network)
+                ? NetworkType.VLAN
+                : network.isVmNetwork() ? NetworkType.VM : NetworkType.NON_VM;
+    }
+
+    /**
+     * Make sure that if the given interface has a VM network on it then there 
is nothing else on the interface, or if
+     * the given interface is a VLAN network, than there is no VM network on 
the interface.<br>
+     * Other combinations are either legal or illegal but are not a concern of 
this method.
+     */
+    private boolean validateNetworkExclusiveOnNic(String nicName, 
List<NetworkType> networksOnIface) {
+        if (networksOnIface.size() <= 1) {
+            return true;
+        }
+
+        HashBag networkTypes = new HashBag(networksOnIface);
+        if (networkTypes.contains(NetworkType.VM) && networkTypes.size() > 1
+                || networkTypes.contains(NetworkType.NON_VM) && 
networkTypes.getCount(NetworkType.NON_VM) > 1) {
+            return false;
+        }
+
+        return true;
+    }
+
+    private enum NetworkType {
+        VM,
+        NON_VM,
+        VLAN
+    }
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I92771cc35560ff4101231c3c7a40e6becbe3e8f2
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