Maor Lipchuk has uploaded a new change for review.

Change subject: core: Block removing network when it's related to iSCSI bond.
......................................................................

core: Block removing network when it's related to iSCSI bond.

Add validation which should block remove of network when it is related
to the iSCSI bond.

Change-Id: I68bd390175d42ee4e33773e12d6184a97a755eed
Bug-Url: https://bugzilla.redhat.com/1094196
Signed-off-by: Maor Lipchuk <mlipc...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/RemoveNetworkCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkValidator.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkValidatorTest.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDao.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDaoDbFacadeImpl.java
M backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/IscsiBondDaoTest.java
M 
frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
M 
frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
M packaging/dbscripts/iscsi_bonds_sp.sql
11 files changed, 86 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/79/30979/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/RemoveNetworkCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/RemoveNetworkCommand.java
index 1e26af6..c3620f7 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/RemoveNetworkCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/dc/RemoveNetworkCommand.java
@@ -82,6 +82,7 @@
         NetworkValidator validator = new 
NetworkValidator(getNetworkDAO().get(getNetwork().getId()));
         return validate(validator.networkIsSet())
                 && validate(validator.notManagementNetwork())
+                && validate(validator.notIscsiBondNetwork())
                 && validate(validator.networkNotUsedByVms())
                 && validate(validator.networkNotUsedByTemplates());
     }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkValidator.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkValidator.java
index 732670e..bdd3209 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkValidator.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/validator/NetworkValidator.java
@@ -5,6 +5,7 @@
 
 import org.ovirt.engine.core.bll.ValidationResult;
 import org.ovirt.engine.core.common.FeatureSupported;
+import org.ovirt.engine.core.common.businessentities.IscsiBond;
 import org.ovirt.engine.core.common.businessentities.Nameable;
 import org.ovirt.engine.core.common.businessentities.StoragePool;
 import org.ovirt.engine.core.common.businessentities.VM;
@@ -142,6 +143,17 @@
                 : ValidationResult.VALID;
     }
 
+    public ValidationResult notIscsiBondNetwork() {
+        List<IscsiBond> iscsiBonds = 
getDbFacade().getIscsiBondDao().getIscsiBondsByNetworkId(network.getId());
+        if (!iscsiBonds.isEmpty()) {
+            Collection<String> replaceNameables = 
ReplacementUtils.replaceWithNameable("IscsiBonds", iscsiBonds);
+            replaceNameables.add(getNetworkNameReplacement());
+            return new 
ValidationResult(VdcBllMessages.NETWORK_CANNOT_REMOVE_ISCSI_BOND_NETWORK,
+                    replaceNameables);
+        }
+        return ValidationResult.VALID;
+    }
+
     private String getNetworkNameReplacement() {
         return String.format("$NetworkName %s", network.getName());
     }
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkValidatorTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkValidatorTest.java
index be0e2da..df1bf52 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkValidatorTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/validator/NetworkValidatorTest.java
@@ -21,6 +21,7 @@
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.ovirt.engine.core.bll.ValidationResult;
+import org.ovirt.engine.core.common.businessentities.IscsiBond;
 import org.ovirt.engine.core.common.businessentities.StoragePool;
 import org.ovirt.engine.core.common.businessentities.VDS;
 import org.ovirt.engine.core.common.businessentities.VM;
@@ -31,6 +32,7 @@
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.compat.Version;
 import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dao.IscsiBondDao;
 import org.ovirt.engine.core.dao.StoragePoolDAO;
 import org.ovirt.engine.core.dao.VdsDAO;
 import org.ovirt.engine.core.dao.VmDAO;
@@ -261,6 +263,13 @@
         assertThat(validator.networkNameNotUsed(), matcher);
     }
 
+    private void notIscsiBondNetworkTest(Matcher<ValidationResult> matcher, 
List<IscsiBond> iscsiBonds) {
+        IscsiBondDao iscsiBondDao = mock(IscsiBondDao.class);
+        
when(iscsiBondDao.getIscsiBondsByNetworkId(any(Guid.class))).thenReturn(iscsiBonds);
+        when(dbFacade.getIscsiBondDao()).thenReturn(iscsiBondDao);
+        assertThat(validator.notIscsiBondNetwork(), matcher);
+    }
+
     private List<Network> getSingletonNamedNetworkList(String networkName, 
Guid networkId) {
         Network network = mock(Network.class);
         when(network.getName()).thenReturn(networkName);
@@ -268,6 +277,25 @@
         return Collections.singletonList(network);
     }
 
+    private List<IscsiBond> getIscsiBondList() {
+        List<IscsiBond> iscsiBondList = new ArrayList<>();
+        IscsiBond iscsiBond = new IscsiBond();
+        iscsiBond.setId(Guid.newGuid());
+        iscsiBond.setName("IscsiBond name");
+        iscsiBondList.add(iscsiBond);
+        return iscsiBondList;
+    }
+
+    @Test
+    public void noIscsiBondsForNetowrkTest() throws Exception {
+        notIscsiBondNetworkTest(isValid(), Collections.<IscsiBond> 
emptyList());
+    }
+
+    @Test
+    public void existingIscsiBondsForNetowrkTest() throws Exception {
+        
notIscsiBondNetworkTest(failsWith(VdcBllMessages.NETWORK_CANNOT_REMOVE_ISCSI_BOND_NETWORK),
 getIscsiBondList());
+    }
+
     @Test
     public void networkNameNoNetworks() throws Exception {
         networkNameAvailableTest(isValid(), Collections.<Network> emptyList());
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
index 0157906..df80919 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/errors/VdcBllMessages.java
@@ -496,6 +496,7 @@
     NETWORK_INTERFACE_VM_CANNOT_BE_SET(ErrorType.BAD_PARAMETERS),
     NETWORK_INTERFACE_NOT_ATTACH_TO_NETWORK(ErrorType.CONFLICT),
     NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK(ErrorType.CONSTRAINT_VIOLATION),
+    NETWORK_CANNOT_REMOVE_ISCSI_BOND_NETWORK(ErrorType.CONSTRAINT_VIOLATION),
     NETWORK_BOND_NAME_EXISTS(ErrorType.CONFLICT),
     NETWORK_BOND_PARAMETERS_INVALID(ErrorType.BAD_PARAMETERS),
     NETWORK_DEFAULT_UPDATE_NAME_INVALID(ErrorType.BAD_PARAMETERS),
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDao.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDao.java
index 04f95dd..89645f3 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDao.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDao.java
@@ -11,6 +11,8 @@
 
     public List<Guid> getNetworkIdsByIscsiBondId(Guid iscsiBondId);
 
+    public List<IscsiBond> getIscsiBondsByNetworkId(Guid netowrkId);
+
     public void addNetworkToIscsiBond(Guid iscsiBondId, Guid networkId);
 
     public void removeNetworkFromIscsiBond(Guid iscsiBondId, Guid networkId);
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDaoDbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDaoDbFacadeImpl.java
index bb3f925..82c2b52 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDaoDbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/IscsiBondDaoDbFacadeImpl.java
@@ -30,6 +30,13 @@
     }
 
     @Override
+    public List<IscsiBond> getIscsiBondsByNetworkId(Guid netowrkId) {
+        return getCallsHandler().executeReadList("GetIscsiBondsByNetworkId",
+                IscsiBondRowMapper.instance,
+                getCustomMapSqlParameterSource().addValue("network_id", 
netowrkId));
+    }
+
+    @Override
     public void addNetworkToIscsiBond(Guid iscsiBondId, Guid networkId) {
         getCallsHandler().executeModification("AddNetworkToIscsiBond",
                 getCustomMapSqlParameterSource()
diff --git 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
index 64524bb..2127aef 100644
--- 
a/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
+++ 
b/backend/manager/modules/dal/src/main/resources/bundles/AppErrors.properties
@@ -613,6 +613,7 @@
 VM_CANNOT_MOVE_TO_CLUSTER_IN_OTHER_STORAGE_POOL=VM can be moved only to a 
Cluster in the same Data Center.
 VM_CLUSTER_IS_NOT_VALID=Cannot ${action} ${type}. Cluster ID is not valid.
 NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK=The Management Network 
('${NetworkName}') is mandatory and cannot be removed.
+NETWORK_CANNOT_REMOVE_ISCSI_BOND_NETWORK=The Network ('${NetworkName}') could 
not be removed since several iSCSI bonds (${IscsiBonds_COUNTER}) are using this 
network:\n ${IscsiBonds}.\nPlease remove the network first from those iSCSI bon
 NETWORK_OLD_NETWORK_NOT_SPECIFIED=Previous network name is required.
 ACTION_TYPE_FAILED_DETECTED_ACTIVE_VMS=Cannot ${action} ${type}. Active VMs 
were detected.\n\
        -Please ensure all VMs associated with this Storage Domain are stopped 
and in the Down state first.
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/IscsiBondDaoTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/IscsiBondDaoTest.java
index 86820e0..e8e48c1 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/IscsiBondDaoTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/IscsiBondDaoTest.java
@@ -110,6 +110,25 @@
     }
 
     @Test
+    public void testGetIscsiBondIdByNetworkId() {
+        List<IscsiBond> fetchedIscsiBonds = 
dao.getIscsiBondsByNetworkId(FixturesTool.NETWORK_ENGINE);
+        assertEquals(1, fetchedIscsiBonds.size());
+        assertEquals(FixturesTool.ISCSI_BOND_ID, 
fetchedIscsiBonds.get(0).getId());
+    }
+
+    @Test
+    public void testGetEmptyIscsiBondIdByNetworkId() {
+        List<IscsiBond> fetchedIscsiBonds = 
dao.getIscsiBondsByNetworkId(networkId);
+        assertEquals(0, fetchedIscsiBonds.size());
+    }
+
+    @Test
+    public void testGetEmptyIscsiBondIdByNotExistingNetworkId() {
+        List<IscsiBond> fetchedIscsiBonds = 
dao.getIscsiBondsByNetworkId(Guid.Empty);
+        assertEquals(0, fetchedIscsiBonds.size());
+    }
+
+    @Test
     public void testRemoveNetworkFromIscsiBond() {
         List<Guid> networks = dao.getNetworkIdsByIscsiBondId(iscsiBondId);
         networkId = networks.get(0);
diff --git 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
index af0164d..f7a9fe1 100644
--- 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
+++ 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/AppErrors.java
@@ -1687,6 +1687,9 @@
     @DefaultStringValue("The Management Network ('${NetworkName}') is 
mandatory and cannot be removed.")
     String NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK();
 
+    @DefaultStringValue("The Network ('${NetworkName}') could not be removed 
since several iSCSI bonds (${IscsiBonds_COUNTER}) are using this network:\n 
${IscsiBonds}.\nPlease remove the network first from those iSCSI bonds, and try 
again.")
+    String NETWORK_CANNOT_REMOVE_ISCSI_BOND_NETWORK();
+
     @DefaultStringValue("Previous network name is required.")
     String NETWORK_OLD_NETWORK_NOT_SPECIFIED();
 
diff --git 
a/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
 
b/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
index 0e7df58..c2ff05e 100644
--- 
a/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
+++ 
b/frontend/webadmin/modules/webadmin/src/main/resources/org/ovirt/engine/ui/frontend/AppErrors.properties
@@ -617,6 +617,7 @@
 VM_CANNOT_MOVE_TO_CLUSTER_IN_OTHER_STORAGE_POOL=VM can be moved only to a 
Cluster in the same Data Center.
 VM_CLUSTER_IS_NOT_VALID=Cannot ${action} ${type}. Cluster ID is not valid.
 NETWORK_CANNOT_REMOVE_MANAGEMENT_NETWORK=The Management Network 
('${NetworkName}') is mandatory and cannot be removed.
+NETWORK_CANNOT_REMOVE_ISCSI_BOND_NETWORK=The Network ('${NetworkName}') could 
not be removed since several iSCSI bonds (${IscsiBonds_COUNTER}) are using this 
network:\n ${IscsiBonds}.\nPlease remove the network first from those iSCSI 
bonds, and try again.
 NETWORK_OLD_NETWORK_NOT_SPECIFIED=Previous network name is required.
 ACTION_TYPE_FAILED_DETECTED_ACTIVE_VMS=Cannot ${action} ${type}. Active VMs 
were detected.\n\
        -Please ensure all VMs associated with this Storage Domain are stopped 
and in the Down state first.
diff --git a/packaging/dbscripts/iscsi_bonds_sp.sql 
b/packaging/dbscripts/iscsi_bonds_sp.sql
index 5d34bd7..003c556 100644
--- a/packaging/dbscripts/iscsi_bonds_sp.sql
+++ b/packaging/dbscripts/iscsi_bonds_sp.sql
@@ -41,6 +41,17 @@
 LANGUAGE plpgsql;
 
 
+Create or replace FUNCTION GetIscsiBondsByNetworkId(v_network_id UUID) RETURNS 
SETOF iscsi_bonds  STABLE
+   AS $procedure$
+BEGIN
+      RETURN QUERY SELECT iscsi_bonds.*
+      FROM iscsi_bonds_networks_map, iscsi_bonds
+      WHERE iscsi_bonds.id = iscsi_bonds_networks_map.iscsi_bond_id
+      AND network_id = v_network_id;
+END; $procedure$
+LANGUAGE plpgsql;
+
+
 Create or replace FUNCTION InsertIscsiBond(v_id UUID,
   v_name VARCHAR(50),
   v_description VARCHAR(4000),


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I68bd390175d42ee4e33773e12d6184a97a755eed
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.5
Gerrit-Owner: Maor Lipchuk <mlipc...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to