Program: 14137
Project: 14137
Buggy source:
public DetachVolumeResponseType detach(DetachVolumeType request) throws EucalyptusCloudException {
  final DetachVolumeResponseType reply=(DetachVolumeResponseType)request.getReply();
  final String volumeId=normalizeVolumeIdentifier(request.getVolumeId());
  final String instanceId=normalizeOptionalInstanceIdentifier(request.getInstanceId());
  final Context ctx=Contexts.lookup();
  Volume vol;
  try {
    vol=Volumes.lookup(ctx.getUserFullName().asAccountFullName(),volumeId);
  }
 catch (  NoSuchElementException ex) {
    try {
      vol=Volumes.lookup(null,volumeId);
    }
 catch (    NoSuchElementException e) {
      throw new ClientComputeException("String_Node_Str","String_Node_Str" + request.getVolumeId() + "String_Node_Str");
    }
  }
  if (!RestrictedTypes.filterPrivileged().apply(vol)) {
    throw new EucalyptusCloudException("String_Node_Str" + volumeId + "String_Node_Str"+ ctx.getUser().getName());
  }
  VmInstance vm=null;
  String remoteDevice=null;
  AttachedVolume volume=null;
  VmVolumeAttachment vmVolAttach=null;
  try {
    vmVolAttach=VmInstances.lookupVolumeAttachment(volumeId);
    remoteDevice=vmVolAttach.getRemoteDevice();
    volume=VmVolumeAttachment.asAttachedVolume(vmVolAttach.getVmInstance()).apply(vmVolAttach);
    vm=vmVolAttach.getVmInstance();
  }
 catch (  NoSuchElementException ex) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + volumeId);
  }
  if (vmVolAttach.getIsRootDevice() && !VmState.STOPPED.equals(vm.getState())) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + volumeId + "String_Node_Str"+ vm.getInstanceId()+ "String_Node_Str"+ vm.getState().toString().toLowerCase());
  }
  if (vm != null && MigrationState.isMigrating(vm)) {
    throw Exceptions.toUndeclared("String_Node_Str" + vm.getInstanceId() + "String_Node_Str"+ vm.getMigrationTask());
  }
  if (volume == null) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + volumeId);
  }
  if (!RestrictedTypes.filterPrivileged().apply(vm)) {
    throw new EucalyptusCloudException("String_Node_Str" + instanceId + "String_Node_Str"+ ctx.getUser().getName());
  }
  if (instanceId != null && vm != null && !vm.getInstanceId().equals(instanceId)) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + instanceId);
  }
  if (request.getDevice() != null && !request.getDevice().equals("String_Node_Str") && !volume.getDevice().equals(request.getDevice())) {
    throw new EucalyptusCloudException("String_Node_Str" + request.getDevice());
  }
  ServiceConfiguration scVm;
  try {
    scVm=Topology.lookup(Storage.class,vm.lookupPartition());
  }
 catch (  Exception ex) {
    LOG.error(ex,ex);
    throw new EucalyptusCloudException("String_Node_Str" + vm.getPartition(),ex);
  }
  if (VmState.STOPPED.equals(vm.getState())) {
    try {
      final DetachStorageVolumeType detach=new DetachStorageVolumeType(volume.getVolumeId());
      AsyncRequests.sendSync(scVm,detach);
    }
 catch (    Exception e) {
      LOG.debug(e);
      Logs.extreme().debug(e,e);
    }
    vm.removeVolumeAttachment(volume.getVolumeId());
  }
 else {
    Cluster cluster=null;
    ServiceConfiguration ccConfig=null;
    try {
      ccConfig=Topology.lookup(ClusterController.class,vm.lookupPartition());
      cluster=Clusters.lookup(ccConfig);
    }
 catch (    NoSuchElementException e) {
      LOG.debug(e,e);
      throw new EucalyptusCloudException("String_Node_Str" + vm.getPartition());
    }
    final ClusterDetachVolumeType detachVolume=new ClusterDetachVolumeType();
    detachVolume.setVolumeId(volume.getVolumeId());
    detachVolume.setRemoteDevice(remoteDevice);
    detachVolume.setDevice(volume.getDevice().replaceAll("String_Node_Str","String_Node_Str"));
    detachVolume.setInstanceId(vm.getInstanceId());
    detachVolume.setForce(request.getForce());
    VolumeDetachCallback ncDetach=new VolumeDetachCallback(detachVolume);
    AsyncRequests.newRequest(ncDetach).dispatch(cluster.getConfiguration());
    vm.updateVolumeAttachment(volumeId,AttachmentState.detaching);
    volume.setStatus("String_Node_Str");
  }
  reply.setDetachedVolume(volume);
  Volumes.fireUsageEvent(vol,VolumeEvent.forVolumeDetach(vm.getInstanceUuid(),vm.getInstanceId()));
  return reply;
}

Fixed version (ground truth):
public DetachVolumeResponseType detach(DetachVolumeType request) throws EucalyptusCloudException {
  final DetachVolumeResponseType reply=(DetachVolumeResponseType)request.getReply();
  final String volumeId=normalizeVolumeIdentifier(request.getVolumeId());
  final String instanceId=normalizeOptionalInstanceIdentifier(request.getInstanceId());
  final Context ctx=Contexts.lookup();
  Volume vol;
  try {
    vol=Volumes.lookup(ctx.getUserFullName().asAccountFullName(),volumeId);
  }
 catch (  NoSuchElementException ex) {
    try {
      vol=Volumes.lookup(null,volumeId);
    }
 catch (    NoSuchElementException e) {
      throw new ClientComputeException("String_Node_Str","String_Node_Str" + request.getVolumeId() + "String_Node_Str");
    }
  }
  if (!RestrictedTypes.filterPrivileged().apply(vol)) {
    throw new ClientUnauthorizedComputeException("String_Node_Str" + volumeId + "String_Node_Str"+ ctx.getUser().getName());
  }
  VmInstance vm=null;
  String remoteDevice=null;
  AttachedVolume volume=null;
  VmVolumeAttachment vmVolAttach=null;
  try {
    vmVolAttach=VmInstances.lookupVolumeAttachment(volumeId);
    remoteDevice=vmVolAttach.getRemoteDevice();
    volume=VmVolumeAttachment.asAttachedVolume(vmVolAttach.getVmInstance()).apply(vmVolAttach);
    vm=vmVolAttach.getVmInstance();
  }
 catch (  NoSuchElementException ex) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + volumeId);
  }
  if (vmVolAttach.getIsRootDevice() && !VmState.STOPPED.equals(vm.getState())) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + volumeId + "String_Node_Str"+ vm.getInstanceId()+ "String_Node_Str"+ vm.getState().toString().toLowerCase());
  }
  if (vm != null && MigrationState.isMigrating(vm)) {
    throw Exceptions.toUndeclared("String_Node_Str" + vm.getInstanceId() + "String_Node_Str"+ vm.getMigrationTask());
  }
  if (volume == null) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + volumeId);
  }
  if (!RestrictedTypes.filterPrivileged().apply(vm)) {
    throw new ClientUnauthorizedComputeException("String_Node_Str" + instanceId + "String_Node_Str"+ ctx.getUser().getName());
  }
  if (instanceId != null && vm != null && !vm.getInstanceId().equals(instanceId)) {
    throw new ClientComputeException("String_Node_Str","String_Node_Str" + instanceId);
  }
  if (request.getDevice() != null && !request.getDevice().equals("String_Node_Str") && !volume.getDevice().equals(request.getDevice())) {
    throw new EucalyptusCloudException("String_Node_Str" + request.getDevice());
  }
  ServiceConfiguration scVm;
  try {
    scVm=Topology.lookup(Storage.class,vm.lookupPartition());
  }
 catch (  Exception ex) {
    LOG.error(ex,ex);
    throw new EucalyptusCloudException("String_Node_Str" + vm.getPartition(),ex);
  }
  if (VmState.STOPPED.equals(vm.getState())) {
    try {
      final DetachStorageVolumeType detach=new DetachStorageVolumeType(volume.getVolumeId());
      AsyncRequests.sendSync(scVm,detach);
    }
 catch (    Exception e) {
      LOG.debug(e);
      Logs.extreme().debug(e,e);
    }
    vm.removeVolumeAttachment(volume.getVolumeId());
  }
 else {
    Cluster cluster=null;
    ServiceConfiguration ccConfig=null;
    try {
      ccConfig=Topology.lookup(ClusterController.class,vm.lookupPartition());
      cluster=Clusters.lookup(ccConfig);
    }
 catch (    NoSuchElementException e) {
      LOG.debug(e,e);
      throw new EucalyptusCloudException("String_Node_Str" + vm.getPartition());
    }
    final ClusterDetachVolumeType detachVolume=new ClusterDetachVolumeType();
    detachVolume.setVolumeId(volume.getVolumeId());
    detachVolume.setRemoteDevice(remoteDevice);
    detachVolume.setDevice(volume.getDevice().replaceAll("String_Node_Str","String_Node_Str"));
    detachVolume.setInstanceId(vm.getInstanceId());
    detachVolume.setForce(request.getForce());
    VolumeDetachCallback ncDetach=new VolumeDetachCallback(detachVolume);
    AsyncRequests.newRequest(ncDetach).dispatch(cluster.getConfiguration());
    vm.updateVolumeAttachment(volumeId,AttachmentState.detaching);
    volume.setStatus("String_Node_Str");
  }
  reply.setDetachedVolume(volume);
  Volumes.fireUsageEvent(vol,VolumeEvent.forVolumeDetach(vm.getInstanceUuid(),vm.getInstanceId()));
  return reply;
}