Program: 89656
Project: 89656
Buggy source:
@Override public void handleTemplateSync(HostVO ssHost){
  Long sserverId=ssHost.getId();
  if (ssHost == null) {
    s_logger.warn("String_Node_Str" + sserverId + "String_Node_Str");
    return;
  }
  if (ssHost.getType() != Host.Type.SecondaryStorage) {
    s_logger.warn("String_Node_Str" + sserverId + "String_Node_Str");
    return;
  }
  Map<String,TemplateInfo> templateInfos=listTemplate(ssHost);
  if (templateInfos == null) {
    return;
  }
  long zoneId=ssHost.getDataCenterId();
  Set<VMTemplateVO> toBeDownloaded=new HashSet<VMTemplateVO>();
  List<VMTemplateVO> allTemplates=_templateDao.listAllInZone(ssHost.getDataCenterId());
  List<VMTemplateVO> rtngTmplts=_templateDao.listAllSystemVMTemplates();
  List<VMTemplateVO> defaultBuiltin=_templateDao.listDefaultBuiltinTemplates();
  if (rtngTmplts != null) {
    for (    VMTemplateVO rtngTmplt : rtngTmplts) {
      if (!allTemplates.contains(rtngTmplt)) {
        allTemplates.add(rtngTmplt);
      }
    }
  }
  if (defaultBuiltin != null) {
    for (    VMTemplateVO builtinTmplt : defaultBuiltin) {
      if (!allTemplates.contains(builtinTmplt)) {
        allTemplates.add(builtinTmplt);
      }
    }
  }
  toBeDownloaded.addAll(allTemplates);
  for (  VMTemplateVO tmplt : allTemplates) {
    String uniqueName=tmplt.getUniqueName();
    VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(sserverId,tmplt.getId());
    if (templateInfos.containsKey(uniqueName)) {
      TemplateInfo tmpltInfo=templateInfos.remove(uniqueName);
      toBeDownloaded.remove(tmplt);
      if (tmpltHost != null) {
        s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str");
        if (tmpltHost.getDownloadState() != Status.DOWNLOADED) {
          tmpltHost.setErrorString("String_Node_Str");
        }
        if (tmpltInfo.isCorrupted()) {
          tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
          tmpltHost.setErrorString("String_Node_Str");
          toBeDownloaded.add(tmplt);
          s_logger.info("String_Node_Str" + tmplt + "String_Node_Str");
          if (tmplt.getUrl() == null) {
            String msg="String_Node_Str" + tmplt + "String_Node_Str"+ tmpltInfo.getInstallPath()+ "String_Node_Str"+ tmpltHost.getHostId();
            s_logger.warn(msg);
          }
 else {
            toBeDownloaded.add(tmplt);
          }
        }
 else {
          tmpltHost.setDownloadPercent(100);
          tmpltHost.setDownloadState(Status.DOWNLOADED);
          tmpltHost.setInstallPath(tmpltInfo.getInstallPath());
          tmpltHost.setSize(tmpltInfo.getSize());
          tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
          tmpltHost.setLastUpdated(new Date());
        }
        _vmTemplateHostDao.update(tmpltHost.getId(),tmpltHost);
      }
 else {
        tmpltHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),100,Status.DOWNLOADED,null,null,null,tmpltInfo.getInstallPath(),tmplt.getUrl());
        tmpltHost.setSize(tmpltInfo.getSize());
        tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
        _vmTemplateHostDao.persist(tmpltHost);
        VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
        if (tmpltZoneVO == null) {
          tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
          _vmTemplateZoneDao.persist(tmpltZoneVO);
        }
 else {
          tmpltZoneVO.setLastUpdated(new Date());
          _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
        }
      }
      continue;
    }
    if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
    }
 else     if (tmpltHost == null) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
      VMTemplateHostVO templtHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),0,Status.NOT_DOWNLOADED,null,null,null,null,tmplt.getUrl());
      _vmTemplateHostDao.persist(templtHost);
      VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
      if (tmpltZoneVO == null) {
        tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
        _vmTemplateZoneDao.persist(tmpltZoneVO);
      }
 else {
        tmpltZoneVO.setLastUpdated(new Date());
        _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
      }
    }
  }
  if (toBeDownloaded.size() > 0) {
    List<HypervisorType> availHypers=_clusterDao.getAvailableHypervisorInZone(ssHost.getDataCenterId());
    availHypers.remove(HypervisorType.BareMetal);
    availHypers.add(HypervisorType.None);
    for (    VMTemplateVO tmplt : toBeDownloaded) {
      if (tmplt.getUrl() == null) {
        VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(ssHost.getId(),tmplt.getId());
        if (tmpltHost != null) {
          tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
          tmpltHost.setDownloadPercent(0);
          tmpltHost.setErrorString("String_Node_Str");
          _vmTemplateHostDao.update(tmpltHost.getId(),tmpltHost);
        }
        continue;
      }
      if (availHypers.contains(tmplt.getHypervisorType())) {
        s_logger.debug("String_Node_Str" + tmplt.getName() + "String_Node_Str"+ ssHost.getName());
        downloadTemplateToStorage(tmplt,ssHost);
      }
    }
  }
  for (  String uniqueName : templateInfos.keySet()) {
    TemplateInfo tInfo=templateInfos.get(uniqueName);
    DeleteTemplateCommand dtCommand=new DeleteTemplateCommand(ssHost.getStorageUrl(),tInfo.getInstallPath());
    long result=_agentMgr.sendToSecStorage(ssHost,dtCommand,null);
    if (result == -1) {
      String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str";
      s_logger.error(description);
      return;
    }
    String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str"+ result;
    s_logger.info(description);
  }
  checksumSync(sserverId);
}

Fixed version (ground truth):
@Override public void handleTemplateSync(HostVO ssHost){
  Long sserverId=ssHost.getId();
  if (ssHost == null) {
    s_logger.warn("String_Node_Str" + sserverId + "String_Node_Str");
    return;
  }
  if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
    s_logger.warn("String_Node_Str" + sserverId + "String_Node_Str");
    return;
  }
  Map<String,TemplateInfo> templateInfos=listTemplate(ssHost);
  if (templateInfos == null) {
    return;
  }
  long zoneId=ssHost.getDataCenterId();
  Set<VMTemplateVO> toBeDownloaded=new HashSet<VMTemplateVO>();
  List<VMTemplateVO> allTemplates=_templateDao.listAllInZone(ssHost.getDataCenterId());
  List<VMTemplateVO> rtngTmplts=_templateDao.listAllSystemVMTemplates();
  List<VMTemplateVO> defaultBuiltin=_templateDao.listDefaultBuiltinTemplates();
  if (rtngTmplts != null) {
    for (    VMTemplateVO rtngTmplt : rtngTmplts) {
      if (!allTemplates.contains(rtngTmplt)) {
        allTemplates.add(rtngTmplt);
      }
    }
  }
  if (defaultBuiltin != null) {
    for (    VMTemplateVO builtinTmplt : defaultBuiltin) {
      if (!allTemplates.contains(builtinTmplt)) {
        allTemplates.add(builtinTmplt);
      }
    }
  }
  toBeDownloaded.addAll(allTemplates);
  for (  VMTemplateVO tmplt : allTemplates) {
    String uniqueName=tmplt.getUniqueName();
    VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(sserverId,tmplt.getId());
    if (templateInfos.containsKey(uniqueName)) {
      TemplateInfo tmpltInfo=templateInfos.remove(uniqueName);
      toBeDownloaded.remove(tmplt);
      if (tmpltHost != null) {
        s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str");
        if (tmpltHost.getDownloadState() != Status.DOWNLOADED) {
          tmpltHost.setErrorString("String_Node_Str");
        }
        if (tmpltInfo.isCorrupted()) {
          tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
          tmpltHost.setErrorString("String_Node_Str");
          toBeDownloaded.add(tmplt);
          s_logger.info("String_Node_Str" + tmplt + "String_Node_Str");
          if (tmplt.getUrl() == null) {
            String msg="String_Node_Str" + tmplt + "String_Node_Str"+ tmpltInfo.getInstallPath()+ "String_Node_Str"+ tmpltHost.getHostId();
            s_logger.warn(msg);
          }
 else {
            toBeDownloaded.add(tmplt);
          }
        }
 else {
          tmpltHost.setDownloadPercent(100);
          tmpltHost.setDownloadState(Status.DOWNLOADED);
          tmpltHost.setInstallPath(tmpltInfo.getInstallPath());
          tmpltHost.setSize(tmpltInfo.getSize());
          tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
          tmpltHost.setLastUpdated(new Date());
        }
        _vmTemplateHostDao.update(tmpltHost.getId(),tmpltHost);
      }
 else {
        tmpltHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),100,Status.DOWNLOADED,null,null,null,tmpltInfo.getInstallPath(),tmplt.getUrl());
        tmpltHost.setSize(tmpltInfo.getSize());
        tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
        _vmTemplateHostDao.persist(tmpltHost);
        VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
        if (tmpltZoneVO == null) {
          tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
          _vmTemplateZoneDao.persist(tmpltZoneVO);
        }
 else {
          tmpltZoneVO.setLastUpdated(new Date());
          _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
        }
      }
      continue;
    }
    if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
    }
 else     if (tmpltHost == null) {
      s_logger.info("String_Node_Str" + uniqueName + "String_Node_Str"+ sserverId+ "String_Node_Str");
      VMTemplateHostVO templtHost=new VMTemplateHostVO(sserverId,tmplt.getId(),new Date(),0,Status.NOT_DOWNLOADED,null,null,null,null,tmplt.getUrl());
      _vmTemplateHostDao.persist(templtHost);
      VMTemplateZoneVO tmpltZoneVO=_vmTemplateZoneDao.findByZoneTemplate(zoneId,tmplt.getId());
      if (tmpltZoneVO == null) {
        tmpltZoneVO=new VMTemplateZoneVO(zoneId,tmplt.getId(),new Date());
        _vmTemplateZoneDao.persist(tmpltZoneVO);
      }
 else {
        tmpltZoneVO.setLastUpdated(new Date());
        _vmTemplateZoneDao.update(tmpltZoneVO.getId(),tmpltZoneVO);
      }
    }
  }
  if (toBeDownloaded.size() > 0) {
    List<HypervisorType> availHypers=_clusterDao.getAvailableHypervisorInZone(ssHost.getDataCenterId());
    availHypers.remove(HypervisorType.BareMetal);
    availHypers.add(HypervisorType.None);
    for (    VMTemplateVO tmplt : toBeDownloaded) {
      if (tmplt.getUrl() == null) {
        VMTemplateHostVO tmpltHost=_vmTemplateHostDao.findByHostTemplate(ssHost.getId(),tmplt.getId());
        if (tmpltHost != null) {
          tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
          tmpltHost.setDownloadPercent(0);
          tmpltHost.setErrorString("String_Node_Str");
          _vmTemplateHostDao.update(tmpltHost.getId(),tmpltHost);
        }
        continue;
      }
      if (availHypers.contains(tmplt.getHypervisorType())) {
        s_logger.debug("String_Node_Str" + tmplt.getName() + "String_Node_Str"+ ssHost.getName());
        downloadTemplateToStorage(tmplt,ssHost);
      }
    }
  }
  for (  String uniqueName : templateInfos.keySet()) {
    TemplateInfo tInfo=templateInfos.get(uniqueName);
    DeleteTemplateCommand dtCommand=new DeleteTemplateCommand(ssHost.getStorageUrl(),tInfo.getInstallPath());
    long result=_agentMgr.sendToSecStorage(ssHost,dtCommand,null);
    if (result == -1) {
      String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str";
      s_logger.error(description);
      return;
    }
    String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ sserverId+ "String_Node_Str"+ result;
    s_logger.info(description);
  }
  if (ssHost.getType() == Host.Type.SecondaryStorage)   checksumSync(sserverId);
}