Program: 106190
Project: 106190
Buggy source:
@Override @DB public long getCommandHostDelegation(long hostId,Command cmd){
  boolean needDelegation=false;
  if (cmd instanceof PrimaryStorageDownloadCommand || cmd instanceof BackupSnapshotCommand || cmd instanceof CreatePrivateTemplateFromVolumeCommand|| cmd instanceof CreatePrivateTemplateFromSnapshotCommand|| cmd instanceof CopyVolumeCommand|| cmd instanceof CreateVolumeOVACommand|| cmd instanceof PrepareOVAPackingCommand|| cmd instanceof CreateVolumeFromSnapshotCommand|| cmd instanceof CopyCommand) {
    if (cmd instanceof CopyCommand) {
      CopyCommand cpyCommand=(CopyCommand)cmd;
      DataTO srcData=cpyCommand.getSrcTO();
      DataStoreTO srcStoreTO=srcData.getDataStore();
      DataTO destData=cpyCommand.getDestTO();
      DataStoreTO destStoreTO=destData.getDataStore();
      if (destData.getObjectType() == DataObjectType.VOLUME && destStoreTO.getRole() == DataStoreRole.Primary && srcData.getObjectType() == DataObjectType.TEMPLATE && srcStoreTO.getRole() == DataStoreRole.Primary) {
        needDelegation=false;
      }
 else {
        needDelegation=true;
      }
    }
 else {
      needDelegation=true;
    }
  }
  if (cmd instanceof PrepareOVAPackingCommand || cmd instanceof CreateVolumeOVACommand) {
    cmd.setContextParam("String_Node_Str",HypervisorType.VMware.toString());
  }
  if (needDelegation) {
    HostVO host=_hostDao.findById(hostId);
    assert(host != null);
    assert(host.getHypervisorType() == HypervisorType.VMware);
    long dcId=host.getDataCenterId();
    Pair<HostVO,SecondaryStorageVmVO> cmdTarget=_secStorageMgr.assignSecStorageVm(dcId,cmd);
    if (cmdTarget != null) {
      cmd.setContextParam("String_Node_Str",HypervisorType.VMware.toString());
      Map<String,String> hostDetails=_hostDetailsDao.findDetails(hostId);
      cmd.setContextParam("String_Node_Str",resolveNameInGuid(hostDetails.get("String_Node_Str")));
      cmd.setContextParam("String_Node_Str",hostDetails.get("String_Node_Str"));
      cmd.setContextParam("String_Node_Str",hostDetails.get("String_Node_Str"));
      cmd.setContextParam("String_Node_Str",_vmwareMgr.getServiceConsolePortGroupName());
      cmd.setContextParam("String_Node_Str",_vmwareMgr.getManagementPortGroupName());
      CommandExecLogVO execLog=new CommandExecLogVO(cmdTarget.first().getId(),cmdTarget.second().getId(),cmd.getClass().getSimpleName(),1);
      _cmdExecLogDao.persist(execLog);
      cmd.setContextParam("String_Node_Str",String.valueOf(execLog.getId()));
      if (cmd instanceof BackupSnapshotCommand || cmd instanceof CreatePrivateTemplateFromVolumeCommand || cmd instanceof CreatePrivateTemplateFromSnapshotCommand|| cmd instanceof CopyVolumeCommand|| cmd instanceof CopyCommand|| cmd instanceof CreateVolumeOVACommand|| cmd instanceof PrepareOVAPackingCommand|| cmd instanceof CreateVolumeFromSnapshotCommand) {
        String workerName=_vmwareMgr.composeWorkerName();
        long checkPointId=1;
        cmd.setContextParam("String_Node_Str",workerName);
        cmd.setContextParam("String_Node_Str",String.valueOf(checkPointId));
        String workerName2=_vmwareMgr.composeWorkerName();
        long checkPointId2=1;
        cmd.setContextParam("String_Node_Str",workerName2);
        cmd.setContextParam("String_Node_Str",String.valueOf(checkPointId2));
      }
      return cmdTarget.first().getId();
    }
  }
  return hostId;
}

Fixed version (ground truth):
@Override @DB public Pair<Boolean,Long> getCommandHostDelegation(long hostId,Command cmd){
  boolean needDelegation=false;
  if (cmd instanceof CopyCommand) {
    CopyCommand cpyCommand=(CopyCommand)cmd;
    DataTO srcData=cpyCommand.getSrcTO();
    DataStoreTO srcStoreTO=srcData.getDataStore();
    DataTO destData=cpyCommand.getDestTO();
    DataStoreTO destStoreTO=destData.getDataStore();
    if (!(HypervisorType.VMware == srcData.getHypervisorType() || HypervisorType.VMware == destData.getHypervisorType())) {
      return new Pair<Boolean,Long>(Boolean.FALSE,new Long(hostId));
    }
    if (destData.getObjectType() == DataObjectType.VOLUME && destStoreTO.getRole() == DataStoreRole.Primary && srcData.getObjectType() == DataObjectType.TEMPLATE && srcStoreTO.getRole() == DataStoreRole.Primary) {
      needDelegation=false;
    }
 else {
      needDelegation=true;
    }
  }
  if (!needDelegation) {
    return new Pair<Boolean,Long>(Boolean.FALSE,new Long(hostId));
  }
  HostVO host=_hostDao.findById(hostId);
  long dcId=host.getDataCenterId();
  Pair<HostVO,SecondaryStorageVmVO> cmdTarget=_secStorageMgr.assignSecStorageVm(dcId,cmd);
  if (cmdTarget != null) {
    cmd.setContextParam("String_Node_Str",HypervisorType.VMware.toString());
    if (host.getType() == Host.Type.Routing) {
      Map<String,String> hostDetails=_hostDetailsDao.findDetails(hostId);
      cmd.setContextParam("String_Node_Str",resolveNameInGuid(hostDetails.get("String_Node_Str")));
      cmd.setContextParam("String_Node_Str",hostDetails.get("String_Node_Str"));
      cmd.setContextParam("String_Node_Str",hostDetails.get("String_Node_Str"));
      cmd.setContextParam("String_Node_Str",_vmwareMgr.getServiceConsolePortGroupName());
      cmd.setContextParam("String_Node_Str",_vmwareMgr.getManagementPortGroupName());
    }
    CommandExecLogVO execLog=new CommandExecLogVO(cmdTarget.first().getId(),cmdTarget.second().getId(),cmd.getClass().getSimpleName(),1);
    _cmdExecLogDao.persist(execLog);
    cmd.setContextParam("String_Node_Str",String.valueOf(execLog.getId()));
    if (cmd instanceof BackupSnapshotCommand || cmd instanceof CreatePrivateTemplateFromVolumeCommand || cmd instanceof CreatePrivateTemplateFromSnapshotCommand|| cmd instanceof CopyVolumeCommand|| cmd instanceof CopyCommand|| cmd instanceof CreateVolumeOVACommand|| cmd instanceof PrepareOVAPackingCommand|| cmd instanceof CreateVolumeFromSnapshotCommand) {
      String workerName=_vmwareMgr.composeWorkerName();
      long checkPointId=1;
      cmd.setContextParam("String_Node_Str",workerName);
      cmd.setContextParam("String_Node_Str",String.valueOf(checkPointId));
      String workerName2=_vmwareMgr.composeWorkerName();
      long checkPointId2=1;
      cmd.setContextParam("String_Node_Str",workerName2);
      cmd.setContextParam("String_Node_Str",String.valueOf(checkPointId2));
    }
    return new Pair<Boolean,Long>(Boolean.TRUE,cmdTarget.first().getId());
  }
  return new Pair<Boolean,Long>(Boolean.FALSE,new Long(hostId));
}