Program: 106008
Project: 106008
Buggy source:
@DB @Override public IPAddressVO associateIPToGuestNetwork(long ipId,long networkId,boolean releaseOnFailure) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException {
  Account caller=CallContext.current().getCallingAccount();
  Account owner=null;
  IPAddressVO ipToAssoc=_ipAddressDao.findById(ipId);
  if (ipToAssoc != null) {
    Network network=_networksDao.findById(networkId);
    if (network == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DataCenter zone=_entityMgr.findById(DataCenter.class,network.getDataCenterId());
    if (zone.getNetworkType() == NetworkType.Advanced) {
      if (network.getGuestType() == Network.GuestType.Shared) {
        if (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
          _accountMgr.checkAccess(CallContext.current().getCallingAccount(),AccessType.UseNetwork,false,network);
        }
 else {
          throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
        }
      }
    }
 else {
      _accountMgr.checkAccess(caller,null,true,ipToAssoc);
    }
    owner=_accountMgr.getAccount(ipToAssoc.getAllocatedToAccountId());
  }
 else {
    s_logger.debug("String_Node_Str" + ipId);
    return null;
  }
  if (ipToAssoc.getAssociatedWithNetworkId() != null) {
    s_logger.debug("String_Node_Str" + ipToAssoc + "String_Node_Str"+ networkId);
    return ipToAssoc;
  }
  Network network=_networksDao.findById(networkId);
  if (network != null) {
    _accountMgr.checkAccess(owner,AccessType.UseNetwork,false,network);
  }
 else {
    s_logger.debug("String_Node_Str" + ipId);
    return null;
  }
  DataCenter zone=_entityMgr.findById(DataCenter.class,network.getDataCenterId());
  if (network.getTrafficType() != TrafficType.Guest) {
    throw new InvalidParameterValueException("String_Node_Str" + TrafficType.Guest);
  }
  if (network.getAccountId() != owner.getId()) {
    if (zone.getNetworkType() != NetworkType.Basic && !(zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Shared)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (zone.getNetworkType() == NetworkType.Advanced) {
    if (network.getGuestType() == GuestType.Isolated && !(_networkModel.areServicesSupportedInNetwork(network.getId(),Service.SourceNat))) {
      throw new InvalidParameterValueException("String_Node_Str" + NetworkType.Advanced + "String_Node_Str"+ GuestType.Isolated+ "String_Node_Str"+ Service.SourceNat.getName()+ "String_Node_Str");
    }
    if (network.getGuestType() == GuestType.Shared && !isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
      throw new InvalidParameterValueException("String_Node_Str" + NetworkType.Advanced + "String_Node_Str"+ GuestType.Shared+ "String_Node_Str"+ "String_Node_Str"+ Service.SourceNat.getName()+ "String_Node_Str"+ Service.StaticNat.getName()+ "String_Node_Str"+ Service.Lb.getName()+ "String_Node_Str"+ Service.PortForwarding.getName()+ "String_Node_Str");
    }
  }
  NetworkOffering offering=_networkOfferingDao.findById(network.getNetworkOfferingId());
  boolean sharedSourceNat=offering.getSharedSourceNat();
  boolean isSourceNat=false;
  if (!sharedSourceNat) {
    if (getExistingSourceNatInNetwork(owner.getId(),networkId) == null) {
      if (network.getGuestType() == GuestType.Isolated && network.getVpcId() == null && !ipToAssoc.isPortable()) {
        isSourceNat=true;
      }
    }
  }
  s_logger.debug("String_Node_Str" + ipToAssoc + "String_Node_Str"+ network);
  IPAddressVO ip=_ipAddressDao.findById(ipId);
  ip.setAssociatedWithNetworkId(networkId);
  ip.setSourceNat(isSourceNat);
  _ipAddressDao.update(ipId,ip);
  boolean success=false;
  try {
    success=applyIpAssociations(network,false);
    if (success) {
      s_logger.debug("String_Node_Str" + ip.getAddress().addr() + "String_Node_Str"+ network);
    }
 else {
      s_logger.warn("String_Node_Str" + ip.getAddress().addr() + "String_Node_Str"+ network);
    }
    return ip;
  }
  finally {
    if (!success && releaseOnFailure) {
      if (ip != null) {
        try {
          s_logger.warn("String_Node_Str" + ip);
          _ipAddressDao.markAsUnavailable(ip.getId());
          if (!applyIpAssociations(network,true)) {
            _ipAddressDao.unassignIpAddress(ip.getId());
          }
        }
 catch (        Exception e) {
          s_logger.warn("String_Node_Str",e);
        }
      }
    }
  }
}

Fixed version (ground truth):
@DB @Override public IPAddressVO associateIPToGuestNetwork(long ipId,long networkId,boolean releaseOnFailure) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException {
  Account caller=CallContext.current().getCallingAccount();
  Account owner=null;
  IPAddressVO ipToAssoc=_ipAddressDao.findById(ipId);
  if (ipToAssoc != null) {
    Network network=_networksDao.findById(networkId);
    if (network == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DataCenter zone=_entityMgr.findById(DataCenter.class,network.getDataCenterId());
    if (zone.getNetworkType() == NetworkType.Advanced) {
      if (network.getGuestType() == Network.GuestType.Shared) {
        if (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
          _accountMgr.checkAccess(CallContext.current().getCallingAccount(),AccessType.UseNetwork,false,network);
        }
 else {
          throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
        }
      }
    }
 else {
      _accountMgr.checkAccess(caller,null,true,ipToAssoc);
    }
    owner=_accountMgr.getAccount(ipToAssoc.getAllocatedToAccountId());
  }
 else {
    s_logger.debug("String_Node_Str" + ipId);
    return null;
  }
  if (ipToAssoc.getAssociatedWithNetworkId() != null) {
    s_logger.debug("String_Node_Str" + ipToAssoc + "String_Node_Str"+ networkId);
    return ipToAssoc;
  }
  Network network=_networksDao.findById(networkId);
  if (network != null) {
    _accountMgr.checkAccess(owner,AccessType.UseNetwork,false,network);
  }
 else {
    s_logger.debug("String_Node_Str" + ipId);
    return null;
  }
  DataCenter zone=_entityMgr.findById(DataCenter.class,network.getDataCenterId());
  if (network.getTrafficType() != TrafficType.Guest) {
    throw new InvalidParameterValueException("String_Node_Str" + TrafficType.Guest);
  }
  if (network.getAccountId() != owner.getId()) {
    if (zone.getNetworkType() != NetworkType.Basic && !(zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Shared)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (zone.getNetworkType() == NetworkType.Advanced) {
    if (network.getGuestType() == GuestType.Isolated && !(_networkModel.areServicesSupportedInNetwork(network.getId(),Service.SourceNat))) {
      throw new InvalidParameterValueException("String_Node_Str" + NetworkType.Advanced + "String_Node_Str"+ GuestType.Isolated+ "String_Node_Str"+ Service.SourceNat.getName()+ "String_Node_Str");
    }
    if (network.getGuestType() == GuestType.Shared && !isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
      throw new InvalidParameterValueException("String_Node_Str" + NetworkType.Advanced + "String_Node_Str"+ GuestType.Shared+ "String_Node_Str"+ "String_Node_Str"+ Service.SourceNat.getName()+ "String_Node_Str"+ Service.StaticNat.getName()+ "String_Node_Str"+ Service.Lb.getName()+ "String_Node_Str"+ Service.PortForwarding.getName()+ "String_Node_Str");
    }
  }
  NetworkOffering offering=_networkOfferingDao.findById(network.getNetworkOfferingId());
  boolean sharedSourceNat=offering.getSharedSourceNat();
  boolean isSourceNat=false;
  if (!sharedSourceNat) {
    if (getExistingSourceNatInNetwork(owner.getId(),networkId) == null) {
      if (network.getGuestType() == GuestType.Isolated && network.getVpcId() == null && !ipToAssoc.isPortable()) {
        isSourceNat=true;
      }
    }
  }
  s_logger.debug("String_Node_Str" + ipToAssoc + "String_Node_Str"+ network);
  IPAddressVO ip=ipToAssoc;
  ip.setAssociatedWithNetworkId(networkId);
  ip.setSourceNat(isSourceNat);
  _ipAddressDao.update(ipId,ip);
  boolean success=false;
  try {
    success=applyIpAssociations(network,false);
    if (success) {
      s_logger.debug("String_Node_Str" + ip.getAddress().addr() + "String_Node_Str"+ network);
    }
 else {
      s_logger.warn("String_Node_Str" + ip.getAddress().addr() + "String_Node_Str"+ network);
    }
    return ip;
  }
  finally {
    if (!success && releaseOnFailure) {
      try {
        s_logger.warn("String_Node_Str" + ip);
        _ipAddressDao.markAsUnavailable(ip.getId());
        if (!applyIpAssociations(network,true)) {
          _ipAddressDao.unassignIpAddress(ip.getId());
        }
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str",e);
      }
    }
  }
}