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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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;
}
});
}
Aggregations