Program: 107224
Project: 107224
Buggy source:
@Override @DB public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException {
  Account caller=UserContext.current().getCaller();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  List<Long> networkList=cmd.getNetworkIds();
  String group=cmd.getGroup();
  Account owner=_accountDao.findActiveAccount(accountName,domainId);
  if (owner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
  }
  _accountMgr.checkAccess(caller,owner);
  long accountId=owner.getId();
  DataCenterVO dc=_dcDao.findById(cmd.getZoneId());
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getZoneId());
  }
  if (dc.getDomainId() != null) {
    DomainVO domain=_domainDao.findById(dc.getDomainId());
    if (domain == null) {
      throw new CloudRuntimeException("String_Node_Str" + dc.getDomainId() + "String_Node_Str"+ dc);
    }
    _accountMgr.checkAccess(caller,domain);
    _accountMgr.checkAccess(owner,domain);
  }
  ServiceOfferingVO offering=_serviceOfferingDao.findById(cmd.getServiceOfferingId());
  if (offering == null || offering.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getServiceOfferingId());
  }
  VMTemplateVO template=_templateDao.findById(cmd.getTemplateId());
  if (template == null || template.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getTemplateId());
  }
  boolean isIso=Storage.ImageFormat.ISO == template.getFormat();
  if (isIso && !template.isBootable()) {
    throw new InvalidParameterValueException("String_Node_Str" + template.getId());
  }
  Pair<DiskOfferingVO,Long> rootDiskOffering=new Pair<DiskOfferingVO,Long>(null,null);
  List<Pair<DiskOfferingVO,Long>> dataDiskOfferings=new ArrayList<Pair<DiskOfferingVO,Long>>();
  if (isIso) {
    if (cmd.getDiskOfferingId() == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
    if (diskOffering == null) {
      throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
    }
    Long size=null;
    if (diskOffering.getDiskSize() == 0) {
      size=cmd.getSize();
      if (size == null) {
        throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
      }
    }
    rootDiskOffering.first(diskOffering);
    rootDiskOffering.second(size);
  }
 else {
    rootDiskOffering.first(offering);
    if (cmd.getDiskOfferingId() != null) {
      DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
      if (diskOffering == null) {
        throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
      }
      Long size=null;
      if (diskOffering.getDiskSize() == 0) {
        size=cmd.getSize();
        if (size == null) {
          throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
        }
      }
      dataDiskOfferings.add(new Pair<DiskOfferingVO,Long>(diskOffering,size));
    }
  }
  String password=PasswordGenerator.generateRandomPassword(6);
  if (!template.getEnablePassword()) {
    password="String_Node_Str";
  }
  if (password == null || password.equals("String_Node_Str") || (!validPassword(password))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String networkDomain=null;
  if (networkDomain == null) {
    networkDomain="String_Node_Str" + Long.toHexString(owner.getId()) + _defaultNetworkDomain;
  }
  String userData=cmd.getUserData();
  byte[] decodedUserData=null;
  if (userData != null) {
    if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    decodedUserData=org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes());
    if (decodedUserData.length > MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (decodedUserData.length < 1) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  String sshPublicKey=null;
  if (cmd.getSSHKeyPairName() != null && !cmd.getSSHKeyPairName().equals("String_Node_Str")) {
    Account account=UserContext.current().getCaller();
    SSHKeyPair pair=_sshKeyPairDao.findByName(account.getAccountId(),account.getDomainId(),cmd.getSSHKeyPairName());
    if (pair == null)     throw new InvalidParameterValueException("String_Node_Str" + cmd.getSSHKeyPairName() + "String_Node_Str");
    sshPublicKey=pair.getPublicKey();
  }
  _accountMgr.checkAccess(caller,template);
  DataCenterDeployment plan=new DataCenterDeployment(dc.getId());
  s_logger.debug("String_Node_Str");
  if (dc.getNetworkType() == NetworkType.Basic && networkList == null) {
    Network defaultNetwork=_networkMgr.getSystemNetworkByZoneAndTrafficType(dc.getId(),TrafficType.Guest);
    if (defaultNetwork == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else {
      networkList=new ArrayList<Long>();
      networkList.add(defaultNetwork.getId());
    }
  }
  if (networkList == null || networkList.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>();
  short defaultNetworkNumber=0;
  for (  Long networkId : networkList) {
    NetworkVO network=_networkDao.findById(networkId);
    if (network == null) {
      throw new InvalidParameterValueException("String_Node_Str" + networkId);
    }
 else {
      if (!network.isShared()) {
        List<NetworkVO> networkMap=_networkDao.listBy(accountId,networkId);
        if (networkMap == null || networkMap.isEmpty()) {
          throw new PermissionDeniedException("String_Node_Str" + networkId + "String_Node_Str");
        }
      }
      if (network.isDefault()) {
        defaultNetworkNumber++;
      }
      networks.add(new Pair<NetworkVO,NicProfile>(network,null));
    }
  }
  if (defaultNetworkNumber == 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
 else   if (defaultNetworkNumber > 1) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  long id=_vmDao.getNextInSequence(Long.class,"String_Node_Str");
  String hostName=cmd.getName();
  String instanceName=VirtualMachineName.getVmName(id,owner.getId(),_instance);
  if (hostName == null) {
    hostName=instanceName;
  }
 else {
    hostName=hostName.toLowerCase();
    UserVm vm=_vmDao.findVmByZoneIdAndName(dc.getId(),hostName);
    if (vm != null && !(vm.getState() == State.Expunging || vm.getState() == State.Error)) {
      throw new InvalidParameterValueException("String_Node_Str" + hostName + "String_Node_Str"+ dc.getId());
    }
 else     if (!NetUtils.verifyHostName(hostName)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
    }
  }
  HypervisorType hypervisorType=null;
  if (template == null || template.getHypervisorType() == null || template.getHypervisorType() == HypervisorType.None) {
    hypervisorType=cmd.getHypervisor();
  }
 else {
    hypervisorType=template.getHypervisorType();
  }
  UserVmVO vm=new UserVmVO(id,instanceName,cmd.getDisplayName(),template.getId(),hypervisorType,template.getGuestOSId(),offering.getOfferHA(),domainId,owner.getId(),offering.getId(),userData,hostName);
  if (sshPublicKey != null)   vm.setDetail("String_Node_Str",sshPublicKey);
  if (_itMgr.allocate(vm,template,offering,rootDiskOffering,dataDiskOfferings,networks,null,plan,cmd.getHypervisor(),owner) == null) {
    return null;
  }
  _vmDao.saveDetails(vm);
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VM_CREATE,accountId,dc.getId(),vm.getId(),vm.getName(),offering.getId(),template.getId(),null);
  _usageEventDao.persist(usageEvent);
  _accountMgr.incrementResourceCount(accountId,ResourceType.user_vm);
  try {
    if (group != null) {
      boolean addToGroup=addInstanceToGroup(Long.valueOf(id),group);
      if (!addToGroup) {
        throw new CloudRuntimeException("String_Node_Str" + group);
      }
    }
  }
 catch (  Exception ex) {
    throw new CloudRuntimeException("String_Node_Str" + group);
  }
  return vm;
}

Fixed version (ground truth):
@Override @DB public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, StorageUnavailableException {
  Account caller=UserContext.current().getCaller();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  List<Long> networkList=cmd.getNetworkIds();
  String group=cmd.getGroup();
  Account owner=_accountDao.findActiveAccount(accountName,domainId);
  if (owner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
  }
  _accountMgr.checkAccess(caller,owner);
  long accountId=owner.getId();
  DataCenterVO dc=_dcDao.findById(cmd.getZoneId());
  if (dc == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getZoneId());
  }
  if (dc.getDomainId() != null) {
    DomainVO domain=_domainDao.findById(dc.getDomainId());
    if (domain == null) {
      throw new CloudRuntimeException("String_Node_Str" + dc.getDomainId() + "String_Node_Str"+ dc);
    }
    _accountMgr.checkAccess(caller,domain);
    _accountMgr.checkAccess(owner,domain);
  }
  List<StoragePoolVO> availablePools=_storagePoolDao.listPoolsByStatus(com.cloud.host.Status.Up);
  if (availablePools == null || availablePools.size() < 1) {
    throw new StorageUnavailableException("String_Node_Str",-1);
  }
  ServiceOfferingVO offering=_serviceOfferingDao.findById(cmd.getServiceOfferingId());
  if (offering == null || offering.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getServiceOfferingId());
  }
  VMTemplateVO template=_templateDao.findById(cmd.getTemplateId());
  if (template == null || template.getRemoved() != null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getTemplateId());
  }
  boolean isIso=Storage.ImageFormat.ISO == template.getFormat();
  if (isIso && !template.isBootable()) {
    throw new InvalidParameterValueException("String_Node_Str" + template.getId());
  }
  Pair<DiskOfferingVO,Long> rootDiskOffering=new Pair<DiskOfferingVO,Long>(null,null);
  List<Pair<DiskOfferingVO,Long>> dataDiskOfferings=new ArrayList<Pair<DiskOfferingVO,Long>>();
  if (isIso) {
    if (cmd.getDiskOfferingId() == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
    if (diskOffering == null) {
      throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
    }
    Long size=null;
    if (diskOffering.getDiskSize() == 0) {
      size=cmd.getSize();
      if (size == null) {
        throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
      }
    }
    rootDiskOffering.first(diskOffering);
    rootDiskOffering.second(size);
  }
 else {
    rootDiskOffering.first(offering);
    if (cmd.getDiskOfferingId() != null) {
      DiskOfferingVO diskOffering=_diskOfferingDao.findById(cmd.getDiskOfferingId());
      if (diskOffering == null) {
        throw new InvalidParameterValueException("String_Node_Str" + cmd.getDiskOfferingId());
      }
      Long size=null;
      if (diskOffering.getDiskSize() == 0) {
        size=cmd.getSize();
        if (size == null) {
          throw new InvalidParameterValueException("String_Node_Str" + diskOffering + "String_Node_Str");
        }
      }
      dataDiskOfferings.add(new Pair<DiskOfferingVO,Long>(diskOffering,size));
    }
  }
  String password=PasswordGenerator.generateRandomPassword(6);
  if (!template.getEnablePassword()) {
    password="String_Node_Str";
  }
  if (password == null || password.equals("String_Node_Str") || (!validPassword(password))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String networkDomain=null;
  if (networkDomain == null) {
    networkDomain="String_Node_Str" + Long.toHexString(owner.getId()) + _defaultNetworkDomain;
  }
  String userData=cmd.getUserData();
  byte[] decodedUserData=null;
  if (userData != null) {
    if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    decodedUserData=org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes());
    if (decodedUserData.length > MAX_USER_DATA_LENGTH_BYTES) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (decodedUserData.length < 1) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  String sshPublicKey=null;
  if (cmd.getSSHKeyPairName() != null && !cmd.getSSHKeyPairName().equals("String_Node_Str")) {
    Account account=UserContext.current().getCaller();
    SSHKeyPair pair=_sshKeyPairDao.findByName(account.getAccountId(),account.getDomainId(),cmd.getSSHKeyPairName());
    if (pair == null)     throw new InvalidParameterValueException("String_Node_Str" + cmd.getSSHKeyPairName() + "String_Node_Str");
    sshPublicKey=pair.getPublicKey();
  }
  _accountMgr.checkAccess(caller,template);
  DataCenterDeployment plan=new DataCenterDeployment(dc.getId());
  s_logger.debug("String_Node_Str");
  if (dc.getNetworkType() == NetworkType.Basic && networkList == null) {
    Network defaultNetwork=_networkMgr.getSystemNetworkByZoneAndTrafficType(dc.getId(),TrafficType.Guest);
    if (defaultNetwork == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else {
      networkList=new ArrayList<Long>();
      networkList.add(defaultNetwork.getId());
    }
  }
  if (networkList == null || networkList.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>();
  short defaultNetworkNumber=0;
  for (  Long networkId : networkList) {
    NetworkVO network=_networkDao.findById(networkId);
    if (network == null) {
      throw new InvalidParameterValueException("String_Node_Str" + networkId);
    }
 else {
      if (!network.isShared()) {
        List<NetworkVO> networkMap=_networkDao.listBy(accountId,networkId);
        if (networkMap == null || networkMap.isEmpty()) {
          throw new PermissionDeniedException("String_Node_Str" + networkId + "String_Node_Str");
        }
      }
      if (network.isDefault()) {
        defaultNetworkNumber++;
      }
      networks.add(new Pair<NetworkVO,NicProfile>(network,null));
    }
  }
  if (defaultNetworkNumber == 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
 else   if (defaultNetworkNumber > 1) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  long id=_vmDao.getNextInSequence(Long.class,"String_Node_Str");
  String hostName=cmd.getName();
  String instanceName=VirtualMachineName.getVmName(id,owner.getId(),_instance);
  if (hostName == null) {
    hostName=instanceName;
  }
 else {
    hostName=hostName.toLowerCase();
    UserVm vm=_vmDao.findVmByZoneIdAndName(dc.getId(),hostName);
    if (vm != null && !(vm.getState() == State.Expunging || vm.getState() == State.Error)) {
      throw new InvalidParameterValueException("String_Node_Str" + hostName + "String_Node_Str"+ dc.getId());
    }
 else     if (!NetUtils.verifyHostName(hostName)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
    }
  }
  HypervisorType hypervisorType=null;
  if (template == null || template.getHypervisorType() == null || template.getHypervisorType() == HypervisorType.None) {
    hypervisorType=cmd.getHypervisor();
  }
 else {
    hypervisorType=template.getHypervisorType();
  }
  UserVmVO vm=new UserVmVO(id,instanceName,cmd.getDisplayName(),template.getId(),hypervisorType,template.getGuestOSId(),offering.getOfferHA(),domainId,owner.getId(),offering.getId(),userData,hostName);
  if (sshPublicKey != null)   vm.setDetail("String_Node_Str",sshPublicKey);
  if (_itMgr.allocate(vm,template,offering,rootDiskOffering,dataDiskOfferings,networks,null,plan,cmd.getHypervisor(),owner) == null) {
    return null;
  }
  _vmDao.saveDetails(vm);
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + vm);
  }
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VM_CREATE,accountId,dc.getId(),vm.getId(),vm.getName(),offering.getId(),template.getId(),null);
  _usageEventDao.persist(usageEvent);
  _accountMgr.incrementResourceCount(accountId,ResourceType.user_vm);
  try {
    if (group != null) {
      boolean addToGroup=addInstanceToGroup(Long.valueOf(id),group);
      if (!addToGroup) {
        throw new CloudRuntimeException("String_Node_Str" + group);
      }
    }
  }
 catch (  Exception ex) {
    throw new CloudRuntimeException("String_Node_Str" + group);
  }
  return vm;
}