Search in sources :

Example 6 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class LunDaoImpl method findByName.

@Override
public LunVO findByName(String name) {
    SearchCriteria sc = LunNameSearch.create();
    sc.setParameters("name", name);
    return findOneBy(sc);
}
Also used : SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 7 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class PoolDaoImpl method findPool.

@Override
public PoolVO findPool(String poolName) {
    SearchCriteria sc = PoolSearch.create();
    sc.setParameters("name", poolName);
    List<PoolVO> poolList = listBy(sc);
    return (poolList.size() > 0 ? poolList.get(0) : null);
}
Also used : PoolVO(com.cloud.netapp.PoolVO) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 8 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class IpAddressManagerImpl method fetchNewPublicIp.

@DB
public PublicIp fetchNewPublicIp(final long dcId, final Long podId, final List<Long> vlanDbIds, final Account owner, final VlanType vlanUse, final Long guestNetworkId, final boolean sourceNat, final boolean assign, final String requestedIp, final boolean isSystem, final Long vpcId, final Boolean displayIp) throws InsufficientAddressCapacityException {
    IPAddressVO addr = Transaction.execute(new TransactionCallbackWithException<IPAddressVO, InsufficientAddressCapacityException>() {

        @Override
        public IPAddressVO doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException {
            StringBuilder errorMessage = new StringBuilder("Unable to get ip adress in ");
            boolean fetchFromDedicatedRange = false;
            List<Long> dedicatedVlanDbIds = new ArrayList<Long>();
            List<Long> nonDedicatedVlanDbIds = new ArrayList<Long>();
            DataCenter zone = _entityMgr.findById(DataCenter.class, dcId);
            SearchCriteria<IPAddressVO> sc = null;
            if (podId != null) {
                sc = AssignIpAddressFromPodVlanSearch.create();
                sc.setJoinParameters("podVlanMapSB", "podId", podId);
                errorMessage.append(" pod id=" + podId);
            } else {
                sc = AssignIpAddressSearch.create();
                errorMessage.append(" zone id=" + dcId);
            }
            // If owner has dedicated Public IP ranges, fetch IP from the dedicated range
            // Otherwise fetch IP from the system pool
            Network network = _networksDao.findById(guestNetworkId);
            //Checking if network is null in the case of system VM's. At the time of allocation of IP address to systemVm, no network is present.
            if (network == null || !(network.getGuestType() == GuestType.Shared && zone.getNetworkType() == NetworkType.Advanced)) {
                List<AccountVlanMapVO> maps = _accountVlanMapDao.listAccountVlanMapsByAccount(owner.getId());
                for (AccountVlanMapVO map : maps) {
                    if (vlanDbIds == null || vlanDbIds.contains(map.getVlanDbId()))
                        dedicatedVlanDbIds.add(map.getVlanDbId());
                }
            }
            List<DomainVlanMapVO> domainMaps = _domainVlanMapDao.listDomainVlanMapsByDomain(owner.getDomainId());
            for (DomainVlanMapVO map : domainMaps) {
                if (vlanDbIds == null || vlanDbIds.contains(map.getVlanDbId()))
                    dedicatedVlanDbIds.add(map.getVlanDbId());
            }
            List<VlanVO> nonDedicatedVlans = _vlanDao.listZoneWideNonDedicatedVlans(dcId);
            for (VlanVO nonDedicatedVlan : nonDedicatedVlans) {
                if (vlanDbIds == null || vlanDbIds.contains(nonDedicatedVlan.getId()))
                    nonDedicatedVlanDbIds.add(nonDedicatedVlan.getId());
            }
            if (dedicatedVlanDbIds != null && !dedicatedVlanDbIds.isEmpty()) {
                fetchFromDedicatedRange = true;
                sc.setParameters("vlanId", dedicatedVlanDbIds.toArray());
                errorMessage.append(", vlanId id=" + Arrays.toString(dedicatedVlanDbIds.toArray()));
            } else if (nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds.isEmpty()) {
                sc.setParameters("vlanId", nonDedicatedVlanDbIds.toArray());
                errorMessage.append(", vlanId id=" + Arrays.toString(nonDedicatedVlanDbIds.toArray()));
            } else {
                if (podId != null) {
                    InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", Pod.class, podId);
                    ex.addProxyObject(ApiDBUtils.findPodById(podId).getUuid());
                    throw ex;
                }
                s_logger.warn(errorMessage.toString());
                InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId);
                ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid());
                throw ex;
            }
            sc.setParameters("dc", dcId);
            // for direct network take ip addresses only from the vlans belonging to the network
            if (vlanUse == VlanType.DirectAttached) {
                sc.setJoinParameters("vlan", "networkId", guestNetworkId);
                errorMessage.append(", network id=" + guestNetworkId);
            }
            sc.setJoinParameters("vlan", "type", vlanUse);
            if (requestedIp != null) {
                sc.addAnd("address", SearchCriteria.Op.EQ, requestedIp);
                errorMessage.append(": requested ip " + requestedIp + " is not available");
            }
            Filter filter = new Filter(IPAddressVO.class, "vlanId", true, 0l, 1l);
            List<IPAddressVO> addrs = _ipAddressDao.lockRows(sc, filter, true);
            // If all the dedicated IPs of the owner are in use fetch an IP from the system pool
            if (addrs.size() == 0 && fetchFromDedicatedRange) {
                // Verify if account is allowed to acquire IPs from the system
                boolean useSystemIps = UseSystemPublicIps.valueIn(owner.getId());
                if (useSystemIps && nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds.isEmpty()) {
                    fetchFromDedicatedRange = false;
                    sc.setParameters("vlanId", nonDedicatedVlanDbIds.toArray());
                    errorMessage.append(", vlanId id=" + Arrays.toString(nonDedicatedVlanDbIds.toArray()));
                    addrs = _ipAddressDao.lockRows(sc, filter, true);
                }
            }
            if (addrs.size() == 0) {
                if (podId != null) {
                    InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", Pod.class, podId);
                    // for now, we hardcode the table names, but we should ideally do a lookup for the tablename from the VO object.
                    ex.addProxyObject(ApiDBUtils.findPodById(podId).getUuid());
                    throw ex;
                }
                s_logger.warn(errorMessage.toString());
                InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId);
                ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid());
                throw ex;
            }
            assert (addrs.size() == 1) : "Return size is incorrect: " + addrs.size();
            if (!fetchFromDedicatedRange && VlanType.VirtualNetwork.equals(vlanUse)) {
                // Check that the maximum number of public IPs for the given accountId will not be exceeded
                try {
                    _resourceLimitMgr.checkResourceLimit(owner, ResourceType.public_ip);
                } catch (ResourceAllocationException ex) {
                    s_logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + owner);
                    throw new AccountLimitException("Maximum number of public IP addresses for account: " + owner.getAccountName() + " has been exceeded.");
                }
            }
            IPAddressVO addr = addrs.get(0);
            addr.setSourceNat(sourceNat);
            addr.setAllocatedTime(new Date());
            addr.setAllocatedInDomainId(owner.getDomainId());
            addr.setAllocatedToAccountId(owner.getId());
            addr.setSystem(isSystem);
            if (displayIp != null) {
                addr.setDisplay(displayIp);
            }
            if (assign) {
                markPublicIpAsAllocated(addr);
            } else {
                addr.setState(IpAddress.State.Allocating);
            }
            addr.setState(assign ? IpAddress.State.Allocated : IpAddress.State.Allocating);
            if (vlanUse != VlanType.DirectAttached) {
                addr.setAssociatedWithNetworkId(guestNetworkId);
                addr.setVpcId(vpcId);
            }
            _ipAddressDao.update(addr.getId(), addr);
            return addr;
        }
    });
    if (vlanUse == VlanType.VirtualNetwork) {
        _firewallMgr.addSystemFirewallRules(addr, owner);
    }
    return PublicIp.createFromAddrAndVlan(addr, _vlanDao.findById(addr.getVlanId()));
}
Also used : Pod(com.cloud.dc.Pod) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) AccountVlanMapVO(com.cloud.dc.AccountVlanMapVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) SearchCriteria(com.cloud.utils.db.SearchCriteria) DomainVlanMapVO(com.cloud.dc.DomainVlanMapVO) Date(java.util.Date) DataCenter(com.cloud.dc.DataCenter) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO) ArrayList(java.util.ArrayList) List(java.util.List) VlanVO(com.cloud.dc.VlanVO) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) AccountLimitException(com.cloud.exception.AccountLimitException) DB(com.cloud.utils.db.DB)

Example 9 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class AutoScaleManagerImpl method checkValidityAndPersist.

@DB
protected AutoScalePolicyVO checkValidityAndPersist(final AutoScalePolicyVO autoScalePolicyVOFinal, final List<Long> conditionIds) {
    final int duration = autoScalePolicyVOFinal.getDuration();
    final int quietTime = autoScalePolicyVOFinal.getQuietTime();
    if (duration < 0) {
        throw new InvalidParameterValueException("duration is an invalid value: " + duration);
    }
    if (quietTime < 0) {
        throw new InvalidParameterValueException("quiettime is an invalid value: " + quietTime);
    }
    return Transaction.execute(new TransactionCallback<AutoScalePolicyVO>() {

        @Override
        public AutoScalePolicyVO doInTransaction(TransactionStatus status) {
            AutoScalePolicyVO autoScalePolicyVO = _autoScalePolicyDao.persist(autoScalePolicyVOFinal);
            if (conditionIds != null) {
                SearchBuilder<ConditionVO> conditionsSearch = _conditionDao.createSearchBuilder();
                conditionsSearch.and("ids", conditionsSearch.entity().getId(), Op.IN);
                conditionsSearch.done();
                SearchCriteria<ConditionVO> sc = conditionsSearch.create();
                sc.setParameters("ids", conditionIds.toArray(new Object[0]));
                List<ConditionVO> conditions = _conditionDao.search(sc, null);
                ControlledEntity[] sameOwnerEntities = conditions.toArray(new ControlledEntity[conditions.size() + 1]);
                sameOwnerEntities[sameOwnerEntities.length - 1] = autoScalePolicyVO;
                _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities);
                if (conditionIds.size() != conditions.size()) {
                    // TODO report the condition id which could not be found
                    throw new InvalidParameterValueException("Unable to find the condition specified");
                }
                ArrayList<Long> counterIds = new ArrayList<Long>();
                for (ConditionVO condition : conditions) {
                    if (counterIds.contains(condition.getCounterid())) {
                        throw new InvalidParameterValueException("atleast two conditions in the conditionids have the same counter. It is not right to apply two different conditions for the same counter");
                    }
                    counterIds.add(condition.getCounterid());
                }
                /* For update case remove the existing mappings and create fresh ones */
                _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(autoScalePolicyVO.getId());
                for (Long conditionId : conditionIds) {
                    AutoScalePolicyConditionMapVO policyConditionMapVO = new AutoScalePolicyConditionMapVO(autoScalePolicyVO.getId(), conditionId);
                    _autoScalePolicyConditionMapDao.persist(policyConditionMapVO);
                }
            }
            return autoScalePolicyVO;
        }
    });
}
Also used : SearchBuilder(com.cloud.utils.db.SearchBuilder) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) SearchCriteria(com.cloud.utils.db.SearchCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ControlledEntity(org.apache.cloudstack.acl.ControlledEntity) ArrayList(java.util.ArrayList) List(java.util.List) DB(com.cloud.utils.db.DB)

Example 10 with SearchCriteria

use of com.cloud.utils.db.SearchCriteria in project cloudstack by apache.

the class VMSnapshotDaoImpl method listByAccountId.

public List<VMSnapshotVO> listByAccountId(Long accountId) {
    SearchCriteria sc = this.AllFieldsSearch.create();
    sc.setParameters("accountId", new Object[] { accountId });
    return listBy(sc, null);
}
Also used : SearchCriteria(com.cloud.utils.db.SearchCriteria)

Aggregations

SearchCriteria (com.cloud.utils.db.SearchCriteria)33 List (java.util.List)25 Account (com.cloud.user.Account)17 ArrayList (java.util.ArrayList)16 TransactionStatus (com.cloud.utils.db.TransactionStatus)8 DB (com.cloud.utils.db.DB)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Filter (com.cloud.utils.db.Filter)5 DomainVO (com.cloud.domain.DomainVO)4 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)3 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 Map (java.util.Map)3 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)2 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)2 DetailVO (com.cloud.host.DetailVO)2 HostVO (com.cloud.host.HostVO)2 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)2