Search in sources :

Example 11 with LbStickinessPolicy

use of com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy in project cloudstack by apache.

the class ElasticLoadBalancerManagerImpl method applyLoadBalancerRules.

@Override
public boolean applyLoadBalancerRules(Network network, List<LoadBalancingRule> rules) throws ResourceUnavailableException {
    if (rules == null || rules.isEmpty()) {
        return true;
    }
    DomainRouterVO elbVm = findElbVmForLb(rules.get(0));
    if (elbVm == null) {
        s_logger.warn("Unable to apply lb rules, ELB vm  doesn't exist in the network " + network.getId());
        throw new ResourceUnavailableException("Unable to apply lb rules", DataCenter.class, network.getDataCenterId());
    }
    if (elbVm.getState() == State.Running) {
        //resend all rules for the public ip
        long sourceIpId = _networkModel.getPublicIpAddress(rules.get(0).getSourceIp().addr(), network.getDataCenterId()).getId();
        List<LoadBalancerVO> lbs = _lbDao.listByIpAddress(sourceIpId);
        List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>();
        for (LoadBalancerVO lb : lbs) {
            List<LbDestination> dstList = _lbMgr.getExistingDestinations(lb.getId());
            List<LbStickinessPolicy> policyList = _lbMgr.getStickinessPolicies(lb.getId());
            List<LbHealthCheckPolicy> hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId());
            Ip sourceIp = _networkModel.getPublicIpAddress(lb.getSourceIpAddressId()).getAddress();
            LbSslCert sslCert = _lbMgr.getLbSslCert(lb.getId());
            LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList, sourceIp, sslCert, lb.getLbProtocol());
            lbRules.add(loadBalancing);
        }
        return applyLBRules(elbVm, lbRules, network.getId());
    } else if (elbVm.getState() == State.Stopped || elbVm.getState() == State.Stopping) {
        s_logger.debug("ELB VM is in " + elbVm.getState() + ", so not sending apply LoadBalancing rules commands to the backend");
        return true;
    } else {
        s_logger.warn("Unable to apply loadbalancing rules, ELB VM is not in the right state " + elbVm.getState());
        throw new ResourceUnavailableException("Unable to apply loadbalancing rules, ELB VM is not in the right state", VirtualRouter.class, elbVm.getId());
    }
}
Also used : LbSslCert(com.cloud.network.lb.LoadBalancingRule.LbSslCert) Ip(com.cloud.utils.net.Ip) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) VirtualRouter(com.cloud.network.router.VirtualRouter) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 12 with LbStickinessPolicy

use of com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy in project cloudstack by apache.

the class ElasticLoadBalancerManagerImpl method finalizeCommandsOnStart.

@Override
public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile profile) {
    DomainRouterVO elbVm = _routerDao.findById(profile.getVirtualMachine().getId());
    DataCenterVO dcVo = _dcDao.findById(elbVm.getDataCenterId());
    NicProfile controlNic = null;
    Long guestNetworkId = null;
    if (profile.getHypervisorType() == HypervisorType.VMware && dcVo.getNetworkType() == NetworkType.Basic) {
        // for basic network mode, we will use the guest NIC for control NIC
        for (NicProfile nic : profile.getNics()) {
            if (nic.getTrafficType() == TrafficType.Guest && nic.getIPv4Address() != null) {
                controlNic = nic;
                guestNetworkId = nic.getNetworkId();
            }
        }
    } else {
        for (NicProfile nic : profile.getNics()) {
            if (nic.getTrafficType() == TrafficType.Control && nic.getIPv4Address() != null) {
                controlNic = nic;
            } else if (nic.getTrafficType() == TrafficType.Guest) {
                guestNetworkId = nic.getNetworkId();
            }
        }
    }
    if (controlNic == null) {
        s_logger.error("Control network doesn't exist for the ELB vm " + elbVm);
        return false;
    }
    cmds.addCommand("checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIPv4Address(), 3922));
    // Re-apply load balancing rules
    List<LoadBalancerVO> lbs = _elbVmMapDao.listLbsForElbVm(elbVm.getId());
    List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>();
    for (LoadBalancerVO lb : lbs) {
        List<LbDestination> dstList = _lbMgr.getExistingDestinations(lb.getId());
        List<LbStickinessPolicy> policyList = _lbMgr.getStickinessPolicies(lb.getId());
        List<LbHealthCheckPolicy> hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId());
        Ip sourceIp = _networkModel.getPublicIpAddress(lb.getSourceIpAddressId()).getAddress();
        LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList, sourceIp);
        lbRules.add(loadBalancing);
    }
    s_logger.debug("Found " + lbRules.size() + " load balancing rule(s) to apply as a part of ELB vm " + elbVm + " start.");
    if (!lbRules.isEmpty()) {
        createApplyLoadBalancingRulesCommands(lbRules, elbVm, cmds, guestNetworkId);
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) CheckSshCommand(com.cloud.agent.api.check.CheckSshCommand) Ip(com.cloud.utils.net.Ip) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) NicProfile(com.cloud.vm.NicProfile) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 13 with LbStickinessPolicy

use of com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy in project cloudstack by apache.

the class VirtualRouterElement method validateHAProxyLBRule.

public static boolean validateHAProxyLBRule(final LoadBalancingRule rule) {
    final String timeEndChar = "dhms";
    if (rule.getSourcePortStart() == NetUtils.HAPROXY_STATS_PORT) {
        s_logger.debug("Can't create LB on port 8081, haproxy is listening for  LB stats on this port");
        return false;
    }
    for (final LbStickinessPolicy stickinessPolicy : rule.getStickinessPolicies()) {
        final List<Pair<String, String>> paramsList = stickinessPolicy.getParams();
        if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
        } else if (StickinessMethodType.SourceBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
            // optional
            String tablesize = "200k";
            // optional
            String expire = "30m";
            /* overwrite default values with the stick parameters */
            for (final Pair<String, String> paramKV : paramsList) {
                final String key = paramKV.first();
                final String value = paramKV.second();
                if ("tablesize".equalsIgnoreCase(key)) {
                    tablesize = value;
                }
                if ("expire".equalsIgnoreCase(key)) {
                    expire = value;
                }
            }
            if (expire != null && !containsOnlyNumbers(expire, timeEndChar)) {
                throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: expire is not in timeformat: " + expire);
            }
            if (tablesize != null && !containsOnlyNumbers(tablesize, "kmg")) {
                throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: tablesize is not in size format: " + tablesize);
            }
        } else if (StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
            // optional
            String length = null;
            // optional
            String holdTime = null;
            for (final Pair<String, String> paramKV : paramsList) {
                final String key = paramKV.first();
                final String value = paramKV.second();
                if ("length".equalsIgnoreCase(key)) {
                    length = value;
                }
                if ("holdtime".equalsIgnoreCase(key)) {
                    holdTime = value;
                }
            }
            if (length != null && !containsOnlyNumbers(length, null)) {
                throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: length is not a number: " + length);
            }
            if (holdTime != null && !containsOnlyNumbers(holdTime, timeEndChar) && !containsOnlyNumbers(holdTime, null)) {
                throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: holdtime is not in timeformat: " + holdTime);
            }
        }
    }
    return true;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) Pair(com.cloud.utils.Pair)

Example 14 with LbStickinessPolicy

use of com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy 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 15 with LbStickinessPolicy

use of com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy 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

LbStickinessPolicy (com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy)16 LbDestination (com.cloud.network.lb.LoadBalancingRule.LbDestination)11 Ip (com.cloud.utils.net.Ip)10 LbHealthCheckPolicy (com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)9 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)7 LoadBalancingRule (com.cloud.network.lb.LoadBalancingRule)7 ArrayList (java.util.ArrayList)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 PublicIp (com.cloud.network.addr.PublicIp)5 LbSslCert (com.cloud.network.lb.LoadBalancingRule.LbSslCert)5 DataCenterVO (com.cloud.dc.DataCenterVO)3 Network (com.cloud.network.Network)3 LoadBalancerConfigCommand (com.cloud.agent.api.routing.LoadBalancerConfigCommand)2 LoadBalancerTO (com.cloud.agent.api.to.LoadBalancerTO)2 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)2 NetworkModel (com.cloud.network.NetworkModel)2 LBStickinessPolicyVO (com.cloud.network.dao.LBStickinessPolicyVO)2 LoadBalancerDao (com.cloud.network.dao.LoadBalancerDao)2 LbAutoScaleVmGroup (com.cloud.network.lb.LoadBalancingRule.LbAutoScaleVmGroup)2 LoadBalancingRulesManager (com.cloud.network.lb.LoadBalancingRulesManager)2