Moti Asayag has uploaded a new change for review.

Change subject: engine: VnicProfile info isn't restored on snapshot preview
......................................................................

engine: VnicProfile info isn't restored on snapshot preview

The vnic profile information wasn't updated when a snapshot
of a VM was preview. Since the snapshot uses the OVF writer
and reader, the vnic profile id isn't kept with the network
name and the profile name. These should be resolved before
adding the interface to the system.

Change-Id: If9880b957bdccc1dae0364919c3958479eabbc30
Bug-Url: https://bugzilla.redhat.com/998634
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/snapshots/SnapshotsManager.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
M 
backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
6 files changed, 64 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/32/18332/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
index c59935f..1b2479b 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreAllSnapshotsCommand.java
@@ -234,7 +234,7 @@
         snapshotsManager.attempToRestoreVmConfigurationFromSnapshot(getVm(),
                 targetSnapshot,
                 targetSnapshot.getId(),
-                getCompensationContext(), 
getVm().getVdsGroupCompatibilityVersion());
+                getCompensationContext(), 
getVm().getVdsGroupCompatibilityVersion(), getCurrentUser().getUserId());
         getSnapshotDao().remove(targetSnapshot.getId());
         // add active snapshot with status locked, so that other commands that 
depend on the VM's snapshots won't run in parallel
         snapshotsManager.addActiveSnapshot(targetSnapshot.getId(),
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
index 0484698..5f313f0 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/TryBackToAllSnapshotsOfVmCommand.java
@@ -27,8 +27,8 @@
 import org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType;
 import org.ovirt.engine.core.common.errors.VdcBLLException;
 import org.ovirt.engine.core.common.errors.VdcBllErrors;
-import org.ovirt.engine.core.common.locks.LockingGroup;
 import org.ovirt.engine.core.common.errors.VdcBllMessages;
+import org.ovirt.engine.core.common.locks.LockingGroup;
 import org.ovirt.engine.core.common.utils.Pair;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dal.dbbroker.DbFacade;
@@ -112,7 +112,7 @@
         snapshotsManager.attempToRestoreVmConfigurationFromSnapshot(getVm(),
                 getDstSnapshot(),
                 getSnapshotDao().getId(getVm().getId(), SnapshotType.ACTIVE),
-                getCompensationContext(), 
getVm().getVdsGroupCompatibilityVersion());
+                getCompensationContext(), 
getVm().getVdsGroupCompatibilityVersion(), getCurrentUser().getUserId());
     }
 
     @Override
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 01f34b8..2e33995 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,6 +3,7 @@
 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;
@@ -12,8 +13,10 @@
 import org.ovirt.engine.core.bll.network.VmInterfaceManager;
 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;
@@ -25,11 +28,15 @@
 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;
@@ -40,7 +47,9 @@
 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;
@@ -297,11 +306,12 @@
      *            The snapshot containing the configuration.
      * @param version
      *            The compatibility version of the VM's cluster
+     * @param userId
      */
     public void attempToRestoreVmConfigurationFromSnapshot(VM vm,
             Snapshot snapshot,
             Guid activeSnapshotId,
-            CompensationContext compensationContext, Version version) {
+            CompensationContext compensationContext, Version version, Guid 
userId) {
         boolean vmUpdatedFromConfiguration = false;
         if (snapshot.getVmConfiguration() != null) {
             vmUpdatedFromConfiguration = updateVmFromConfiguration(vm, 
snapshot.getVmConfiguration());
@@ -317,7 +327,7 @@
 
         if (vmUpdatedFromConfiguration) {
             getVmStaticDao().update(vm.getStaticData());
-            synchronizeNics(vm.getId(), vm.getInterfaces(), 
compensationContext, version);
+            synchronizeNics(vm, compensationContext, userId);
 
             for (VmDevice vmDevice : 
getVmDeviceDao().getVmDeviceByVmId(vm.getId())) {
                 if (deviceCanBeRemoved(vmDevice)) {
@@ -404,18 +414,50 @@
      * @param version
      *            The compatibility version of the VM's cluster
      */
-    protected void synchronizeNics(Guid vmId, List<VmNetworkInterface> nics, 
CompensationContext compensationContext, Version version) {
+    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());
 
-        vmInterfaceManager.removeAll(vmId);
-        for (VmNetworkInterface vmInterface : nics) {
+        vmInterfaceManager.removeAll(vm.getId());
+        for (VmNetworkInterface vmInterface : vm.getInterfaces()) {
+            vmInterface.setVmId(vm.getId());
             // These fields might not be saved in the OVF, so fill them with 
reasonable values.
             if (vmInterface.getId() == null) {
                 vmInterface.setId(Guid.newGuid());
             }
-            vmInterface.setVmId(vmId);
 
-            vmInterfaceManager.add(vmInterface, compensationContext, false, 
version);
+            if (!vmInterfaceManager.updateNicWithVnicProfile(vmInterface,
+                    vm.getVdsGroupCompatibilityVersion(),
+                    networksInClusterByName,
+                    vnicProfilesInDc,
+                    userId)) {
+                markNicHasNoProfile(invalidNetworkNames, invalidIfaceNames, 
vmInterface);
+            }
+
+            vmInterfaceManager.add(vmInterface, compensationContext, false, 
vm.getVdsGroupCompatibilityVersion());
+        }
+
+        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);
+    }
+
+    private 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, 
AuditLogType.IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES);
         }
     }
 
@@ -528,4 +570,12 @@
     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/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
index b8c10b9..e22f1bc 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
@@ -756,6 +756,8 @@
     */
     HA_VM_RESTART_FAILED(9603),
 
+    IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES(9606, 
AuditLogTimeInterval.MINUTE.getValue()),
+
     // DWH
     DWH_STOPPED(9701),
     DWH_STARTED(9700),
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
index b898e35..3471ef4 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
@@ -729,6 +729,7 @@
         
severities.put(AuditLogType.IMPORTEXPORT_IMPORT_TEMPLATE_INVALID_INTERFACES, 
AuditLogSeverity.NORMAL);
         
severities.put(AuditLogType.VM_IMPORT_FROM_CONFIGURATION_EXECUTED_SUCCESSFULLY, 
AuditLogSeverity.NORMAL);
         
severities.put(AuditLogType.VM_IMPORT_FROM_CONFIGURATION_ATTACH_DISKS_FAILED, 
AuditLogSeverity.WARNING);
+        
severities.put(AuditLogType.IMPORTEXPORT_SNAPSHOT_VM_INVALID_INTERFACES, 
AuditLogSeverity.WARNING);
     }
 
     private static void initNetworkSeverities() {
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 a5eae2d..24b4736 100644
--- 
a/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
+++ 
b/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
@@ -592,6 +592,7 @@
 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}.
 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/18332
To unsubscribe, visit http://gerrit.ovirt.org/settings

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