Program: 105853
Project: 105853
Buggy source:
/** 
 * This method will process the affinity group of type 'Explicit Dedication' for a deployment of a VM that demands dedicated resources. For ExplicitDedicationProcessor we need to add dedicated resources into the IncludeList based on the level we have dedicated resources available. For eg. if admin dedicates a pod to a domain, then all the user in that domain can use the resources of that pod. We need to take care of the situation when dedicated resources further have resources dedicated to sub-domain/account. This IncludeList is then used to update the avoid list for a given data center.
 */
@Override public void process(VirtualMachineProfile vmProfile,DeploymentPlan plan,ExcludeList avoid) throws AffinityConflictException {
  VirtualMachine vm=vmProfile.getVirtualMachine();
  List<AffinityGroupVMMapVO> vmGroupMappings=_affinityGroupVMMapDao.findByVmIdType(vm.getId(),getType());
  DataCenter dc=_dcDao.findById(vm.getDataCenterId());
  List<DedicatedResourceVO> resourceList=new ArrayList<DedicatedResourceVO>();
  if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) {
    for (    AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
      if (vmGroupMapping != null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + vmGroupMapping.getAffinityGroupId() + "String_Node_Str"+ vm.getId());
        }
        long affinityGroupId=vmGroupMapping.getAffinityGroupId();
        List<DedicatedResourceVO> dr=_dedicatedDao.listByAffinityGroupId(affinityGroupId);
        resourceList.addAll(dr);
      }
    }
    boolean canUse=false;
    if (plan.getHostId() != null) {
      HostVO host=_hostDao.findById(plan.getHostId());
      ClusterVO clusterofHost=_clusterDao.findById(host.getClusterId());
      HostPodVO podOfHost=_podDao.findById(host.getPodId());
      DataCenterVO zoneOfHost=_dcDao.findById(host.getDataCenterId());
      if (resourceList != null && resourceList.size() != 0) {
        for (        DedicatedResourceVO resource : resourceList) {
          if ((resource.getHostId() != null && resource.getHostId() == plan.getHostId()) || (resource.getClusterId() != null && resource.getClusterId() == clusterofHost.getId()) || (resource.getPodId() != null && resource.getPodId() == podOfHost.getId())|| (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfHost.getId())) {
            canUse=true;
          }
        }
      }
      if (!canUse) {
        throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str");
      }
    }
 else     if (plan.getClusterId() != null) {
      ClusterVO cluster=_clusterDao.findById(plan.getClusterId());
      HostPodVO podOfCluster=_podDao.findById(cluster.getPodId());
      DataCenterVO zoneOfCluster=_dcDao.findById(cluster.getDataCenterId());
      List<HostVO> hostToUse=new ArrayList<HostVO>();
      if (resourceList != null && resourceList.size() != 0) {
        for (        DedicatedResourceVO resource : resourceList) {
          if ((resource.getClusterId() != null && resource.getClusterId() == cluster.getId()) || (resource.getPodId() != null && resource.getPodId() == podOfCluster.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfCluster.getId())) {
            canUse=true;
          }
          if (!canUse) {
            if (resource.getHostId() != null) {
              HostVO dHost=_hostDao.findById(resource.getHostId());
              if (dHost.getClusterId() == cluster.getId()) {
                hostToUse.add(dHost);
              }
            }
          }
        }
      }
      if (hostToUse.isEmpty() && !canUse) {
        throw new CloudRuntimeException("String_Node_Str" + cluster.getName() + "String_Node_Str");
      }
      if (hostToUse != null && hostToUse.size() != 0) {
        List<HostVO> hostList=_hostDao.findByClusterId(cluster.getId());
        for (        HostVO host : hostList) {
          if (!hostToUse.contains(host)) {
            avoid.addHost(host.getId());
          }
        }
      }
    }
 else     if (plan.getPodId() != null) {
      HostPodVO pod=_podDao.findById(plan.getPodId());
      DataCenterVO zoneOfPod=_dcDao.findById(pod.getDataCenterId());
      List<ClusterVO> clustersToUse=new ArrayList<ClusterVO>();
      List<HostVO> hostsToUse=new ArrayList<HostVO>();
      if (resourceList != null && resourceList.size() != 0) {
        for (        DedicatedResourceVO resource : resourceList) {
          if ((resource.getPodId() != null && resource.getPodId() == pod.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfPod.getId())) {
            canUse=true;
          }
          if (!canUse) {
            if (resource.getClusterId() != null) {
              ClusterVO dCluster=_clusterDao.findById(resource.getClusterId());
              if (dCluster.getPodId() == pod.getId()) {
                clustersToUse.add(dCluster);
              }
            }
            if (resource.getHostId() != null) {
              HostVO dHost=_hostDao.findById(resource.getHostId());
              if (dHost.getPodId() == pod.getId()) {
                hostsToUse.add(dHost);
              }
            }
          }
        }
      }
      if (hostsToUse.isEmpty() && clustersToUse.isEmpty() && !canUse) {
        throw new CloudRuntimeException("String_Node_Str" + pod.getName() + "String_Node_Str");
      }
      if (clustersToUse != null && clustersToUse.size() != 0) {
        List<ClusterVO> clusterList=_clusterDao.listByPodId(pod.getId());
        for (        ClusterVO cluster : clusterList) {
          if (!clustersToUse.contains(cluster)) {
            avoid.addCluster(cluster.getId());
          }
        }
      }
      if (hostsToUse != null && hostsToUse.size() != 0) {
        List<HostVO> hostList=_hostDao.findByPodId(pod.getId());
        for (        HostVO host : hostList) {
          if (!hostsToUse.contains(host)) {
            avoid.addHost(host.getId());
          }
        }
      }
    }
 else {
      if (resourceList != null && resourceList.size() != 0) {
        avoid=updateAvoidList(resourceList,avoid,dc);
      }
 else {
        avoid.addDataCenter(dc.getId());
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str");
        }
      }
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + avoid.getPodsToAvoid() + "String_Node_Str"+ avoid.getClustersToAvoid()+ "String_Node_Str"+ avoid.getHostsToAvoid());
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * This method will process the affinity group of type 'Explicit Dedication' for a deployment of a VM that demands dedicated resources. For ExplicitDedicationProcessor we need to add dedicated resources into the IncludeList based on the level we have dedicated resources available. For eg. if admin dedicates a pod to a domain, then all the user in that domain can use the resources of that pod. We need to take care of the situation when dedicated resources further have resources dedicated to sub-domain/account. This IncludeList is then used to update the avoid list for a given data center.
 */
@Override public void process(VirtualMachineProfile vmProfile,DeploymentPlan plan,ExcludeList avoid) throws AffinityConflictException {
  VirtualMachine vm=vmProfile.getVirtualMachine();
  List<AffinityGroupVMMapVO> vmGroupMappings=_affinityGroupVMMapDao.findByVmIdType(vm.getId(),getType());
  DataCenter dc=_dcDao.findById(vm.getDataCenterId());
  List<DedicatedResourceVO> resourceList=new ArrayList<DedicatedResourceVO>();
  if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) {
    for (    AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
      if (vmGroupMapping != null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + vmGroupMapping.getAffinityGroupId() + "String_Node_Str"+ vm.getId());
        }
        long affinityGroupId=vmGroupMapping.getAffinityGroupId();
        List<DedicatedResourceVO> dr=_dedicatedDao.listByAffinityGroupId(affinityGroupId);
        resourceList.addAll(dr);
      }
    }
    boolean canUse=false;
    if (plan.getHostId() != null) {
      HostVO host=_hostDao.findById(plan.getHostId());
      ClusterVO clusterofHost=_clusterDao.findById(host.getClusterId());
      HostPodVO podOfHost=_podDao.findById(host.getPodId());
      DataCenterVO zoneOfHost=_dcDao.findById(host.getDataCenterId());
      if (resourceList != null && resourceList.size() != 0) {
        for (        DedicatedResourceVO resource : resourceList) {
          if ((resource.getHostId() != null && resource.getHostId().longValue() == plan.getHostId().longValue()) || (resource.getClusterId() != null && resource.getClusterId().longValue() == clusterofHost.getId()) || (resource.getPodId() != null && resource.getPodId().longValue() == podOfHost.getId())|| (resource.getDataCenterId() != null && resource.getDataCenterId().longValue() == zoneOfHost.getId())) {
            canUse=true;
          }
        }
      }
      if (!canUse) {
        throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str");
      }
    }
 else     if (plan.getClusterId() != null) {
      ClusterVO cluster=_clusterDao.findById(plan.getClusterId());
      HostPodVO podOfCluster=_podDao.findById(cluster.getPodId());
      DataCenterVO zoneOfCluster=_dcDao.findById(cluster.getDataCenterId());
      List<HostVO> hostToUse=new ArrayList<HostVO>();
      if (resourceList != null && resourceList.size() != 0) {
        for (        DedicatedResourceVO resource : resourceList) {
          if ((resource.getClusterId() != null && resource.getClusterId() == cluster.getId()) || (resource.getPodId() != null && resource.getPodId() == podOfCluster.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfCluster.getId())) {
            canUse=true;
          }
          if (!canUse) {
            if (resource.getHostId() != null) {
              HostVO dHost=_hostDao.findById(resource.getHostId());
              if (dHost.getClusterId() == cluster.getId()) {
                hostToUse.add(dHost);
              }
            }
          }
        }
      }
      if (hostToUse.isEmpty() && !canUse) {
        throw new CloudRuntimeException("String_Node_Str" + cluster.getName() + "String_Node_Str");
      }
      if (hostToUse != null && hostToUse.size() != 0) {
        List<HostVO> hostList=_hostDao.findByClusterId(cluster.getId());
        for (        HostVO host : hostList) {
          if (!hostToUse.contains(host)) {
            avoid.addHost(host.getId());
          }
        }
      }
    }
 else     if (plan.getPodId() != null) {
      HostPodVO pod=_podDao.findById(plan.getPodId());
      DataCenterVO zoneOfPod=_dcDao.findById(pod.getDataCenterId());
      List<ClusterVO> clustersToUse=new ArrayList<ClusterVO>();
      List<HostVO> hostsToUse=new ArrayList<HostVO>();
      if (resourceList != null && resourceList.size() != 0) {
        for (        DedicatedResourceVO resource : resourceList) {
          if ((resource.getPodId() != null && resource.getPodId() == pod.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfPod.getId())) {
            canUse=true;
          }
          if (!canUse) {
            if (resource.getClusterId() != null) {
              ClusterVO dCluster=_clusterDao.findById(resource.getClusterId());
              if (dCluster.getPodId() == pod.getId()) {
                clustersToUse.add(dCluster);
              }
            }
            if (resource.getHostId() != null) {
              HostVO dHost=_hostDao.findById(resource.getHostId());
              if (dHost.getPodId() == pod.getId()) {
                hostsToUse.add(dHost);
              }
            }
          }
        }
      }
      if (hostsToUse.isEmpty() && clustersToUse.isEmpty() && !canUse) {
        throw new CloudRuntimeException("String_Node_Str" + pod.getName() + "String_Node_Str");
      }
      if (clustersToUse != null && clustersToUse.size() != 0) {
        List<ClusterVO> clusterList=_clusterDao.listByPodId(pod.getId());
        for (        ClusterVO cluster : clusterList) {
          if (!clustersToUse.contains(cluster)) {
            avoid.addCluster(cluster.getId());
          }
        }
      }
      if (hostsToUse != null && hostsToUse.size() != 0) {
        List<HostVO> hostList=_hostDao.findByPodId(pod.getId());
        for (        HostVO host : hostList) {
          if (!hostsToUse.contains(host)) {
            avoid.addHost(host.getId());
          }
        }
      }
    }
 else {
      if (resourceList != null && resourceList.size() != 0) {
        avoid=updateAvoidList(resourceList,avoid,dc);
      }
 else {
        avoid.addDataCenter(dc.getId());
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str");
        }
      }
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + avoid.getPodsToAvoid() + "String_Node_Str"+ avoid.getClustersToAvoid()+ "String_Node_Str"+ avoid.getHostsToAvoid());
      }
    }
  }
}