Program: 105261
Project: 105261
Buggy source:
private boolean collectDirectNetworkUsage(final HostVO host){
  s_logger.debug("String_Node_Str");
  final long zoneId=host.getDataCenterId();
  final DetailVO lastCollectDetail=_detailsDao.findDetail(host.getId(),"String_Node_Str");
  if (lastCollectDetail == null) {
    s_logger.warn("String_Node_Str" + host.getId());
    return false;
  }
  Date lastCollection=new Date(new Long(lastCollectDetail.getValue()));
  List<IPAddressVO> allocatedIps=listAllocatedDirectIps(zoneId);
  Calendar rightNow=Calendar.getInstance();
  rightNow.add(Calendar.HOUR_OF_DAY,-2);
  final Date now=rightNow.getTime();
  if (lastCollection.after(now)) {
    s_logger.debug("String_Node_Str" + lastCollection.toString() + "String_Node_Str");
    return false;
  }
  List<UsageEventVO> IpEvents=_eventDao.listDirectIpEvents(lastCollection,now,zoneId);
  Map<String,Date> ipAssigment=new HashMap<String,Date>();
  List<UsageIPAddressVO> IpPartialUsage=new ArrayList<UsageIPAddressVO>();
  List<UsageIPAddressVO> fullDurationIpUsage=new ArrayList<UsageIPAddressVO>();
  for (  UsageEventVO IpEvent : IpEvents) {
    String address=IpEvent.getResourceName();
    if (EventTypes.EVENT_NET_IP_ASSIGN.equals(IpEvent.getType())) {
      ipAssigment.put(address,IpEvent.getCreateDate());
    }
 else     if (EventTypes.EVENT_NET_IP_RELEASE.equals(IpEvent.getType())) {
      if (ipAssigment.containsKey(address)) {
        Date assigned=ipAssigment.get(address);
        ipAssigment.remove(address);
        IpPartialUsage.add(new UsageIPAddressVO(IpEvent.getAccountId(),address,assigned,IpEvent.getCreateDate()));
      }
 else {
        IpPartialUsage.add(new UsageIPAddressVO(IpEvent.getAccountId(),address,lastCollection,IpEvent.getCreateDate()));
      }
    }
  }
  List<String> IpList=new ArrayList<String>();
  for (  IPAddressVO ip : allocatedIps) {
    if (ip.getAllocatedToAccountId() == Account.ACCOUNT_ID_SYSTEM) {
      continue;
    }
    String address=(ip.getAddress()).toString();
    if (ipAssigment.containsKey(address)) {
      IpPartialUsage.add(new UsageIPAddressVO(ip.getAllocatedToAccountId(),address,ipAssigment.get(address),now));
    }
 else {
      fullDurationIpUsage.add(new UsageIPAddressVO(ip.getAllocatedToAccountId(),address,lastCollection,now));
      IpList.add(address);
    }
  }
  final List<UserStatisticsVO> collectedStats=new ArrayList<UserStatisticsVO>();
  if (fullDurationIpUsage.size() > 0) {
    DirectNetworkUsageCommand cmd=new DirectNetworkUsageCommand(IpList,lastCollection,now,_TSinclZones,_TSexclZones);
    DirectNetworkUsageAnswer answer=(DirectNetworkUsageAnswer)_agentMgr.easySend(host.getId(),cmd);
    if (answer == null || !answer.getResult()) {
      String details=(answer != null) ? answer.getDetails() : "String_Node_Str";
      String msg="String_Node_Str" + host.getId() + "String_Node_Str"+ details+ "String_Node_Str";
      s_logger.error(msg);
      return false;
    }
 else {
      for (      UsageIPAddressVO usageIp : fullDurationIpUsage) {
        String publicIp=usageIp.getAddress();
        long[] bytesSentRcvd=answer.get(publicIp);
        Long bytesSent=bytesSentRcvd[0];
        Long bytesRcvd=bytesSentRcvd[1];
        if (bytesSent == null || bytesRcvd == null) {
          s_logger.debug("String_Node_Str" + publicIp);
          continue;
        }
        if (bytesSent == 0L && bytesRcvd == 0L) {
          s_logger.trace("String_Node_Str" + publicIp);
          continue;
        }
        UserStatisticsVO stats=new UserStatisticsVO(usageIp.getAccountId(),zoneId,null,null,null,null);
        stats.setCurrentBytesSent(bytesSent);
        stats.setCurrentBytesReceived(bytesRcvd);
        collectedStats.add(stats);
      }
    }
  }
  for (  UsageIPAddressVO usageIp : IpPartialUsage) {
    IpList=new ArrayList<String>();
    IpList.add(usageIp.getAddress());
    DirectNetworkUsageCommand cmd=new DirectNetworkUsageCommand(IpList,usageIp.getAssigned(),usageIp.getReleased(),_TSinclZones,_TSexclZones);
    DirectNetworkUsageAnswer answer=(DirectNetworkUsageAnswer)_agentMgr.easySend(host.getId(),cmd);
    if (answer == null || !answer.getResult()) {
      String details=(answer != null) ? answer.getDetails() : "String_Node_Str";
      String msg="String_Node_Str" + host.getId() + "String_Node_Str"+ details+ "String_Node_Str";
      s_logger.error(msg);
      return false;
    }
 else {
      String publicIp=usageIp.getAddress();
      long[] bytesSentRcvd=answer.get(publicIp);
      Long bytesSent=bytesSentRcvd[0];
      Long bytesRcvd=bytesSentRcvd[1];
      if (bytesSent == null || bytesRcvd == null) {
        s_logger.debug("String_Node_Str" + publicIp);
        continue;
      }
      if (bytesSent == 0L && bytesRcvd == 0L) {
        s_logger.trace("String_Node_Str" + publicIp);
        continue;
      }
      UserStatisticsVO stats=new UserStatisticsVO(usageIp.getAccountId(),zoneId,null,null,null,null);
      stats.setCurrentBytesSent(bytesSent);
      stats.setCurrentBytesReceived(bytesRcvd);
      collectedStats.add(stats);
    }
  }
  if (collectedStats.size() == 0) {
    s_logger.debug("String_Node_Str");
    return false;
  }
  Transaction.execute(new TransactionCallbackNoReturn(){
    @Override public void doInTransactionWithoutResult(    TransactionStatus status){
      for (      UserStatisticsVO stat : collectedStats) {
        UserStatisticsVO stats=_statsDao.lock(stat.getAccountId(),stat.getDataCenterId(),0L,null,host.getId(),"String_Node_Str");
        if (stats == null) {
          stats=new UserStatisticsVO(stat.getAccountId(),zoneId,null,host.getId(),"String_Node_Str",0L);
          stats.setCurrentBytesSent(stat.getCurrentBytesSent());
          stats.setCurrentBytesReceived(stat.getCurrentBytesReceived());
          _statsDao.persist(stats);
        }
 else {
          stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent());
          stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived());
          _statsDao.update(stats.getId(),stats);
        }
      }
      lastCollectDetail.setValue("String_Node_Str" + now.getTime());
      _detailsDao.update(lastCollectDetail.getId(),lastCollectDetail);
    }
  }
);
  return true;
}

Fixed version (ground truth):
private boolean collectDirectNetworkUsage(final HostVO host){
  s_logger.debug("String_Node_Str");
  final long zoneId=host.getDataCenterId();
  final DetailVO lastCollectDetail=_detailsDao.findDetail(host.getId(),"String_Node_Str");
  if (lastCollectDetail == null) {
    s_logger.warn("String_Node_Str" + host.getId());
    return false;
  }
  Date lastCollection=new Date(Long.parseLong(lastCollectDetail.getValue()));
  List<IPAddressVO> allocatedIps=listAllocatedDirectIps(zoneId);
  Calendar rightNow=Calendar.getInstance();
  rightNow.add(Calendar.HOUR_OF_DAY,-2);
  final Date now=rightNow.getTime();
  if (lastCollection.after(now)) {
    s_logger.debug("String_Node_Str" + lastCollection.toString() + "String_Node_Str");
    return false;
  }
  List<UsageEventVO> IpEvents=_eventDao.listDirectIpEvents(lastCollection,now,zoneId);
  Map<String,Date> ipAssigment=new HashMap<String,Date>();
  List<UsageIPAddressVO> IpPartialUsage=new ArrayList<UsageIPAddressVO>();
  List<UsageIPAddressVO> fullDurationIpUsage=new ArrayList<UsageIPAddressVO>();
  for (  UsageEventVO IpEvent : IpEvents) {
    String address=IpEvent.getResourceName();
    if (EventTypes.EVENT_NET_IP_ASSIGN.equals(IpEvent.getType())) {
      ipAssigment.put(address,IpEvent.getCreateDate());
    }
 else     if (EventTypes.EVENT_NET_IP_RELEASE.equals(IpEvent.getType())) {
      if (ipAssigment.containsKey(address)) {
        Date assigned=ipAssigment.get(address);
        ipAssigment.remove(address);
        IpPartialUsage.add(new UsageIPAddressVO(IpEvent.getAccountId(),address,assigned,IpEvent.getCreateDate()));
      }
 else {
        IpPartialUsage.add(new UsageIPAddressVO(IpEvent.getAccountId(),address,lastCollection,IpEvent.getCreateDate()));
      }
    }
  }
  List<String> IpList=new ArrayList<String>();
  for (  IPAddressVO ip : allocatedIps) {
    if (ip.getAllocatedToAccountId() == Account.ACCOUNT_ID_SYSTEM) {
      continue;
    }
    String address=(ip.getAddress()).toString();
    if (ipAssigment.containsKey(address)) {
      IpPartialUsage.add(new UsageIPAddressVO(ip.getAllocatedToAccountId(),address,ipAssigment.get(address),now));
    }
 else {
      fullDurationIpUsage.add(new UsageIPAddressVO(ip.getAllocatedToAccountId(),address,lastCollection,now));
      IpList.add(address);
    }
  }
  final List<UserStatisticsVO> collectedStats=new ArrayList<UserStatisticsVO>();
  if (fullDurationIpUsage.size() > 0) {
    DirectNetworkUsageCommand cmd=new DirectNetworkUsageCommand(IpList,lastCollection,now,_TSinclZones,_TSexclZones);
    DirectNetworkUsageAnswer answer=(DirectNetworkUsageAnswer)_agentMgr.easySend(host.getId(),cmd);
    if (answer == null || !answer.getResult()) {
      String details=(answer != null) ? answer.getDetails() : "String_Node_Str";
      String msg="String_Node_Str" + host.getId() + "String_Node_Str"+ details+ "String_Node_Str";
      s_logger.error(msg);
      return false;
    }
 else {
      for (      UsageIPAddressVO usageIp : fullDurationIpUsage) {
        String publicIp=usageIp.getAddress();
        long[] bytesSentRcvd=answer.get(publicIp);
        Long bytesSent=bytesSentRcvd[0];
        Long bytesRcvd=bytesSentRcvd[1];
        if (bytesSent == null || bytesRcvd == null) {
          s_logger.debug("String_Node_Str" + publicIp);
          continue;
        }
        if (bytesSent == 0L && bytesRcvd == 0L) {
          s_logger.trace("String_Node_Str" + publicIp);
          continue;
        }
        UserStatisticsVO stats=new UserStatisticsVO(usageIp.getAccountId(),zoneId,null,null,null,null);
        stats.setCurrentBytesSent(bytesSent);
        stats.setCurrentBytesReceived(bytesRcvd);
        collectedStats.add(stats);
      }
    }
  }
  for (  UsageIPAddressVO usageIp : IpPartialUsage) {
    IpList=new ArrayList<String>();
    IpList.add(usageIp.getAddress());
    DirectNetworkUsageCommand cmd=new DirectNetworkUsageCommand(IpList,usageIp.getAssigned(),usageIp.getReleased(),_TSinclZones,_TSexclZones);
    DirectNetworkUsageAnswer answer=(DirectNetworkUsageAnswer)_agentMgr.easySend(host.getId(),cmd);
    if (answer == null || !answer.getResult()) {
      String details=(answer != null) ? answer.getDetails() : "String_Node_Str";
      String msg="String_Node_Str" + host.getId() + "String_Node_Str"+ details+ "String_Node_Str";
      s_logger.error(msg);
      return false;
    }
 else {
      String publicIp=usageIp.getAddress();
      long[] bytesSentRcvd=answer.get(publicIp);
      Long bytesSent=bytesSentRcvd[0];
      Long bytesRcvd=bytesSentRcvd[1];
      if (bytesSent == null || bytesRcvd == null) {
        s_logger.debug("String_Node_Str" + publicIp);
        continue;
      }
      if (bytesSent == 0L && bytesRcvd == 0L) {
        s_logger.trace("String_Node_Str" + publicIp);
        continue;
      }
      UserStatisticsVO stats=new UserStatisticsVO(usageIp.getAccountId(),zoneId,null,null,null,null);
      stats.setCurrentBytesSent(bytesSent);
      stats.setCurrentBytesReceived(bytesRcvd);
      collectedStats.add(stats);
    }
  }
  if (collectedStats.size() == 0) {
    s_logger.debug("String_Node_Str");
    return false;
  }
  Transaction.execute(new TransactionCallbackNoReturn(){
    @Override public void doInTransactionWithoutResult(    TransactionStatus status){
      for (      UserStatisticsVO stat : collectedStats) {
        UserStatisticsVO stats=_statsDao.lock(stat.getAccountId(),stat.getDataCenterId(),0L,null,host.getId(),"String_Node_Str");
        if (stats == null) {
          stats=new UserStatisticsVO(stat.getAccountId(),zoneId,null,host.getId(),"String_Node_Str",0L);
          stats.setCurrentBytesSent(stat.getCurrentBytesSent());
          stats.setCurrentBytesReceived(stat.getCurrentBytesReceived());
          _statsDao.persist(stats);
        }
 else {
          stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent());
          stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived());
          _statsDao.update(stats.getId(),stats);
        }
      }
      lastCollectDetail.setValue("String_Node_Str" + now.getTime());
      _detailsDao.update(lastCollectDetail.getId(),lastCollectDetail);
    }
  }
);
  return true;
}