Search in sources :

Example 81 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class XenServer610WrapperTest method testMigrateWithStorageSendCommandNetException.

@Test
public void testMigrateWithStorageSendCommandNetException() {
    final String vmName = "small";
    final String path = "/";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO volume1 = Mockito.mock(VolumeTO.class);
    final VolumeTO volume2 = Mockito.mock(VolumeTO.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final VDI vdi1 = Mockito.mock(VDI.class);
    final VDI vdi2 = Mockito.mock(VDI.class);
    final NicTO nic1 = Mockito.mock(NicTO.class);
    final NicTO nic2 = Mockito.mock(NicTO.class);
    final Gson gson = new Gson();
    final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<>();
    volumeToSr.add(new Pair<>(volume1, sr1));
    volumeToSr.add(new Pair<>(volume2, sr2));
    final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<>();
    nicToNetwork.add(new Pair<>(nic1, new String("a")));
    nicToNetwork.add(new Pair<>(nic2, new String("b")));
    final Map<String, String> token = new HashMap<>();
    final MigrateWithStorageSendCommand migrateStorageCommand = new MigrateWithStorageSendCommand(vmSpec, volumeToSr, nicToNetwork, token);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(volume1.getPath()).thenReturn(path);
    when(volume2.getPath()).thenReturn(path);
    when(xenServer610Resource.getVDIbyUuid(conn, volume1.getPath())).thenReturn(vdi1);
    when(xenServer610Resource.getVDIbyUuid(conn, volume2.getPath())).thenReturn(vdi2);
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) Answer(com.cloud.legacymodel.communication.answer.Answer) VolumeTO(com.cloud.legacymodel.to.VolumeTO) VDI(com.xensource.xenapi.VDI) MigrateWithStorageSendCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageSendCommand) SR(com.xensource.xenapi.SR) NicTO(com.cloud.legacymodel.to.NicTO) Pair(com.cloud.legacymodel.utils.Pair) Test(org.junit.Test)

Example 82 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class XenServer610WrapperTest method testMigrateWithStorageSendCommand.

@Test
public void testMigrateWithStorageSendCommand() {
    final String vmName = "small";
    final String path = "/";
    final String mac = "3c:15:c2:c4:4f:18";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO volume1 = Mockito.mock(VolumeTO.class);
    final VolumeTO volume2 = Mockito.mock(VolumeTO.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final VDI vdi1 = Mockito.mock(VDI.class);
    final VDI vdi2 = Mockito.mock(VDI.class);
    final NicTO nic1 = Mockito.mock(NicTO.class);
    final NicTO nic2 = Mockito.mock(NicTO.class);
    final Network network1 = Mockito.mock(Network.class);
    final Network network2 = Mockito.mock(Network.class);
    final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<>();
    volumeToSr.add(new Pair<>(volume1, sr1));
    volumeToSr.add(new Pair<>(volume2, sr2));
    final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<>();
    nicToNetwork.add(new Pair<>(nic1, network1));
    nicToNetwork.add(new Pair<>(nic2, network2));
    final Map<String, String> token = new HashMap<>();
    final VIF vif1 = Mockito.mock(VIF.class);
    final VIF vif2 = Mockito.mock(VIF.class);
    final MigrateWithStorageSendCommand migrateStorageCommand = new MigrateWithStorageSendCommand(vmSpec, volumeToSr, nicToNetwork, token);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(volume1.getPath()).thenReturn(path);
    when(volume2.getPath()).thenReturn(path);
    when(nic1.getMac()).thenReturn(mac);
    when(nic2.getMac()).thenReturn(mac);
    when(xenServer610Resource.getVDIbyUuid(conn, volume1.getPath())).thenReturn(vdi1);
    when(xenServer610Resource.getVDIbyUuid(conn, volume2.getPath())).thenReturn(vdi2);
    try {
        when(xenServer610Resource.getVifByMac(conn, null, nic1.getMac())).thenReturn(vif1);
        when(xenServer610Resource.getVifByMac(conn, null, nic2.getMac())).thenReturn(vif2);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    try {
        verify(xenServer610Resource, times(2)).getVDIbyUuid(conn, volume1.getPath());
        verify(xenServer610Resource, times(2)).getVifByMac(conn, null, nic1.getMac());
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    assertFalse(answer.getResult());
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) Answer(com.cloud.legacymodel.communication.answer.Answer) VolumeTO(com.cloud.legacymodel.to.VolumeTO) VIF(com.xensource.xenapi.VIF) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VDI(com.xensource.xenapi.VDI) MigrateWithStorageSendCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageSendCommand) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR) NicTO(com.cloud.legacymodel.to.NicTO) Pair(com.cloud.legacymodel.utils.Pair) Test(org.junit.Test)

Example 83 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class CapacityDaoImpl method orderPodsByAggregateCapacity.

@Override
public Pair<List<Long>, Map<Long, Double>> orderPodsByAggregateCapacity(final long zoneId, final short capacityTypeForOrdering) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    final List<Long> result = new ArrayList<>();
    final Map<Long, Double> podCapacityMap = new HashMap<>();
    StringBuilder sql = null;
    try {
        if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU | capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
            sql = new StringBuilder(ORDER_PODS_BY_AGGREGATE_OVERCOMMIT_CAPACITY);
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
            pstmt.setLong(1, zoneId);
            pstmt.setShort(2, capacityTypeForOrdering);
            if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU) {
                pstmt.setString(3, "cpuOvercommitRatio");
            } else if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
                pstmt.setString(3, "memoryOvercommitRatio");
            }
        } else {
            sql = new StringBuilder(ORDER_PODS_BY_AGGREGATE_CAPACITY);
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
            pstmt.setLong(1, zoneId);
            pstmt.setShort(2, capacityTypeForOrdering);
        }
        final ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            final Long podId = rs.getLong(1);
            result.add(podId);
            podCapacityMap.put(podId, rs.getDouble(2));
        }
        return new Pair<>(result, podCapacityMap);
    } catch (final SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ResultSet(java.sql.ResultSet) Pair(com.cloud.legacymodel.utils.Pair)

Example 84 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForCacheStoresInternal.

private Pair<List<ImageStoreJoinVO>, Integer> searchForCacheStoresInternal(final ListSecondaryStagingStoresCmd cmd) {
    final Long zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), cmd.getZoneId());
    final Object id = cmd.getId();
    final Object name = cmd.getStoreName();
    final String provider = cmd.getProvider();
    final String protocol = cmd.getProtocol();
    final Object keyword = cmd.getKeyword();
    final Long startIndex = cmd.getStartIndex();
    final Long pageSize = cmd.getPageSizeVal();
    final Filter searchFilter = new Filter(ImageStoreJoinVO.class, "id", Boolean.TRUE, startIndex, pageSize);
    final SearchBuilder<ImageStoreJoinVO> sb = _imageStoreJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    // ids
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
    sb.and("protocol", sb.entity().getProtocol(), SearchCriteria.Op.EQ);
    sb.and("provider", sb.entity().getProviderName(), SearchCriteria.Op.EQ);
    sb.and("role", sb.entity().getRole(), SearchCriteria.Op.EQ);
    final SearchCriteria<ImageStoreJoinVO> sc = sb.create();
    sc.setParameters("role", DataStoreRole.ImageCache);
    if (keyword != null) {
        final SearchCriteria<ImageStoreJoinVO> ssc = _imageStoreJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("provider", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (provider != null) {
        sc.setParameters("provider", provider);
    }
    if (protocol != null) {
        sc.setParameters("protocol", protocol);
    }
    // search Store details by ids
    final Pair<List<ImageStoreJoinVO>, Integer> uniqueStorePair = _imageStoreJoinDao.searchAndCount(sc, searchFilter);
    final Integer count = uniqueStorePair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueStorePair;
    }
    final List<ImageStoreJoinVO> uniqueStores = uniqueStorePair.first();
    final Long[] vrIds = new Long[uniqueStores.size()];
    int i = 0;
    for (final ImageStoreJoinVO v : uniqueStores) {
        vrIds[i++] = v.getId();
    }
    final List<ImageStoreJoinVO> vrs = _imageStoreJoinDao.searchByIds(vrIds);
    return new Pair<>(vrs, count);
}
Also used : ImageStoreJoinVO(com.cloud.api.query.vo.ImageStoreJoinVO) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.legacymodel.utils.Pair)

Example 85 with Pair

use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForVolumesInternal.

private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(final ListVolumesCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    final List<Long> permittedAccounts = new ArrayList<>();
    final Long id = cmd.getId();
    final Long vmInstanceId = cmd.getVirtualMachineId();
    final String name = cmd.getVolumeName();
    final String keyword = cmd.getKeyword();
    final String type = cmd.getType();
    final Map<String, String> tags = cmd.getTags();
    final Long storageId = cmd.getStorageId();
    final Long diskOffId = cmd.getDiskOfferingId();
    final Boolean display = cmd.getDisplay();
    final Long zoneId = cmd.getZoneId();
    final Long podId = cmd.getPodId();
    final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    final Long domainId = domainIdRecursiveListProject.first();
    final Boolean isRecursive = domainIdRecursiveListProject.second();
    final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    final Filter searchFilter = new Filter(VolumeJoinVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    // hack for now, this should be done better but due to needing a join I
    // opted to
    // do this quickly and worry about making it pretty later
    final SearchBuilder<VolumeJoinVO> sb = _volumeJoinDao.createSearchBuilder();
    // select distinct
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    // ids to get
    // number of
    // records with
    // pagination
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("volumeType", sb.entity().getVolumeType(), SearchCriteria.Op.LIKE);
    sb.and("instanceId", sb.entity().getVmId(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("storageId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
    sb.and("diskOfferingId", sb.entity().getDiskOfferingId(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplayVolume(), SearchCriteria.Op.EQ);
    // Only return volumes that are not destroyed
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("systemUse", sb.entity().isSystemUse(), SearchCriteria.Op.NEQ);
    // now set the SC criteria...
    final SearchCriteria<VolumeJoinVO> sc = sb.create();
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        final SearchCriteria<VolumeJoinVO> ssc = _volumeJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("volumeType", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    // normal users and domain admins cannot see volumes of type system
    sc.setParameters("systemUse", 1);
    // root admins can see them
    if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
        sc.setParameters("systemUse", -1);
    }
    if (tags != null && !tags.isEmpty()) {
        final SearchCriteria<VolumeJoinVO> tagSc = _volumeJoinDao.createSearchCriteria();
        for (final String key : tags.keySet()) {
            final SearchCriteria<VolumeJoinVO> tsc = _volumeJoinDao.createSearchCriteria();
            tsc.addAnd("tagKey", SearchCriteria.Op.EQ, key);
            tsc.addAnd("tagValue", SearchCriteria.Op.EQ, tags.get(key));
            tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
    }
    if (diskOffId != null) {
        sc.setParameters("diskOfferingId", diskOffId);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (type != null) {
        sc.setParameters("volumeType", "%" + type + "%");
    }
    if (vmInstanceId != null) {
        sc.setParameters("instanceId", vmInstanceId);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (storageId != null) {
        sc.setParameters("storageId", storageId);
    }
    // Only return volumes that are not destroyed
    sc.setParameters("state", Volume.State.Destroy);
    // search Volume details by ids
    final Pair<List<VolumeJoinVO>, Integer> uniqueVolPair = _volumeJoinDao.searchAndCount(sc, searchFilter);
    final Integer count = uniqueVolPair.second();
    if (count.intValue() == 0) {
        // empty result
        return uniqueVolPair;
    }
    final List<VolumeJoinVO> uniqueVols = uniqueVolPair.first();
    final Long[] vrIds = new Long[uniqueVols.size()];
    int i = 0;
    for (final VolumeJoinVO v : uniqueVols) {
        vrIds[i++] = v.getId();
    }
    final List<VolumeJoinVO> vrs = _volumeJoinDao.searchByIds(vrIds);
    return new Pair<>(vrs, count);
}
Also used : Account(com.cloud.legacymodel.user.Account) Ternary(com.cloud.legacymodel.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) ArrayList(java.util.ArrayList) List(java.util.List) VolumeJoinVO(com.cloud.api.query.vo.VolumeJoinVO) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

Pair (com.cloud.legacymodel.utils.Pair)139 ArrayList (java.util.ArrayList)87 List (java.util.List)64 Account (com.cloud.legacymodel.user.Account)49 Filter (com.cloud.utils.db.Filter)48 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)38 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)27 HashMap (java.util.HashMap)27 Ternary (com.cloud.legacymodel.utils.Ternary)23 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)22 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)20 SSHKeyPair (com.cloud.legacymodel.user.SSHKeyPair)16 TemplateFilter (com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter)13 Map (java.util.Map)13 DB (com.cloud.utils.db.DB)11 DomainVO (com.cloud.domain.DomainVO)10 VolumeVO (com.cloud.storage.VolumeVO)10 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)9 Network (com.cloud.legacymodel.network.Network)9 ResourceTagVO (com.cloud.tags.ResourceTagVO)9