package org.ovirt.engine.core.bll.scheduling.policyunits;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.CpuFlagsManagerHandler;
import org.ovirt.engine.core.bll.scheduling.PolicyUnitImpl;
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.common.scheduling.PolicyUnit;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CpuLevelFilterPolicyUnit extends PolicyUnitImpl {
    public CpuLevelFilterPolicyUnit(PolicyUnit policyUnit) {
        super(policyUnit);
    }

    @Override
    public List<VDS> filter(List<VDS> hosts, VM vm, Map<String, String> parameters, List<String> messages) {
        boolean filteredOutHosts = false;
        if (StringUtils.isNotEmpty(vm.getCpuName())) {
            List<VDS> hostsToRunOn = new ArrayList<VDS>();
            for (VDS host : hosts) {
                String hostCpuName = CpuFlagsManagerHandler.FindMaxServerCpuByFlags(host.getCpuFlags(), host.getVdsGroupCompatibilityVersion()).getCpuName();
                if (StringUtils.isNotEmpty(hostCpuName)) {
                    int compareResult = CpuFlagsManagerHandler.compareCpuLevels(vm.getCpuName(), hostCpuName, vm.getVdsGroupCompatibilityVersion());
                    if (compareResult <= 0) {
                        hostsToRunOn.add(host);
                        log.debugFormat("Host {0} wasn't filtered out as it has a CPU level ({1}) which is higher or equal than the CPU level the VM was run with ({2})",
                                host.getName(),
                                hostCpuName,
                                vm.getCpuName());
                    } else {
                        log.debugFormat("Host {0} was filtered out as it has a CPU level ({1}) which is lower than the CPU level the VM was run with ({2})",
                                host.getName(),
                                hostCpuName,
                                vm.getCpuName());
                        filteredOutHosts = true;
                    }
                }
            }

            if (filteredOutHosts) {
                messages.add(VdcBllMessages.ACTION_TYPE_FAILED_VDS_VM_CPU_LEVEL.toString());
            }

            return hostsToRunOn;
        } else {
            return hosts;
        }
    }
}

@@ -213,16 +213,80 @@
 itImpl;%0A
+import org.ovirt.engine.core.common.businessentities.ServerCpu;%0A
 import o
@@ -1080,25 +1080,20 @@
    S
-tring hostCpuName
+erverCpu cpu
  = C
@@ -1197,16 +1197,79 @@
 rsion())
+;%0A                String hostCpuName = cpu == null ? null : cpu
 .getCpuN

Fix NULL_RETURNS error in CpuLevelFilterPolicyUnit