Moti Asayag has uploaded a new change for review.

Change subject: engine: Import VM configure the vnic profile
......................................................................

engine: Import VM configure the vnic profile

The patch consider the vnic profile as it is provided
by the OVF for a vnic.

Change-Id: I890a214e879c740dd1dbdebca25529786231cbd6
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
1 file changed, 97 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/40/17440/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 cc333e4..e76b01f 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,6 +10,7 @@
 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;
@@ -63,6 +64,8 @@
 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;
 import org.ovirt.engine.core.common.locks.LockingGroup;
@@ -1029,31 +1032,114 @@
         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<String>();
-        List<String> invalidIfaceNames = new ArrayList<String>();
+        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());
 
         for (VmNetworkInterface iface : getVm().getInterfaces()) {
             initInterface(iface);
-            if (!vmInterfaceManager.isValidVmNetwork(iface,
-                    networksInClusterByName,
-                    getVdsGroup().getcompatibility_version())) {
-                invalidNetworkNames.add(iface.getNetworkName());
-                invalidIfaceNames.add(iface.getName());
-                iface.setVnicProfileId(null);
+
+            if (iface.getNetworkName() == null) {
+                if 
(FeatureSupported.networkLinking(getVdsGroup().getcompatibility_version())) {
+                    iface.setVnicProfileId(null);
+                } else {
+                    markNicHasNoProfile(invalidNetworkNames, 
invalidIfaceNames, iface);
+                }
+
+                addVnic(vmInterfaceManager, iface);
+                continue;
             }
 
-            vmInterfaceManager.add(iface, getCompensationContext(), 
getParameters().isImportAsNewEntity(),
-                    getVdsGroup().getcompatibility_version());
-            macsAdded.add(iface.getMacAddress());
+            Network network = 
networksInClusterByName.get(iface.getNetworkName());
+            if (network == null || !network.isVmNetwork()) {
+                markNicHasNoProfile(invalidNetworkNames, invalidIfaceNames, 
iface);
+                addVnic(vmInterfaceManager, iface);
+                continue;
+            }
+
+            VnicProfile vnicProfile = 
getVnicProfileForNetwork(vnicProfilesInDc, network, iface.getVnicProfileName());
+            if (vnicProfile == null) {
+                vnicProfile = 
findVnicProfileForUser(getCurrentUser().getUserId(), network);
+                if (vnicProfile == null) {
+                    markNicHasNoProfile(invalidNetworkNames, 
invalidIfaceNames, iface);
+                } else {
+                    iface.setVnicProfileId(vnicProfile.getId());
+                }
+
+                addVnic(vmInterfaceManager, iface);
+                continue;
+            }
+
+            iface.setVnicProfileId(vnicProfile.getId());
+            addVnic(vmInterfaceManager, iface);
+            continue;
         }
 
         auditInvalidInterfaces(invalidNetworkNames, invalidIfaceNames);
     }
 
+    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 addVnic(VmInterfaceManager vmInterfaceManager, 
VmNetworkInterface iface) {
+        vmInterfaceManager.add(iface, getCompensationContext(), 
getParameters().isImportAsNewEntity(),
+                getVdsGroup().getcompatibility_version());
+        macsAdded.add(iface.getMacAddress());
+    }
+
+    private void markNicHasNoProfile(List<String> invalidNetworkNames,
+            List<String> invalidIfaceNames,
+            VmNetworkInterface iface) {
+        invalidNetworkNames.add(iface.getNetworkName());
+        invalidIfaceNames.add(iface.getName());
+        iface.setVnicProfileId(null);
+    }
+
     private void initInterface(VmNic iface) {
         if (iface.getId() == null) {
             iface.setId(Guid.newGuid());


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

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