Moti Asayag has uploaded a new change for review. Change subject: engine: Extracts method to find VMs using nets (#824497) ......................................................................
engine: Extracts method to find VMs using nets (#824497) https://bugzilla.redhat.com/824497 The patch extracts a method which finds VMs that uses a given list of networks. The method will be used by SetupNetworks to enforce same validation. Change-Id: I07cfac66a34618772aaf87bafb01b870d4d38322 Signed-off-by: Moti Asayag <[email protected]> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachNetworkFromVdsInterfaceCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java 2 files changed, 36 insertions(+), 14 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/58/7558/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachNetworkFromVdsInterfaceCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachNetworkFromVdsInterfaceCommand.java index 741bd55..5fc97f8 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachNetworkFromVdsInterfaceCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachNetworkFromVdsInterfaceCommand.java @@ -1,18 +1,17 @@ package org.ovirt.engine.core.bll; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.commons.lang.StringUtils; +import org.ovirt.engine.core.bll.network.VmInterfaceManager; import org.ovirt.engine.core.common.AuditLogType; import org.ovirt.engine.core.common.action.AttachNetworkToVdsParameters; import org.ovirt.engine.core.common.businessentities.Network; import org.ovirt.engine.core.common.businessentities.NetworkStatus; import org.ovirt.engine.core.common.businessentities.VDS; import org.ovirt.engine.core.common.businessentities.VDSStatus; -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.common.vdscommands.NetworkVdsmVDSCommandParameters; import org.ovirt.engine.core.common.vdscommands.VDSCommandType; import org.ovirt.engine.core.common.vdscommands.VDSReturnValue; @@ -138,17 +137,9 @@ } } - List<VM> runningVms = getVmDAO().getAllRunningForVds(vds.getId()); - List<String> vmNames = new ArrayList<String>(); - for (VM vm : runningVms) { - List<VmNetworkInterface> vmInterfaces = getVmNetworkInterfaceDAO().getAllForVm(vm.getId()); - for (VmNetworkInterface vmNic : vmInterfaces) { - if (StringUtils.equals(vmNic.getNetworkName(), getParameters().getNetwork().getname())) { - vmNames.add(vm.getvm_name()); - break; - } - } - } + List<String> vmNames = + new VmInterfaceManager().findActiveVmsUsingNetworks(vds.getId(), + Collections.singletonList(getParameters().getNetwork().getName())); if (!vmNames.isEmpty()) { addCanDoActionMessage(VdcBllMessages.NETWORK_CANNOT_DETACH_NETWORK_USED_BY_VMS); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java index fbd6921..82ff2b7 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java @@ -1,5 +1,6 @@ package org.ovirt.engine.core.bll.network; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -7,11 +8,13 @@ import org.ovirt.engine.core.bll.context.CompensationContext; import org.ovirt.engine.core.common.AuditLogType; import org.ovirt.engine.core.common.businessentities.Network; +import org.ovirt.engine.core.common.businessentities.VM; import org.ovirt.engine.core.common.businessentities.VmNetworkInterface; import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.dal.dbbroker.DbFacade; import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector; import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase; +import org.ovirt.engine.core.dao.VmDAO; import org.ovirt.engine.core.dao.VmNetworkInterfaceDAO; import org.ovirt.engine.core.dao.VmNetworkStatisticsDAO; import org.ovirt.engine.core.utils.log.Log; @@ -88,6 +91,30 @@ } /** + * Finds active VMs which uses a network from a given networks list + * + * @param vdsId + * The host id on which VMs are running + * @param networks + * the networks to check if used + * @return A list of VM names which uses the networks + */ + public List<String> findActiveVmsUsingNetworks(Guid vdsId, List<String> networks) { + List<VM> runningVms = getVmDAO().getAllRunningForVds(vdsId); + List<String> vmNames = new ArrayList<String>(); + for (VM vm : runningVms) { + List<VmNetworkInterface> vmInterfaces = getVmNetworkInterfaceDAO().getAllForVm(vm.getId()); + for (VmNetworkInterface vmNic : vmInterfaces) { + if (networks.contains(vmNic.getNetworkName())) { + vmNames.add(vm.getvm_name()); + break; + } + } + } + return vmNames; + } + + /** * Log the given loggable & message to the {@link AuditLogDirector}. * * @param logable @@ -108,4 +135,8 @@ protected VmNetworkInterfaceDAO getVmNetworkInterfaceDAO() { return DbFacade.getInstance().getVmNetworkInterfaceDAO(); } + + protected VmDAO getVmDAO() { + return DbFacade.getInstance().getVmDAO(); + } } -- To view, visit http://gerrit.ovirt.org/7558 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I07cfac66a34618772aaf87bafb01b870d4d38322 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
