/**
   * Retrieve a storage domain using a specified storage domain ID.
   *
   * @param storageDomainId the domain's ID
   * @return the storage domain
   */
  @SuppressWarnings("unchecked")
  protected StorageDomain getStorageDomainById(Guid storageDomainId) {
    VDSReturnValue returnValue;

    try {
      returnValue =
          executeHSMGetStorageDomainInfo(
              new HSMGetStorageDomainInfoVDSCommandParameters(
                  getParameters().getVdsId(), storageDomainId));
    } catch (RuntimeException e) {
      log.error(
          "Could not get info for storage domain ID: '{}': {}", storageDomainId, e.getMessage());
      log.debug("Exception", e);
      return null;
    }

    Pair<StorageDomainStatic, SANState> result =
        (Pair<StorageDomainStatic, SANState>) returnValue.getReturnValue();
    StorageDomainStatic storageDomainStatic = result.getFirst();
    storageDomainStatic.setStorageType(getParameters().getStorageType());

    StorageDomain storageDomain = new StorageDomain();
    storageDomain.setStorageStaticData(storageDomainStatic);

    return storageDomain;
  }
 private VolumeType getVolumeTypeForDomain() {
   if (volumeTypeForDomain == null) {
     StorageDomainStatic sdStatic =
         DbFacade.getInstance().getStorageDomainStaticDao().get(storageDomainId);
     volumeTypeForDomain =
         HibernateVmCommand.getMemoryVolumeTypeForStorageDomain(sdStatic.getStorageType());
   }
   return volumeTypeForDomain;
 }
 @Override
 public void vdsNonOperational(
     Guid vdsId, NonOperationalReason reason, boolean logCommand, Guid domainId) {
   StorageDomainStatic storageDomain =
       DbFacade.getInstance().getStorageDomainStaticDao().get(domainId);
   Map<String, String> customLogValues = null;
   if (storageDomain != null) {
     customLogValues = Collections.singletonMap("StorageDomainNames", storageDomain.getName());
   }
   vdsNonOperational(vdsId, reason, logCommand, domainId, customLogValues);
 }
 public static boolean checkImageConfiguration(
     StorageDomainStatic storageDomain, VolumeType volumeType, VolumeFormat volumeFormat) {
   return !((volumeType == VolumeType.Preallocated && volumeFormat == VolumeFormat.COW)
       || (storageDomain.getStorageType().isBlockDomain()
           && volumeType == VolumeType.Sparse
           && volumeFormat == VolumeFormat.RAW)
       || volumeFormat == VolumeFormat.Unassigned
       || volumeType == VolumeType.Unassigned);
 }
  private static Pair<StorageDomainStatic, Guid> BuildStorageStaticFromXmlRpcStruct(
      Map<String, Object> xmlRpcStruct) {
    Pair<StorageDomainStatic, Guid> returnValue = new Pair<StorageDomainStatic, Guid>();
    StorageDomainStatic sdStatic = new StorageDomainStatic();
    if (xmlRpcStruct.containsKey("name")) {
      sdStatic.setStorageName(xmlRpcStruct.get("name").toString());
    }
    if (xmlRpcStruct.containsKey("type")) {
      sdStatic.setStorageType(
          EnumUtils.valueOf(StorageType.class, xmlRpcStruct.get("type").toString(), true));
    }
    if (xmlRpcStruct.containsKey("class")) {
      String domainType = xmlRpcStruct.get("class").toString();
      if ("backup".equalsIgnoreCase(domainType)) {
        sdStatic.setStorageDomainType(StorageDomainType.ImportExport);
      } else {
        sdStatic.setStorageDomainType(EnumUtils.valueOf(StorageDomainType.class, domainType, true));
      }
    }
    if (xmlRpcStruct.containsKey("version")) {
      sdStatic.setStorageFormat(StorageFormatType.forValue(xmlRpcStruct.get("version").toString()));
    }
    if (sdStatic.getStorageType() != StorageType.UNKNOWN) {
      if (sdStatic.getStorageType().isFileDomain() && xmlRpcStruct.containsKey("remotePath")) {
        String path = xmlRpcStruct.get("remotePath").toString();
        List<StorageServerConnections> connections =
            DbFacade.getInstance().getStorageServerConnectionDao().getAllForStorage(path);
        if (connections.isEmpty()) {
          sdStatic.setConnection(new StorageServerConnections());
          sdStatic.getConnection().setconnection(path);
          sdStatic.getConnection().setstorage_type(sdStatic.getStorageType());
        } else {
          sdStatic.setStorage(connections.get(0).getid());
          sdStatic.setConnection(connections.get(0));
        }
      } else if (sdStatic.getStorageType() != StorageType.NFS
          && (xmlRpcStruct.containsKey("vguuid"))) {
        sdStatic.setStorage(xmlRpcStruct.get("vguuid").toString());
      }
    }
    if (xmlRpcStruct.containsKey("state")) {
      sdStatic.setSanState(
          EnumUtils.valueOf(
              SANState.class, xmlRpcStruct.get("state").toString().toUpperCase(), false));
    }
    returnValue.setFirst(sdStatic);
    Object[] poolUUIDs = (Object[]) xmlRpcStruct.get("pool");
    if (poolUUIDs.length != 0) {
      returnValue.setSecond(Guid.createGuidFromString(poolUUIDs[0].toString()));
    }

    return returnValue;
  }
  @Test
  public void statusSetInMap() {
    Guid storageDomainId = Guid.newGuid();
    Guid poolId = Guid.newGuid();
    AttachStorageDomainToPoolParameters params =
        new AttachStorageDomainToPoolParameters(storageDomainId, poolId);
    AttachStorageDomainToPoolCommand<AttachStorageDomainToPoolParameters> cmd =
        spy(new AttachStorageDomainToPoolCommand<>(params));

    doNothing().when(cmd).attemptToActivateDomain();
    doReturn(Collections.emptyList()).when(cmd).connectHostsInUpToDomainStorageServer();
    doReturn(isoMapDao).when(cmd).getStoragePoolIsoMapDao();
    doReturn(storagePoolDao).when(cmd).getStoragePoolDao();
    doReturn(vdsDao).when(cmd).getVdsDao();
    doReturn(storageDomainDao).when(cmd).getStorageDomainDao();
    doReturn(storageDomainStaticDao).when(cmd).getStorageDomainStaticDao();
    doReturn(vdsBrokerFrontend).when(cmd).getVdsBroker();

    StoragePool pool = new StoragePool();
    pool.setId(poolId);
    pool.setStatus(StoragePoolStatus.Up);
    when(storagePoolDao.get(any(Guid.class))).thenReturn(pool);
    when(isoMapDao.get(any(StoragePoolIsoMapId.class))).thenReturn(map);
    when(storageDomainDao.getForStoragePool(any(Guid.class), any(Guid.class)))
        .thenReturn(new StorageDomain());
    when(storageDomainStaticDao.get(any(Guid.class))).thenReturn(new StorageDomainStatic());
    doReturn(pool.getId()).when(cmd).getStoragePoolIdFromVds();
    doReturn(backendInternal).when(cmd).getBackend();
    VdcReturnValueBase vdcReturnValue = new VdcReturnValueBase();
    vdcReturnValue.setSucceeded(true);
    when(backendInternal.runInternalAction(
            any(VdcActionType.class),
            any(VdcActionParametersBase.class),
            any(CommandContext.class)))
        .thenReturn(vdcReturnValue);
    StorageDomainStatic storageDomain = new StorageDomainStatic();
    storageDomain.setId(Guid.newGuid());
    storageDomain.setStorageDomainType(StorageDomainType.ImportExport);
    mockGetStorageDomainInfoVdsCommand(storageDomain);
    mockAttachStorageDomainVdsCommand();
    when(vdsDao.get(any(Guid.class))).thenReturn(vds);
    doReturn(Collections.emptyList())
        .when(cmd)
        .getEntitiesFromStorageOvfDisk(storageDomainId, pool.getId());
    doReturn(Collections.emptyList()).when(cmd).getAllOVFDisks(storageDomainId, pool.getId());
    doAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                map = (StoragePoolIsoMap) invocation.getArguments()[0];
                return null;
              }
            })
        .when(isoMapDao)
        .save(any(StoragePoolIsoMap.class));

    cmd.setCompensationContext(mock(CompensationContext.class));
    cmd.executeCommand();
    assertNotNull(map);
    assertEquals(StorageDomainStatus.Maintenance, map.getStatus());
  }