Program: 106392
Project: 106392
Buggy source:
private MappingNic getLoadBalancingIpNic(DataCenterVO zone,Network network,long sourceIpId,boolean revoked,String existedGuestIp) throws ResourceUnavailableException {
  String srcIp=_networkMgr.getIp(sourceIpId).getAddress().addr();
  InlineLoadBalancerNicMapVO mapping=_inlineLoadBalancerNicMapDao.findByPublicIpAddress(srcIp);
  NicVO loadBalancingIpNic=null;
  MappingNic nic=new MappingNic();
  nic.setState(MappingState.Unchanged);
  if (!revoked) {
    if (mapping == null) {
      String loadBalancingIpAddress=existedGuestIp;
      if (loadBalancingIpAddress == null) {
        loadBalancingIpAddress=_networkMgr.acquireGuestIpAddress(network,null);
      }
      if (loadBalancingIpAddress == null) {
        String msg="String_Node_Str";
        s_logger.error(msg);
        throw new ResourceUnavailableException(msg,DataCenter.class,network.getDataCenterId());
      }
      loadBalancingIpNic=_nicDao.findByIp4AddressAndNetworkId(loadBalancingIpAddress,network.getId());
      if (loadBalancingIpNic == null) {
        loadBalancingIpNic=savePlaceholderNic(network,loadBalancingIpAddress);
      }
      mapping=new InlineLoadBalancerNicMapVO(srcIp,loadBalancingIpNic.getId());
      _inlineLoadBalancerNicMapDao.persist(mapping);
      try {
        applyStaticNatRuleForInlineLBRule(zone,network,revoked,srcIp,loadBalancingIpNic.getIp4Address());
      }
 catch (      ResourceUnavailableException ex) {
        _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
        _nicDao.expunge(loadBalancingIpNic.getId());
        throw ex;
      }
      s_logger.debug("String_Node_Str");
      nic.setState(MappingState.Create);
    }
 else {
      loadBalancingIpNic=_nicDao.findById(mapping.getNicId());
    }
  }
 else {
    if (mapping != null) {
      loadBalancingIpNic=_nicDao.findById(mapping.getNicId());
      int count=_networkMgr.getRuleCountForIp(sourceIpId,Purpose.LoadBalancing,FirewallRule.State.Active);
      if (count == 0) {
        applyStaticNatRuleForInlineLBRule(zone,network,revoked,srcIp,loadBalancingIpNic.getIp4Address());
        _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
        _nicDao.expunge(loadBalancingIpNic.getId());
        s_logger.debug("String_Node_Str");
        nic.setState(MappingState.Remove);
      }
    }
 else {
      s_logger.debug("String_Node_Str");
      return null;
    }
  }
  nic.setNic(loadBalancingIpNic);
  return nic;
}

Fixed version (ground truth):
private MappingNic getLoadBalancingIpNic(DataCenterVO zone,Network network,long sourceIpId,boolean revoked,String existedGuestIp) throws ResourceUnavailableException {
  String srcIp=_networkModel.getIp(sourceIpId).getAddress().addr();
  InlineLoadBalancerNicMapVO mapping=_inlineLoadBalancerNicMapDao.findByPublicIpAddress(srcIp);
  NicVO loadBalancingIpNic=null;
  MappingNic nic=new MappingNic();
  nic.setState(MappingState.Unchanged);
  if (!revoked) {
    if (mapping == null) {
      String loadBalancingIpAddress=existedGuestIp;
      if (loadBalancingIpAddress == null) {
        loadBalancingIpAddress=_networkMgr.acquireGuestIpAddress(network,null);
      }
      if (loadBalancingIpAddress == null) {
        String msg="String_Node_Str";
        s_logger.error(msg);
        throw new ResourceUnavailableException(msg,DataCenter.class,network.getDataCenterId());
      }
      loadBalancingIpNic=_nicDao.findByIp4AddressAndNetworkId(loadBalancingIpAddress,network.getId());
      if (loadBalancingIpNic == null) {
        loadBalancingIpNic=savePlaceholderNic(network,loadBalancingIpAddress);
      }
      mapping=new InlineLoadBalancerNicMapVO(srcIp,loadBalancingIpNic.getId());
      _inlineLoadBalancerNicMapDao.persist(mapping);
      try {
        applyStaticNatRuleForInlineLBRule(zone,network,revoked,srcIp,loadBalancingIpNic.getIp4Address());
      }
 catch (      ResourceUnavailableException ex) {
        _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
        _nicDao.expunge(loadBalancingIpNic.getId());
        throw ex;
      }
      s_logger.debug("String_Node_Str");
      nic.setState(MappingState.Create);
    }
 else {
      loadBalancingIpNic=_nicDao.findById(mapping.getNicId());
    }
  }
 else {
    if (mapping != null) {
      loadBalancingIpNic=_nicDao.findById(mapping.getNicId());
      int count=_networkMgr.getRuleCountForIp(sourceIpId,Purpose.LoadBalancing,FirewallRule.State.Active);
      if (count == 0) {
        applyStaticNatRuleForInlineLBRule(zone,network,revoked,srcIp,loadBalancingIpNic.getIp4Address());
        _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
        _nicDao.expunge(loadBalancingIpNic.getId());
        s_logger.debug("String_Node_Str");
        nic.setState(MappingState.Remove);
      }
    }
 else {
      s_logger.debug("String_Node_Str");
      return null;
    }
  }
  nic.setNic(loadBalancingIpNic);
  return nic;
}