Program: 107390
Project: 107390
Buggy source:
@Override public UserVm deployVirtualMachine(DeployVMCmd cmd) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException {
  Account ctxAccount=UserContext.current().getAccount();
  Long userId=UserContext.current().getUserId();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Long accountId=null;
  long dataCenterId=cmd.getZoneId();
  long serviceOfferingId=cmd.getServiceOfferingId();
  long templateId=cmd.getTemplateId();
  Long diskOfferingId=cmd.getDiskOfferingId();
  String domain=null;
  String password=generateRandomPassword();
  String displayName=cmd.getDisplayName();
  String group=cmd.getGroup();
  String userData=cmd.getUserData();
  String[] networkGroups=null;
  Long sizeObj=cmd.getSize();
  long size=(sizeObj == null) ? 0 : sizeObj;
  DataCenterVO dc=_dcDao.findById(dataCenterId);
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + dataCenterId);
  }
  if ((ctxAccount == null) || isAdmin(ctxAccount.getType())) {
    if (domainId != null) {
      if ((ctxAccount != null) && !_domainDao.isChildDomain(ctxAccount.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      if (accountName != null) {
        Account userAccount=_accountDao.findActiveAccount(accountName,domainId);
        if (userAccount == null) {
          throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
        }
        accountId=userAccount.getId();
      }
    }
 else {
      accountId=((ctxAccount != null) ? ctxAccount.getId() : null);
    }
  }
 else {
    accountId=ctxAccount.getId();
  }
  if (accountId == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (domainId == null) {
    domainId=dc.getDomainId();
    if (domainId == null) {
    }
 else {
      if (!_domainDao.isChildDomain(ctxAccount.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
    }
  }
  List<String> netGrpList=cmd.getNetworkGroupList();
  if ((netGrpList != null) && !netGrpList.isEmpty()) {
    networkGroups=netGrpList.toArray(new String[netGrpList.size()]);
  }
  AccountVO account=_accountDao.findById(accountId);
  if (account == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountId);
  }
  ServiceOfferingVO offering=_offeringsDao.findById(serviceOfferingId);
  if (offering == null) {
    throw new InvalidParameterValueException("String_Node_Str" + serviceOfferingId);
  }
  VMTemplateVO template=_templateDao.findById(templateId);
  if (template == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  boolean isIso=Storage.ImageFormat.ISO.equals(template.getFormat());
  if (isIso && !template.isBootable()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  DiskOfferingVO diskOffering=null;
  if (diskOfferingId != null) {
    diskOffering=_diskOfferingDao.findById(diskOfferingId);
  }
  if (isIso && diskOffering == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (isIso) {
    template.setHypervisorType(HypervisorType.getType(cmd.getHypervisor()));
  }
  if (diskOffering != null && diskOffering.isCustomized() && size <= 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (diskOffering != null && diskOffering.isCustomized() && size > _maxVolumeSizeInGb) {
    throw new InvalidParameterValueException("String_Node_Str" + _maxVolumeSizeInGb);
  }
  if (!template.isPublicTemplate()) {
    Long templateOwner=template.getAccountId();
    if (!BaseCmd.isAdmin(account.getType()) && ((templateOwner == null) || (templateOwner.longValue() != accountId))) {
      LaunchPermissionVO permission=_launchPermissionDao.findByTemplateAndAccount(templateId,account.getId());
      if (permission == null) {
        throw new PermissionDeniedException("String_Node_Str" + account.getAccountName() + "String_Node_Str"+ template.getName());
      }
    }
  }
  byte[] decodedUserData=null;
  if (userData != null) {
    if (userData.length() >= 2 * UserVmManager.MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    decodedUserData=org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes());
    if (decodedUserData.length > UserVmManager.MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (decodedUserData.length < 1) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (offering.getGuestIpType() != NetworkOffering.GuestIpType.Virtualized) {
    _networkGroupMgr.createDefaultNetworkGroup(accountId);
  }
  if (networkGroups != null) {
    if (offering.getGuestIpType() == NetworkOffering.GuestIpType.Virtualized) {
      throw new InvalidParameterValueException("String_Node_Str" + offering.getName());
    }
    Set<String> nameSet=new HashSet<String>();
    nameSet.addAll(Arrays.asList(networkGroups));
    nameSet.add(NetworkGroupManager.DEFAULT_GROUP_NAME);
    networkGroups=nameSet.toArray(new String[nameSet.size()]);
    List<NetworkGroupVO> networkGroupVOs=_networkSecurityGroupDao.findByAccountAndNames(accountId,networkGroups);
    if (networkGroupVOs.size() != nameSet.size()) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
 else {
    if (offering.getGuestIpType() != NetworkOffering.GuestIpType.Virtualized && _networkGroupsEnabled) {
      networkGroups=new String[]{NetworkGroupManager.DEFAULT_GROUP_NAME};
    }
  }
  Long eventId=cmd.getStartEventId();
  try {
    return deployVirtualMachineImpl(userId,accountId,dataCenterId,serviceOfferingId,template,diskOfferingId,domain,password,displayName,group,userData,networkGroups,eventId,size);
  }
 catch (  ResourceAllocationException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  ExecutionException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  InvalidParameterValueException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  InsufficientStorageCapacityException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  PermissionDeniedException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  ConcurrentOperationException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  Exception e) {
    s_logger.warn("String_Node_Str" + e.getMessage(),e);
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw new CloudRuntimeException("String_Node_Str" + e.getMessage());
  }
}

Fixed version (ground truth):
@Override public UserVm deployVirtualMachine(DeployVMCmd cmd) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException {
  Account ctxAccount=UserContext.current().getAccount();
  Long userId=UserContext.current().getUserId();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Long accountId=null;
  long dataCenterId=cmd.getZoneId();
  long serviceOfferingId=cmd.getServiceOfferingId();
  long templateId=cmd.getTemplateId();
  Long diskOfferingId=cmd.getDiskOfferingId();
  String domain=null;
  String password=generateRandomPassword();
  String displayName=cmd.getDisplayName();
  String group=cmd.getGroup();
  String userData=cmd.getUserData();
  String[] networkGroups=null;
  Long sizeObj=cmd.getSize();
  long size=(sizeObj == null) ? 0 : sizeObj;
  DataCenterVO dc=_dcDao.findById(dataCenterId);
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + dataCenterId);
  }
  if ((ctxAccount == null) || isAdmin(ctxAccount.getType())) {
    if (domainId != null) {
      if ((ctxAccount != null) && !_domainDao.isChildDomain(ctxAccount.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      if (accountName != null) {
        Account userAccount=_accountDao.findActiveAccount(accountName,domainId);
        if (userAccount == null) {
          throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
        }
        accountId=userAccount.getId();
      }
    }
 else {
      accountId=((ctxAccount != null) ? ctxAccount.getId() : null);
    }
  }
 else {
    accountId=ctxAccount.getId();
  }
  if (accountId == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (domainId == null) {
    domainId=dc.getDomainId();
    if (domainId == null) {
    }
 else {
      if (ctxAccount != null) {
        if ((ctxAccount.getType() == Account.ACCOUNT_TYPE_NORMAL) || ctxAccount.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
          if (domainId == ctxAccount.getDomainId()) {
          }
 else           if ((!_domainDao.isChildDomain(domainId,ctxAccount.getDomainId()))) {
            throw new PermissionDeniedException("String_Node_Str" + domainId);
          }
        }
      }
    }
  }
  List<String> netGrpList=cmd.getNetworkGroupList();
  if ((netGrpList != null) && !netGrpList.isEmpty()) {
    networkGroups=netGrpList.toArray(new String[netGrpList.size()]);
  }
  AccountVO account=_accountDao.findById(accountId);
  if (account == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountId);
  }
  ServiceOfferingVO offering=_offeringsDao.findById(serviceOfferingId);
  if (offering == null) {
    throw new InvalidParameterValueException("String_Node_Str" + serviceOfferingId);
  }
  VMTemplateVO template=_templateDao.findById(templateId);
  if (template == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  boolean isIso=Storage.ImageFormat.ISO.equals(template.getFormat());
  if (isIso && !template.isBootable()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  DiskOfferingVO diskOffering=null;
  if (diskOfferingId != null) {
    diskOffering=_diskOfferingDao.findById(diskOfferingId);
  }
  if (isIso && diskOffering == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (isIso) {
    template.setHypervisorType(HypervisorType.getType(cmd.getHypervisor()));
  }
  if (diskOffering != null && diskOffering.isCustomized() && size <= 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (diskOffering != null && diskOffering.isCustomized() && size > _maxVolumeSizeInGb) {
    throw new InvalidParameterValueException("String_Node_Str" + _maxVolumeSizeInGb);
  }
  if (!template.isPublicTemplate()) {
    Long templateOwner=template.getAccountId();
    if (!BaseCmd.isAdmin(account.getType()) && ((templateOwner == null) || (templateOwner.longValue() != accountId))) {
      LaunchPermissionVO permission=_launchPermissionDao.findByTemplateAndAccount(templateId,account.getId());
      if (permission == null) {
        throw new PermissionDeniedException("String_Node_Str" + account.getAccountName() + "String_Node_Str"+ template.getName());
      }
    }
  }
  byte[] decodedUserData=null;
  if (userData != null) {
    if (userData.length() >= 2 * UserVmManager.MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    decodedUserData=org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes());
    if (decodedUserData.length > UserVmManager.MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (decodedUserData.length < 1) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (offering.getGuestIpType() != NetworkOffering.GuestIpType.Virtualized) {
    _networkGroupMgr.createDefaultNetworkGroup(accountId);
  }
  if (networkGroups != null) {
    if (offering.getGuestIpType() == NetworkOffering.GuestIpType.Virtualized) {
      throw new InvalidParameterValueException("String_Node_Str" + offering.getName());
    }
    Set<String> nameSet=new HashSet<String>();
    nameSet.addAll(Arrays.asList(networkGroups));
    nameSet.add(NetworkGroupManager.DEFAULT_GROUP_NAME);
    networkGroups=nameSet.toArray(new String[nameSet.size()]);
    List<NetworkGroupVO> networkGroupVOs=_networkSecurityGroupDao.findByAccountAndNames(accountId,networkGroups);
    if (networkGroupVOs.size() != nameSet.size()) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
 else {
    if (offering.getGuestIpType() != NetworkOffering.GuestIpType.Virtualized && _networkGroupsEnabled) {
      networkGroups=new String[]{NetworkGroupManager.DEFAULT_GROUP_NAME};
    }
  }
  Long eventId=cmd.getStartEventId();
  try {
    return deployVirtualMachineImpl(userId,accountId,dataCenterId,serviceOfferingId,template,diskOfferingId,domain,password,displayName,group,userData,networkGroups,eventId,size);
  }
 catch (  ResourceAllocationException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  ExecutionException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  InvalidParameterValueException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  InsufficientStorageCapacityException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  PermissionDeniedException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  ConcurrentOperationException e) {
    if (s_logger.isDebugEnabled())     s_logger.debug("String_Node_Str" + e.getMessage());
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw e;
  }
catch (  Exception e) {
    s_logger.warn("String_Node_Str" + e.getMessage(),e);
    EventUtils.saveEvent(userId,accountId,EventVO.LEVEL_ERROR,EventTypes.EVENT_VM_CREATE,"String_Node_Str",null,eventId);
    throw new CloudRuntimeException("String_Node_Str" + e.getMessage());
  }
}