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

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

import org.ovirt.engine.core.bll.scheduling.PolicyUnitImpl;
import org.ovirt.engine.core.common.businessentities.MigrationSupport;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.scheduling.PolicyUnit;

public class PinToHostPolicyUnit extends PolicyUnitImpl {

    public PinToHostPolicyUnit(PolicyUnit policyUnit) {
        super(policyUnit);
    }

    @Override
    public List<VDS> filter(List<VDS> hosts, VM vm, Map<String, String> parameters, List<String> messages) {
        if (vm.getMigrationSupport() == MigrationSupport.PINNED_TO_HOST) {
            for (VDS host : hosts) {
                if (host.getId().equals(vm.getDedicatedVmForVds())) {
                    return Arrays.asList(host);
                }
            }

            // if flow reaches here, the VM is pinned but there is no dedicated host.
            return new ArrayList<>();
        }

        return hosts;
    }
}

@@ -800,24 +800,136 @@
 _TO_HOST) %7B%0A
+            // host has been specified for pin to host.%0A            if(vm.getDedicatedVmForVds() != null) %7B%0A    
             
@@ -949,24 +949,28 @@
  : hosts) %7B%0A
+    
             
@@ -1043,24 +1043,28 @@
             
+    
 return Array
@@ -1075,24 +1075,250 @@
 List(host);%0A
+                    %7D%0A                %7D%0A            %7D else %7B%0A                // check pin to any (the VM should be down/ no migration allowed).%0A                if (vm.getRunOnVds() == null) %7B%0A                    return hosts;%0A
             

support pin-to-any