Program: 105432
Project: 105432
Buggy source:
protected void createVbd(Connect conn,VirtualMachineTO vmSpec,String vmName,LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
  List<DiskTO> disks=Arrays.asList(vmSpec.getDisks());
  Collections.sort(disks,new Comparator<DiskTO>(){
    @Override public int compare(    DiskTO arg0,    DiskTO arg1){
      return arg0.getDiskSeq() > arg1.getDiskSeq() ? 1 : -1;
    }
  }
);
  for (  DiskTO volume : disks) {
    KVMPhysicalDisk physicalDisk=null;
    KVMStoragePool pool=null;
    DataTO data=volume.getData();
    if (volume.getType() == Volume.Type.ISO && data.getPath() != null) {
      NfsTO nfsStore=(NfsTO)data.getDataStore();
      String volPath=nfsStore.getUrl() + File.separator + data.getPath();
      int index=volPath.lastIndexOf("String_Node_Str");
      String volDir=volPath.substring(0,index);
      String volName=volPath.substring(index + 1);
      KVMStoragePool secondaryStorage=_storagePoolMgr.getStoragePoolByURI(volDir);
      physicalDisk=secondaryStorage.getPhysicalDisk(volName);
    }
 else     if (volume.getType() != Volume.Type.ISO) {
      PrimaryDataStoreTO store=(PrimaryDataStoreTO)data.getDataStore();
      physicalDisk=_storagePoolMgr.getPhysicalDisk(store.getPoolType(),store.getUuid(),data.getPath());
      pool=physicalDisk.getPool();
    }
    String volPath=null;
    if (physicalDisk != null) {
      volPath=physicalDisk.getPath();
    }
    DiskDef.diskBus diskBusType=null;
    Map<String,String> params=vmSpec.getDetails();
    if (params != null && params.get("String_Node_Str") != null && !params.get("String_Node_Str").isEmpty()) {
      String rootDiskController=params.get("String_Node_Str");
      s_logger.debug("String_Node_Str" + rootDiskController);
      for (      DiskDef.diskBus bus : DiskDef.diskBus.values()) {
        if (bus.toString().equalsIgnoreCase(rootDiskController)) {
          s_logger.debug("String_Node_Str" + rootDiskController);
          diskBusType=bus;
          break;
        }
      }
    }
    if (diskBusType == null) {
      diskBusType=getGuestDiskModel(vmSpec.getPlatformEmulator());
    }
    DiskDef disk=new DiskDef();
    if (volume.getType() == Volume.Type.ISO) {
      if (volPath == null) {
        disk.defISODisk(null);
      }
 else {
        disk.defISODisk(volPath);
      }
    }
 else {
      int devId=volume.getDiskSeq().intValue();
      if (pool.getType() == StoragePoolType.RBD) {
        disk.defNetworkBasedDisk(physicalDisk.getPath().replace("String_Node_Str","String_Node_Str"),pool.getSourceHost(),pool.getSourcePort(),pool.getAuthUserName(),pool.getUuid(),devId,diskBusType,diskProtocol.RBD,DiskDef.diskFmtType.RAW);
      }
 else       if (pool.getType() == StoragePoolType.Gluster) {
        String mountpoint=pool.getLocalPath();
        String path=physicalDisk.getPath();
        String glusterVolume=pool.getSourceDir().replace("String_Node_Str","String_Node_Str");
        disk.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint,"String_Node_Str"),pool.getSourceHost(),pool.getSourcePort(),null,null,devId,diskBusType,diskProtocol.GLUSTER,DiskDef.diskFmtType.QCOW2);
      }
 else       if (pool.getType() == StoragePoolType.CLVM || physicalDisk.getFormat() == PhysicalDiskFormat.RAW) {
        disk.defBlockBasedDisk(physicalDisk.getPath(),devId,diskBusType);
      }
 else {
        if (volume.getType() == Volume.Type.DATADISK) {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,DiskDef.diskBus.VIRTIO,DiskDef.diskFmtType.QCOW2);
        }
 else {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,diskBusType,DiskDef.diskFmtType.QCOW2);
        }
      }
    }
    if (data instanceof VolumeObjectTO) {
      VolumeObjectTO volumeObjectTO=(VolumeObjectTO)data;
      if ((volumeObjectTO.getBytesReadRate() != null) && (volumeObjectTO.getBytesReadRate() > 0))       disk.setBytesReadRate(volumeObjectTO.getBytesReadRate());
      if ((volumeObjectTO.getBytesWriteRate() != null) && (volumeObjectTO.getBytesWriteRate() > 0))       disk.setBytesWriteRate(volumeObjectTO.getBytesWriteRate());
      if ((volumeObjectTO.getIopsReadRate() != null) && (volumeObjectTO.getIopsReadRate() > 0))       disk.setIopsReadRate(volumeObjectTO.getIopsReadRate());
      if ((volumeObjectTO.getIopsWriteRate() != null) && (volumeObjectTO.getIopsWriteRate() > 0))       disk.setIopsWriteRate(volumeObjectTO.getIopsWriteRate());
      if (volumeObjectTO.getCacheMode() != null)       disk.setCacheMode(DiskDef.diskCacheMode.valueOf(volumeObjectTO.getCacheMode().toString()));
    }
    vm.getDevices().addDevice(disk);
  }
  if (vmSpec.getType() != VirtualMachine.Type.User) {
    if (_sysvmISOPath != null) {
      DiskDef iso=new DiskDef();
      iso.defISODisk(_sysvmISOPath);
      vm.getDevices().addDevice(iso);
    }
  }
  if (HypervisorType.LXC.toString().toLowerCase().equals(vm.getHvsType())) {
    for (    DiskTO volume : disks) {
      DataTO data=volume.getData();
      PrimaryDataStoreTO store=(PrimaryDataStoreTO)data.getDataStore();
      if (volume.getType() == Volume.Type.ROOT) {
        KVMPhysicalDisk physicalDisk=_storagePoolMgr.getPhysicalDisk(store.getPoolType(),store.getUuid(),data.getPath());
        FilesystemDef rootFs=new FilesystemDef(physicalDisk.getPath(),"String_Node_Str");
        vm.getDevices().addDevice(rootFs);
      }
 else       if (volume.getType() == Volume.Type.DATADISK) {
        KVMPhysicalDisk physicalDisk=_storagePoolMgr.getPhysicalDisk(store.getPoolType(),store.getUuid(),data.getPath());
        KVMStoragePool pool=physicalDisk.getPool();
        if (StoragePoolType.RBD.equals(pool.getType())) {
          int devId=volume.getDiskSeq().intValue();
          String device=mapRbdDevice(physicalDisk);
          if (device != null) {
            s_logger.debug("String_Node_Str" + device);
            DiskDef diskdef=new DiskDef();
            diskdef.defBlockBasedDisk(device,devId,DiskDef.diskBus.VIRTIO);
            diskdef.setQemuDriver(false);
            vm.getDevices().addDevice(diskdef);
          }
        }
      }
    }
  }
}

Fixed version (ground truth):
protected void createVbd(Connect conn,VirtualMachineTO vmSpec,String vmName,LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
  List<DiskTO> disks=Arrays.asList(vmSpec.getDisks());
  Collections.sort(disks,new Comparator<DiskTO>(){
    @Override public int compare(    DiskTO arg0,    DiskTO arg1){
      return arg0.getDiskSeq() > arg1.getDiskSeq() ? 1 : -1;
    }
  }
);
  for (  DiskTO volume : disks) {
    KVMPhysicalDisk physicalDisk=null;
    KVMStoragePool pool=null;
    DataTO data=volume.getData();
    if (volume.getType() == Volume.Type.ISO && data.getPath() != null) {
      NfsTO nfsStore=(NfsTO)data.getDataStore();
      String volPath=nfsStore.getUrl() + File.separator + data.getPath();
      int index=volPath.lastIndexOf("String_Node_Str");
      String volDir=volPath.substring(0,index);
      String volName=volPath.substring(index + 1);
      KVMStoragePool secondaryStorage=_storagePoolMgr.getStoragePoolByURI(volDir);
      physicalDisk=secondaryStorage.getPhysicalDisk(volName);
    }
 else     if (volume.getType() != Volume.Type.ISO) {
      PrimaryDataStoreTO store=(PrimaryDataStoreTO)data.getDataStore();
      physicalDisk=_storagePoolMgr.getPhysicalDisk(store.getPoolType(),store.getUuid(),data.getPath());
      pool=physicalDisk.getPool();
    }
    String volPath=null;
    if (physicalDisk != null) {
      volPath=physicalDisk.getPath();
    }
    DiskDef.diskBus diskBusType=null;
    Map<String,String> params=vmSpec.getDetails();
    if (params != null && params.get("String_Node_Str") != null && !params.get("String_Node_Str").isEmpty()) {
      String rootDiskController=params.get("String_Node_Str");
      s_logger.debug("String_Node_Str" + rootDiskController);
      for (      DiskDef.diskBus bus : DiskDef.diskBus.values()) {
        if (bus.toString().equalsIgnoreCase(rootDiskController)) {
          s_logger.debug("String_Node_Str" + rootDiskController);
          diskBusType=bus;
          break;
        }
      }
    }
    if (diskBusType == null) {
      diskBusType=getGuestDiskModel(vmSpec.getPlatformEmulator());
    }
    DiskDef disk=new DiskDef();
    if (volume.getType() == Volume.Type.ISO) {
      if (volPath == null) {
        disk.defISODisk(null);
      }
 else {
        disk.defISODisk(volPath);
      }
    }
 else {
      int devId=volume.getDiskSeq().intValue();
      if (pool.getType() == StoragePoolType.RBD) {
        disk.defNetworkBasedDisk(physicalDisk.getPath().replace("String_Node_Str","String_Node_Str"),pool.getSourceHost(),pool.getSourcePort(),pool.getAuthUserName(),pool.getUuid(),devId,diskBusType,diskProtocol.RBD,DiskDef.diskFmtType.RAW);
      }
 else       if (pool.getType() == StoragePoolType.Gluster) {
        String mountpoint=pool.getLocalPath();
        String path=physicalDisk.getPath();
        String glusterVolume=pool.getSourceDir().replace("String_Node_Str","String_Node_Str");
        disk.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint,"String_Node_Str"),pool.getSourceHost(),pool.getSourcePort(),null,null,devId,diskBusType,diskProtocol.GLUSTER,DiskDef.diskFmtType.QCOW2);
      }
 else       if (pool.getType() == StoragePoolType.CLVM || physicalDisk.getFormat() == PhysicalDiskFormat.RAW) {
        disk.defBlockBasedDisk(physicalDisk.getPath(),devId,diskBusType);
      }
 else {
        if (volume.getType() == Volume.Type.DATADISK) {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,DiskDef.diskBus.VIRTIO,DiskDef.diskFmtType.QCOW2);
        }
 else {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,diskBusType,DiskDef.diskFmtType.QCOW2);
        }
      }
    }
    if (data instanceof VolumeObjectTO) {
      VolumeObjectTO volumeObjectTO=(VolumeObjectTO)data;
      if ((volumeObjectTO.getBytesReadRate() != null) && (volumeObjectTO.getBytesReadRate() > 0))       disk.setBytesReadRate(volumeObjectTO.getBytesReadRate());
      if ((volumeObjectTO.getBytesWriteRate() != null) && (volumeObjectTO.getBytesWriteRate() > 0))       disk.setBytesWriteRate(volumeObjectTO.getBytesWriteRate());
      if ((volumeObjectTO.getIopsReadRate() != null) && (volumeObjectTO.getIopsReadRate() > 0))       disk.setIopsReadRate(volumeObjectTO.getIopsReadRate());
      if ((volumeObjectTO.getIopsWriteRate() != null) && (volumeObjectTO.getIopsWriteRate() > 0))       disk.setIopsWriteRate(volumeObjectTO.getIopsWriteRate());
      if (volumeObjectTO.getCacheMode() != null)       disk.setCacheMode(DiskDef.diskCacheMode.valueOf(volumeObjectTO.getCacheMode().toString().toUpperCase()));
    }
    vm.getDevices().addDevice(disk);
  }
  if (vmSpec.getType() != VirtualMachine.Type.User) {
    if (_sysvmISOPath != null) {
      DiskDef iso=new DiskDef();
      iso.defISODisk(_sysvmISOPath);
      vm.getDevices().addDevice(iso);
    }
  }
  if (HypervisorType.LXC.toString().toLowerCase().equals(vm.getHvsType())) {
    for (    DiskTO volume : disks) {
      DataTO data=volume.getData();
      PrimaryDataStoreTO store=(PrimaryDataStoreTO)data.getDataStore();
      if (volume.getType() == Volume.Type.ROOT) {
        KVMPhysicalDisk physicalDisk=_storagePoolMgr.getPhysicalDisk(store.getPoolType(),store.getUuid(),data.getPath());
        FilesystemDef rootFs=new FilesystemDef(physicalDisk.getPath(),"String_Node_Str");
        vm.getDevices().addDevice(rootFs);
      }
 else       if (volume.getType() == Volume.Type.DATADISK) {
        KVMPhysicalDisk physicalDisk=_storagePoolMgr.getPhysicalDisk(store.getPoolType(),store.getUuid(),data.getPath());
        KVMStoragePool pool=physicalDisk.getPool();
        if (StoragePoolType.RBD.equals(pool.getType())) {
          int devId=volume.getDiskSeq().intValue();
          String device=mapRbdDevice(physicalDisk);
          if (device != null) {
            s_logger.debug("String_Node_Str" + device);
            DiskDef diskdef=new DiskDef();
            diskdef.defBlockBasedDisk(device,devId,DiskDef.diskBus.VIRTIO);
            diskdef.setQemuDriver(false);
            vm.getDevices().addDevice(diskdef);
          }
        }
      }
    }
  }
}