Arik Hadas has uploaded a new change for review.

Change subject: core: add base
......................................................................

core: add base

Change-Id: Ie060ea182ea125db7f088ad5135e45a31a70b22b
Signed-off-by: Arik Hadas <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommandBase.java
2 files changed, 216 insertions(+), 173 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/54/33054/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 04bc717..06e0356 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
@@ -15,7 +15,6 @@
 import org.ovirt.engine.core.bll.context.CommandContext;
 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.profiles.CpuProfileHelper;
 import org.ovirt.engine.core.bll.profiles.DiskProfileHelper;
 import org.ovirt.engine.core.bll.quota.QuotaConsumptionParameter;
@@ -59,10 +58,7 @@
 import org.ovirt.engine.core.common.businessentities.StorageDomainType;
 import org.ovirt.engine.core.common.businessentities.VDSGroup;
 import org.ovirt.engine.core.common.businessentities.VM;
-import org.ovirt.engine.core.common.businessentities.VMStatus;
-import org.ovirt.engine.core.common.businessentities.VmDynamic;
 import org.ovirt.engine.core.common.businessentities.VmStatic;
-import org.ovirt.engine.core.common.businessentities.VmStatistics;
 import org.ovirt.engine.core.common.businessentities.VmTemplate;
 import org.ovirt.engine.core.common.businessentities.VmTemplateStatus;
 import org.ovirt.engine.core.common.businessentities.VolumeFormat;
@@ -93,22 +89,18 @@
 import org.ovirt.engine.core.utils.linq.Predicate;
 import org.ovirt.engine.core.utils.log.Log;
 import org.ovirt.engine.core.utils.log.LogFactory;
-import org.ovirt.engine.core.utils.ovf.OvfLogEventHandler;
-import org.ovirt.engine.core.utils.ovf.VMStaticOvfLogHandler;
 import org.ovirt.engine.core.utils.transaction.TransactionMethod;
 import org.ovirt.engine.core.utils.transaction.TransactionSupport;
 
 @DisableInPrepareMode
 @NonTransactiveCommandAttribute(forceCompensation = true)
-public class ImportVmCommand<T extends ImportVmParameters> extends 
MoveOrCopyTemplateCommand<T>
+public class ImportVmCommand<T extends ImportVmParameters> extends 
ImportVmCommandBase<T>
         implements QuotaStorageDependent, TaskHandlerCommand<T> {
 
     private static final Log log = LogFactory.getLog(ImportVmCommand.class);
 
-    private static VmStatic vmStaticForDefaultValues = new VmStatic();
     private List<DiskImage> imageList;
 
-    private final List<String> macsAdded = new ArrayList<String>();
     private final SnapshotsManager snapshotsManager = new SnapshotsManager();
 
     public ImportVmCommand(T parameters) {
@@ -680,37 +672,13 @@
     }
 
     @Override
-    protected void executeCommand() {
-        try {
-            addVmToDb();
-            processImages(!isImagesAlreadyOnTarget());
-            // if there aren't tasks - we can just perform the end
-            // vm related ops
-            if (getReturnValue().getVdsmTaskIdList().isEmpty()) {
-                endVmRelatedOps();
-            }
-            // Save Vm Init
-            VmHandler.addVmInitToDB(getVm().getStaticData());
-        } catch (RuntimeException e) {
-            getMacPool().freeMacs(macsAdded);
-            throw e;
+    protected void copyImages() {
+        processImages(!isImagesAlreadyOnTarget());
+        // if there aren't tasks - we can just perform the end
+        // vm related ops
+        if (getReturnValue().getVdsmTaskIdList().isEmpty()) {
+            endVmRelatedOps();
         }
-        setSucceeded(true);
-    }
-
-    private void addVmToDb() {
-        TransactionSupport.executeInNewTransaction(new 
TransactionMethod<Void>() {
-
-            @Override
-            public Void runInTransaction() {
-                addVmStatic();
-                addVmDynamic();
-                addVmInterfaces();
-                addVmStatistics();
-                getCompensationContext().stateChanged();
-                return null;
-            }
-        });
     }
 
     private void processImages(final boolean useCopyImages) {
@@ -1040,139 +1008,6 @@
                         null));
     }
 
-    protected void addVmStatic() {
-
-        logImportEvents();
-        getVm().getStaticData().setId(getVmId());
-        getVm().getStaticData().setCreationDate(new Date());
-        getVm().getStaticData().setVdsGroupId(getParameters().getVdsGroupId());
-        getVm().getStaticData().setMinAllocatedMem(computeMinAllocatedMem());
-        getVm().getStaticData().setQuotaId(getParameters().getQuotaId());
-
-        if (getVm().getOriginalTemplateGuid() != null && 
!VmTemplateHandler.BLANK_VM_TEMPLATE_ID.equals(getVm().getOriginalTemplateGuid()))
 {
-            // no need to check this for blank
-            VmTemplate originalTemplate = 
getVmTemplateDAO().get(getVm().getOriginalTemplateGuid());
-            if (originalTemplate != null) {
-                // in case the original template name has been changed in the 
meantime
-                
getVm().getStaticData().setOriginalTemplateName(originalTemplate.getName());
-            }
-        }
-
-        if (getParameters().getCopyCollapse()) {
-            getVm().setVmtGuid(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
-        }
-        getVmStaticDAO().save(getVm().getStaticData());
-        getCompensationContext().snapshotNewEntity(getVm().getStaticData());
-    }
-
-    private int computeMinAllocatedMem() {
-        if (getVm().getMinAllocatedMem() > 0) {
-            return getVm().getMinAllocatedMem();
-        }
-
-        VDSGroup vdsGroup = getVdsGroup();
-        if (vdsGroup != null && vdsGroup.getmax_vds_memory_over_commit() > 0) {
-            return (getVm().getMemSizeMb() * 100) / 
vdsGroup.getmax_vds_memory_over_commit();
-        }
-
-        return getVm().getMemSizeMb();
-    }
-
-    private void logImportEvents() {
-        // Some values at the OVF file are used for creating events at the GUI
-        // for the sake of providing information on the content of the VM that
-        // was exported,
-        // but not setting it in the imported VM
-        VmStatic vmStaticFromOvf = getVm().getStaticData();
-
-        OvfLogEventHandler<VmStatic> handler = new 
VMStaticOvfLogHandler(vmStaticFromOvf);
-        Map<String, String> aliasesValuesMap = handler.getAliasesValuesMap();
-
-        for (Map.Entry<String, String> entry : aliasesValuesMap.entrySet()) {
-            String fieldName = entry.getKey();
-            String fieldValue = entry.getValue();
-            logField(vmStaticFromOvf, fieldName, fieldValue);
-        }
-
-        handler.resetDefaults(vmStaticForDefaultValues);
-
-    }
-
-    private static void logField(VmStatic vmStaticFromOvf, String fieldName, 
String fieldValue) {
-        String vmName = vmStaticFromOvf.getName();
-        AuditLogableBase logable = new AuditLogableBase();
-        logable.addCustomValue("FieldName", fieldName);
-        logable.addCustomValue("VmName", vmName);
-        logable.addCustomValue("FieldValue", fieldValue);
-        AuditLogDirector.log(logable, AuditLogType.VM_IMPORT_INFO);
-    }
-
-    protected void addVmInterfaces() {
-        VmInterfaceManager vmInterfaceManager = new 
VmInterfaceManager(getMacPool());
-
-        VnicProfileHelper vnicProfileHelper =
-                new VnicProfileHelper(getVdsGroupId(),
-                        getStoragePoolId(),
-                        getVdsGroup().getcompatibility_version(),
-                        
AuditLogType.IMPORTEXPORT_IMPORT_VM_INVALID_INTERFACES);
-
-        List<VmNetworkInterface> nics = getVm().getInterfaces();
-
-        vmInterfaceManager.sortVmNics(nics, 
getVm().getStaticData().getManagedDeviceMap());
-
-        // If we import it as a new entity, then we allocate all MAC addresses 
in advance
-        if (getParameters().isImportAsNewEntity()) {
-            List<String> macAddresses = 
getMacPool().allocateMacAddresses(nics.size());
-            for (int i = 0; i < nics.size(); ++i) {
-                nics.get(i).setMacAddress(macAddresses.get(i));
-            }
-        }
-
-        for (VmNetworkInterface iface : getVm().getInterfaces()) {
-            initInterface(iface);
-            vnicProfileHelper.updateNicWithVnicProfileForUser(iface, 
getCurrentUser());
-
-            vmInterfaceManager.add(iface,
-                                   getCompensationContext(),
-                                   !getParameters().isImportAsNewEntity(),
-                                   getVm().getOs(),
-                                   getVdsGroup().getcompatibility_version());
-            macsAdded.add(iface.getMacAddress());
-        }
-
-        vnicProfileHelper.auditInvalidInterfaces(getVmName());
-    }
-
-    private void initInterface(VmNic iface) {
-        if (iface.getId() == null) {
-            iface.setId(Guid.newGuid());
-        }
-        fillMacAddressIfMissing(iface);
-        iface.setVmTemplateId(null);
-        iface.setVmId(getVmId());
-    }
-
-    private void addVmDynamic() {
-        VmDynamic tempVar = new VmDynamic();
-        tempVar.setId(getVmId());
-        tempVar.setStatus(VMStatus.ImageLocked);
-        tempVar.setVmHost("");
-        tempVar.setVmIp("");
-        tempVar.setVmFQDN("");
-        tempVar.setLastStopTime(new Date());
-        
tempVar.setAppList(getParameters().getVm().getDynamicData().getAppList());
-        getVmDynamicDAO().save(tempVar);
-        getCompensationContext().snapshotNewEntity(tempVar);
-    }
-
-    private void addVmStatistics() {
-        VmStatistics stats = new VmStatistics();
-        stats.setId(getVmId());
-        getVmStatisticsDAO().save(stats);
-        getCompensationContext().snapshotNewEntity(stats);
-        getCompensationContext().stateChanged();
-    }
-
     @Override
     protected void endSuccessfully() {
         checkTrustedService();
@@ -1188,7 +1023,7 @@
         else if (!getVm().isTrustedService() && 
getVdsGroup().supportsTrustedService()) {
             AuditLogDirector.log(logable, 
AuditLogType.IMPORTEXPORT_IMPORT_VM_FROM_UNTRUSTED_TO_TRUSTED);
         }
-     }
+    }
 
     @Override
     protected void endActionOnAllImageGroups() {
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommandBase.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommandBase.java
new file mode 100644
index 0000000..c90c12b
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommandBase.java
@@ -0,0 +1,208 @@
+package org.ovirt.engine.core.bll;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import org.ovirt.engine.core.bll.context.CommandContext;
+import org.ovirt.engine.core.bll.network.VmInterfaceManager;
+import org.ovirt.engine.core.bll.network.vm.VnicProfileHelper;
+import org.ovirt.engine.core.common.AuditLogType;
+import org.ovirt.engine.core.common.action.ImportVmParameters;
+import org.ovirt.engine.core.common.businessentities.VDSGroup;
+import org.ovirt.engine.core.common.businessentities.VMStatus;
+import org.ovirt.engine.core.common.businessentities.VmDynamic;
+import org.ovirt.engine.core.common.businessentities.VmStatic;
+import org.ovirt.engine.core.common.businessentities.VmStatistics;
+import org.ovirt.engine.core.common.businessentities.VmTemplate;
+import 
org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface;
+import org.ovirt.engine.core.common.businessentities.network.VmNic;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
+import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
+import org.ovirt.engine.core.utils.ovf.OvfLogEventHandler;
+import org.ovirt.engine.core.utils.ovf.VMStaticOvfLogHandler;
+import org.ovirt.engine.core.utils.transaction.TransactionMethod;
+import org.ovirt.engine.core.utils.transaction.TransactionSupport;
+
+public abstract class ImportVmCommandBase<T extends ImportVmParameters> 
extends MoveOrCopyTemplateCommand<T> {
+
+    private final List<String> macsAdded = new ArrayList<>();
+    private static VmStatic vmStaticForDefaultValues = new VmStatic();
+
+    protected ImportVmCommandBase(Guid commandId) {
+        super(commandId);
+    }
+
+    protected ImportVmCommandBase(T parameters) {
+        this(parameters, null);
+    }
+
+    protected ImportVmCommandBase(T parameters, CommandContext cmdContext) {
+        super(parameters, cmdContext);
+    }
+
+    @Override
+    protected void executeCommand() {
+        try {
+            addVmToDb();
+            copyImages();
+            VmHandler.addVmInitToDB(getVm().getStaticData());
+        } catch (RuntimeException e) {
+            getMacPool().freeMacs(macsAdded);
+            throw e;
+        }
+
+        setSucceeded(true);
+    }
+
+    protected abstract void copyImages();
+
+    protected void addVmToDb() {
+        TransactionSupport.executeInNewTransaction(new 
TransactionMethod<Void>() {
+
+            @Override
+            public Void runInTransaction() {
+                addVmStatic();
+                addVmDynamic();
+                addVmInterfaces();
+                addVmStatistics();
+                getCompensationContext().stateChanged();
+                return null;
+            }
+        });
+    }
+
+    protected void addVmStatic() {
+        logImportEvents();
+        getVm().getStaticData().setId(getVmId());
+        getVm().getStaticData().setCreationDate(new Date());
+        getVm().getStaticData().setVdsGroupId(getParameters().getVdsGroupId());
+        getVm().getStaticData().setMinAllocatedMem(computeMinAllocatedMem());
+        getVm().getStaticData().setQuotaId(getParameters().getQuotaId());
+
+        if (getVm().getOriginalTemplateGuid() != null && 
!VmTemplateHandler.BLANK_VM_TEMPLATE_ID.equals(getVm().getOriginalTemplateGuid()))
 {
+            // no need to check this for blank
+            VmTemplate originalTemplate = 
getVmTemplateDAO().get(getVm().getOriginalTemplateGuid());
+            if (originalTemplate != null) {
+                // in case the original template name has been changed in the 
meantime
+                
getVm().getStaticData().setOriginalTemplateName(originalTemplate.getName());
+            }
+        }
+
+        if (getParameters().getCopyCollapse()) {
+            getVm().setVmtGuid(VmTemplateHandler.BLANK_VM_TEMPLATE_ID);
+        }
+        getVmStaticDAO().save(getVm().getStaticData());
+        getCompensationContext().snapshotNewEntity(getVm().getStaticData());
+    }
+
+    private void logImportEvents() {
+        // Some values at the OVF file are used for creating events at the GUI
+        // for the sake of providing information on the content of the VM that
+        // was exported,
+        // but not setting it in the imported VM
+        VmStatic vmStaticFromOvf = getVm().getStaticData();
+
+        OvfLogEventHandler<VmStatic> handler = new 
VMStaticOvfLogHandler(vmStaticFromOvf);
+        Map<String, String> aliasesValuesMap = handler.getAliasesValuesMap();
+
+        for (Map.Entry<String, String> entry : aliasesValuesMap.entrySet()) {
+            String fieldName = entry.getKey();
+            String fieldValue = entry.getValue();
+            logField(vmStaticFromOvf, fieldName, fieldValue);
+        }
+
+        handler.resetDefaults(vmStaticForDefaultValues);
+
+    }
+
+    private static void logField(VmStatic vmStaticFromOvf, String fieldName, 
String fieldValue) {
+        String vmName = vmStaticFromOvf.getName();
+        AuditLogableBase logable = new AuditLogableBase();
+        logable.addCustomValue("FieldName", fieldName);
+        logable.addCustomValue("VmName", vmName);
+        logable.addCustomValue("FieldValue", fieldValue);
+        AuditLogDirector.log(logable, AuditLogType.VM_IMPORT_INFO);
+    }
+
+    protected void addVmInterfaces() {
+        VmInterfaceManager vmInterfaceManager = new 
VmInterfaceManager(getMacPool());
+
+        VnicProfileHelper vnicProfileHelper =
+                new VnicProfileHelper(getVdsGroupId(),
+                        getStoragePoolId(),
+                        getVdsGroup().getcompatibility_version(),
+                        
AuditLogType.IMPORTEXPORT_IMPORT_VM_INVALID_INTERFACES);
+
+        List<VmNetworkInterface> nics = getVm().getInterfaces();
+
+        vmInterfaceManager.sortVmNics(nics, 
getVm().getStaticData().getManagedDeviceMap());
+
+        // If we import it as a new entity, then we allocate all MAC addresses 
in advance
+        if (getParameters().isImportAsNewEntity()) {
+            List<String> macAddresses = 
getMacPool().allocateMacAddresses(nics.size());
+            for (int i = 0; i < nics.size(); ++i) {
+                nics.get(i).setMacAddress(macAddresses.get(i));
+            }
+        }
+
+        for (VmNetworkInterface iface : getVm().getInterfaces()) {
+            initInterface(iface);
+            vnicProfileHelper.updateNicWithVnicProfileForUser(iface, 
getCurrentUser());
+
+            vmInterfaceManager.add(iface,
+                                   getCompensationContext(),
+                                   !getParameters().isImportAsNewEntity(),
+                                   getVm().getOs(),
+                                   getVdsGroup().getcompatibility_version());
+            macsAdded.add(iface.getMacAddress());
+        }
+
+        vnicProfileHelper.auditInvalidInterfaces(getVmName());
+    }
+
+    private void initInterface(VmNic iface) {
+        if (iface.getId() == null) {
+            iface.setId(Guid.newGuid());
+        }
+        fillMacAddressIfMissing(iface);
+        iface.setVmTemplateId(null);
+        iface.setVmId(getVmId());
+    }
+
+    private void addVmDynamic() {
+        VmDynamic tempVar = new VmDynamic();
+        tempVar.setId(getVmId());
+        tempVar.setStatus(VMStatus.ImageLocked);
+        tempVar.setVmHost("");
+        tempVar.setVmIp("");
+        tempVar.setVmFQDN("");
+        tempVar.setLastStopTime(new Date());
+        
tempVar.setAppList(getParameters().getVm().getDynamicData().getAppList());
+        getVmDynamicDAO().save(tempVar);
+        getCompensationContext().snapshotNewEntity(tempVar);
+    }
+
+    private void addVmStatistics() {
+        VmStatistics stats = new VmStatistics();
+        stats.setId(getVmId());
+        getVmStatisticsDAO().save(stats);
+        getCompensationContext().snapshotNewEntity(stats);
+        getCompensationContext().stateChanged();
+    }
+
+    private int computeMinAllocatedMem() {
+        if (getVm().getMinAllocatedMem() > 0) {
+            return getVm().getMinAllocatedMem();
+        }
+
+        VDSGroup vdsGroup = getVdsGroup();
+        if (vdsGroup != null && vdsGroup.getmax_vds_memory_over_commit() > 0) {
+            return (getVm().getMemSizeMb() * 100) / 
vdsGroup.getmax_vds_memory_over_commit();
+        }
+
+        return getVm().getMemSizeMb();
+    }
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie060ea182ea125db7f088ad5135e45a31a70b22b
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Arik Hadas <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to