Moti Asayag has uploaded a new change for review.

Change subject: engine: Extract vnic profile resolution to VmInterfaceManager
......................................................................

engine: Extract vnic profile resolution to VmInterfaceManager

The patch extracts the vnic profile resolution to VmInterfaceManager
for re-usability by the ImportVmTemplate.

Change-Id: I7588ca75a766927bffcafed86490e68fdb7bce05
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/network/VmInterfaceManager.java
2 files changed, 114 insertions(+), 97 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/43/17443/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 ea5332a..4451574 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
@@ -10,7 +10,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.commons.lang.ObjectUtils;
 import org.apache.commons.lang.StringUtils;
 import org.ovirt.engine.core.bll.job.ExecutionContext;
 import org.ovirt.engine.core.bll.job.ExecutionHandler;
@@ -64,7 +63,6 @@
 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.VdcBllMessages;
@@ -81,7 +79,6 @@
 import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.compat.NotImplementedException;
-import org.ovirt.engine.core.compat.Version;
 import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
 import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
 import org.ovirt.engine.core.utils.GuidUtils;
@@ -1033,24 +1030,6 @@
         AuditLogDirector.log(logable, AuditLogType.VM_IMPORT_INFO);
     }
 
-    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;
-    }
-
     protected void addVmInterfaces() {
         VmInterfaceManager vmInterfaceManager = new VmInterfaceManager();
         List<String> invalidNetworkNames = new ArrayList<>();
@@ -1062,12 +1041,13 @@
 
         for (VmNetworkInterface iface : getVm().getInterfaces()) {
             initInterface(iface);
-            if (!updateNicWithVnicProfile(iface,
+            if (!vmInterfaceManager.updateNicWithVnicProfile(iface,
                     iface.getNetworkName(),
                     iface.getVnicProfileName(),
                     getVdsGroup().getcompatibility_version(),
                     networksInClusterByName,
-                    vnicProfilesInDc)) {
+                    vnicProfilesInDc,
+                    getCurrentUser().getUserId())) {
                 markNicHasNoProfile(invalidNetworkNames, invalidIfaceNames, 
iface);
             }
 
@@ -1079,80 +1059,6 @@
         auditInvalidInterfaces(invalidNetworkNames, invalidIfaceNames);
     }
 
-    /**
-     * 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 networkName
-     *            The network name which the vnic profile is associated with
-     * @param vnicProfileName
-     *            The vnic profile name
-     * @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
-     * @return {@code true} if the vnic profile id is updated, else {@code 
false}
-     */
-    public boolean updateNicWithVnicProfile(VmNic iface,
-            String networkName,
-            String vnicProfileName,
-            Version compatibilityVersion,
-            Map<String, Network> networksInClusterByName,
-            List<VnicProfileView> vnicProfilesInDc) {
-
-        if (networkName == null) {
-            if 
(FeatureSupported.networkLinking(getVdsGroup().getcompatibility_version())) {
-                iface.setVnicProfileId(null);
-                return true;
-            } else {
-                return false;
-            }
-        }
-
-        Network network = networksInClusterByName.get(networkName);
-        if (network == null || !network.isVmNetwork()) {
-            return false;
-        }
-
-        VnicProfile vnicProfile = getVnicProfileForNetwork(vnicProfilesInDc, 
network, vnicProfileName);
-        if (vnicProfile == null) {
-            vnicProfile = findVnicProfileForUser(getCurrentUser().getUserId(), 
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 (profile.isPortMirroring()) {
-                if (isVnicProfilePermitted(userId, profile, 
ActionGroup.PORT_MIRRORING)) {
-                    return profile;
-                }
-            } else {
-                if (isVnicProfilePermitted(userId, profile, 
ActionGroup.CONFIGURE_VM_NETWORK)) {
-                    return profile;
-                }
-            }
-        }
-
-        return null;
-    }
-
-    private boolean isVnicProfilePermitted(Guid userId, VnicProfile profile, 
ActionGroup actionGroup) {
-        return getPermissionDAO().getEntityPermissions(userId,
-                actionGroup,
-                profile.getId(),
-                VdcObjectType.VnicProfile) != null;
-    }
 
     private void markNicHasNoProfile(List<String> invalidNetworkNames,
             List<String> invalidIfaceNames,
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 215963f..2b42001 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
@@ -4,13 +4,18 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.lang.ObjectUtils;
 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.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.compat.Guid;
@@ -18,10 +23,12 @@
 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.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;
@@ -167,6 +174,102 @@
     }
 
     /**
+     * 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 networkName
+     *            The network name which the vnic profile is associated with
+     * @param vnicProfileName
+     *            The vnic profile name
+     * @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(VmNic iface,
+            String networkName,
+            String vnicProfileName,
+            Version compatibilityVersion,
+            Map<String, Network> networksInClusterByName,
+            List<VnicProfileView> vnicProfilesInDc,
+            Guid userId) {
+
+        if (networkName == null) {
+            if (FeatureSupported.networkLinking(compatibilityVersion)) {
+                iface.setVnicProfileId(null);
+                return true;
+            } else {
+                return false;
+            }
+        }
+
+        Network network = networksInClusterByName.get(networkName);
+        if (network == null || !network.isVmNetwork()) {
+            return false;
+        }
+
+        VnicProfile vnicProfile = getVnicProfileForNetwork(vnicProfilesInDc, 
network, vnicProfileName);
+        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 (profile.isPortMirroring()) {
+                if (isVnicProfilePermitted(userId, profile, 
ActionGroup.PORT_MIRRORING)) {
+                    return profile;
+                }
+            } else {
+                if (isVnicProfilePermitted(userId, profile, 
ActionGroup.CONFIGURE_VM_NETWORK)) {
+                    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, 
ActionGroup actionGroup) {
+        return getPermissionDAO().getEntityPermissions(userId,
+                actionGroup,
+                profile.getId(),
+                VdcObjectType.VnicProfile) != null;
+    }
+
+    /**
      * Log the given loggable & message to the {@link AuditLogDirector}.
      *
      * @param logable
@@ -196,6 +299,14 @@
         return DbFacade.getInstance().getVmDao();
     }
 
+    private VnicProfileDao getVnicProfileDao() {
+        return DbFacade.getInstance().getVnicProfileDao();
+    }
+
+    private PermissionDAO getPermissionDAO() {
+        return DbFacade.getInstance().getPermissionDao();
+    }
+
     private AuditLogableBase createAuditLog(final VmNic iface) {
         AuditLogableBase logable = new AuditLogableBase();
         logable.setVmId(iface.getVmId());


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

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

Reply via email to