package org.ovirt.engine.core.bll;

import org.ovirt.engine.core.common.action.MigrateVmToServerParameters;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.errors.VdcBllMessages;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

@LockIdNameAttribute(isReleaseAtEndOfExecute = false)
public class MigrateVmToServerCommand<T extends MigrateVmToServerParameters> extends MigrateVmCommand<T> {
    public MigrateVmToServerCommand(T parameters) {
        super(parameters);
        setVdsDestinationId(parameters.getVdsId());
    }

    @Override
    protected boolean canDoAction() {
        VM vm = getVm();
        Guid destinationId = getVdsDestinationId();
        VDS vds = DbFacade.getInstance().getVdsDao().get(destinationId);
        if (vds == null) {
            return failCanDoAction(VdcBllMessages.VDS_INVALID_SERVER_ID);
        }

        if (vm.getRunOnVds() != null && vm.getRunOnVds().equals(destinationId)) {
            return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_MIGRATION_TO_SAME_HOST);
        }

        if (!vm.getVdsGroupId().equals(getDestinationVds().getVdsGroupId())) {
            return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_MIGRATE_BETWEEN_TWO_CLUSTERS);
        }

        return super.canDoAction();
    }

    /**
     * In case we failed to migrate to that specific server, the VM should no longer be pending,
     * and we report failure, without an attempt to rerun
     */
    @Override
    public void rerun() {
        decreasePendingVms(getDestinationVds().getId());
        _isRerun = false;
        setSucceeded(false);

        determineMigrationFailureForAuditLog();
        log();
    }
}

@@ -320,60 +320,8 @@
 uid;
-%0Aimport org.ovirt.engine.core.dal.dbbroker.DbFacade;
 %0A%0A@L
@@ -673,33 +673,8 @@
 ) %7B%0A
-        VM vm = getVm();%0A
     
@@ -743,40 +743,17 @@
 s = 
-DbFacade.getInstance().
 getVdsD
-ao
+AO
 ().g
@@ -875,32 +875,131 @@
 ID);%0A        %7D%0A%0A
+        if (!super.canDoAction()) %7B%0A            return false;%0A        %7D%0A%0A        VM vm = getVm();%0A%0A
         if (vm.g
@@ -1374,35 +1374,20 @@
  return 
-super.canDoAction()
+true
 ;%0A    %7D%0A

fix possible NPE in migrate vm to server