Moti Asayag has uploaded a new change for review. Change subject: engine: Extract vnic profile helper methods to VnicProfileHelper ......................................................................
engine: Extract vnic profile helper methods to VnicProfileHelper The patch extracts the methods used to resolve the vnic profile for a given vnic into a new helper so it can be properly reused. Signed-off-by: Moti Asayag <masa...@redhat.com> Change-Id: Ic03ae924a6fb31a948723a57b331597ec7d76977 Signed-off-by: Moti Asayag <masa...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/MoveOrCopyTemplateCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/template/VmTemplateInterfaceCommandBase.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/AbstractVmInterfaceCommand.java A backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/VnicProfileHelper.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java M backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties 9 files changed, 219 insertions(+), 263 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/97/18797/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java index 8700304..3ccfec3 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java @@ -17,6 +17,7 @@ import org.ovirt.engine.core.bll.memory.MemoryUtils; import org.ovirt.engine.core.bll.network.MacPoolManager; import org.ovirt.engine.core.bll.network.VmInterfaceManager; +import org.ovirt.engine.core.bll.network.vm.VnicProfileHelper; import org.ovirt.engine.core.bll.quota.QuotaConsumptionParameter; import org.ovirt.engine.core.bll.quota.QuotaStorageConsumptionParameter; import org.ovirt.engine.core.bll.quota.QuotaStorageDependent; @@ -60,10 +61,8 @@ import org.ovirt.engine.core.common.businessentities.VmTemplateStatus; import org.ovirt.engine.core.common.businessentities.VolumeFormat; import org.ovirt.engine.core.common.businessentities.VolumeType; -import org.ovirt.engine.core.common.businessentities.network.Network; import org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface; import org.ovirt.engine.core.common.businessentities.network.VmNic; -import org.ovirt.engine.core.common.businessentities.network.VnicProfileView; import org.ovirt.engine.core.common.errors.VdcBLLException; import org.ovirt.engine.core.common.errors.VdcBllMessages; import org.ovirt.engine.core.common.locks.LockingGroup; @@ -1031,38 +1030,21 @@ protected void addVmInterfaces() { VmInterfaceManager vmInterfaceManager = new VmInterfaceManager(); - List<String> invalidNetworkNames = new ArrayList<>(); - List<String> invalidIfaceNames = new ArrayList<>(); - Map<String, Network> networksInClusterByName = - Entities.entitiesByName(getNetworkDAO().getAllForCluster(getVm().getVdsGroupId())); - List<VnicProfileView> vnicProfilesInDc = - getDbFacade().getVnicProfileViewDao().getAllForDataCenter(getStoragePoolId()); + + VnicProfileHelper vnicProfileHelper = + new VnicProfileHelper(getVm().getVdsGroupId(), + getStoragePoolId(), + getVdsGroup().getcompatibility_version()); for (VmNetworkInterface iface : getVm().getInterfaces()) { initInterface(iface); - if (!vmInterfaceManager.updateNicWithVnicProfile(iface, - getVdsGroup().getcompatibility_version(), - networksInClusterByName, - vnicProfilesInDc, - getCurrentUser().getUserId())) { - markNicHasNoProfile(invalidNetworkNames, invalidIfaceNames, iface); - } - + vnicProfileHelper.updateNicWithVnicProfileForUser(iface, getCurrentUser().getUserId()); vmInterfaceManager.add(iface, getCompensationContext(), getParameters().isImportAsNewEntity(), getVdsGroup().getcompatibility_version()); macsAdded.add(iface.getMacAddress()); } - auditInvalidInterfaces(invalidNetworkNames, invalidIfaceNames); - } - - - private void markNicHasNoProfile(List<String> invalidNetworkNames, - List<String> invalidIfaceNames, - VmNetworkInterface iface) { - invalidNetworkNames.add(iface.getNetworkName()); - invalidIfaceNames.add(iface.getName()); - iface.setVnicProfileId(null); + vnicProfileHelper.auditInvalidInterfaces(getVmName(), getAuditLogTypeForInvalidInterfaces()); } private void initInterface(VmNic iface) { diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java index c43c3c7..1c3f2b4 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmTemplateCommand.java @@ -7,7 +7,7 @@ import java.util.Map; import org.ovirt.engine.core.bll.job.ExecutionHandler; -import org.ovirt.engine.core.bll.network.VmInterfaceManager; +import org.ovirt.engine.core.bll.network.vm.VnicProfileHelper; import org.ovirt.engine.core.bll.quota.QuotaConsumptionParameter; import org.ovirt.engine.core.bll.quota.QuotaStorageConsumptionParameter; import org.ovirt.engine.core.bll.quota.QuotaStorageDependent; @@ -35,11 +35,9 @@ import org.ovirt.engine.core.common.businessentities.VolumeFormat; import org.ovirt.engine.core.common.businessentities.VolumeType; import org.ovirt.engine.core.common.businessentities.image_storage_domain_map; -import org.ovirt.engine.core.common.businessentities.network.Network; import org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface; import org.ovirt.engine.core.common.businessentities.network.VmNetworkStatistics; import org.ovirt.engine.core.common.businessentities.network.VmNic; -import org.ovirt.engine.core.common.businessentities.network.VnicProfileView; import org.ovirt.engine.core.common.errors.VdcBLLException; import org.ovirt.engine.core.common.errors.VdcBllErrors; import org.ovirt.engine.core.common.errors.VdcBllMessages; @@ -373,16 +371,12 @@ } protected void addVmInterfaces() { - VmInterfaceManager vmInterfaceManager = new VmInterfaceManager(); - List<VmNetworkInterface> interfaces = getVmTemplate().getInterfaces(); - List<String> invalidNetworkNames = new ArrayList<String>(); - List<String> invalidIfaceNames = new ArrayList<String>(); - Map<String, Network> networksInVdsByName = - Entities.entitiesByName(getNetworkDAO().getAllForCluster(getVmTemplate().getVdsGroupId())); - List<VnicProfileView> vnicProfilesInDc = - getDbFacade().getVnicProfileViewDao().getAllForDataCenter(getStoragePoolId()); + VnicProfileHelper vnicProfileHelper = + new VnicProfileHelper(getVmTemplate().getVdsGroupId(), + getStoragePoolId(), + getVdsGroup().getcompatibility_version()); - for (VmNetworkInterface iface : interfaces) { + for (VmNetworkInterface iface : getVmTemplate().getInterfaces()) { if (iface.getId() == null) { iface.setId(Guid.newGuid()); } @@ -396,15 +390,7 @@ nic.setSpeed(iface.getSpeed()); nic.setType(iface.getType()); - if (!vmInterfaceManager.updateNicWithVnicProfile(iface, - getVdsGroup().getcompatibility_version(), - networksInVdsByName, - vnicProfilesInDc, - getCurrentUser().getUserId())) { - invalidNetworkNames.add(iface.getNetworkName()); - invalidIfaceNames.add(iface.getName()); - } - + vnicProfileHelper.updateNicWithVnicProfileForUser(iface, getCurrentUser().getUserId()); nic.setVnicProfileId(iface.getVnicProfileId()); getVmNicDao().save(nic); getCompensationContext().snapshotNewEntity(nic); @@ -417,7 +403,7 @@ getCompensationContext().snapshotNewEntity(iStat); } - auditInvalidInterfaces(invalidNetworkNames, invalidIfaceNames); + vnicProfileHelper.auditInvalidInterfaces(getVmTemplateName(), getAuditLogTypeForInvalidInterfaces()); } @Override diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/MoveOrCopyTemplateCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/MoveOrCopyTemplateCommand.java index 163fddc..8eab3a1 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/MoveOrCopyTemplateCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/MoveOrCopyTemplateCommand.java @@ -40,8 +40,6 @@ import org.ovirt.engine.core.common.vdscommands.VDSReturnValue; 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.utils.transaction.TransactionMethod; import org.ovirt.engine.core.utils.transaction.TransactionSupport; @@ -365,22 +363,6 @@ return false; } return true; - } - - /** - * Log to audit-log the list of VmInterfaces that are attached to invalid networks. A network is invalid if it is a - * Non-VM Network, or if the Network is not in the Cluster - * - * @param invalidIfaceNames - * @param invalidNetworkNames - */ - protected void auditInvalidInterfaces(List<String> invalidNetworkNames, List<String> invalidIfaceNames) { - if (invalidNetworkNames.size() > 0) { - AuditLogableBase logable = new AuditLogableBase(); - logable.addCustomValue("Networks", StringUtils.join(invalidNetworkNames, ',')); - logable.addCustomValue("Interfaces", StringUtils.join(invalidIfaceNames, ',')); - AuditLogDirector.log(logable, getAuditLogTypeForInvalidInterfaces()); - } } /** 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 cdd3d05..38783ec 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 @@ -2,37 +2,24 @@ import java.util.ArrayList; import java.util.List; -import java.util.Map; -import org.apache.commons.lang.ObjectUtils; -import org.ovirt.engine.core.bll.ValidationResult; import org.ovirt.engine.core.bll.context.CompensationContext; import org.ovirt.engine.core.common.AuditLogType; import org.ovirt.engine.core.common.FeatureSupported; -import org.ovirt.engine.core.common.VdcObjectType; -import org.ovirt.engine.core.common.businessentities.ActionGroup; import org.ovirt.engine.core.common.businessentities.VM; -import org.ovirt.engine.core.common.businessentities.VmBase; -import org.ovirt.engine.core.common.businessentities.network.Network; import org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface; import org.ovirt.engine.core.common.businessentities.network.VmNic; -import org.ovirt.engine.core.common.businessentities.network.VnicProfile; -import org.ovirt.engine.core.common.businessentities.network.VnicProfileView; import org.ovirt.engine.core.common.errors.VdcBLLException; import org.ovirt.engine.core.common.errors.VdcBllErrors; -import org.ovirt.engine.core.common.errors.VdcBllMessages; 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.dal.dbbroker.auditloghandling.AuditLogDirector; import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase; -import org.ovirt.engine.core.dao.PermissionDAO; import org.ovirt.engine.core.dao.VmDAO; -import org.ovirt.engine.core.dao.network.NetworkDao; import org.ovirt.engine.core.dao.network.VmNetworkInterfaceDao; import org.ovirt.engine.core.dao.network.VmNetworkStatisticsDao; import org.ovirt.engine.core.dao.network.VmNicDao; -import org.ovirt.engine.core.dao.network.VnicProfileDao; import org.ovirt.engine.core.utils.log.Log; import org.ovirt.engine.core.utils.log.LogFactory; import org.ovirt.engine.core.utils.transaction.TransactionMethod; @@ -164,121 +151,6 @@ } /** - * Updates the vnic profile id of a given {@link VmNic} by a network name and vnic profile name. - * - * @param iface - * The vm network interface to be updated - * @param compatibilityVersion - * The compatibility version of the cluster in which the VM exists - * @param networksInClusterByName - * The networks which are assigned to the cluster - * @param vnicProfilesInDc - * The vnic profiles for the data-center in which the VM exists - * @param userId - * The id of the user which performs the action - * @return {@code true} if the vnic profile id is updated, else {@code false} - */ - public boolean updateNicWithVnicProfile(VmNetworkInterface iface, - Version compatibilityVersion, - Map<String, Network> networksInClusterByName, - List<VnicProfileView> vnicProfilesInDc, - Guid userId) { - - if (iface.getNetworkName() == null) { - if (FeatureSupported.networkLinking(compatibilityVersion)) { - iface.setVnicProfileId(null); - return true; - } else { - return false; - } - } - - Network network = networksInClusterByName.get(iface.getNetworkName()); - if (network == null || !network.isVmNetwork()) { - return false; - } - - VnicProfile vnicProfile = getVnicProfileForNetwork(vnicProfilesInDc, network, iface.getVnicProfileName()); - if (vnicProfile == null) { - vnicProfile = findVnicProfileForUser(userId, network); - if (vnicProfile == null) { - return false; - } - } - - iface.setVnicProfileId(vnicProfile.getId()); - return true; - } - - public ValidationResult updateNicForBackwardCompatibility(VmNetworkInterface nic, VmBase vm, Guid userId) { - if (nic.getVnicProfileId() != null) { - return ValidationResult.VALID; - } - - if (nic.getNetworkName() == null) { - if (nic.isPortMirroring()) { - return new ValidationResult(VdcBllMessages.PORT_MIRRORING_REQUIRES_NETWORK); - } else { - return ValidationResult.VALID; - } - } - - Network network = getNetworkDao().getByNameAndCluster(nic.getNetworkName(), vm.getVdsGroupId()); - - if (network == null) { - return new ValidationResult(VdcBllMessages.NETWORK_NOT_EXISTS_IN_CLUSTER); - } - - List<VnicProfile> vnicProfiles = getVnicProfileDao().getAllForNetwork(network.getId()); - for (VnicProfile profile : vnicProfiles) { - if (isVnicProfilePermitted(userId, profile, nic.isPortMirroring())) { - nic.setVnicProfileId(profile.getId()); - return ValidationResult.VALID; - } - } - - return new ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_FIND_VNIC_PROFILE_FOR_NETWORK); - } - - private VnicProfile findVnicProfileForUser(Guid userId, Network network) { - List<VnicProfile> networkProfiles = getVnicProfileDao().getAllForNetwork(network.getId()); - - for (VnicProfile profile : networkProfiles) { - if (isVnicProfilePermitted(userId, profile, false)) { - return profile; - } - } - - return null; - } - - private VnicProfile getVnicProfileForNetwork(List<VnicProfileView> vnicProfiles, - Network network, - String vnicProfileName) { - - if (vnicProfileName == null) { - return null; - } - - for (VnicProfileView vnicProfile : vnicProfiles) { - if (ObjectUtils.equals(vnicProfile.getNetworkId(), network.getId()) - && vnicProfileName.equals(vnicProfile.getName())) { - return vnicProfile; - } - } - - return null; - } - - private boolean isVnicProfilePermitted(Guid userId, VnicProfile profile, boolean portMirroringRequired) { - return portMirroringRequired == profile.isPortMirroring() - && getPermissionDAO().getEntityPermissions(userId, - ActionGroup.CONFIGURE_VM_NETWORK, - profile.getId(), - VdcObjectType.VnicProfile) != null; - } - - /** * Log the given loggable & message to the {@link AuditLogDirector}. * * @param logable @@ -306,18 +178,6 @@ protected VmDAO getVmDAO() { return DbFacade.getInstance().getVmDao(); - } - - private VnicProfileDao getVnicProfileDao() { - return DbFacade.getInstance().getVnicProfileDao(); - } - - private PermissionDAO getPermissionDAO() { - return DbFacade.getInstance().getPermissionDao(); - } - - private NetworkDao getNetworkDao() { - return DbFacade.getInstance().getNetworkDao(); } private AuditLogableBase createAuditLog(final VmNic iface) { diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/template/VmTemplateInterfaceCommandBase.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/template/VmTemplateInterfaceCommandBase.java index a97c612..7639b29 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/template/VmTemplateInterfaceCommandBase.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/template/VmTemplateInterfaceCommandBase.java @@ -5,7 +5,7 @@ import org.ovirt.engine.core.bll.ValidationResult; import org.ovirt.engine.core.bll.VmHandler; import org.ovirt.engine.core.bll.VmTemplateCommand; -import org.ovirt.engine.core.bll.network.VmInterfaceManager; +import org.ovirt.engine.core.bll.network.vm.VnicProfileHelper; import org.ovirt.engine.core.common.action.AddVmTemplateInterfaceParameters; import org.ovirt.engine.core.common.businessentities.network.VmInterfaceType; import org.ovirt.engine.core.common.businessentities.network.VmNic; @@ -48,8 +48,8 @@ } protected boolean updateVnicForBackwardCompatibility() { - VmInterfaceManager nicManager = new VmInterfaceManager(); - if (!validate(nicManager.updateNicForBackwardCompatibility(getParameters().getInterface(), + VnicProfileHelper vnicProfileHelper = new VnicProfileHelper(); + if (!validate(vnicProfileHelper.updateNicForBackwardCompatibility(getParameters().getInterface(), getVmTemplate(), getCurrentUser().getUserId()))) { return false; diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/AbstractVmInterfaceCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/AbstractVmInterfaceCommand.java index bffe9f8..11050a3 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/AbstractVmInterfaceCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/AbstractVmInterfaceCommand.java @@ -7,7 +7,6 @@ import org.ovirt.engine.core.bll.VmCommand; import org.ovirt.engine.core.bll.VmHandler; import org.ovirt.engine.core.bll.network.MacPoolManager; -import org.ovirt.engine.core.bll.network.VmInterfaceManager; import org.ovirt.engine.core.common.action.ActivateDeactivateVmNicParameters; import org.ovirt.engine.core.common.action.AddVmInterfaceParameters; import org.ovirt.engine.core.common.action.PlugAction; @@ -102,8 +101,8 @@ } protected boolean updateVnicForBackwardCompatibility() { - VmInterfaceManager nicManager = new VmInterfaceManager(); - if (!validate(nicManager.updateNicForBackwardCompatibility(getParameters().getInterface(), + VnicProfileHelper vnicProfileHelper = new VnicProfileHelper(); + if (!validate(vnicProfileHelper.updateNicForBackwardCompatibility(getParameters().getInterface(), getVm().getStaticData(), getCurrentUser().getUserId()))) { return false; diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/VnicProfileHelper.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/VnicProfileHelper.java new file mode 100644 index 0000000..23199c4 --- /dev/null +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/vm/VnicProfileHelper.java @@ -0,0 +1,190 @@ +package org.ovirt.engine.core.bll.network.vm; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringUtils; +import org.ovirt.engine.core.bll.ValidationResult; +import org.ovirt.engine.core.common.AuditLogType; +import org.ovirt.engine.core.common.FeatureSupported; +import org.ovirt.engine.core.common.VdcObjectType; +import org.ovirt.engine.core.common.businessentities.ActionGroup; +import org.ovirt.engine.core.common.businessentities.Entities; +import org.ovirt.engine.core.common.businessentities.VmBase; +import org.ovirt.engine.core.common.businessentities.network.Network; +import org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface; +import org.ovirt.engine.core.common.businessentities.network.VnicProfile; +import org.ovirt.engine.core.common.businessentities.network.VnicProfileView; +import org.ovirt.engine.core.common.errors.VdcBllMessages; +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.dal.dbbroker.auditloghandling.AuditLogDirector; +import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase; +import org.ovirt.engine.core.dao.PermissionDAO; +import org.ovirt.engine.core.dao.network.NetworkDao; +import org.ovirt.engine.core.dao.network.VnicProfileDao; +import org.ovirt.engine.core.dao.network.VnicProfileViewDao; + +public class VnicProfileHelper { + private List<String> invalidNetworkNames; + private List<String> invalidIfaceNames; + private Map<String, Network> networksInClusterByName; + private List<VnicProfileView> vnicProfilesInDc; + private Version compatibilityVersion; + + public VnicProfileHelper() { + } + + public VnicProfileHelper(Guid clusterId, Guid dataCenterId, Version compatibilityVersion) { + invalidNetworkNames = new ArrayList<>(); + invalidIfaceNames = new ArrayList<>(); + networksInClusterByName = Entities.entitiesByName(getNetworkDao().getAllForCluster(clusterId)); + vnicProfilesInDc = getVnicProfileViewDao().getAllForDataCenter(dataCenterId); + this.compatibilityVersion = compatibilityVersion; + } + + public void updateNicWithVnicProfileForUser(VmNetworkInterface vmInterface, Guid userId) { + if (!updateNicWithVnicProfile(vmInterface, userId)) { + markNicHasNoProfile(vmInterface); + } + } + + /** + * Updates the vnic profile id of a given {@code VmNic} by a network name and vnic profile name. + * + * @param iface + * The vm network interface to be updated + * @param userId + * The id of the user which performs the action + * @return {@code true} if the vnic profile id is updated, else {@code false} + */ + private boolean updateNicWithVnicProfile(VmNetworkInterface iface, Guid userId) { + + if (iface.getNetworkName() == null) { + if (FeatureSupported.networkLinking(compatibilityVersion)) { + iface.setVnicProfileId(null); + return true; + } else { + return false; + } + } + + Network network = networksInClusterByName.get(iface.getNetworkName()); + if (network == null || !network.isVmNetwork()) { + return false; + } + + VnicProfile vnicProfile = getVnicProfileForNetwork(vnicProfilesInDc, network, iface.getVnicProfileName()); + if (vnicProfile == null) { + vnicProfile = findVnicProfileForUser(userId, network); + if (vnicProfile == null) { + return false; + } + } + + iface.setVnicProfileId(vnicProfile.getId()); + return true; + } + + private VnicProfile findVnicProfileForUser(Guid userId, Network network) { + List<VnicProfile> networkProfiles = getVnicProfileDao().getAllForNetwork(network.getId()); + + for (VnicProfile profile : networkProfiles) { + if (isVnicProfilePermitted(userId, profile, false)) { + return profile; + } + } + + return null; + } + + private VnicProfile getVnicProfileForNetwork(List<VnicProfileView> vnicProfiles, + Network network, + String vnicProfileName) { + + if (vnicProfileName == null) { + return null; + } + + for (VnicProfileView vnicProfile : vnicProfiles) { + if (ObjectUtils.equals(vnicProfile.getNetworkId(), network.getId()) + && vnicProfileName.equals(vnicProfile.getName())) { + return vnicProfile; + } + } + + return null; + } + + private boolean isVnicProfilePermitted(Guid userId, VnicProfile profile, boolean portMirroringRequired) { + return portMirroringRequired == profile.isPortMirroring() + && getPermissionDAO().getEntityPermissions(userId, + ActionGroup.CONFIGURE_VM_NETWORK, + profile.getId(), + VdcObjectType.VnicProfile) != null; + } + + private void markNicHasNoProfile(VmNetworkInterface iface) { + invalidNetworkNames.add(iface.getNetworkName()); + invalidIfaceNames.add(iface.getName()); + iface.setVnicProfileId(null); + } + + public void auditInvalidInterfaces(String entityName, AuditLogType logType) { + if (!invalidNetworkNames.isEmpty()) { + AuditLogableBase logable = new AuditLogableBase(); + logable.addCustomValue("EntityName", entityName); + logable.addCustomValue("Networks", StringUtils.join(invalidNetworkNames, ',')); + logable.addCustomValue("Interfaces", StringUtils.join(invalidIfaceNames, ',')); + AuditLogDirector.log(logable, logType); + } + } + + public ValidationResult updateNicForBackwardCompatibility(VmNetworkInterface nic, VmBase vm, Guid userId) { + if (nic.getVnicProfileId() != null) { + return ValidationResult.VALID; + } + + if (nic.getNetworkName() == null) { + if (nic.isPortMirroring()) { + return new ValidationResult(VdcBllMessages.PORT_MIRRORING_REQUIRES_NETWORK); + } else { + return ValidationResult.VALID; + } + } + + Network network = getNetworkDao().getByNameAndCluster(nic.getNetworkName(), vm.getVdsGroupId()); + if (network == null) { + return new ValidationResult(VdcBllMessages.NETWORK_NOT_EXISTS_IN_CLUSTER); + } + + List<VnicProfile> vnicProfiles = getVnicProfileDao().getAllForNetwork(network.getId()); + for (VnicProfile profile : vnicProfiles) { + if (isVnicProfilePermitted(userId, profile, nic.isPortMirroring())) { + nic.setVnicProfileId(profile.getId()); + return ValidationResult.VALID; + } + } + + return new ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_FIND_VNIC_PROFILE_FOR_NETWORK); + } + + private NetworkDao getNetworkDao() { + return DbFacade.getInstance().getNetworkDao(); + } + + private VnicProfileViewDao getVnicProfileViewDao() { + return DbFacade.getInstance().getVnicProfileViewDao(); + } + + private VnicProfileDao getVnicProfileDao() { + return DbFacade.getInstance().getVnicProfileDao(); + } + + private PermissionDAO getPermissionDAO() { + return DbFacade.getInstance().getPermissionDao(); + } +} diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java index 49226a8..040bb1c 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; @@ -11,12 +10,12 @@ import org.ovirt.engine.core.bll.context.CompensationContext; import org.ovirt.engine.core.bll.memory.MemoryUtils; import org.ovirt.engine.core.bll.network.VmInterfaceManager; +import org.ovirt.engine.core.bll.network.vm.VnicProfileHelper; import org.ovirt.engine.core.bll.utils.ClusterUtils; import org.ovirt.engine.core.bll.utils.VmDeviceUtils; import org.ovirt.engine.core.common.AuditLogType; import org.ovirt.engine.core.common.businessentities.Disk; import org.ovirt.engine.core.common.businessentities.DiskImage; -import org.ovirt.engine.core.common.businessentities.Entities; import org.ovirt.engine.core.common.businessentities.ImageStatus; import org.ovirt.engine.core.common.businessentities.Snapshot; import org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotStatus; @@ -28,15 +27,11 @@ import org.ovirt.engine.core.common.businessentities.VmStatic; import org.ovirt.engine.core.common.businessentities.VmTemplate; import org.ovirt.engine.core.common.businessentities.image_storage_domain_map; -import org.ovirt.engine.core.common.businessentities.network.Network; import org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface; -import org.ovirt.engine.core.common.businessentities.network.VnicProfileView; import org.ovirt.engine.core.common.utils.VmDeviceType; 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.dal.dbbroker.auditloghandling.AuditLogDirector; -import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase; import org.ovirt.engine.core.dao.BaseDiskDao; import org.ovirt.engine.core.dao.DiskDao; import org.ovirt.engine.core.dao.DiskImageDAO; @@ -47,9 +42,7 @@ import org.ovirt.engine.core.dao.VmDynamicDAO; import org.ovirt.engine.core.dao.VmStaticDAO; import org.ovirt.engine.core.dao.VmTemplateDAO; -import org.ovirt.engine.core.dao.network.NetworkDao; import org.ovirt.engine.core.dao.network.VmNetworkInterfaceDao; -import org.ovirt.engine.core.dao.network.VnicProfileViewDao; import org.ovirt.engine.core.utils.log.Log; import org.ovirt.engine.core.utils.log.LogFactory; import org.ovirt.engine.core.utils.ovf.OvfManager; @@ -416,11 +409,8 @@ */ protected void synchronizeNics(VM vm, CompensationContext compensationContext, Guid userId) { VmInterfaceManager vmInterfaceManager = new VmInterfaceManager(); - List<String> invalidNetworkNames = new ArrayList<>(); - List<String> invalidIfaceNames = new ArrayList<>(); - Map<String, Network> networksInClusterByName = - Entities.entitiesByName(getNetworkDao().getAllForCluster(vm.getVdsGroupId())); - List<VnicProfileView> vnicProfilesInDc = getVnicProfileViewDao().getAllForDataCenter(vm.getStoragePoolId()); + VnicProfileHelper vnicProfileHelper = + new VnicProfileHelper(vm.getVdsGroupId(), vm.getStoragePoolId(), vm.getVdsGroupCompatibilityVersion()); vmInterfaceManager.removeAll(vm.getId()); for (VmNetworkInterface vmInterface : vm.getInterfaces()) { @@ -430,36 +420,11 @@ vmInterface.setId(Guid.newGuid()); } - if (!vmInterfaceManager.updateNicWithVnicProfile(vmInterface, - vm.getVdsGroupCompatibilityVersion(), - networksInClusterByName, - vnicProfilesInDc, - userId)) { - markNicHasNoProfile(invalidNetworkNames, invalidIfaceNames, vmInterface); - } - + vnicProfileHelper.updateNicWithVnicProfileForUser(vmInterface, userId); vmInterfaceManager.add(vmInterface, compensationContext, false, vm.getVdsGroupCompatibilityVersion()); } - auditInvalidInterfaces(invalidNetworkNames, invalidIfaceNames, vm.getName()); - } - - private void markNicHasNoProfile(List<String> invalidNetworkNames, - List<String> invalidIfaceNames, - VmNetworkInterface iface) { - invalidNetworkNames.add(iface.getNetworkName()); - invalidIfaceNames.add(iface.getName()); - iface.setVnicProfileId(null); - } - - private void auditInvalidInterfaces(List<String> invalidNetworkNames, List<String> invalidIfaceNames, String vmName) { - if (invalidNetworkNames.size() > 0) { - AuditLogableBase logable = new AuditLogableBase(); - logable.addCustomValue("VmName", vmName); - logable.addCustomValue("Networks", StringUtils.join(invalidNetworkNames, ',')); - logable.addCustomValue("Interfaces", StringUtils.join(invalidIfaceNames, ',')); - AuditLogDirector.log(logable, AuditLogType.IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES); - } + vnicProfileHelper.auditInvalidInterfaces(vm.getName(), AuditLogType.IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES); } /** @@ -570,13 +535,5 @@ protected VmNetworkInterfaceDao getVmNetworkInterfaceDao() { return DbFacade.getInstance().getVmNetworkInterfaceDao(); - } - - private NetworkDao getNetworkDao() { - return DbFacade.getInstance().getNetworkDao(); - } - - private VnicProfileViewDao getVnicProfileViewDao() { - return DbFacade.getInstance().getVnicProfileViewDao(); } } diff --git a/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties b/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties index 24b4736..42dc162 100644 --- a/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties +++ b/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties @@ -590,9 +590,9 @@ USER_ADD_EXTERNAL_JOB=New external Job ${description} was added by user ${UserName} USER_ADD_EXTERNAL_JOB_FAILED=Failed to add new external Job ${description} STORAGE_DOMAIN_TASKS_ERROR=Storage Domain ${StorageDomainName} is down while there are tasks running on it. These tasks may fail. -IMPORTEXPORT_IMPORT_VM_INVALID_INTERFACES=While importing VM ${VmName}, the Network/s ${Networks} were found to be Non-VM Networks or do not exist in Cluster. Network Name was not set in the Interface/s ${Interfaces}. -IMPORTEXPORT_IMPORT_TEMPLATE_INVALID_INTERFACES=While importing Template ${VmTemplateName}, the Network/s ${Networks} were found to be Non-VM Networks or do not exist in Cluster. Network Name was not set in the Interface/s ${Interfaces}. -IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES=While previewing a snapshot of VM ${VmName}, the Network/s ${Networks} were found to be Non-VM Networks or do not exist in Cluster. Network Name was not set in the Interface/s ${Interfaces}. +IMPORTEXPORT_IMPORT_VM_INVALID_INTERFACES=While importing VM ${EntityName}, the Network/s ${Networks} were found to be Non-VM Networks or do not exist in Cluster. Network Name was not set in the Interface/s ${Interfaces}. +IMPORTEXPORT_IMPORT_TEMPLATE_INVALID_INTERFACES=While importing Template ${EntityName}, the Network/s ${Networks} were found to be Non-VM Networks or do not exist in Cluster. Network Name was not set in the Interface/s ${Interfaces}. +IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES=While previewing a snapshot of VM ${EntityName}, the Network/s ${Networks} were found to be Non-VM Networks or do not exist in Cluster. Network Name was not set in the Interface/s ${Interfaces}. VDS_SET_NON_OPERATIONAL_VM_NETWORK_IS_BRIDGELESS=Host ${VdsName} does not comply with the cluster ${VdsGroupName} networks, the following VM networks are non-VM networks: '${Networks}' EMULATED_MACHINES_INCOMPATIBLE_WITH_CLUSTER=Host ${VdsName} does not comply with the cluster ${VdsGroupName} emulated machines. The Hosts emulated machines are ${hostSupportedEmulatedMachines} and the cluster is ${clusterEmulatedMachines}} # Gluster Messages -- To view, visit http://gerrit.ovirt.org/18797 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic03ae924a6fb31a948723a57b331597ec7d76977 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Moti Asayag <masa...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches