Search in sources :

Example 16 with SearchCriteria

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

the class UsageManagerImpl method createLoadBalancerHelperEvent.

private void createLoadBalancerHelperEvent(UsageEventVO event) {
    long zoneId = -1L;
    long id = event.getResourceId();
    if (EventTypes.EVENT_LOAD_BALANCER_CREATE.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Creating load balancer : " + id + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = _accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageLoadBalancerPolicyVO lbVO = new UsageLoadBalancerPolicyVO(id, zoneId, event.getAccountId(), acct.getDomainId(), event.getCreateDate(), null);
        _usageLoadBalancerPolicyDao.persist(lbVO);
    } else if (EventTypes.EVENT_LOAD_BALANCER_DELETE.equals(event.getType())) {
        SearchCriteria<UsageLoadBalancerPolicyVO> sc = _usageLoadBalancerPolicyDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageLoadBalancerPolicyVO> lbVOs = _usageLoadBalancerPolicyDao.search(sc, null);
        if (lbVOs.size() > 1) {
            s_logger.warn("More that one usage entry for load balancer policy: " + id + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageLoadBalancerPolicyVO lbVO : lbVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting load balancer policy: " + lbVO.getId() + " from account: " + lbVO.getAccountId());
            }
            // there really shouldn't be more than one
            lbVO.setDeleted(event.getCreateDate());
            _usageLoadBalancerPolicyDao.update(lbVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) ArrayList(java.util.ArrayList) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 17 with SearchCriteria

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

the class UsageManagerImpl method createSecurityGroupEvent.

private void createSecurityGroupEvent(UsageEventVO event) {
    long zoneId = -1L;
    long vmId = event.getResourceId();
    long sgId = event.getOfferingId();
    if (EventTypes.EVENT_SECURITY_GROUP_ASSIGN.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Assigning : security group" + sgId + " to Vm: " + vmId + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = _accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsageSecurityGroupVO securityGroup = new UsageSecurityGroupVO(zoneId, event.getAccountId(), acct.getDomainId(), vmId, sgId, event.getCreateDate(), null);
        _usageSecurityGroupDao.persist(securityGroup);
    } else if (EventTypes.EVENT_SECURITY_GROUP_REMOVE.equals(event.getType())) {
        SearchCriteria<UsageSecurityGroupVO> sc = _usageSecurityGroupDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("vmInstanceId", SearchCriteria.Op.EQ, vmId);
        sc.addAnd("securityGroupId", SearchCriteria.Op.EQ, sgId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageSecurityGroupVO> sgVOs = _usageSecurityGroupDao.search(sc, null);
        if (sgVOs.size() > 1) {
            s_logger.warn("More that one usage entry for security group: " + sgId + " for Vm: " + vmId + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageSecurityGroupVO sgVO : sgVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting security group: " + sgVO.getSecurityGroupId() + " from Vm: " + sgVO.getVmInstanceId());
            }
            // there really shouldn't be more than one
            sgVO.setDeleted(event.getCreateDate());
            _usageSecurityGroupDao.update(sgVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) ArrayList(java.util.ArrayList) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 18 with SearchCriteria

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

the class UsageManagerImpl method createPortForwardingHelperEvent.

private void createPortForwardingHelperEvent(UsageEventVO event) {
    long zoneId = -1L;
    long id = event.getResourceId();
    if (EventTypes.EVENT_NET_RULE_ADD.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Creating port forwarding rule : " + id + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = _accountDao.findByIdIncludingRemoved(event.getAccountId());
        UsagePortForwardingRuleVO pfVO = new UsagePortForwardingRuleVO(id, zoneId, event.getAccountId(), acct.getDomainId(), event.getCreateDate(), null);
        _usagePortForwardingRuleDao.persist(pfVO);
    } else if (EventTypes.EVENT_NET_RULE_DELETE.equals(event.getType())) {
        SearchCriteria<UsagePortForwardingRuleVO> sc = _usagePortForwardingRuleDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsagePortForwardingRuleVO> pfVOs = _usagePortForwardingRuleDao.search(sc, null);
        if (pfVOs.size() > 1) {
            s_logger.warn("More that one usage entry for port forwarding rule: " + id + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsagePortForwardingRuleVO pfVO : pfVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting port forwarding rule: " + pfVO.getId() + " from account: " + pfVO.getAccountId());
            }
            // there really shouldn't be more than one
            pfVO.setDeleted(event.getCreateDate());
            _usagePortForwardingRuleDao.update(pfVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) ArrayList(java.util.ArrayList) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 19 with SearchCriteria

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

the class UsageManagerImpl method createNetworkOfferingEvent.

private void createNetworkOfferingEvent(UsageEventVO event) {
    long zoneId = -1L;
    long vmId = event.getResourceId();
    long networkOfferingId = event.getOfferingId();
    long nicId = 0;
    try {
        nicId = Long.parseLong(event.getResourceName());
    } catch (Exception e) {
        s_logger.warn("failed to get nic id from resource name, resource name is: " + event.getResourceName());
    }
    if (EventTypes.EVENT_NETWORK_OFFERING_CREATE.equals(event.getType()) || EventTypes.EVENT_NETWORK_OFFERING_ASSIGN.equals(event.getType())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Creating networking offering: " + networkOfferingId + " for Vm: " + vmId + " for account: " + event.getAccountId());
        }
        zoneId = event.getZoneId();
        Account acct = _accountDao.findByIdIncludingRemoved(event.getAccountId());
        boolean isDefault = (event.getSize() == 1) ? true : false;
        UsageNetworkOfferingVO networkOffering = new UsageNetworkOfferingVO(zoneId, event.getAccountId(), acct.getDomainId(), vmId, networkOfferingId, nicId, isDefault, event.getCreateDate(), null);
        _usageNetworkOfferingDao.persist(networkOffering);
    } else if (EventTypes.EVENT_NETWORK_OFFERING_DELETE.equals(event.getType()) || EventTypes.EVENT_NETWORK_OFFERING_REMOVE.equals(event.getType())) {
        SearchCriteria<UsageNetworkOfferingVO> sc = _usageNetworkOfferingDao.createSearchCriteria();
        sc.addAnd("accountId", SearchCriteria.Op.EQ, event.getAccountId());
        sc.addAnd("vmInstanceId", SearchCriteria.Op.EQ, vmId);
        sc.addAnd("nicId", SearchCriteria.Op.EQ, nicId);
        sc.addAnd("networkOfferingId", SearchCriteria.Op.EQ, networkOfferingId);
        sc.addAnd("deleted", SearchCriteria.Op.NULL);
        List<UsageNetworkOfferingVO> noVOs = _usageNetworkOfferingDao.search(sc, null);
        if (noVOs.size() > 1) {
            s_logger.warn("More that one usage entry for networking offering: " + networkOfferingId + " for Vm: " + vmId + " assigned to account: " + event.getAccountId() + "; marking them all as deleted...");
        }
        for (UsageNetworkOfferingVO noVO : noVOs) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("deleting network offering: " + noVO.getNetworkOfferingId() + " from Vm: " + noVO.getVmInstanceId());
            }
            // there really shouldn't be more than one
            noVO.setDeleted(event.getCreateDate());
            _usageNetworkOfferingDao.update(noVO);
        }
    }
}
Also used : Account(com.cloud.user.Account) List(java.util.List) ArrayList(java.util.ArrayList) ConfigurationException(javax.naming.ConfigurationException) SQLException(java.sql.SQLException) SearchCriteria(com.cloud.utils.db.SearchCriteria)

Example 20 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)

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