Program: 106270
Project: 106270
Buggy source:
@Override public boolean removeNicFromVm(VirtualMachine vm,NicVO nic) throws ConcurrentOperationException, ResourceUnavailableException {
  VMInstanceVO vmVO=_vmDao.findById(vm.getId());
  NetworkVO network=_networkDao.findById(nic.getNetworkId());
  ReservationContext context=new ReservationContextImpl(null,null,_accountMgr.getActiveUser(User.UID_SYSTEM),_accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM));
  VirtualMachineProfileImpl<VMInstanceVO> vmProfile=new VirtualMachineProfileImpl<VMInstanceVO>(vmVO,null,null,null,null);
  DataCenter dc=_configMgr.getZone(network.getDataCenterId());
  Host host=_hostDao.findById(vm.getHostId());
  DeployDestination dest=new DeployDestination(dc,null,null,host);
  VirtualMachineGuru<VMInstanceVO> vmGuru=getVmGuru(vmVO);
  HypervisorGuru hvGuru=_hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
  VirtualMachineTO vmTO=hvGuru.implement(vmProfile);
  if (nic.isDefaultNic() && vm.getType() == VirtualMachine.Type.User) {
    s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ network+ "String_Node_Str");
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ network+ "String_Node_Str");
  }
  NicProfile nicProfile=new NicProfile(nic,network,nic.getBroadcastUri(),nic.getIsolationUri(),_networkModel.getNetworkRate(network.getId(),vm.getId()),_networkModel.isSecurityGroupSupportedInNetwork(network),_networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(),network));
  if (vm.getState() == State.Running) {
    NicTO nicTO=toNicTO(nicProfile,vmProfile.getVirtualMachine().getHypervisorType());
    s_logger.debug("String_Node_Str" + nic + "String_Node_Str"+ vm+ "String_Node_Str"+ network);
    boolean result=vmGuru.unplugNic(network,nicTO,vmTO,context,dest);
    if (result) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ network);
      long isDefault=(nic.isDefaultNic()) ? 1 : 0;
      UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vm.getAccountId(),vm.getDataCenterId(),vm.getId(),Long.toString(nic.getId()),network.getNetworkOfferingId(),null,isDefault,VirtualMachine.class.getName(),vm.getUuid());
    }
 else {
      s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ network);
      return false;
    }
  }
 else   if (vm.getState() != State.Stopped) {
    s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ network);
    throw new ResourceUnavailableException("String_Node_Str" + vm + "String_Node_Str",DataCenter.class,vm.getDataCenterId());
  }
  _networkMgr.releaseNic(vmProfile,nic);
  s_logger.debug("String_Node_Str" + nic + "String_Node_Str"+ vm);
  _networkMgr.removeNic(vmProfile,nic);
  _nicsDao.expunge(nic.getId());
  return true;
}

Fixed version (ground truth):
@Override public boolean removeNicFromVm(VirtualMachine vm,NicVO nic) throws ConcurrentOperationException, ResourceUnavailableException {
  VMInstanceVO vmVO=_vmDao.findById(vm.getId());
  NetworkVO network=_networkDao.findById(nic.getNetworkId());
  ReservationContext context=new ReservationContextImpl(null,null,_accountMgr.getActiveUser(User.UID_SYSTEM),_accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM));
  VirtualMachineProfileImpl<VMInstanceVO> vmProfile=new VirtualMachineProfileImpl<VMInstanceVO>(vmVO,null,null,null,null);
  DataCenter dc=_configMgr.getZone(network.getDataCenterId());
  Host host=_hostDao.findById(vm.getHostId());
  DeployDestination dest=new DeployDestination(dc,null,null,host);
  VirtualMachineGuru<VMInstanceVO> vmGuru=getVmGuru(vmVO);
  HypervisorGuru hvGuru=_hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
  VirtualMachineTO vmTO=hvGuru.implement(vmProfile);
  if (nic.isDefaultNic() && vm.getType() == VirtualMachine.Type.User) {
    s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ network+ "String_Node_Str");
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ network+ "String_Node_Str");
  }
  if (rulesMgr.listAssociatedRulesForGuestNic(nic).size() > 0) {
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ network+ "String_Node_Str");
  }
  NicProfile nicProfile=new NicProfile(nic,network,nic.getBroadcastUri(),nic.getIsolationUri(),_networkModel.getNetworkRate(network.getId(),vm.getId()),_networkModel.isSecurityGroupSupportedInNetwork(network),_networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(),network));
  if (vm.getState() == State.Running) {
    NicTO nicTO=toNicTO(nicProfile,vmProfile.getVirtualMachine().getHypervisorType());
    s_logger.debug("String_Node_Str" + nic + "String_Node_Str"+ vm+ "String_Node_Str"+ network);
    boolean result=vmGuru.unplugNic(network,nicTO,vmTO,context,dest);
    if (result) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ network);
      long isDefault=(nic.isDefaultNic()) ? 1 : 0;
      UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vm.getAccountId(),vm.getDataCenterId(),vm.getId(),Long.toString(nic.getId()),network.getNetworkOfferingId(),null,isDefault,VirtualMachine.class.getName(),vm.getUuid());
    }
 else {
      s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ network);
      return false;
    }
  }
 else   if (vm.getState() != State.Stopped) {
    s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ network);
    throw new ResourceUnavailableException("String_Node_Str" + vm + "String_Node_Str",DataCenter.class,vm.getDataCenterId());
  }
  _networkMgr.releaseNic(vmProfile,nic);
  s_logger.debug("String_Node_Str" + nic + "String_Node_Str"+ vm);
  _networkMgr.removeNic(vmProfile,nic);
  _nicsDao.expunge(nic.getId());
  return true;
}