Program: 89567
Project: 89567
Buggy source:
protected void assignPublicIpAddress(Connection conn,final String vmName,final String privateIpAddress,final String publicIpAddress,final boolean add,final boolean firstIP,final boolean sourceNat,final String vlanId,final String vlanGateway,final String vlanNetmask,final String vifMacAddress,String guestIp,Integer networkRate) throws InternalErrorException {
  try {
    VM router=getVM(conn,vmName);
    VIF correctVif=getCorrectVif(conn,router,vlanId);
    boolean addVif=false;
    boolean removeVif=false;
    if (add && correctVif == null) {
      addVif=true;
    }
 else     if (!add && firstIP) {
      removeVif=true;
    }
    if (addVif) {
      String vifDeviceNum=getLowestAvailableVIFDeviceNum(conn,router);
      if (vifDeviceNum == null) {
        throw new InternalErrorException("String_Node_Str" + router.getNameLabel(conn));
      }
      NicTO nic=new NicTO();
      nic.setMac(vifMacAddress);
      nic.setType(TrafficType.Public);
      if (vlanId == null) {
        nic.setBroadcastType(BroadcastDomainType.Native);
      }
 else {
        nic.setBroadcastType(BroadcastDomainType.Vlan);
        nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
      }
      nic.setDeviceId(Integer.parseInt(vifDeviceNum));
      nic.setNetworkRateMbps(networkRate);
      correctVif=createVif(conn,vmName,router,nic);
      correctVif.plug(conn);
      networkUsage(conn,privateIpAddress,"String_Node_Str","String_Node_Str" + correctVif.getDevice(conn));
    }
    if (correctVif == null) {
      throw new InternalErrorException("String_Node_Str");
    }
    String args=privateIpAddress;
    if (add) {
      args+="String_Node_Str";
    }
 else {
      args+="String_Node_Str";
    }
    String cidrSize=Long.toString(NetUtils.getCidrSize(vlanNetmask));
    if (sourceNat) {
      args+="String_Node_Str";
      args+="String_Node_Str";
      args+=publicIpAddress + "String_Node_Str" + cidrSize;
    }
 else     if (firstIP) {
      args+="String_Node_Str";
      args+="String_Node_Str";
      args+=publicIpAddress + "String_Node_Str" + cidrSize;
    }
 else {
      args+="String_Node_Str";
      args+=publicIpAddress;
    }
    args+="String_Node_Str";
    args+="String_Node_Str" + correctVif.getDevice(conn);
    String result=callHostPlugin(conn,"String_Node_Str","String_Node_Str","String_Node_Str",args);
    if (result == null || result.isEmpty()) {
      throw new InternalErrorException("String_Node_Str");
    }
    if (removeVif) {
      Network network=correctVif.getNetwork(conn);
      networkUsage(conn,privateIpAddress,"String_Node_Str","String_Node_Str" + correctVif.getDevice(conn));
      correctVif.unplug(conn);
      correctVif.destroy(conn);
      disableVlanNetwork(conn,network);
    }
  }
 catch (  XenAPIException e) {
    String msg="String_Node_Str" + e.toString();
    s_logger.warn(msg,e);
    throw new InternalErrorException(msg);
  }
catch (  final XmlRpcException e) {
    String msg="String_Node_Str" + e.getMessage();
    s_logger.warn(msg,e);
    throw new InternalErrorException(msg);
  }
}

Fixed version (ground truth):
protected void assignPublicIpAddress(Connection conn,String vmName,String privateIpAddress,String publicIpAddress,boolean add,boolean firstIP,boolean sourceNat,String vlanId,String vlanGateway,String vlanNetmask,String vifMacAddress,String guestIp,Integer networkRate,TrafficType trafficType,String[] tags) throws InternalErrorException {
  try {
    String tag=tags != null && tags.length > 0 ? tags[0] : null;
    VM router=getVM(conn,vmName);
    NicTO nic=new NicTO();
    nic.setMac(vifMacAddress);
    nic.setType(trafficType);
    if (vlanId == null) {
      nic.setBroadcastType(BroadcastDomainType.Native);
    }
 else {
      nic.setBroadcastType(BroadcastDomainType.Vlan);
      nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
    }
    nic.setDeviceId(0);
    nic.setNetworkRateMbps(networkRate);
    if (tags != null) {
      nic.setTags(Arrays.asList(tags));
    }
    Network network=getNetwork(conn,nic);
    VIF correctVif=getCorrectVif(conn,router,network);
    boolean addVif=false;
    boolean removeVif=false;
    if (add && correctVif == null) {
      addVif=true;
    }
 else     if (!add && firstIP) {
      removeVif=true;
    }
    if (addVif) {
      String vifDeviceNum=getLowestAvailableVIFDeviceNum(conn,router);
      if (vifDeviceNum == null) {
        throw new InternalErrorException("String_Node_Str" + router.getNameLabel(conn));
      }
      nic.setDeviceId(Integer.parseInt(vifDeviceNum));
      correctVif=createVif(conn,vmName,router,nic);
      correctVif.plug(conn);
      networkUsage(conn,privateIpAddress,"String_Node_Str","String_Node_Str" + correctVif.getDevice(conn));
    }
    if (correctVif == null) {
      throw new InternalErrorException("String_Node_Str");
    }
    String args=privateIpAddress;
    if (add) {
      args+="String_Node_Str";
    }
 else {
      args+="String_Node_Str";
    }
    String cidrSize=Long.toString(NetUtils.getCidrSize(vlanNetmask));
    if (sourceNat) {
      args+="String_Node_Str";
      args+="String_Node_Str";
      args+=publicIpAddress + "String_Node_Str" + cidrSize;
    }
 else     if (firstIP) {
      args+="String_Node_Str";
      args+="String_Node_Str";
      args+=publicIpAddress + "String_Node_Str" + cidrSize;
    }
 else {
      args+="String_Node_Str";
      args+=publicIpAddress;
    }
    args+="String_Node_Str";
    args+="String_Node_Str" + correctVif.getDevice(conn);
    String result=callHostPlugin(conn,"String_Node_Str","String_Node_Str","String_Node_Str",args);
    if (result == null || result.isEmpty()) {
      throw new InternalErrorException("String_Node_Str");
    }
    if (removeVif) {
      network=correctVif.getNetwork(conn);
      networkUsage(conn,privateIpAddress,"String_Node_Str","String_Node_Str" + correctVif.getDevice(conn));
      correctVif.unplug(conn);
      correctVif.destroy(conn);
      disableVlanNetwork(conn,network);
    }
  }
 catch (  XenAPIException e) {
    String msg="String_Node_Str" + e.toString();
    s_logger.warn(msg,e);
    throw new InternalErrorException(msg);
  }
catch (  final XmlRpcException e) {
    String msg="String_Node_Str" + e.getMessage();
    s_logger.warn(msg,e);
    throw new InternalErrorException(msg);
  }
}