package org.ovirt.engine.core.bll.validator;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.ValidationResult;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VDSStatus;
import org.ovirt.engine.core.common.businessentities.VDSType;
import org.ovirt.engine.core.common.errors.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

public class UpgradeHostValidator extends HostValidator {

    public UpgradeHostValidator(DbFacade dbFacade, VDS host) {
        super(dbFacade, host);
    }

    public ValidationResult hostExists() {
        return ValidationResult.failWith(VdcBllMessages.VDS_INVALID_SERVER_ID).when(getHost() == null);
    }

    public ValidationResult statusSupportedForHostUpgrade() {
        return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_INSTALL_STATUS_ILLEGAL)
                .when(getHost().getStatus() != VDSStatus.Maintenance
                        && getHost().getStatus() != VDSStatus.InstallFailed
                        && getHost().getStatus() != VDSStatus.Up);
    }

    public ValidationResult statusSupportedForHostUpgradeInternal() {
        return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_INSTALL_STATUS_ILLEGAL)
                .when(getHost().getStatus() != VDSStatus.Maintenance);
    }

    public ValidationResult updatesAvailable() {
        return ValidationResult.failWith(VdcBllMessages.NO_AVAILABLE_UPDATES_FOR_HOST)
                .unless(getHost().getVdsType() == VDSType.oVirtNode || getHost().isUpdateAvailable());
    }

    public ValidationResult imageProvidedForOvirtNode(String image) {
        return ValidationResult.failWith(VdcBllMessages.VDS_CANNOT_INSTALL_MISSING_IMAGE_FILE)
                .when(getHost().getVdsType() == VDSType.oVirtNode && StringUtils.isBlank(image));
    }

    public ValidationResult hostWasInstalled() {
        return ValidationResult.failWith(VdcBllMessages.CANNOT_UPGRADE_HOST_WITHOUT_OS)
                .when(getHost().getHostOs() == null);
    }
}

@@ -39,16 +39,42 @@
 dator;%0A%0A
+import java.util.Arrays;%0A%0A
 import o
@@ -160,16 +160,126 @@
 Result;%0A
+import org.ovirt.engine.core.common.VdcActionUtils;%0Aimport org.ovirt.engine.core.common.action.VdcActionType;%0A
 import o
@@ -1054,200 +1054,104 @@
    .
-when(getHost().getStatus() != VDSStatus.Maintenance%0A                        && getHost().getStatus() != VDSStatus.InstallFailed%0A                        && getHost().getStatus() != VDSStatus.Up
+unless(VdcActionUtils.canExecute(Arrays.asList(getHost()), VDS.class, VdcActionType.UpgradeHost)
 );%0A 

Support host upgrade in NonOperational status