Search in sources :

Example 21 with Pair

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

the class ManagementServerImpl method listStoragePoolsForMigrationOfVolume.

@Override
public Pair<List<? extends StoragePool>, List<? extends StoragePool>> listStoragePoolsForMigrationOfVolume(final Long volumeId) {
    final Account caller = getCaller();
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Caller is not a root admin, permission denied to migrate the volume");
        }
        throw new PermissionDeniedException("No permission to migrate volume, only root admin can migrate a volume");
    }
    final VolumeVO volume = _volumeDao.findById(volumeId);
    if (volume == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find volume with" + " specified id.");
        ex.addProxyObject(volumeId.toString(), "volumeId");
        throw ex;
    }
    // Volume must be attached to an instance for live migration.
    final List<StoragePool> allPools = new ArrayList<>();
    final List<StoragePool> suitablePools = new ArrayList<>();
    // Volume must be in Ready state to be migrated.
    if (!Volume.State.Ready.equals(volume.getState())) {
        s_logger.info("Volume " + volume + " must be in ready state for migration.");
        return new Pair<>(allPools, suitablePools);
    }
    if (!_volumeMgr.volumeOnSharedStoragePool(volume)) {
        s_logger.info("Volume " + volume + " is on local storage. It cannot be migrated to another pool.");
        return new Pair<>(allPools, suitablePools);
    }
    final Long instanceId = volume.getInstanceId();
    VMInstanceVO vm = null;
    if (instanceId != null) {
        vm = _vmInstanceDao.findById(instanceId);
    }
    if (vm == null) {
        s_logger.info("Volume " + volume + " isn't attached to any vm. Looking for storage pools in the " + "zone to which this volumes can be migrated.");
    } else if (vm.getState() != State.Running) {
        s_logger.info("Volume " + volume + " isn't attached to any running vm. Looking for storage pools in the " + "cluster to which this volumes can be migrated.");
    } else {
        s_logger.info("Volume " + volume + " is attached to any running vm. Looking for storage pools in the " + "cluster to which this volumes can be migrated.");
        boolean storageMotionSupported = false;
        // Check if the underlying hypervisor supports storage motion.
        final Long hostId = vm.getHostId();
        if (hostId != null) {
            final HostVO host = _hostDao.findById(hostId);
            HypervisorCapabilitiesVO capabilities = null;
            if (host != null) {
                capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(host.getHypervisorType(), host.getHypervisorVersion());
            } else {
                s_logger.error("Details of the host on which the vm " + vm + ", to which volume " + volume + " is " + "attached, couldn't be retrieved.");
            }
            if (capabilities != null) {
                storageMotionSupported = capabilities.isStorageMotionSupported();
            } else {
                s_logger.error("Capabilities for host " + host + " couldn't be retrieved.");
            }
        }
        if (!storageMotionSupported) {
            s_logger.info("Volume " + volume + " is attached to a running vm and the hypervisor doesn't support" + " storage motion.");
            return new Pair<>(allPools, suitablePools);
        }
    }
    // Source pool of the volume.
    final StoragePoolVO srcVolumePool = _poolDao.findById(volume.getPoolId());
    // Get all the pools available. Only shared pools are considered because only a volume on a shared pools
    // can be live migrated while the virtual machine stays on the same host.
    final List<StoragePoolVO> storagePools;
    if (srcVolumePool.getClusterId() == null) {
        storagePools = _poolDao.findZoneWideStoragePoolsByTags(volume.getDataCenterId(), null);
    } else {
        storagePools = _poolDao.findPoolsByTags(volume.getDataCenterId(), srcVolumePool.getPodId(), srcVolumePool.getClusterId(), null);
    }
    storagePools.remove(srcVolumePool);
    for (final StoragePoolVO pool : storagePools) {
        if (pool.isShared()) {
            allPools.add((StoragePool) dataStoreMgr.getPrimaryDataStore(pool.getId()));
        }
    }
    // Get all the suitable pools.
    // Exclude the current pool from the list of pools to which the volume can be migrated.
    final ExcludeList avoid = new ExcludeList();
    avoid.addPool(srcVolumePool.getId());
    // Volume stays in the same cluster after migration.
    final DataCenterDeployment plan = new DataCenterDeployment(volume.getDataCenterId(), srcVolumePool.getPodId(), srcVolumePool.getClusterId(), null, null, null);
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
    final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, profile.getHypervisorType());
    // Call the storage pool allocator to find the list of storage pools.
    for (final StoragePoolAllocator allocator : _storagePoolAllocators) {
        final List<StoragePool> pools = allocator.allocateToPool(diskProfile, profile, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL);
        if (pools != null && !pools.isEmpty()) {
            suitablePools.addAll(pools);
            break;
        }
    }
    return new Pair<>(allPools, suitablePools);
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.legacymodel.user.Account) StoragePool(com.cloud.legacymodel.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) DiskProfile(com.cloud.legacymodel.storage.DiskProfile) HostVO(com.cloud.host.HostVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) StoragePoolAllocator(com.cloud.engine.subsystem.api.storage.StoragePoolAllocator) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair)

Example 22 with Pair

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

the class ManagementServerImpl method searchForConfigurations.

@Override
public Pair<List<? extends Configuration>, Integer> searchForConfigurations(final ListCfgsByCmd cmd) {
    final Filter searchFilter = new Filter(ConfigurationVO.class, "name", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchCriteria<ConfigurationVO> sc = _configDao.createSearchCriteria();
    final Long userId = CallContext.current().getCallingUserId();
    final Account caller = CallContext.current().getCallingAccount();
    final User user = _userDao.findById(userId);
    final Object name = cmd.getConfigName();
    final Object category = cmd.getCategory();
    final Object keyword = cmd.getKeyword();
    final Long zoneId = cmd.getZoneId();
    final Long clusterId = cmd.getClusterId();
    final Long storagepoolId = cmd.getStoragepoolId();
    final Long accountId = cmd.getAccountId();
    String scope = null;
    Long id = null;
    int paramCountCheck = 0;
    if (!_accountMgr.isRootAdmin(caller.getId()) && accountId == null) {
        throw new InvalidParameterValueException("Please specify AccountId to list the config for the given account.");
    }
    if (accountId != null) {
        final Account accountToUpdate = _accountDao.findById(accountId);
        _accountMgr.checkAccess(caller, null, true, accountToUpdate);
        scope = ConfigKey.Scope.Account.toString();
        id = accountId;
        paramCountCheck++;
    }
    if (_accountMgr.isRootAdmin(caller.getId())) {
        if (zoneId != null) {
            scope = ConfigKey.Scope.Zone.toString();
            id = zoneId;
            paramCountCheck++;
        }
        if (clusterId != null) {
            scope = ConfigKey.Scope.Cluster.toString();
            id = clusterId;
            paramCountCheck++;
        }
        if (storagepoolId != null) {
            scope = ConfigKey.Scope.StoragePool.toString();
            id = storagepoolId;
            paramCountCheck++;
        }
    }
    if (paramCountCheck > 1) {
        throw new InvalidParameterValueException("cannot handle multiple IDs, provide only one ID corresponding to the scope");
    }
    if (keyword != null) {
        final SearchCriteria<ConfigurationVO> ssc = _configDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("instance", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("component", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("category", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("value", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + name + "%");
    }
    if (category != null) {
        sc.addAnd("category", SearchCriteria.Op.EQ, category);
    }
    // hidden configurations are not displayed using the search API
    sc.addAnd("category", SearchCriteria.Op.NEQ, "Hidden");
    if (scope != null && !scope.isEmpty()) {
        // getting the list of parameters at requested scope
        sc.addAnd("scope", SearchCriteria.Op.EQ, scope);
    }
    final Pair<List<ConfigurationVO>, Integer> result = _configDao.searchAndCount(sc, searchFilter);
    if (scope != null && !scope.isEmpty()) {
        // Populate values corresponding the resource id
        final List<ConfigurationVO> configVOList = new ArrayList<>();
        for (final ConfigurationVO param : result.first()) {
            final ConfigurationVO configVo = _configDao.findByName(param.getName());
            if (configVo != null) {
                final ConfigKey<?> key = _configDepot.get(param.getName());
                if (key != null) {
                    configVo.setValue(key.valueIn(id).toString());
                    configVOList.add(configVo);
                } else {
                    s_logger.warn("ConfigDepot could not find parameter " + param.getName() + " for scope " + scope);
                }
            } else {
                s_logger.warn("Configuration item  " + param.getName() + " not found in " + scope);
            }
        }
        return new Pair<>(configVOList, configVOList.size());
    }
    return new Pair<>(result.first(), result.second());
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) ArrayList(java.util.ArrayList) ConfigurationVO(com.cloud.framework.config.impl.ConfigurationVO) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair)

Example 23 with Pair

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

the class ManagementServerImpl method searchForClusters.

@Override
public Pair<List<? extends Cluster>, Integer> searchForClusters(final ListClustersCmd cmd) {
    final Object id = cmd.getId();
    final Object name = cmd.getClusterName();
    final Object podId = cmd.getPodId();
    Long zoneId = cmd.getZoneId();
    final Object hypervisorType = cmd.getHypervisorType();
    final Object clusterType = cmd.getClusterType();
    final Object allocationState = cmd.getAllocationState();
    final String keyword = cmd.getKeyword();
    zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), zoneId);
    final Filter searchFilter = new Filter(ClusterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<ClusterVO> sb = _clusterDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
    sb.and("clusterType", sb.entity().getClusterType(), SearchCriteria.Op.EQ);
    sb.and("allocationState", sb.entity().getAllocationState(), SearchCriteria.Op.EQ);
    final SearchCriteria<ClusterVO> sc = sb.create();
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (hypervisorType != null) {
        sc.setParameters("hypervisorType", hypervisorType);
    }
    if (clusterType != null) {
        sc.setParameters("clusterType", clusterType);
    }
    if (allocationState != null) {
        sc.setParameters("allocationState", allocationState);
    }
    if (keyword != null) {
        final SearchCriteria<ClusterVO> ssc = _clusterDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("hypervisorType", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    final Pair<List<ClusterVO>, Integer> result = _clusterDao.searchAndCount(sc, searchFilter);
    return new Pair<>(result.first(), result.second());
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair)

Example 24 with Pair

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

the class ManagementServerImpl method searchForSystemVm.

@Override
public Pair<List<? extends VirtualMachine>, Integer> searchForSystemVm(final ListSystemVMsCmd cmd) {
    final String type = cmd.getSystemVmType();
    final Long zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), cmd.getZoneId());
    final Long id = cmd.getId();
    final String name = cmd.getSystemVmName();
    final String state = cmd.getState();
    final String keyword = cmd.getKeyword();
    final Long podId = cmd.getPodId();
    final Long hostId = cmd.getHostId();
    final Long storageId = cmd.getStorageId();
    final Filter searchFilter = new Filter(VMInstanceVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<VMInstanceVO> sb = _vmInstanceDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("hostName", sb.entity().getHostName(), SearchCriteria.Op.LIKE);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ);
    sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
    sb.and("nulltype", sb.entity().getType(), SearchCriteria.Op.IN);
    if (storageId != null) {
        final SearchBuilder<VolumeVO> volumeSearch = _volumeDao.createSearchBuilder();
        volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
        sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
    }
    final SearchCriteria<VMInstanceVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<VMInstanceVO> ssc = _vmInstanceDao.createSearchCriteria();
        ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("hostName", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (name != null) {
        sc.setParameters("hostName", name);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (podId != null) {
        sc.setParameters("podId", podId);
    }
    if (hostId != null) {
        sc.setParameters("hostId", hostId);
    }
    if (type != null) {
        sc.setParameters("type", type);
    } else {
        sc.setParameters("nulltype", VirtualMachineType.SecondaryStorageVm, VirtualMachineType.ConsoleProxy);
    }
    if (storageId != null) {
        sc.setJoinParameters("volumeSearch", "poolId", storageId);
    }
    final Pair<List<VMInstanceVO>, Integer> result = _vmInstanceDao.searchAndCount(sc, searchFilter);
    return new Pair<>(result.first(), result.second());
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) Filter(com.cloud.utils.db.Filter) VMInstanceVO(com.cloud.vm.VMInstanceVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) SSHKeyPair(com.cloud.legacymodel.user.SSHKeyPair) Pair(com.cloud.legacymodel.utils.Pair)

Example 25 with Pair

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

the class SHA256SaltedUserAuthenticator method authenticate.

/* (non-Javadoc)
     * @see com.cloud.server.auth.UserAuthenticator#authenticate(java.lang.String, java.lang.String, java.lang.Long, java.util.Map)
     */
@Override
public Pair<Boolean, ActionOnFailedAuthentication> authenticate(final String username, final String password, final Long domainId, final Map<String, Object[]> requestParameters) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Retrieving user: " + username);
    }
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        s_logger.debug("Username or Password cannot be empty");
        return new Pair<>(false, null);
    }
    boolean realUser = true;
    final UserAccount user = _userAccountDao.getUserAccount(username, domainId);
    if (user == null) {
        s_logger.debug("Unable to find user with " + username + " in domain " + domainId);
        realUser = false;
    }
    /* Fake Data */
    String realPassword = new String(s_defaultPassword);
    byte[] salt = new String(s_defaultSalt).getBytes();
    if (realUser) {
        final String[] storedPassword = user.getPassword().split(":");
        if (storedPassword.length != 2) {
            s_logger.warn("The stored password for " + username + " isn't in the right format for this authenticator");
            realUser = false;
        } else {
            realPassword = storedPassword[1];
            salt = Base64.decode(storedPassword[0]);
        }
    }
    try {
        final String hashedPassword = encode(password, salt);
        /* constantTimeEquals comes first in boolean since we need to thwart timing attacks */
        final boolean result = constantTimeEquals(realPassword, hashedPassword) && realUser;
        ActionOnFailedAuthentication action = null;
        if (!result && realUser) {
            action = ActionOnFailedAuthentication.INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT;
        }
        return new Pair<>(result, action);
    } catch (final NoSuchAlgorithmException e) {
        throw new CloudRuntimeException("Unable to hash password", e);
    } catch (final UnsupportedEncodingException e) {
        throw new CloudRuntimeException("Unable to hash password", e);
    }
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UserAccount(com.cloud.legacymodel.user.UserAccount) 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