Program: 89315
Project: 89315
Buggy source:
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_NET_RULE_ADD,eventDescription="String_Node_Str",create=true) public PortForwardingRule createPortForwardingRule(PortForwardingRule rule,Long vmId,boolean openFirewall) throws NetworkRuleConflictException {
  UserContext ctx=UserContext.current();
  Account caller=ctx.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.isOneToOneNat()) {
    throw new InvalidParameterValueException("String_Node_Str" + ipAddrId + "String_Node_Str");
  }
  _firewallMgr.validateFirewallRule(caller,ipAddress,rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol(),Purpose.PortForwarding);
  Long networkId=ipAddress.getAssociatedWithNetworkId();
  Long accountId=ipAddress.getAccountId();
  Long domainId=ipAddress.getDomainId();
  if (rule.getDestinationPortStart() > rule.getDestinationPortEnd()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if ((rule.getDestinationPortEnd() - rule.getDestinationPortStart()) != (rule.getSourcePortEnd() - rule.getSourcePortStart())) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  UserVm vm=_vmDao.findById(vmId);
  if (vm == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipAddress + "String_Node_Str"+ vmId+ "String_Node_Str");
  }
 else {
    checkRuleAndUserVm(rule,vm,caller);
  }
  Ip dstIp=rule.getDestinationIpAddress();
  Nic guestNic=_networkMgr.getNicInNetwork(vmId,networkId);
  if (guestNic == null || guestNic.getIp4Address() == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
 else {
    dstIp=new Ip(guestNic.getIp4Address());
  }
  Transaction txn=Transaction.currentTxn();
  txn.start();
  PortForwardingRuleVO newRule=new PortForwardingRuleVO(rule.getXid(),rule.getSourceIpAddressId(),rule.getSourcePortStart(),rule.getSourcePortEnd(),dstIp,rule.getDestinationPortStart(),rule.getDestinationPortEnd(),rule.getProtocol().toLowerCase(),networkId,accountId,domainId,vmId);
  newRule=_forwardingDao.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(),ipAddress.getDataCenterId(),newRule.getId(),null);
    _usageEventDao.persist(usageEvent);
    txn.commit();
    return newRule;
  }
 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" + ipAddrId,e);
  }
}

Fixed version (ground truth):
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_NET_RULE_ADD,eventDescription="String_Node_Str",create=true) public PortForwardingRule createPortForwardingRule(PortForwardingRule rule,Long vmId,boolean openFirewall) throws NetworkRuleConflictException {
  UserContext ctx=UserContext.current();
  Account caller=ctx.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.isOneToOneNat()) {
    throw new InvalidParameterValueException("String_Node_Str" + ipAddrId + "String_Node_Str");
  }
  _firewallMgr.validateFirewallRule(caller,ipAddress,rule.getSourcePortStart(),rule.getSourcePortEnd(),rule.getProtocol(),Purpose.PortForwarding);
  Long networkId=ipAddress.getAssociatedWithNetworkId();
  Long accountId=ipAddress.getAccountId();
  Long domainId=ipAddress.getDomainId();
  if (rule.getDestinationPortStart() > rule.getDestinationPortEnd()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if ((rule.getDestinationPortEnd() - rule.getDestinationPortStart()) != (rule.getSourcePortEnd() - rule.getSourcePortStart())) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  UserVm vm=_vmDao.findById(vmId);
  if (vm == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipAddress + "String_Node_Str"+ vmId+ "String_Node_Str");
  }
 else {
    checkRuleAndUserVm(rule,vm,caller);
  }
  Ip dstIp=rule.getDestinationIpAddress();
  Nic guestNic=_networkMgr.getNicInNetwork(vmId,networkId);
  if (guestNic == null || guestNic.getIp4Address() == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
 else {
    dstIp=new Ip(guestNic.getIp4Address());
  }
  Transaction txn=Transaction.currentTxn();
  txn.start();
  PortForwardingRuleVO newRule=new PortForwardingRuleVO(rule.getXid(),rule.getSourceIpAddressId(),rule.getSourcePortStart(),rule.getSourcePortEnd(),dstIp,rule.getDestinationPortStart(),rule.getDestinationPortEnd(),rule.getProtocol().toLowerCase(),networkId,accountId,domainId,vmId);
  newRule=_forwardingDao.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(),ipAddress.getDataCenterId(),newRule.getId(),null);
    _usageEventDao.persist(usageEvent);
    txn.commit();
    return newRule;
  }
 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" + ipAddrId,e);
  }
}