Program: 89152
Project: 89152
Buggy source:
@Override public boolean manageGuestNetworkWithExternalFirewall(boolean add,Network network) throws ResourceUnavailableException, InsufficientCapacityException {
  if (network.getTrafficType() != TrafficType.Guest) {
    s_logger.trace("String_Node_Str");
    return false;
  }
  long zoneId=network.getDataCenterId();
  DataCenterVO zone=_dcDao.findById(zoneId);
  HostVO externalFirewall=null;
  if (add) {
    GlobalLock deviceMapLock=GlobalLock.getInternLock("String_Node_Str");
    try {
      if (deviceMapLock.lock(120)) {
        try {
          ExternalFirewallDeviceVO device=findSuitableFirewallForNetwork(network);
          long externalFirewallId=device.getId();
          NetworkExternalFirewallVO networkFW=new NetworkExternalFirewallVO(network.getId(),externalFirewallId);
          _networkExternalFirewallDao.persist(networkFW);
          externalFirewall=_hostDao.findById(device.getHostId());
        }
  finally {
          deviceMapLock.unlock();
        }
      }
    }
  finally {
      deviceMapLock.releaseRef();
    }
  }
 else {
    ExternalFirewallDeviceVO fwDeviceVO=getExternalFirewallForNetwork(network);
    externalFirewall=_hostDao.findById(fwDeviceVO.getHostId());
  }
  Account account=_accountDao.findByIdIncludingRemoved(network.getAccountId());
  NetworkOffering offering=_networkOfferingDao.findById(network.getNetworkOfferingId());
  boolean sharedSourceNat=offering.getSharedSourceNat();
  IPAddressVO sourceNatIp=null;
  if (!sharedSourceNat) {
    List<IPAddressVO> sourceNatIps=_networkMgr.listPublicIpAddressesInVirtualNetwork(network.getAccountId(),zoneId,true,null);
    if (sourceNatIps.size() != 1) {
      String errorMsg="String_Node_Str" + account.getAccountName();
      s_logger.error(errorMsg);
      return true;
    }
 else {
      sourceNatIp=sourceNatIps.get(0);
    }
  }
  long guestVlanTag=Long.parseLong(network.getBroadcastUri().getHost());
  String guestVlanGateway=network.getGateway();
  String guestVlanCidr=network.getCidr();
  String sourceNatIpAddress=null;
  String publicVlanTag=null;
  if (sourceNatIp != null) {
    sourceNatIpAddress=sourceNatIp.getAddress().addr();
    VlanVO publicVlan=_vlanDao.findById(sourceNatIp.getVlanId());
    publicVlanTag=publicVlan.getVlanTag();
  }
  Integer networkRate=_networkMgr.getNetworkRate(network.getId(),null);
  IpAddressTO ip=new IpAddressTO(account.getAccountId(),sourceNatIpAddress,add,false,!sharedSourceNat,publicVlanTag,null,null,null,null,networkRate,false);
  IpAddressTO[] ips=new IpAddressTO[1];
  ips[0]=ip;
  IpAssocCommand cmd=new IpAssocCommand(ips);
  cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY,guestVlanGateway);
  cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR,guestVlanCidr);
  cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG,String.valueOf(guestVlanTag));
  Answer answer=_agentMgr.easySend(externalFirewall.getId(),cmd);
  if (answer == null || !answer.getResult()) {
    String action=add ? "String_Node_Str" : "String_Node_Str";
    String answerDetails=(answer != null) ? answer.getDetails() : "String_Node_Str";
    String msg="String_Node_Str" + action + "String_Node_Str"+ zone.getName()+ "String_Node_Str"+ answerDetails;
    s_logger.error(msg);
    throw new ResourceUnavailableException(msg,DataCenter.class,zoneId);
  }
  List<String> reservedIpAddressesForGuestNetwork=_nicDao.listIpAddressInNetwork(network.getId());
  if (add && (!reservedIpAddressesForGuestNetwork.contains(network.getGateway()))) {
    savePlaceholderNic(network,network.getGateway());
  }
  List<NicVO> nicsInNetwork=_nicDao.listByNetworkId(network.getId());
  for (  NicVO nic : nicsInNetwork) {
    InlineLoadBalancerNicMapVO mapping=_inlineLoadBalancerNicMapDao.findByNicId(nic.getId());
    if (mapping != null) {
      _nicDao.expunge(mapping.getNicId());
      _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
    }
  }
  String action=add ? "String_Node_Str" : "String_Node_Str";
  s_logger.debug("String_Node_Str" + action + "String_Node_Str"+ account.getAccountName()+ "String_Node_Str"+ account.getAccountId()+ "String_Node_Str"+ guestVlanTag);
  return true;
}

Fixed version (ground truth):
@Override public boolean manageGuestNetworkWithExternalFirewall(boolean add,Network network) throws ResourceUnavailableException, InsufficientCapacityException {
  if (network.getTrafficType() != TrafficType.Guest) {
    s_logger.trace("String_Node_Str");
    return false;
  }
  long zoneId=network.getDataCenterId();
  DataCenterVO zone=_dcDao.findById(zoneId);
  HostVO externalFirewall=null;
  if (add) {
    GlobalLock deviceMapLock=GlobalLock.getInternLock("String_Node_Str");
    try {
      if (deviceMapLock.lock(120)) {
        try {
          ExternalFirewallDeviceVO device=findSuitableFirewallForNetwork(network);
          long externalFirewallId=device.getId();
          NetworkExternalFirewallVO networkFW=new NetworkExternalFirewallVO(network.getId(),externalFirewallId);
          _networkExternalFirewallDao.persist(networkFW);
          externalFirewall=_hostDao.findById(device.getHostId());
        }
  finally {
          deviceMapLock.unlock();
        }
      }
    }
  finally {
      deviceMapLock.releaseRef();
    }
  }
 else {
    ExternalFirewallDeviceVO fwDeviceVO=getExternalFirewallForNetwork(network);
    if (fwDeviceVO == null) {
      s_logger.warn("String_Node_Str" + "String_Node_Str");
      return true;
    }
    externalFirewall=_hostDao.findById(fwDeviceVO.getHostId());
  }
  Account account=_accountDao.findByIdIncludingRemoved(network.getAccountId());
  NetworkOffering offering=_networkOfferingDao.findById(network.getNetworkOfferingId());
  boolean sharedSourceNat=offering.getSharedSourceNat();
  IPAddressVO sourceNatIp=null;
  if (!sharedSourceNat) {
    List<IPAddressVO> sourceNatIps=_networkMgr.listPublicIpAddressesInVirtualNetwork(network.getAccountId(),zoneId,true,null);
    if (sourceNatIps.size() != 1) {
      String errorMsg="String_Node_Str" + account.getAccountName();
      s_logger.error(errorMsg);
      return true;
    }
 else {
      sourceNatIp=sourceNatIps.get(0);
    }
  }
  long guestVlanTag=Long.parseLong(network.getBroadcastUri().getHost());
  String guestVlanGateway=network.getGateway();
  String guestVlanCidr=network.getCidr();
  String sourceNatIpAddress=null;
  String publicVlanTag=null;
  if (sourceNatIp != null) {
    sourceNatIpAddress=sourceNatIp.getAddress().addr();
    VlanVO publicVlan=_vlanDao.findById(sourceNatIp.getVlanId());
    publicVlanTag=publicVlan.getVlanTag();
  }
  Integer networkRate=_networkMgr.getNetworkRate(network.getId(),null);
  IpAddressTO ip=new IpAddressTO(account.getAccountId(),sourceNatIpAddress,add,false,!sharedSourceNat,publicVlanTag,null,null,null,null,networkRate,false);
  IpAddressTO[] ips=new IpAddressTO[1];
  ips[0]=ip;
  IpAssocCommand cmd=new IpAssocCommand(ips);
  cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY,guestVlanGateway);
  cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR,guestVlanCidr);
  cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG,String.valueOf(guestVlanTag));
  Answer answer=_agentMgr.easySend(externalFirewall.getId(),cmd);
  if (answer == null || !answer.getResult()) {
    String action=add ? "String_Node_Str" : "String_Node_Str";
    String answerDetails=(answer != null) ? answer.getDetails() : "String_Node_Str";
    String msg="String_Node_Str" + action + "String_Node_Str"+ zone.getName()+ "String_Node_Str"+ answerDetails;
    s_logger.error(msg);
    throw new ResourceUnavailableException(msg,DataCenter.class,zoneId);
  }
  List<String> reservedIpAddressesForGuestNetwork=_nicDao.listIpAddressInNetwork(network.getId());
  if (add && (!reservedIpAddressesForGuestNetwork.contains(network.getGateway()))) {
    savePlaceholderNic(network,network.getGateway());
  }
  List<NicVO> nicsInNetwork=_nicDao.listByNetworkId(network.getId());
  for (  NicVO nic : nicsInNetwork) {
    InlineLoadBalancerNicMapVO mapping=_inlineLoadBalancerNicMapDao.findByNicId(nic.getId());
    if (mapping != null) {
      _nicDao.expunge(mapping.getNicId());
      _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
    }
  }
  String action=add ? "String_Node_Str" : "String_Node_Str";
  s_logger.debug("String_Node_Str" + action + "String_Node_Str"+ account.getAccountName()+ "String_Node_Str"+ account.getAccountId()+ "String_Node_Str"+ guestVlanTag);
  return true;
}