Program: 48939
Project: 48939
Buggy source:
public void callInternal() throws Exception {
  final VcVirtualMachine template=VcCache.get(vmSchema.diskSchema.getParent());
  VcSnapshot snap=template.getSnapshotByName(vmSchema.diskSchema.getParentSnap());
  ConfigSpecImpl configSpec=new ConfigSpecImpl();
  ResourceSchemaUtil.setResourceSchema(configSpec,vmSchema.resourceSchema);
  HashMap<String,Disk.Operation> diskMap=new HashMap<String,Disk.Operation>();
  if (requireClone()) {
    VcVirtualMachine.CreateSpec vmSpec=new VcVirtualMachine.CreateSpec(newVmName,snap,targetRp,targetDs,vmFolder,host,linkedClone,configSpec);
    vcVm=template.cloneVm(vmSpec,null);
  }
 else {
    copyParentVmSettings(template,configSpec);
    vcVm=targetRp.createVm(configSpec,targetDs,vmFolder);
  }
  configSpec=new ConfigSpecImpl();
  NetworkSchemaUtil.setNetworkSchema(configSpec,targetRp.getVcCluster(),vmSchema.networkSchema,vcVm);
  vcVm.reconfigure(configSpec);
  if (host != null) {
    vcVm.disableDrs();
  }
  List<VcHost> hostList=new ArrayList<VcHost>();
  List<DiskCreateSpec> addDisks=DiskSchemaUtil.getDisksToAdd(hostList,targetRp,targetDs,vmSchema.diskSchema,diskMap);
  DiskCreateSpec[] tmpAddDisks=addDisks.toArray(new DiskCreateSpec[addDisks.size()]);
  if (hostList.size() > 0 && !hostList.contains(vcVm.getHost())) {
    vcVm.migrate(hostList.get(0));
  }
  vcVm.changeDisks(null,tmpAddDisks);
  List<VirtualDeviceSpec> deviceChange=new ArrayList<VirtualDeviceSpec>();
  for (  Disk disk : vmSchema.diskSchema.getDisks()) {
    if (disk.vmdkPath == null || disk.vmdkPath.isEmpty())     continue;
    VirtualDisk.FlatVer2BackingInfo backing=new VirtualDiskImpl.FlatVer2BackingInfoImpl();
    backing.setFileName(disk.vmdkPath);
    backing.setDiskMode(disk.mode.toString());
    deviceChange.add(vcVm.attachVirtualDiskSpec(new DeviceId(disk.externalAddress),backing,false,DiskSize.sizeFromMB(disk.initialSizeMB)));
  }
  if (!deviceChange.isEmpty()) {
    vcVm.reconfigure(VmConfigUtil.createConfigSpec(deviceChange));
  }
  if (linkedClone) {
    ArrayList<DeviceId> disksToPromote=new ArrayList<DeviceId>();
    for (    Entry<String,Disk.Operation> entry : diskMap.entrySet()) {
      if (entry.getValue() == Disk.Operation.PROMOTE) {
        disksToPromote.add(new DeviceId(entry.getKey()));
      }
    }
    if (disksToPromote.size() >= 1) {
      vcVm.promoteDisks(disksToPromote.toArray(new DeviceId[0]));
    }
  }
  if (bootupConfigs != null) {
    vcVm.setGuestConfigs(bootupConfigs);
  }
  if (prePowerOn != null) {
    prePowerOn.setVm(vcVm);
    prePowerOn.call();
  }
  vcVm.powerOn(host);
  if (postPowerOn != null) {
    postPowerOn.setVm(vcVm);
    postPowerOn.call();
  }
}

Fixed version (ground truth):
public void callInternal() throws Exception {
  final VcVirtualMachine template=VcCache.get(vmSchema.diskSchema.getParent());
  VcSnapshot snap=template.getSnapshotByName(vmSchema.diskSchema.getParentSnap());
  if (snap == null) {
    snap=template.createSnapshot(vmSchema.diskSchema.getParentSnap(),"String_Node_Str");
  }
  ConfigSpecImpl configSpec=new ConfigSpecImpl();
  ResourceSchemaUtil.setResourceSchema(configSpec,vmSchema.resourceSchema);
  HashMap<String,Disk.Operation> diskMap=new HashMap<String,Disk.Operation>();
  if (requireClone()) {
    VcVirtualMachine.CreateSpec vmSpec=new VcVirtualMachine.CreateSpec(newVmName,snap,targetRp,targetDs,vmFolder,host,linkedClone,configSpec);
    vcVm=template.cloneVm(vmSpec,null);
  }
 else {
    copyParentVmSettings(template,configSpec);
    vcVm=targetRp.createVm(configSpec,targetDs,vmFolder);
  }
  configSpec=new ConfigSpecImpl();
  NetworkSchemaUtil.setNetworkSchema(configSpec,targetRp.getVcCluster(),vmSchema.networkSchema,vcVm);
  vcVm.reconfigure(configSpec);
  if (host != null) {
    vcVm.disableDrs();
  }
  List<VcHost> hostList=new ArrayList<VcHost>();
  List<DiskCreateSpec> addDisks=DiskSchemaUtil.getDisksToAdd(hostList,targetRp,targetDs,vmSchema.diskSchema,diskMap);
  DiskCreateSpec[] tmpAddDisks=addDisks.toArray(new DiskCreateSpec[addDisks.size()]);
  if (hostList.size() > 0 && !hostList.contains(vcVm.getHost())) {
    vcVm.migrate(hostList.get(0));
  }
  vcVm.changeDisks(null,tmpAddDisks);
  List<VirtualDeviceSpec> deviceChange=new ArrayList<VirtualDeviceSpec>();
  for (  Disk disk : vmSchema.diskSchema.getDisks()) {
    if (disk.vmdkPath == null || disk.vmdkPath.isEmpty())     continue;
    VirtualDisk.FlatVer2BackingInfo backing=new VirtualDiskImpl.FlatVer2BackingInfoImpl();
    backing.setFileName(disk.vmdkPath);
    backing.setDiskMode(disk.mode.toString());
    deviceChange.add(vcVm.attachVirtualDiskSpec(new DeviceId(disk.externalAddress),backing,false,DiskSize.sizeFromMB(disk.initialSizeMB)));
  }
  if (!deviceChange.isEmpty()) {
    vcVm.reconfigure(VmConfigUtil.createConfigSpec(deviceChange));
  }
  if (linkedClone) {
    ArrayList<DeviceId> disksToPromote=new ArrayList<DeviceId>();
    for (    Entry<String,Disk.Operation> entry : diskMap.entrySet()) {
      if (entry.getValue() == Disk.Operation.PROMOTE) {
        disksToPromote.add(new DeviceId(entry.getKey()));
      }
    }
    if (disksToPromote.size() >= 1) {
      vcVm.promoteDisks(disksToPromote.toArray(new DeviceId[0]));
    }
  }
  if (bootupConfigs != null) {
    vcVm.setGuestConfigs(bootupConfigs);
  }
  if (prePowerOn != null) {
    prePowerOn.setVm(vcVm);
    prePowerOn.call();
  }
  vcVm.powerOn(host);
  if (postPowerOn != null) {
    postPowerOn.setVm(vcVm);
    postPowerOn.call();
  }
}