Program: 107347
Project: 107347
Buggy source:
@Override @DB public boolean assignToLoadBalancer(AssignToLoadBalancerRuleCmd cmd) throws NetworkRuleConflictException {
  Long loadBalancerId=cmd.getLoadBalancerId();
  Long instanceIdParam=cmd.getVirtualMachineId();
  List<Long> instanceIds=cmd.getVirtualMachineIds();
  if ((instanceIdParam == null) && (instanceIds == null)) {
    throw new InvalidParameterValueException("String_Node_Str" + loadBalancerId + "String_Node_Str");
  }
  if ((instanceIds == null) && (instanceIdParam != null)) {
    instanceIds=new ArrayList<Long>();
    instanceIds.add(instanceIdParam);
  }
  LoadBalancerVO loadBalancer=_loadBalancerDao.findById(loadBalancerId);
  if (loadBalancer == null) {
    throw new InvalidParameterValueException("String_Node_Str" + loadBalancerId + "String_Node_Str");
  }
  DomainRouterVO syncObject=_routerMgr.getRouter(loadBalancer.getIpAddress());
  if (syncObject == null) {
    throw new InvalidParameterValueException("String_Node_Str" + loadBalancerId + "String_Node_Str"+ loadBalancer.getIpAddress());
  }
 else {
    cmd.synchronizeCommand("String_Node_Str",syncObject.getId());
  }
  Account account=UserContext.current().getAccount();
  if (account != null) {
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
      if (!_domainDao.isChildDomain(account.getDomainId(),loadBalancer.getDomainId())) {
        throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
      }
    }
 else     if (account.getType() != Account.ACCOUNT_TYPE_ADMIN && account.getId() != loadBalancer.getAccountId()) {
      throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
    }
  }
  Transaction txn=Transaction.currentTxn();
  List<FirewallRuleVO> firewallRulesToApply=new ArrayList<FirewallRuleVO>();
  long accountId=0;
  DomainRouterVO router=null;
  List<LoadBalancerVMMapVO> mappedInstances=_loadBalancerVMMapDao.listByLoadBalancerId(loadBalancerId,false);
  Set<Long> mappedInstanceIds=new HashSet<Long>();
  if (mappedInstances != null) {
    for (    LoadBalancerVMMapVO mappedInstance : mappedInstances) {
      mappedInstanceIds.add(Long.valueOf(mappedInstance.getInstanceId()));
    }
  }
  List<Long> finalInstanceIds=new ArrayList<Long>();
  for (  Long instanceId : instanceIds) {
    if (mappedInstanceIds.contains(instanceId)) {
      continue;
    }
 else {
      finalInstanceIds.add(instanceId);
    }
    UserVmVO userVm=_vmDao.findById(instanceId);
    if (userVm == null) {
      s_logger.warn("String_Node_Str" + instanceId);
      throw new InvalidParameterValueException("String_Node_Str" + instanceId);
    }
 else {
      ServiceOfferingVO offering=_serviceOfferingDao.findById(userVm.getServiceOfferingId());
      if ((offering == null) || !GuestIpType.Virtualized.equals(offering.getGuestIpType())) {
        _loadBalancerVMMapDao.remove(loadBalancerId,instanceIds,Boolean.TRUE);
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + userVm.toString() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ ((offering == null) ? "String_Node_Str" : offering.getGuestIpType())+ "String_Node_Str");
        }
        throw new InvalidParameterValueException("String_Node_Str" + userVm.toString() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ ((offering == null) ? "String_Node_Str" : offering.getGuestIpType())+ "String_Node_Str");
      }
    }
    if (accountId == 0) {
      accountId=userVm.getAccountId();
    }
 else     if (accountId != userVm.getAccountId()) {
      s_logger.warn("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ userVm.getAccountId()+ "String_Node_Str"+ accountId);
      throw new InvalidParameterValueException("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ userVm.getAccountId()+ "String_Node_Str"+ accountId);
    }
    DomainRouterVO nextRouter=null;
    if (userVm.getDomainRouterId() != null)     nextRouter=_routerMgr.getRouter(userVm.getDomainRouterId());
    if (nextRouter == null) {
      s_logger.warn("String_Node_Str" + userVm.getDomainRouterId() + "String_Node_Str"+ instanceId);
      throw new InvalidParameterValueException("String_Node_Str" + userVm.getDomainRouterId() + "String_Node_Str"+ instanceId);
    }
    if (router == null) {
      router=nextRouter;
      if (router.getAccountId() != loadBalancer.getAccountId()) {
        throw new InvalidParameterValueException("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ loadBalancer.getName()+ "String_Node_Str"+ loadBalancer.getAccountId()+ "String_Node_Str");
      }
    }
 else     if (router.getId() != nextRouter.getId()) {
      throw new InvalidParameterValueException("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ nextRouter.getHostName()+ "String_Node_Str"+ router.getHostName());
    }
    String ipAddress=loadBalancer.getIpAddress();
    String privateIpAddress=userVm.getGuestIpAddress();
    List<FirewallRuleVO> existingRulesOnPubIp=_rulesDao.listIPForwarding(ipAddress);
    if (existingRulesOnPubIp != null) {
      for (      FirewallRuleVO fwRule : existingRulesOnPubIp) {
        if (!((fwRule.isForwarding() == false) && (fwRule.getGroupId() != null) && (fwRule.getGroupId() == loadBalancer.getId()))) {
          if (fwRule.getPublicPort().equals(loadBalancer.getPublicPort())) {
            throw new NetworkRuleConflictException("String_Node_Str" + ipAddress + "String_Node_Str"+ loadBalancer.getPublicPort()+ "String_Node_Str"+ loadBalancer.getName()+ "String_Node_Str"+ loadBalancer.getId()+ "String_Node_Str"+ userVm.getHostName()+ "String_Node_Str");
          }
        }
 else         if (fwRule.getPrivateIpAddress().equals(privateIpAddress) && fwRule.getPrivatePort().equals(loadBalancer.getPrivatePort()) && fwRule.isEnabled()) {
          continue;
        }
      }
    }
    FirewallRuleVO newFwRule=new FirewallRuleVO();
    newFwRule.setAlgorithm(loadBalancer.getAlgorithm());
    newFwRule.setEnabled(true);
    newFwRule.setForwarding(false);
    newFwRule.setPrivatePort(loadBalancer.getPrivatePort());
    newFwRule.setPublicPort(loadBalancer.getPublicPort());
    newFwRule.setPublicIpAddress(loadBalancer.getIpAddress());
    newFwRule.setPrivateIpAddress(userVm.getGuestIpAddress());
    newFwRule.setGroupId(loadBalancer.getId());
    firewallRulesToApply.add(newFwRule);
  }
  if (firewallRulesToApply.isEmpty()) {
    return true;
  }
  IPAddressVO ipAddr=_ipAddressDao.findById(loadBalancer.getIpAddress());
  List<IPAddressVO> ipAddrs=listPublicIpAddressesInVirtualNetwork(accountId,ipAddr.getDataCenterId(),null);
  for (  IPAddressVO ipv : ipAddrs) {
    List<FirewallRuleVO> rules=_rulesDao.listIpForwardingRulesForLoadBalancers(ipv.getAddress());
    firewallRulesToApply.addAll(rules);
  }
  txn.start();
  List<FirewallRuleVO> updatedRules=null;
  if (router.getState().equals(State.Starting)) {
    updatedRules=firewallRulesToApply;
  }
 else {
    updatedRules=updateFirewallRules(loadBalancer.getIpAddress(),firewallRulesToApply,router);
  }
  String description;
  String type=EventTypes.EVENT_NET_RULE_ADD;
  String ruleName="String_Node_Str";
  String level=EventVO.LEVEL_INFO;
  LoadBalancerVO loadBalancerLock=null;
  try {
    loadBalancerLock=_loadBalancerDao.acquireInLockTable(loadBalancerId);
    if (loadBalancerLock == null) {
      s_logger.warn("String_Node_Str" + loadBalancerId + "String_Node_Str");
    }
    if ((updatedRules != null) && (updatedRules.size() == firewallRulesToApply.size())) {
      for (      Long addedInstanceId : finalInstanceIds) {
        LoadBalancerVMMapVO mappedVM=new LoadBalancerVMMapVO(loadBalancerId,addedInstanceId);
        _loadBalancerVMMapDao.persist(mappedVM);
      }
      for (      FirewallRuleVO updatedRule : updatedRules) {
        _rulesDao.persist(updatedRule);
        description="String_Node_Str" + ruleName + "String_Node_Str"+ updatedRule.getPublicIpAddress()+ "String_Node_Str"+ updatedRule.getPublicPort()+ "String_Node_Str"+ updatedRule.getPrivateIpAddress()+ "String_Node_Str"+ updatedRule.getPrivatePort()+ "String_Node_Str"+ "String_Node_Str"+ updatedRule.getProtocol();
        EventUtils.saveEvent(UserContext.current().getUserId(),loadBalancer.getAccountId(),level,type,description);
      }
      txn.commit();
      return true;
    }
 else {
      _loadBalancerVMMapDao.remove(loadBalancerId,instanceIds,null);
      txn.commit();
      s_logger.warn("String_Node_Str" + loadBalancer.getName() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ StringUtils.join(instanceIds,"String_Node_Str"));
      throw new CloudRuntimeException("String_Node_Str" + loadBalancer.getName() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ StringUtils.join(instanceIds,"String_Node_Str"));
    }
  }
  finally {
    if (loadBalancerLock != null) {
      _loadBalancerDao.releaseFromLockTable(loadBalancerId);
    }
  }
}

Fixed version (ground truth):
@Override @DB public boolean assignToLoadBalancer(AssignToLoadBalancerRuleCmd cmd) throws NetworkRuleConflictException {
  Long loadBalancerId=cmd.getLoadBalancerId();
  Long instanceIdParam=cmd.getVirtualMachineId();
  List<Long> instanceIds=cmd.getVirtualMachineIds();
  if ((instanceIdParam == null) && (instanceIds == null)) {
    throw new InvalidParameterValueException("String_Node_Str" + loadBalancerId + "String_Node_Str");
  }
  if ((instanceIds == null) && (instanceIdParam != null)) {
    instanceIds=new ArrayList<Long>();
    instanceIds.add(instanceIdParam);
  }
  LoadBalancerVO loadBalancer=_loadBalancerDao.findById(loadBalancerId);
  if (loadBalancer == null) {
    throw new InvalidParameterValueException("String_Node_Str" + loadBalancerId + "String_Node_Str");
  }
  Account account=UserContext.current().getAccount();
  if (account != null) {
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
      if (!_domainDao.isChildDomain(account.getDomainId(),loadBalancer.getDomainId())) {
        throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
      }
    }
 else     if (account.getType() != Account.ACCOUNT_TYPE_ADMIN && account.getId() != loadBalancer.getAccountId()) {
      throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
    }
  }
  Transaction txn=Transaction.currentTxn();
  List<FirewallRuleVO> firewallRulesToApply=new ArrayList<FirewallRuleVO>();
  long accountId=0;
  DomainRouterVO router=null;
  List<LoadBalancerVMMapVO> mappedInstances=_loadBalancerVMMapDao.listByLoadBalancerId(loadBalancerId,false);
  Set<Long> mappedInstanceIds=new HashSet<Long>();
  if (mappedInstances != null) {
    for (    LoadBalancerVMMapVO mappedInstance : mappedInstances) {
      mappedInstanceIds.add(Long.valueOf(mappedInstance.getInstanceId()));
    }
  }
  List<Long> finalInstanceIds=new ArrayList<Long>();
  for (  Long instanceId : instanceIds) {
    if (mappedInstanceIds.contains(instanceId)) {
      continue;
    }
 else {
      finalInstanceIds.add(instanceId);
    }
    UserVmVO userVm=_vmDao.findById(instanceId);
    if (userVm == null) {
      s_logger.warn("String_Node_Str" + instanceId);
      throw new InvalidParameterValueException("String_Node_Str" + instanceId);
    }
 else {
      ServiceOfferingVO offering=_serviceOfferingDao.findById(userVm.getServiceOfferingId());
      if ((offering == null) || !GuestIpType.Virtualized.equals(offering.getGuestIpType())) {
        _loadBalancerVMMapDao.remove(loadBalancerId,instanceIds,Boolean.TRUE);
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + userVm.toString() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ ((offering == null) ? "String_Node_Str" : offering.getGuestIpType())+ "String_Node_Str");
        }
        throw new InvalidParameterValueException("String_Node_Str" + userVm.toString() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ ((offering == null) ? "String_Node_Str" : offering.getGuestIpType())+ "String_Node_Str");
      }
    }
    if (accountId == 0) {
      accountId=userVm.getAccountId();
    }
 else     if (accountId != userVm.getAccountId()) {
      s_logger.warn("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ userVm.getAccountId()+ "String_Node_Str"+ accountId);
      throw new InvalidParameterValueException("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ userVm.getAccountId()+ "String_Node_Str"+ accountId);
    }
    DomainRouterVO nextRouter=null;
    if (userVm.getDomainRouterId() != null)     nextRouter=_routerMgr.getRouter(userVm.getDomainRouterId());
    if (nextRouter == null) {
      s_logger.warn("String_Node_Str" + userVm.getDomainRouterId() + "String_Node_Str"+ instanceId);
      throw new InvalidParameterValueException("String_Node_Str" + userVm.getDomainRouterId() + "String_Node_Str"+ instanceId);
    }
    if (router == null) {
      router=nextRouter;
      if (router.getAccountId() != loadBalancer.getAccountId()) {
        throw new InvalidParameterValueException("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ loadBalancer.getName()+ "String_Node_Str"+ loadBalancer.getAccountId()+ "String_Node_Str");
      }
    }
 else     if (router.getId() != nextRouter.getId()) {
      throw new InvalidParameterValueException("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ nextRouter.getHostName()+ "String_Node_Str"+ router.getHostName());
    }
    String ipAddress=loadBalancer.getIpAddress();
    String privateIpAddress=userVm.getGuestIpAddress();
    List<FirewallRuleVO> existingRulesOnPubIp=_rulesDao.listIPForwarding(ipAddress);
    if (existingRulesOnPubIp != null) {
      for (      FirewallRuleVO fwRule : existingRulesOnPubIp) {
        if (!((fwRule.isForwarding() == false) && (fwRule.getGroupId() != null) && (fwRule.getGroupId() == loadBalancer.getId()))) {
          if (fwRule.getPublicPort().equals(loadBalancer.getPublicPort())) {
            throw new NetworkRuleConflictException("String_Node_Str" + ipAddress + "String_Node_Str"+ loadBalancer.getPublicPort()+ "String_Node_Str"+ loadBalancer.getName()+ "String_Node_Str"+ loadBalancer.getId()+ "String_Node_Str"+ userVm.getHostName()+ "String_Node_Str");
          }
        }
 else         if (fwRule.getPrivateIpAddress().equals(privateIpAddress) && fwRule.getPrivatePort().equals(loadBalancer.getPrivatePort()) && fwRule.isEnabled()) {
          continue;
        }
      }
    }
    FirewallRuleVO newFwRule=new FirewallRuleVO();
    newFwRule.setAlgorithm(loadBalancer.getAlgorithm());
    newFwRule.setEnabled(true);
    newFwRule.setForwarding(false);
    newFwRule.setPrivatePort(loadBalancer.getPrivatePort());
    newFwRule.setPublicPort(loadBalancer.getPublicPort());
    newFwRule.setPublicIpAddress(loadBalancer.getIpAddress());
    newFwRule.setPrivateIpAddress(userVm.getGuestIpAddress());
    newFwRule.setGroupId(loadBalancer.getId());
    firewallRulesToApply.add(newFwRule);
  }
  if (firewallRulesToApply.isEmpty()) {
    return true;
  }
  if (router == null) {
    throw new InvalidParameterValueException("String_Node_Str" + loadBalancerId + "String_Node_Str"+ loadBalancer.getIpAddress());
  }
 else {
    cmd.synchronizeCommand("String_Node_Str",router.getId());
  }
  IPAddressVO ipAddr=_ipAddressDao.findById(loadBalancer.getIpAddress());
  List<IPAddressVO> ipAddrs=listPublicIpAddressesInVirtualNetwork(accountId,ipAddr.getDataCenterId(),null);
  for (  IPAddressVO ipv : ipAddrs) {
    List<FirewallRuleVO> rules=_rulesDao.listIpForwardingRulesForLoadBalancers(ipv.getAddress());
    firewallRulesToApply.addAll(rules);
  }
  txn.start();
  List<FirewallRuleVO> updatedRules=null;
  if (router.getState().equals(State.Starting)) {
    updatedRules=firewallRulesToApply;
  }
 else {
    updatedRules=updateFirewallRules(loadBalancer.getIpAddress(),firewallRulesToApply,router);
  }
  String description;
  String type=EventTypes.EVENT_NET_RULE_ADD;
  String ruleName="String_Node_Str";
  String level=EventVO.LEVEL_INFO;
  LoadBalancerVO loadBalancerLock=null;
  try {
    loadBalancerLock=_loadBalancerDao.acquireInLockTable(loadBalancerId);
    if (loadBalancerLock == null) {
      s_logger.warn("String_Node_Str" + loadBalancerId + "String_Node_Str");
    }
    if ((updatedRules != null) && (updatedRules.size() == firewallRulesToApply.size())) {
      for (      Long addedInstanceId : finalInstanceIds) {
        LoadBalancerVMMapVO mappedVM=new LoadBalancerVMMapVO(loadBalancerId,addedInstanceId);
        _loadBalancerVMMapDao.persist(mappedVM);
      }
      for (      FirewallRuleVO updatedRule : updatedRules) {
        _rulesDao.persist(updatedRule);
        description="String_Node_Str" + ruleName + "String_Node_Str"+ updatedRule.getPublicIpAddress()+ "String_Node_Str"+ updatedRule.getPublicPort()+ "String_Node_Str"+ updatedRule.getPrivateIpAddress()+ "String_Node_Str"+ updatedRule.getPrivatePort()+ "String_Node_Str"+ "String_Node_Str"+ updatedRule.getProtocol();
        EventUtils.saveEvent(UserContext.current().getUserId(),loadBalancer.getAccountId(),level,type,description);
      }
      txn.commit();
      return true;
    }
 else {
      _loadBalancerVMMapDao.remove(loadBalancerId,instanceIds,null);
      txn.commit();
      s_logger.warn("String_Node_Str" + loadBalancer.getName() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ StringUtils.join(instanceIds,"String_Node_Str"));
      throw new CloudRuntimeException("String_Node_Str" + loadBalancer.getName() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ StringUtils.join(instanceIds,"String_Node_Str"));
    }
  }
  finally {
    if (loadBalancerLock != null) {
      _loadBalancerDao.releaseFromLockTable(loadBalancerId);
    }
  }
}