Moti Asayag has uploaded a new change for review.

Change subject: engine: Deny removal of used network (#824497)
......................................................................

engine: Deny removal of used network (#824497)

https://bugzilla.redhat.com/824497

The patch denies removal of a network which is currently used by running
VMs via SetupNetworks command. If the network is used, an appropriate
error message will appear.

Change-Id: Idbe45c13d0e86fd64096dd28d7df4381feeb075c
Signed-off-by: Moti Asayag <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SetupNetworksHelper.java
1 file changed, 41 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/18/7518/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SetupNetworksHelper.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SetupNetworksHelper.java
index 830a6c7..0d4a38c 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SetupNetworksHelper.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/SetupNetworksHelper.java
@@ -6,6 +6,7 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import org.apache.commons.lang.ObjectUtils;
@@ -14,7 +15,9 @@
 import org.ovirt.engine.core.common.businessentities.Entities;
 import org.ovirt.engine.core.common.businessentities.Network;
 import org.ovirt.engine.core.common.businessentities.NetworkBootProtocol;
+import org.ovirt.engine.core.common.businessentities.VM;
 import org.ovirt.engine.core.common.businessentities.VdsNetworkInterface;
+import org.ovirt.engine.core.common.businessentities.VmNetworkInterface;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dal.VdcBllMessages;
 import org.ovirt.engine.core.dal.dbbroker.DbFacade;
@@ -87,11 +90,12 @@
         }
 
         validateBondSlavesCount();
-        extractRemovedNetworks();
+        List<String> violations = extractRemovedNetworks();
         extractRemovedBonds();
         detectSlaveChanges();
+        violations.addAll(translateViolations());
 
-        return translateViolations();
+        return violations;
     }
 
     private void addViolation(VdcBllMessages violation, String 
violatingEntity) {
@@ -385,14 +389,48 @@
     /**
      * Calculate the networks that should be removed - If the network was 
attached to a NIC and is no longer attached to
      * it, then it will be removed.
+     *
+     * @return a list of error messages if a network remove action should be 
blocked
      */
-    private void extractRemovedNetworks() {
+    private List<String> extractRemovedNetworks() {
+        List<String> messages = new ArrayList<String>();
         for (VdsNetworkInterface iface : getExistingIfaces().values()) {
             String net = iface.getNetworkName();
             if (StringUtils.isNotBlank(net) && 
!attachedNetworksNames.contains(net)) {
                 removedNetworks.add(net);
             }
         }
+
+        if (!removedNetworks.isEmpty()) {
+            Map<String, Map<String, VmNetworkInterface>> vmToInterface = 
createRunningVmsIfaceMap();
+            for (String networkName : removedNetworks) {
+                List<String> vmNames = new ArrayList<String>();
+                for (Entry<String, Map<String, VmNetworkInterface>> entry : 
vmToInterface.entrySet()) {
+                    if (entry.getValue().containsKey(networkName)) {
+                        vmNames.add(entry.getKey());
+                    }
+                }
+
+                if (!vmNames.isEmpty()) {
+                    
messages.add(VdcBllMessages.NETWORK_CANNOT_DETACH_NETWORK_USED_BY_VMS.name());
+                    messages.add(String.format("$Network %1$s", networkName));
+                    messages.add(String.format("$VmNames %1$s", 
StringUtils.join(vmNames, ",")));
+                }
+            }
+        }
+        return messages;
+    }
+
+    private Map<String, Map<String, VmNetworkInterface>> 
createRunningVmsIfaceMap() {
+        List<VM> runningVms = 
getDbFacade().getVmDAO().getAllRunningForVds(params.getVdsId());
+        Map<String, Map<String, VmNetworkInterface>> vmToInterface =
+                new HashMap<String, Map<String, VmNetworkInterface>>();
+        for (VM vm : runningVms) {
+            List<VmNetworkInterface> vmInterfaces =
+                    
getDbFacade().getVmNetworkInterfaceDAO().getAllForVm(vm.getId());
+            vmToInterface.put(vm.getvm_name(), 
Entities.interfacesByNetworkName(vmInterfaces));
+        }
+        return vmToInterface;
     }
 
     public List<Network> getNetworks() {


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

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