Search in sources :

Example 1 with LBStickinessPolicyVO

use of com.cloud.network.dao.LBStickinessPolicyVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method updateLBStickinessPolicy.

@Override
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_UPDATE, eventDescription = "updating lb stickiness policy", async = true)
public StickinessPolicy updateLBStickinessPolicy(long id, String customId, Boolean forDisplay) {
    LBStickinessPolicyVO policy = _lb2stickinesspoliciesDao.findById(id);
    if (policy == null) {
        throw new InvalidParameterValueException("Fail to find stickiness policy with " + id);
    }
    LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(policy.getLoadBalancerId()));
    if (loadBalancer == null) {
        throw new InvalidParameterException("Invalid Load balancer : " + policy.getLoadBalancerId() + " for Stickiness policy id: " + id);
    }
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, loadBalancer);
    if (customId != null) {
        policy.setUuid(customId);
    }
    if (forDisplay != null) {
        policy.setDisplay(forDisplay);
    }
    _lb2stickinesspoliciesDao.update(id, policy);
    return _lb2stickinesspoliciesDao.findById(id);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO) ActionEvent(com.cloud.event.ActionEvent)

Example 2 with LBStickinessPolicyVO

use of com.cloud.network.dao.LBStickinessPolicyVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method genericValidator.

private boolean genericValidator(CreateLBStickinessPolicyCmd cmd) throws InvalidParameterValueException {
    LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId());
    /* Validation : check for valid Method name and params */
    List<LbStickinessMethod> stickinessMethodList = getStickinessMethods(loadBalancer.getNetworkId());
    boolean methodMatch = false;
    if (stickinessMethodList == null) {
        throw new InvalidParameterValueException("Failed:  No Stickiness method available for LB rule:" + cmd.getLbRuleId());
    }
    for (LbStickinessMethod method : stickinessMethodList) {
        if (method.getMethodName().equalsIgnoreCase(cmd.getStickinessMethodName())) {
            methodMatch = true;
            Map apiParamList = cmd.getparamList();
            List<LbStickinessMethodParam> methodParamList = method.getParamList();
            Map<String, String> tempParamList = new HashMap<String, String>();
            /*
                 * validation-1: check for any extra params that are not
                 * required by the policymethod(capability), FIXME: make the
                 * below loop simple without using raw data type
                 */
            if (apiParamList != null) {
                Collection userGroupCollection = apiParamList.values();
                Iterator iter = userGroupCollection.iterator();
                while (iter.hasNext()) {
                    HashMap<String, String> paramKVpair = (HashMap) iter.next();
                    String paramName = paramKVpair.get("name");
                    String paramValue = paramKVpair.get("value");
                    tempParamList.put(paramName, paramValue);
                    Boolean found = false;
                    for (LbStickinessMethodParam param : methodParamList) {
                        if (param.getParamName().equalsIgnoreCase(paramName)) {
                            if ((param.getIsflag() == false) && (paramValue == null)) {
                                throw new InvalidParameterValueException("Failed : Value expected for the Param :" + param.getParamName());
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        throw new InvalidParameterValueException("Failed : Stickiness policy does not support param name :" + paramName);
                    }
                }
            }
            /* validation-2: check for mandatory params */
            for (LbStickinessMethodParam param : methodParamList) {
                if (param.getRequired()) {
                    if (tempParamList.get(param.getParamName()) == null) {
                        throw new InvalidParameterValueException("Failed : Missing Manadatory Param :" + param.getParamName());
                    }
                }
            }
            /* Successfully completed the Validation */
            break;
        }
    }
    if (methodMatch == false) {
        throw new InvalidParameterValueException("Failed to match Stickiness method name for LB rule:" + cmd.getLbRuleId());
    }
    /* Validation : check for the multiple policies to the rule id */
    List<LBStickinessPolicyVO> stickinessPolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId(), false);
    if (stickinessPolicies.size() > 1) {
        throw new InvalidParameterValueException("Failed to create Stickiness policy: Already two policies attached " + cmd.getLbRuleId());
    }
    return true;
}
Also used : HashMap(java.util.HashMap) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO) LbStickinessMethod(com.cloud.network.rules.LbStickinessMethod) LbStickinessMethodParam(com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Iterator(java.util.Iterator) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with LBStickinessPolicyVO

use of com.cloud.network.dao.LBStickinessPolicyVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method deleteLBStickinessPolicy.

@Override
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_DELETE, eventDescription = "revoking LB Stickiness policy ", async = true)
public boolean deleteLBStickinessPolicy(long stickinessPolicyId, boolean apply) {
    boolean success = true;
    CallContext caller = CallContext.current();
    LBStickinessPolicyVO stickinessPolicy = _lb2stickinesspoliciesDao.findById(stickinessPolicyId);
    if (stickinessPolicy == null) {
        throw new InvalidParameterException("Invalid Stickiness policy id value: " + stickinessPolicyId);
    }
    LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(stickinessPolicy.getLoadBalancerId()));
    if (loadBalancer == null) {
        throw new InvalidParameterException("Invalid Load balancer : " + stickinessPolicy.getLoadBalancerId() + " for Stickiness policy id: " + stickinessPolicyId);
    }
    long loadBalancerId = loadBalancer.getId();
    FirewallRule.State backupState = loadBalancer.getState();
    _accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer);
    if (apply) {
        if (loadBalancer.getState() == FirewallRule.State.Active) {
            loadBalancer.setState(FirewallRule.State.Add);
            _lbDao.persist(loadBalancer);
        }
        boolean backupStickyState = stickinessPolicy.isRevoke();
        stickinessPolicy.setRevoke(true);
        _lb2stickinesspoliciesDao.persist(stickinessPolicy);
        s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", stickinesspolicyID " + stickinessPolicyId);
        try {
            if (!applyLoadBalancerConfig(loadBalancerId)) {
                s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId);
                throw new CloudRuntimeException("Failed to remove load balancer rule id " + loadBalancerId + " for stickinesspolicyID " + stickinessPolicyId);
            }
        } catch (ResourceUnavailableException e) {
            if (isRollBackAllowedForProvider(loadBalancer)) {
                stickinessPolicy.setRevoke(backupStickyState);
                _lb2stickinesspoliciesDao.persist(stickinessPolicy);
                loadBalancer.setState(backupState);
                _lbDao.persist(loadBalancer);
                s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + "  while deleting sticky policy: " + stickinessPolicyId);
            }
            s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
            success = false;
        }
    } else {
        _lb2stickinesspoliciesDao.expunge(stickinessPolicyId);
    }
    return success;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO) CallContext(org.apache.cloudstack.context.CallContext) FirewallRule(com.cloud.network.rules.FirewallRule) ActionEvent(com.cloud.event.ActionEvent)

Example 4 with LBStickinessPolicyVO

use of com.cloud.network.dao.LBStickinessPolicyVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method getStickinessPolicies.

@Override
public List<LbStickinessPolicy> getStickinessPolicies(long lbId) {
    List<LbStickinessPolicy> stickinessPolicies = new ArrayList<LbStickinessPolicy>();
    List<LBStickinessPolicyVO> sDbpolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(lbId, false);
    for (LBStickinessPolicyVO sDbPolicy : sDbpolicies) {
        LbStickinessPolicy sPolicy = new LbStickinessPolicy(sDbPolicy.getMethodName(), sDbPolicy.getParams(), sDbPolicy.isRevoke());
        stickinessPolicies.add(sPolicy);
    }
    return stickinessPolicies;
}
Also used : ArrayList(java.util.ArrayList) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO)

Example 5 with LBStickinessPolicyVO

use of com.cloud.network.dao.LBStickinessPolicyVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method createLBStickinessPolicy.

@SuppressWarnings("rawtypes")
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "create lb stickinesspolicy to load balancer", create = true)
public StickinessPolicy createLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) throws NetworkRuleConflictException {
    CallContext caller = CallContext.current();
    /* Validation : check corresponding load balancer rule exist */
    LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId());
    if (loadBalancer == null) {
        throw new InvalidParameterValueException("Failed: LB rule id: " + cmd.getLbRuleId() + " not present ");
    }
    _accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer);
    if (loadBalancer.getState() == FirewallRule.State.Revoke) {
        throw new InvalidParameterValueException("Failed:  LB rule id: " + cmd.getLbRuleId() + " is in deleting state: ");
    }
    /* Generic validations */
    if (!genericValidator(cmd)) {
        throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation Failed " + cmd.getLbRuleId());
    }
    /*
         * Specific validations using network element validator for specific
         * validations
         */
    LBStickinessPolicyVO lbpolicy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription());
    List<LbStickinessPolicy> policyList = new ArrayList<LbStickinessPolicy>();
    policyList.add(new LbStickinessPolicy(cmd.getStickinessMethodName(), lbpolicy.getParams()));
    Ip sourceIp = getSourceIp(loadBalancer);
    LoadBalancingRule lbRule = new LoadBalancingRule(loadBalancer, getExistingDestinations(lbpolicy.getId()), policyList, null, sourceIp, null, loadBalancer.getLbProtocol());
    if (!validateLbRule(lbRule)) {
        throw new InvalidParameterValueException("Failed to create Stickiness policy: Validation Failed " + cmd.getLbRuleId());
    }
    /* Finally Insert into DB */
    LBStickinessPolicyVO policy = new LBStickinessPolicyVO(loadBalancer.getId(), cmd.getLBStickinessPolicyName(), cmd.getStickinessMethodName(), cmd.getparamList(), cmd.getDescription());
    Boolean forDisplay = cmd.getDisplay();
    if (forDisplay != null) {
        policy.setDisplay(forDisplay);
    }
    policy = _lb2stickinesspoliciesDao.persist(policy);
    return policy;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) CallContext(org.apache.cloudstack.context.CallContext) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

LBStickinessPolicyVO (com.cloud.network.dao.LBStickinessPolicyVO)7 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)6 ActionEvent (com.cloud.event.ActionEvent)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 InvalidParameterException (java.security.InvalidParameterException)3 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)2 LbStickinessPolicy (com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy)2 FirewallRule (com.cloud.network.rules.FirewallRule)2 DB (com.cloud.utils.db.DB)2 ArrayList (java.util.ArrayList)2 CallContext (org.apache.cloudstack.context.CallContext)2 PublicIp (com.cloud.network.addr.PublicIp)1 LbStickinessMethod (com.cloud.network.rules.LbStickinessMethod)1 LbStickinessMethodParam (com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam)1 Account (com.cloud.user.Account)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Ip (com.cloud.utils.net.Ip)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1