Program: 106396
Project: 106396
Buggy source:
@Override @DB public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb,boolean openFirewall) throws NetworkRuleConflictException {
  UserContext caller=UserContext.current();
  int srcPortStart=lb.getSourcePortStart();
  int defPortStart=lb.getDefaultPortStart();
  int srcPortEnd=lb.getSourcePortEnd();
  long sourceIpId=lb.getSourceIpAddressId();
  IPAddressVO ipAddr=_ipAddressDao.findById(sourceIpId);
  if (ipAddr == null || !ipAddr.readyToUse()) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(ipAddr,sourceIpId,"String_Node_Str");
    throw ex;
  }
 else   if (ipAddr.isOneToOneNat()) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(ipAddr,sourceIpId,"String_Node_Str");
    throw ex;
  }
  _firewallMgr.validateFirewallRule(caller.getCaller(),ipAddr,srcPortStart,srcPortEnd,lb.getProtocol(),Purpose.LoadBalancing,FirewallRuleType.User);
  Long networkId=ipAddr.getAssociatedWithNetworkId();
  if (networkId == null) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(ipAddr,sourceIpId,"String_Node_Str");
    throw ex;
  }
  NetworkVO network=_networkDao.findById(networkId);
  _accountMgr.checkAccess(caller.getCaller(),null,true,ipAddr);
  if (!_networkModel.areServicesSupportedInNetwork(network.getId(),Service.Lb)) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(network,networkId,"String_Node_Str");
    throw ex;
  }
  Transaction txn=Transaction.currentTxn();
  txn.start();
  LoadBalancerVO newRule=new LoadBalancerVO(lb.getXid(),lb.getName(),lb.getDescription(),lb.getSourceIpAddressId(),lb.getSourcePortEnd(),lb.getDefaultPortStart(),lb.getAlgorithm(),network.getId(),ipAddr.getAllocatedToAccountId(),ipAddr.getAllocatedInDomainId());
  LoadBalancingRule loadBalancing=new LoadBalancingRule(newRule,new ArrayList<LbDestination>(),new ArrayList<LbStickinessPolicy>());
  if (!_networkMgr.validateRule(loadBalancing)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  newRule=_lbDao.persist(newRule);
  if (openFirewall) {
    _firewallMgr.createRuleForAllCidrs(sourceIpId,caller.getCaller(),lb.getSourcePortStart(),lb.getSourcePortEnd(),lb.getProtocol(),null,null,newRule.getId(),networkId);
  }
  boolean success=true;
  try {
    _firewallMgr.detectRulesConflict(newRule);
    if (!_firewallDao.setStateToAdd(newRule)) {
      throw new CloudRuntimeException("String_Node_Str" + newRule);
    }
    s_logger.debug("String_Node_Str" + newRule.getId() + "String_Node_Str"+ sourceIpId+ "String_Node_Str"+ srcPortStart+ "String_Node_Str"+ defPortStart+ "String_Node_Str");
    UserContext.current().setEventDetails("String_Node_Str" + newRule.getId());
    UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_CREATE,ipAddr.getAllocatedToAccountId(),ipAddr.getDataCenterId(),newRule.getId(),null);
    _usageEventDao.persist(usageEvent);
    txn.commit();
    return newRule;
  }
 catch (  Exception e) {
    success=false;
    if (e instanceof NetworkRuleConflictException) {
      throw (NetworkRuleConflictException)e;
    }
    throw new CloudRuntimeException("String_Node_Str" + newRule.getSourceIpAddressId(),e);
  }
 finally {
    if (!success && newRule != null) {
      txn.start();
      _firewallMgr.revokeRelatedFirewallRule(newRule.getId(),false);
      removeLBRule(newRule);
      txn.commit();
    }
  }
}

Fixed version (ground truth):
@Override @DB public LoadBalancer createLoadBalancer(CreateLoadBalancerRuleCmd lb,boolean openFirewall) throws NetworkRuleConflictException {
  UserContext caller=UserContext.current();
  int srcPortStart=lb.getSourcePortStart();
  int defPortStart=lb.getDefaultPortStart();
  int srcPortEnd=lb.getSourcePortEnd();
  long sourceIpId=lb.getSourceIpAddressId();
  IPAddressVO ipAddr=_ipAddressDao.findById(sourceIpId);
  if (ipAddr == null || !ipAddr.readyToUse()) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(ipAddr,sourceIpId,"String_Node_Str");
    throw ex;
  }
 else   if (ipAddr.isOneToOneNat()) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(ipAddr,sourceIpId,"String_Node_Str");
    throw ex;
  }
  _firewallMgr.validateFirewallRule(caller.getCaller(),ipAddr,srcPortStart,srcPortEnd,lb.getProtocol(),Purpose.LoadBalancing,FirewallRuleType.User);
  Long networkId=ipAddr.getAssociatedWithNetworkId();
  if (networkId == null) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(ipAddr,sourceIpId,"String_Node_Str");
    throw ex;
  }
  NetworkVO network=_networkDao.findById(networkId);
  _accountMgr.checkAccess(caller.getCaller(),null,true,ipAddr);
  if (!_networkModel.areServicesSupportedInNetwork(network.getId(),Service.Lb)) {
    InvalidParameterValueException ex=new InvalidParameterValueException("String_Node_Str");
    ex.addProxyObject(network,networkId,"String_Node_Str");
    throw ex;
  }
  Transaction txn=Transaction.currentTxn();
  txn.start();
  LoadBalancerVO newRule=new LoadBalancerVO(lb.getXid(),lb.getName(),lb.getDescription(),lb.getSourceIpAddressId(),lb.getSourcePortEnd(),lb.getDefaultPortStart(),lb.getAlgorithm(),network.getId(),ipAddr.getAllocatedToAccountId(),ipAddr.getAllocatedInDomainId());
  LoadBalancingRule loadBalancing=new LoadBalancingRule(newRule,new ArrayList<LbDestination>(),new ArrayList<LbStickinessPolicy>());
  if (validateRule(loadBalancing)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  newRule=_lbDao.persist(newRule);
  if (openFirewall) {
    _firewallMgr.createRuleForAllCidrs(sourceIpId,caller.getCaller(),lb.getSourcePortStart(),lb.getSourcePortEnd(),lb.getProtocol(),null,null,newRule.getId(),networkId);
  }
  boolean success=true;
  try {
    _firewallMgr.detectRulesConflict(newRule);
    if (!_firewallDao.setStateToAdd(newRule)) {
      throw new CloudRuntimeException("String_Node_Str" + newRule);
    }
    s_logger.debug("String_Node_Str" + newRule.getId() + "String_Node_Str"+ sourceIpId+ "String_Node_Str"+ srcPortStart+ "String_Node_Str"+ defPortStart+ "String_Node_Str");
    UserContext.current().setEventDetails("String_Node_Str" + newRule.getId());
    UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_CREATE,ipAddr.getAllocatedToAccountId(),ipAddr.getDataCenterId(),newRule.getId(),null);
    _usageEventDao.persist(usageEvent);
    txn.commit();
    return newRule;
  }
 catch (  Exception e) {
    success=false;
    if (e instanceof NetworkRuleConflictException) {
      throw (NetworkRuleConflictException)e;
    }
    throw new CloudRuntimeException("String_Node_Str" + newRule.getSourceIpAddressId(),e);
  }
 finally {
    if (!success && newRule != null) {
      txn.start();
      _firewallMgr.revokeRelatedFirewallRule(newRule.getId(),false);
      removeLBRule(newRule);
      txn.commit();
    }
  }
}