Program: 106692
Project: 106692
Buggy source:
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_NET_RULE_ADD,eventDescription="String_Node_Str",create=true) public StaticNatRule createStaticNatRule(StaticNatRule rule,boolean openFirewall) throws NetworkRuleConflictException {
  Account caller=UserContext.current().getCaller();
  Long ipAddrId=rule.getSourceIpAddressId();
  IPAddressVO ipAddress=_ipAddressDao.findById(ipAddrId);
  if (ipAddress == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipAddrId + "String_Node_Str");
  }
 else   if (ipAddress.isSourceNat() || !ipAddress.isOneToOneNat() || ipAddress.getAssociatedWithVmId() == null) {
    throw new NetworkRuleConflictException("String_Node_Str" + ipAddress.getAddress());
  }
  _firewallMgr.validateFirewallRule(caller,ipAddress,rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol(),Purpose.StaticNat);
  Long networkId=ipAddress.getAssociatedWithNetworkId();
  Long accountId=ipAddress.getAccountId();
  Long domainId=ipAddress.getDomainId();
  String dstIp=_networkMgr.getIpInNetwork(ipAddress.getAssociatedWithVmId(),networkId);
  Transaction txn=Transaction.currentTxn();
  txn.start();
  FirewallRuleVO newRule=new FirewallRuleVO(rule.getXid(),rule.getSourceIpAddressId(),rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol().toLowerCase(),networkId,accountId,domainId,rule.getPurpose(),null,null,null,null);
  newRule=_firewallDao.persist(newRule);
  if (openFirewall) {
    _firewallMgr.createRuleForAllCidrs(ipAddrId,caller,rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol(),null,null,newRule.getId());
  }
  try {
    _firewallMgr.detectRulesConflict(newRule,ipAddress);
    if (!_firewallDao.setStateToAdd(newRule)) {
      throw new CloudRuntimeException("String_Node_Str" + newRule);
    }
    UserContext.current().setEventDetails("String_Node_Str" + newRule.getId());
    UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_NET_RULE_ADD,newRule.getAccountId(),0,newRule.getId(),null);
    _usageEventDao.persist(usageEvent);
    txn.commit();
    StaticNatRule staticNatRule=new StaticNatRuleImpl(newRule,dstIp);
    return staticNatRule;
  }
 catch (  Exception e) {
    txn.start();
    _firewallMgr.revokeRelatedFirewallRule(newRule.getId(),false);
    _forwardingDao.remove(newRule.getId());
    txn.commit();
    if (e instanceof NetworkRuleConflictException) {
      throw (NetworkRuleConflictException)e;
    }
    throw new CloudRuntimeException("String_Node_Str" + newRule.getSourceIpAddressId(),e);
  }
}

Fixed version (ground truth):
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_NET_RULE_ADD,eventDescription="String_Node_Str",create=true) public StaticNatRule createStaticNatRule(StaticNatRule rule,boolean openFirewall) throws NetworkRuleConflictException {
  Account caller=UserContext.current().getCaller();
  Long ipAddrId=rule.getSourceIpAddressId();
  IPAddressVO ipAddress=_ipAddressDao.findById(ipAddrId);
  if (ipAddress == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipAddrId + "String_Node_Str");
  }
 else   if (ipAddress.isSourceNat() || !ipAddress.isOneToOneNat() || ipAddress.getAssociatedWithVmId() == null) {
    throw new NetworkRuleConflictException("String_Node_Str" + ipAddress.getAddress());
  }
  _firewallMgr.validateFirewallRule(caller,ipAddress,rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol(),Purpose.StaticNat);
  Long networkId=ipAddress.getAssociatedWithNetworkId();
  Long accountId=ipAddress.getAccountId();
  Long domainId=ipAddress.getDomainId();
  String dstIp=_networkMgr.getIpInNetwork(ipAddress.getAssociatedWithVmId(),networkId);
  Transaction txn=Transaction.currentTxn();
  txn.start();
  FirewallRuleVO newRule=new FirewallRuleVO(rule.getXid(),rule.getSourceIpAddressId(),rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol().toLowerCase(),networkId,accountId,domainId,rule.getPurpose(),null,null,null,null);
  newRule=_firewallDao.persist(newRule);
  if (openFirewall) {
    _firewallMgr.createRuleForAllCidrs(ipAddrId,caller,rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol(),null,null,newRule.getId());
  }
  try {
    _firewallMgr.detectRulesConflict(newRule,ipAddress);
    if (!_firewallDao.setStateToAdd(newRule)) {
      throw new CloudRuntimeException("String_Node_Str" + newRule);
    }
    UserContext.current().setEventDetails("String_Node_Str" + newRule.getId());
    UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_NET_RULE_ADD,newRule.getAccountId(),0,newRule.getId(),null);
    _usageEventDao.persist(usageEvent);
    txn.commit();
    StaticNatRule staticNatRule=new StaticNatRuleImpl(newRule,dstIp);
    return staticNatRule;
  }
 catch (  Exception e) {
    if (newRule != null) {
      txn.start();
      _firewallMgr.revokeRelatedFirewallRule(newRule.getId(),false);
      _forwardingDao.remove(newRule.getId());
      txn.commit();
    }
    if (e instanceof NetworkRuleConflictException) {
      throw (NetworkRuleConflictException)e;
    }
    throw new CloudRuntimeException("String_Node_Str" + newRule.getSourceIpAddressId(),e);
  }
}