Program: 106584
Project: 106584
Buggy source:
@Override public boolean enableStaticNat(long ipId,long vmId) throws NetworkRuleConflictException {
  Account caller=UserContext.current().getCaller();
  UserVmVO vm=_vmDao.findById(vmId);
  if (vm == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str"+ vmId+ "String_Node_Str");
  }
  IPAddressVO ipAddress=_ipAddressDao.findById(ipId);
  if (ipAddress == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId);
  }
  checkIpAndUserVm(ipAddress,vm,caller);
  Long networkId=ipAddress.getAssociatedWithNetworkId();
  if (networkId == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str");
  }
  Nic guestNic=_networkMgr.getNicInNetwork(vmId,networkId);
  if (guestNic == null) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId);
  }
  Network network=_networkMgr.getNetwork(networkId);
  if (!_networkMgr.areServicesSupportedInNetwork(network.getId(),Service.Firewall)) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId);
  }
  if (ipAddress.isSourceNat()) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str");
  }
  if (!ipAddress.isOneToOneNat()) {
    List<FirewallRuleVO> portForwardingRules=_firewallDao.listByIpAndPurposeAndNotRevoked(ipId,Purpose.PortForwarding);
    if (portForwardingRules != null && !portForwardingRules.isEmpty()) {
      throw new NetworkRuleConflictException("String_Node_Str" + ipId + "String_Node_Str");
    }
    List<FirewallRuleVO> loadBalancingRules=_firewallDao.listByIpAndPurposeAndNotRevoked(ipId,Purpose.LoadBalancing);
    if (loadBalancingRules != null && !loadBalancingRules.isEmpty()) {
      throw new NetworkRuleConflictException("String_Node_Str" + ipId + "String_Node_Str");
    }
  }
 else {
    if (ipAddress.getAssociatedWithVmId() != null && ipAddress.getAssociatedWithVmId().longValue() != vmId) {
      throw new NetworkRuleConflictException("String_Node_Str" + ipId + "String_Node_Str"+ vmId+ "String_Node_Str");
    }
  }
  IPAddressVO ip=_ipAddressDao.findByAssociatedVmId(vmId);
  if (ip != null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str"+ vmId+ "String_Node_Str"+ ip.getId());
  }
  _networkMgr.checkIpForService(ip,Service.StaticNat);
  ipAddress.setOneToOneNat(true);
  ipAddress.setAssociatedWithVmId(vmId);
  if (_ipAddressDao.update(ipAddress.getId(),ipAddress)) {
    s_logger.trace("String_Node_Str" + ipAddress + "String_Node_Str"+ vmId+ "String_Node_Str");
    if (applyStaticNatForIp(ipId,false,caller,false)) {
      return true;
    }
 else {
      ipAddress.setOneToOneNat(false);
      ipAddress.setAssociatedWithVmId(null);
      _ipAddressDao.update(ipAddress.getId(),ipAddress);
      s_logger.warn("String_Node_Str" + ipId + "String_Node_Str");
      return false;
    }
  }
 else {
    s_logger.warn("String_Node_Str" + ipAddress + "String_Node_Str");
    return false;
  }
}

Fixed version (ground truth):
@Override public boolean enableStaticNat(long ipId,long vmId) throws NetworkRuleConflictException {
  Account caller=UserContext.current().getCaller();
  UserVmVO vm=_vmDao.findById(vmId);
  if (vm == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str"+ vmId+ "String_Node_Str");
  }
  IPAddressVO ipAddress=_ipAddressDao.findById(ipId);
  if (ipAddress == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId);
  }
  checkIpAndUserVm(ipAddress,vm,caller);
  Long networkId=ipAddress.getAssociatedWithNetworkId();
  if (networkId == null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str");
  }
  Nic guestNic=_networkMgr.getNicInNetwork(vmId,networkId);
  if (guestNic == null) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId);
  }
  Network network=_networkMgr.getNetwork(networkId);
  if (!_networkMgr.areServicesSupportedInNetwork(network.getId(),Service.Firewall)) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId);
  }
  if (ipAddress.isSourceNat()) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str");
  }
  if (!ipAddress.isOneToOneNat()) {
    List<FirewallRuleVO> portForwardingRules=_firewallDao.listByIpAndPurposeAndNotRevoked(ipId,Purpose.PortForwarding);
    if (portForwardingRules != null && !portForwardingRules.isEmpty()) {
      throw new NetworkRuleConflictException("String_Node_Str" + ipId + "String_Node_Str");
    }
    List<FirewallRuleVO> loadBalancingRules=_firewallDao.listByIpAndPurposeAndNotRevoked(ipId,Purpose.LoadBalancing);
    if (loadBalancingRules != null && !loadBalancingRules.isEmpty()) {
      throw new NetworkRuleConflictException("String_Node_Str" + ipId + "String_Node_Str");
    }
  }
 else {
    if (ipAddress.getAssociatedWithVmId() != null && ipAddress.getAssociatedWithVmId().longValue() != vmId) {
      throw new NetworkRuleConflictException("String_Node_Str" + ipId + "String_Node_Str"+ vmId+ "String_Node_Str");
    }
  }
  IPAddressVO ip=_ipAddressDao.findByAssociatedVmId(vmId);
  if (ip != null) {
    throw new InvalidParameterValueException("String_Node_Str" + ipId + "String_Node_Str"+ vmId+ "String_Node_Str"+ ip.getId());
  }
  _networkMgr.checkIpForService(ipAddress,Service.StaticNat);
  ipAddress.setOneToOneNat(true);
  ipAddress.setAssociatedWithVmId(vmId);
  if (_ipAddressDao.update(ipAddress.getId(),ipAddress)) {
    s_logger.trace("String_Node_Str" + ipAddress + "String_Node_Str"+ vmId+ "String_Node_Str");
    if (applyStaticNatForIp(ipId,false,caller,false)) {
      return true;
    }
 else {
      ipAddress.setOneToOneNat(false);
      ipAddress.setAssociatedWithVmId(null);
      _ipAddressDao.update(ipAddress.getId(),ipAddress);
      s_logger.warn("String_Node_Str" + ipId + "String_Node_Str");
      return false;
    }
  }
 else {
    s_logger.warn("String_Node_Str" + ipAddress + "String_Node_Str");
    return false;
  }
}