Program: 106269
Project: 106269
Buggy source:
@Override public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, CloudRuntimeException {
  Long vmId=cmd.getVmId();
  Long networkId=cmd.getNetworkId();
  String ipAddress=cmd.getIpAddress();
  Account caller=UserContext.current().getCaller();
  UserVmVO vmInstance=_vmDao.findById(vmId);
  if (vmInstance == null) {
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  NetworkVO network=_networkDao.findById(networkId);
  if (network == null) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId);
  }
  NicProfile profile=new NicProfile(null,null);
  if (ipAddress != null) {
    profile=new NicProfile(ipAddress,null);
  }
  _accountMgr.checkAccess(caller,null,true,vmInstance);
  DataCenterVO dc=_dcDao.findById(vmInstance.getDataCenterId());
  if (dc.getNetworkType() == DataCenter.NetworkType.Basic) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance.getDataCenterId() + "String_Node_Str");
  }
  if (network.getGuestType() != Network.GuestType.Shared) {
    List<NetworkVO> networkMap=_networkDao.listBy(caller.getId(),network.getId());
    if ((networkMap == null || networkMap.isEmpty()) && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      throw new PermissionDeniedException("String_Node_Str" + network.getId() + "String_Node_Str");
    }
  }
  if (network.getDataCenterId() != vmInstance.getDataCenterId()) {
    throw new CloudRuntimeException(vmInstance + "String_Node_Str" + vmInstance.getDataCenterId()+ "String_Node_Str"+ network+ "String_Node_Str"+ network.getDataCenterId());
  }
  if (_networkModel.getNicInNetwork(vmInstance.getId(),network.getId()) != null) {
    s_logger.debug(vmInstance + "String_Node_Str" + network+ "String_Node_Str");
  }
 else {
    List<String> hostNames=_vmInstanceDao.listDistinctHostNames(network.getId());
    if (hostNames.contains(vmInstance.getHostName())) {
      throw new CloudRuntimeException(network + "String_Node_Str" + vmInstance.getHostName());
    }
  }
  NicProfile guestNic=null;
  try {
    guestNic=_itMgr.addVmToNetwork(vmInstance,network,profile);
  }
 catch (  ResourceUnavailableException e) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str"+ e);
  }
catch (  InsufficientCapacityException e) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str"+ e);
  }
catch (  ConcurrentOperationException e) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str"+ e);
  }
  if (guestNic == null) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance);
  }
  s_logger.debug("String_Node_Str" + network + "String_Node_Str"+ vmInstance);
  return _vmDao.findById(vmInstance.getId());
}

Fixed version (ground truth):
@Override public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, CloudRuntimeException {
  Long vmId=cmd.getVmId();
  Long networkId=cmd.getNetworkId();
  String ipAddress=cmd.getIpAddress();
  Account caller=UserContext.current().getCaller();
  UserVmVO vmInstance=_vmDao.findById(vmId);
  if (vmInstance == null) {
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  NetworkVO network=_networkDao.findById(networkId);
  if (network == null) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId);
  }
  List<NicVO> allNics=_nicDao.listByVmId(vmInstance.getId());
  for (  NicVO nic : allNics) {
    if (nic.getNetworkId() == network.getId())     throw new CloudRuntimeException("String_Node_Str" + vmInstance.getInstanceName() + "String_Node_Str"+ network.getUuid());
  }
  NicProfile profile=new NicProfile(null,null);
  if (ipAddress != null) {
    profile=new NicProfile(ipAddress,null);
  }
  _accountMgr.checkAccess(caller,null,true,vmInstance);
  DataCenterVO dc=_dcDao.findById(vmInstance.getDataCenterId());
  if (dc.getNetworkType() == DataCenter.NetworkType.Basic) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance.getDataCenterId() + "String_Node_Str");
  }
  if (network.getGuestType() != Network.GuestType.Shared) {
    List<NetworkVO> networkMap=_networkDao.listBy(caller.getId(),network.getId());
    if ((networkMap == null || networkMap.isEmpty()) && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      throw new PermissionDeniedException("String_Node_Str" + network.getId() + "String_Node_Str");
    }
  }
  if (network.getDataCenterId() != vmInstance.getDataCenterId()) {
    throw new CloudRuntimeException(vmInstance + "String_Node_Str" + vmInstance.getDataCenterId()+ "String_Node_Str"+ network+ "String_Node_Str"+ network.getDataCenterId());
  }
  if (_networkModel.getNicInNetwork(vmInstance.getId(),network.getId()) != null) {
    s_logger.debug(vmInstance + "String_Node_Str" + network+ "String_Node_Str");
  }
 else {
    List<String> hostNames=_vmInstanceDao.listDistinctHostNames(network.getId());
    if (hostNames.contains(vmInstance.getHostName())) {
      throw new CloudRuntimeException(network + "String_Node_Str" + vmInstance.getHostName());
    }
  }
  NicProfile guestNic=null;
  try {
    guestNic=_itMgr.addVmToNetwork(vmInstance,network,profile);
  }
 catch (  ResourceUnavailableException e) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str"+ e);
  }
catch (  InsufficientCapacityException e) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str"+ e);
  }
catch (  ConcurrentOperationException e) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str"+ e);
  }
  if (guestNic == null) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance);
  }
  s_logger.debug("String_Node_Str" + network + "String_Node_Str"+ vmInstance);
  return _vmDao.findById(vmInstance.getId());
}