package org.ovirt.engine.core.bll;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.job.ExecutionHandler;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.action.SetNonOperationalVdsParameters;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.action.VdsActionParameters;
import org.ovirt.engine.core.common.action.VdsGroupOperationParameters;
import org.ovirt.engine.core.common.businessentities.NonOperationalReason;
import org.ovirt.engine.core.common.businessentities.ServerCpu;
import org.ovirt.engine.core.common.businessentities.VDSGroup;
import org.ovirt.engine.core.compat.TransactionScopeOption;
import org.ovirt.engine.core.dal.VdcBllMessages;
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.utils.log.Log;
import org.ovirt.engine.core.utils.log.LogFactory;

public class HandleVdsCpuFlagsOrClusterChangedCommand<T extends VdsActionParameters> extends VdsCommand<T> {
    public HandleVdsCpuFlagsOrClusterChangedCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected boolean canDoAction() {
        boolean result = true;
        if (getVds() == null) {
            addCanDoActionMessage(VdcBllMessages.VDS_INVALID_SERVER_ID);
            result = false;
        }
        return result;
    }

    private boolean _hasFlags = true;

    @Override
    protected void executeCommand() {
        String vdsGroupCpuName = getVds().getvds_group_cpu_name();
        boolean foundCPU = true;
        // if cluster doesnt have cpu then get the cpu from the vds
        if (StringUtils.isEmpty(vdsGroupCpuName)) {
            ServerCpu sc = CpuFlagsManagerHandler.FindMaxServerCpuByFlags(getVds().getcpu_flags(), getVds()
                    .getvds_group_compatibility_version());
            if (sc == null) {
                // if there are flags and no cpu found, mark to be non
                // operational
                if (!StringUtils.isEmpty(getVds().getcpu_flags())) {
                    foundCPU = false;
                } else {
                    _hasFlags = false;
                }
                log.errorFormat("Could not find server cpu for server {0}:{1}, flags: {2}", getVdsId(), getVds()
                        .getvds_name(), getVds().getcpu_flags());
            } else {
                // update group with the cpu name
                VDSGroup grp = DbFacade.getInstance().getVdsGroupDao().get(getVds().getvds_group_id());
                grp.setcpu_name(sc.getCpuName());

                // use suppress in order to update group even if action fails
                // (out of the transaction)
                VdsGroupOperationParameters tempVar = new VdsGroupOperationParameters(grp);
                tempVar.setTransactionScopeOption(TransactionScopeOption.Suppress);
                tempVar.setIsInternalCommand(true);
                Backend.getInstance().runInternalAction(VdcActionType.UpdateVdsGroup, tempVar);

                vdsGroupCpuName = sc.getCpuName();
            }
        }

        List<String> missingFlags = CpuFlagsManagerHandler.missingServerCpuFlags(vdsGroupCpuName, getVds()
                .getcpu_flags(), getVds().getvds_group_compatibility_version());
        if (!StringUtils.isEmpty(getVds().getcpu_flags()) && (!foundCPU || missingFlags != null)) {
            if (missingFlags != null) {
                AddCustomValue("CpuFlags", StringUtils.join(missingFlags, ", "));
                if (missingFlags.contains("nx")) {
                    AuditLogableBase logable = new AuditLogableBase(getVds().getId());
                    AuditLogDirector.log(logable, AuditLogType.CPU_FLAGS_NX_IS_MISSING);
                }
            }

            SetNonOperationalVdsParameters tempVar2 = new SetNonOperationalVdsParameters(getVdsId(),
                    NonOperationalReason.CPU_TYPE_INCOMPATIBLE_WITH_CLUSTER);
            tempVar2.setSaveToDb(true);
            Backend.getInstance().runInternalAction(VdcActionType.SetNonOperationalVds, tempVar2,  ExecutionHandler.createInternalJobContext());
        } else {
            // if no need to change to non operational then dont log the command
            setCommandShouldBeLogged(false);
        }
        setSucceeded(true);
    }

    @Override
    public AuditLogType getAuditLogTypeValue() {
        // check first if no flags and then if succeeded
        return (!_hasFlags) ? AuditLogType.VDS_CPU_RETRIEVE_FAILED : AuditLogType.VDS_CPU_LOWER_THAN_CLUSTER;
    }

    private static Log log = LogFactory.getLog(HandleVdsCpuFlagsOrClusterChangedCommand.class);
}

@@ -992,103 +992,40 @@
 se;%0A
-import org.ovirt.engine.core.utils.log.Log;%0Aimport org.ovirt.engine.core.utils.log.LogFactory;%0A
+%0A@NonTransactiveCommandAttribute
 %0Apub
@@ -1130,16 +1130,127 @@
 nd%3CT%3E %7B%0A
+%0A    private boolean _hasFlags = true;%0A    private static final long serialVersionUID = 8734106870574677571L;%0A%0A
     publ
@@ -1603,47 +1603,8 @@
  %7D%0A%0A
-    private boolean _hasFlags = true;%0A%0A
     
@@ -1778,16 +1778,17 @@
 er doesn
+'
 t have c
@@ -4403,16 +4403,17 @@
 then don
+'
 t log th
@@ -4751,104 +4751,7 @@
    %7D
-%0A%0A    private static Log log = LogFactory.getLog(HandleVdsCpuFlagsOrClusterChangedCommand.class);
 %0A%7D%0A

Clean up of HandleVdsCpuFlagsOrClusterChangedCommand