Program: 107447
Project: 107447
Buggy source:
@Override @DB public void assignToLoadBalancer(AssignToLoadBalancerRuleCmd cmd) throws NetworkRuleConflictException, InternalErrorException, PermissionDeniedException, InvalidParameterValueException {
  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());
  cmd.synchronizeCommand("String_Node_Str",syncObject.getId());
  Account account=(Account)UserContext.current().getAccountObject();
  if (account != null) {
    if ((account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || (account.getType() == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)) {
      if (!_domainDao.isChildDomain(account.getDomainId(),loadBalancer.getDomainId())) {
        throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
      }
    }
 else     if (account.getId() != loadBalancer.getAccountId()) {
      throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
    }
  }
  Transaction txn=Transaction.currentTxn();
  try {
    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()));
      }
    }
    for (    Long instanceId : instanceIds) {
      if (mappedInstanceIds.contains(instanceId)) {
        continue;
      }
      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.getName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ userVm.getAccountId()+ "String_Node_Str"+ accountId);
        throw new InvalidParameterValueException("String_Node_Str" + userVm.getName() + "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.getName() + "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.getName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ nextRouter.getName()+ "String_Node_Str"+ router.getName());
      }
      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().longValue()))) {
            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.getName()+ "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;
    }
    IPAddressVO ipAddr=_ipAddressDao.findById(loadBalancer.getIpAddress());
    List<IPAddressVO> ipAddrs=listPublicIpAddressesInVirtualNetwork(accountId,ipAddr.getDataCenterId(),null);
    for (    IPAddressVO ipv : ipAddrs) {
      List<FirewallRuleVO> rules=_rulesDao.listIPForwarding(ipv.getAddress(),false);
      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.acquire(loadBalancerId);
      if (loadBalancerLock == null) {
        s_logger.warn("String_Node_Str" + loadBalancerId + "String_Node_Str");
      }
      if ((updatedRules != null) && (updatedRules.size() == firewallRulesToApply.size())) {
        List<LoadBalancerVMMapVO> pendingMappedVMs=_loadBalancerVMMapDao.listByLoadBalancerId(loadBalancerId,true);
        for (        LoadBalancerVMMapVO pendingMappedVM : pendingMappedVMs) {
          if (instanceIds.contains(pendingMappedVM.getInstanceId())) {
            LoadBalancerVMMapVO pendingMappedVMForUpdate=_loadBalancerVMMapDao.createForUpdate();
            pendingMappedVMForUpdate.setPending(false);
            _loadBalancerVMMapDao.update(pendingMappedVM.getId(),pendingMappedVMForUpdate);
          }
        }
        for (        FirewallRuleVO updatedRule : updatedRules) {
          if (updatedRule.getId() == null) {
            _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);
          }
        }
      }
 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 InternalErrorException("String_Node_Str" + loadBalancer.getName() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ StringUtils.join(instanceIds,"String_Node_Str"));
      }
    }
  finally {
      if (loadBalancerLock != null) {
        _loadBalancerDao.release(loadBalancerId);
      }
    }
    txn.commit();
  }
 catch (  Throwable e) {
    txn.rollback();
    if (e instanceof NetworkRuleConflictException) {
      throw (NetworkRuleConflictException)e;
    }
 else     if (e instanceof InvalidParameterValueException) {
      throw (InvalidParameterValueException)e;
    }
 else     if (e instanceof PermissionDeniedException) {
      throw (PermissionDeniedException)e;
    }
 else     if (e instanceof InternalErrorException) {
      s_logger.warn("String_Node_Str",e);
      throw (InternalErrorException)e;
    }
    s_logger.warn("String_Node_Str",e);
  }
}

Fixed version (ground truth):
@Override @DB public void assignToLoadBalancer(AssignToLoadBalancerRuleCmd cmd) throws NetworkRuleConflictException, InternalErrorException, PermissionDeniedException, InvalidParameterValueException {
  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());
  cmd.synchronizeCommand("String_Node_Str",syncObject.getId());
  Account account=(Account)UserContext.current().getAccountObject();
  if (account != null) {
    if ((account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || (account.getType() == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)) {
      if (!_domainDao.isChildDomain(account.getDomainId(),loadBalancer.getDomainId())) {
        throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
      }
    }
 else     if (account.getId() != loadBalancer.getAccountId()) {
      throw new PermissionDeniedException("String_Node_Str" + loadBalancerId + "String_Node_Str");
    }
  }
  Transaction txn=Transaction.currentTxn();
  try {
    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.getName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ userVm.getAccountId()+ "String_Node_Str"+ accountId);
        throw new InvalidParameterValueException("String_Node_Str" + userVm.getName() + "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.getName() + "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.getName() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ nextRouter.getName()+ "String_Node_Str"+ router.getName());
      }
      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().longValue()))) {
            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.getName()+ "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;
    }
    IPAddressVO ipAddr=_ipAddressDao.findById(loadBalancer.getIpAddress());
    List<IPAddressVO> ipAddrs=listPublicIpAddressesInVirtualNetwork(accountId,ipAddr.getDataCenterId(),null);
    for (    IPAddressVO ipv : ipAddrs) {
      List<FirewallRuleVO> rules=_rulesDao.listIPForwarding(ipv.getAddress(),false);
      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.acquire(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) {
          if (updatedRule.getId() == null) {
            _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);
          }
        }
      }
 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 InternalErrorException("String_Node_Str" + loadBalancer.getName() + "String_Node_Str"+ loadBalancerId+ "String_Node_Str"+ StringUtils.join(instanceIds,"String_Node_Str"));
      }
    }
  finally {
      if (loadBalancerLock != null) {
        _loadBalancerDao.release(loadBalancerId);
      }
    }
    txn.commit();
  }
 catch (  Throwable e) {
    txn.rollback();
    if (e instanceof NetworkRuleConflictException) {
      throw (NetworkRuleConflictException)e;
    }
 else     if (e instanceof InvalidParameterValueException) {
      throw (InvalidParameterValueException)e;
    }
 else     if (e instanceof PermissionDeniedException) {
      throw (PermissionDeniedException)e;
    }
 else     if (e instanceof InternalErrorException) {
      s_logger.warn("String_Node_Str",e);
      throw (InternalErrorException)e;
    }
    s_logger.warn("String_Node_Str",e);
  }
}