Search in sources :

Example 6 with LbDestination

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

the class LoadBalancingRulesManagerImpl method createPublicLoadBalancer.

@DB
@Override
public LoadBalancer createPublicLoadBalancer(final String xId, final String name, final String description, final int srcPort, final int destPort, final long sourceIpId, final String protocol, final String algorithm, final boolean openFirewall, final CallContext caller, final String lbProtocol, final Boolean forDisplay) throws NetworkRuleConflictException {
    if (!NetUtils.isValidPort(destPort)) {
        throw new InvalidParameterValueException("privatePort is an invalid value: " + destPort);
    }
    if ((algorithm == null) || !NetUtils.isValidAlgorithm(algorithm)) {
        throw new InvalidParameterValueException("Invalid algorithm: " + algorithm);
    }
    final IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId);
    // make sure ip address exists
    if (ipAddr == null || !ipAddr.readyToUse()) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id specified");
        if (ipAddr == null) {
            ex.addProxyObject(String.valueOf(sourceIpId), "sourceIpId");
        } else {
            ex.addProxyObject(ipAddr.getUuid(), "sourceIpId");
        }
        throw ex;
    } else if (ipAddr.isOneToOneNat()) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule; specified sourceip id has static nat enabled");
        ex.addProxyObject(ipAddr.getUuid(), "sourceIpId");
        throw ex;
    }
    _accountMgr.checkAccess(caller.getCallingAccount(), null, true, ipAddr);
    final Long networkId = ipAddr.getAssociatedWithNetworkId();
    if (networkId == null) {
        InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule ; specified sourceip id is not associated with any network");
        ex.addProxyObject(ipAddr.getUuid(), "sourceIpId");
        throw ex;
    }
    // verify that lb service is supported by the network
    isLbServiceSupportedInNetwork(networkId, Scheme.Public);
    _firewallMgr.validateFirewallRule(caller.getCallingAccount(), ipAddr, srcPort, srcPort, protocol, Purpose.LoadBalancing, FirewallRuleType.User, networkId, null);
    LoadBalancerVO newRule = new LoadBalancerVO(xId, name, description, sourceIpId, srcPort, destPort, algorithm, networkId, ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId(), lbProtocol);
    // verify rule is supported by Lb provider of the network
    Ip sourceIp = getSourceIp(newRule);
    LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList<LbDestination>(), new ArrayList<LbStickinessPolicy>(), new ArrayList<LbHealthCheckPolicy>(), sourceIp, null, lbProtocol);
    if (!validateLbRule(loadBalancing)) {
        throw new InvalidParameterValueException("LB service provider cannot support this rule");
    }
    return Transaction.execute(new TransactionCallbackWithException<LoadBalancerVO, NetworkRuleConflictException>() {

        @Override
        public LoadBalancerVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException {
            LoadBalancerVO newRule = new LoadBalancerVO(xId, name, description, sourceIpId, srcPort, destPort, algorithm, networkId, ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId(), lbProtocol);
            if (forDisplay != null) {
                newRule.setDisplay(forDisplay);
            }
            // verify rule is supported by Lb provider of the network
            Ip sourceIp = getSourceIp(newRule);
            LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList<LbDestination>(), new ArrayList<LbStickinessPolicy>(), new ArrayList<LbHealthCheckPolicy>(), sourceIp, null, lbProtocol);
            if (!validateLbRule(loadBalancing)) {
                throw new InvalidParameterValueException("LB service provider cannot support this rule");
            }
            newRule = _lbDao.persist(newRule);
            // create rule for all CIDRs
            if (openFirewall) {
                _firewallMgr.createRuleForAllCidrs(sourceIpId, caller.getCallingAccount(), srcPort, srcPort, protocol, null, null, newRule.getId(), networkId);
            }
            boolean success = true;
            try {
                _firewallMgr.detectRulesConflict(newRule);
                if (!_firewallDao.setStateToAdd(newRule)) {
                    throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
                }
                s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port " + srcPort + ", private port " + destPort + " is added successfully.");
                CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId());
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, ipAddr.getAllocatedToAccountId(), ipAddr.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), newRule.getUuid());
                return newRule;
            } catch (Exception e) {
                success = false;
                if (e instanceof NetworkRuleConflictException) {
                    throw (NetworkRuleConflictException) e;
                }
                throw new CloudRuntimeException("Unable to add rule for ip address id=" + newRule.getSourceIpAddressId(), e);
            } finally {
                if (!success && newRule != null) {
                    _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false);
                    removeLBRule(newRule);
                }
            }
        }
    });
}
Also used : Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy) IPAddressVO(com.cloud.network.dao.IPAddressVO) DB(com.cloud.utils.db.DB)

Example 7 with LbDestination

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

the class NetscalerElement method applyElasticLoadBalancerRules.

public boolean applyElasticLoadBalancerRules(Network network, List<LoadBalancingRule> loadBalancingRules) throws ResourceUnavailableException {
    if (loadBalancingRules == null || loadBalancingRules.isEmpty()) {
        return true;
    }
    String errMsg = null;
    ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
    if (lbDeviceVO == null) {
        try {
            lbDeviceVO = allocateLoadBalancerForNetwork(network);
        } catch (Exception e) {
            errMsg = "Could not allocate a NetSclaer load balancer for configuring elastic load balancer rules due to " + e.getMessage();
            s_logger.error(errMsg);
            throw new ResourceUnavailableException(errMsg, this.getClass(), 0);
        }
    }
    if (!isNetscalerDevice(lbDeviceVO.getDeviceName())) {
        errMsg = "There are no NetScaler load balancer assigned for this network. So NetScaler element can not be handle elastic load balancer rules.";
        s_logger.error(errMsg);
        throw new ResourceUnavailableException(errMsg, this.getClass(), 0);
    }
    List<LoadBalancerTO> loadBalancersToApply = new ArrayList<LoadBalancerTO>();
    for (int i = 0; i < loadBalancingRules.size(); i++) {
        LoadBalancingRule rule = loadBalancingRules.get(i);
        boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke));
        String protocol = rule.getProtocol();
        String algorithm = rule.getAlgorithm();
        String lbUuid = rule.getUuid();
        String srcIp = rule.getSourceIp().addr();
        int srcPort = rule.getSourcePortStart();
        List<LbDestination> destinations = rule.getDestinations();
        if ((destinations != null && !destinations.isEmpty()) || rule.isAutoScaleConfig()) {
            LoadBalancerTO loadBalancer = new LoadBalancerTO(lbUuid, srcIp, srcPort, protocol, algorithm, revoked, false, false, destinations, rule.getStickinessPolicies(), rule.getHealthCheckPolicies(), rule.getLbSslCert(), rule.getLbProtocol());
            if (rule.isAutoScaleConfig()) {
                loadBalancer.setAutoScaleVmGroup(rule.getAutoScaleVmGroup());
            }
            loadBalancersToApply.add(loadBalancer);
        }
    }
    if (loadBalancersToApply.size() > 0) {
        int numLoadBalancersForCommand = loadBalancersToApply.size();
        LoadBalancerTO[] loadBalancersForCommand = loadBalancersToApply.toArray(new LoadBalancerTO[numLoadBalancersForCommand]);
        LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(loadBalancersForCommand, null);
        HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
        Answer answer = _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
        if (answer == null || !answer.getResult()) {
            String details = (answer != null) ? answer.getDetails() : "details unavailable";
            String msg = "Unable to apply elastic load balancer rules to the external load balancer appliance in zone " + network.getDataCenterId() + " due to: " + details + ".";
            s_logger.error(msg);
            throw new ResourceUnavailableException(msg, DataCenter.class, network.getDataCenterId());
        }
    }
    return true;
}
Also used : LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) ArrayList(java.util.ArrayList) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) JSONException(org.json.JSONException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) HostVO(com.cloud.host.HostVO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) Answer(com.cloud.agent.api.Answer) SetStaticNatRulesAnswer(com.cloud.agent.api.routing.SetStaticNatRulesAnswer) HealthCheckLBConfigAnswer(com.cloud.agent.api.routing.HealthCheckLBConfigAnswer) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) GlobalLoadBalancerConfigCommand(com.cloud.agent.api.routing.GlobalLoadBalancerConfigCommand) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Example 8 with LbDestination

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

the class FirewallRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    _purpose = _rules.get(0).getPurpose();
    if (_purpose == Purpose.LoadBalancing) {
        LoadBalancerDao loadBalancerDao = visitor.getVirtualNetworkApplianceFactory().getLoadBalancerDao();
        // for load balancer we have to resend all lb rules for the network or vpc
        final List<LoadBalancerVO> lbs = loadBalancerDao.listByNetworkIdOrVpcIdAndScheme(_network.getId(), _network.getVpcId(), Scheme.Public);
        _loadbalancingRules = new ArrayList<LoadBalancingRule>();
        LoadBalancingRulesManager lbMgr = visitor.getVirtualNetworkApplianceFactory().getLbMgr();
        NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
        for (final LoadBalancerVO lb : lbs) {
            final List<LbDestination> dstList = lbMgr.getExistingDestinations(lb.getId());
            final List<LbStickinessPolicy> policyList = lbMgr.getStickinessPolicies(lb.getId());
            final List<LbHealthCheckPolicy> hcPolicyList = lbMgr.getHealthCheckPolicies(lb.getId());
            final LbSslCert sslCert = lbMgr.getLbSslCert(lb.getId());
            final Ip sourceIp = networkModel.getPublicIpAddress(lb.getSourceIpAddressId()).getAddress();
            final LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList, sourceIp, sslCert, lb.getLbProtocol());
            _loadbalancingRules.add(loadBalancing);
        }
    }
    return visitor.visit(this);
}
Also used : LoadBalancerDao(com.cloud.network.dao.LoadBalancerDao) LbSslCert(com.cloud.network.lb.LoadBalancingRule.LbSslCert) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) LoadBalancingRulesManager(com.cloud.network.lb.LoadBalancingRulesManager) Ip(com.cloud.utils.net.Ip) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) NetworkModel(com.cloud.network.NetworkModel) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)

Example 9 with LbDestination

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

the class LoadBalancingRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    LoadBalancerDao loadBalancerDao = visitor.getVirtualNetworkApplianceFactory().getLoadBalancerDao();
    // For load balancer we have to resend all lb rules for the network or vpc
    final List<LoadBalancerVO> lbs = loadBalancerDao.listByNetworkIdOrVpcIdAndScheme(_network.getId(), _network.getVpcId(), Scheme.Public);
    // We are cleaning it before because all the rules have to be sent to the router.
    _rules.clear();
    LoadBalancingRulesManager lbMgr = visitor.getVirtualNetworkApplianceFactory().getLbMgr();
    NetworkModel networkModel = visitor.getVirtualNetworkApplianceFactory().getNetworkModel();
    for (final LoadBalancerVO lb : lbs) {
        final List<LbDestination> dstList = lbMgr.getExistingDestinations(lb.getId());
        final List<LbStickinessPolicy> policyList = lbMgr.getStickinessPolicies(lb.getId());
        final List<LbHealthCheckPolicy> hcPolicyList = lbMgr.getHealthCheckPolicies(lb.getId());
        final LbSslCert sslCert = lbMgr.getLbSslCert(lb.getId());
        final Ip sourceIp = networkModel.getPublicIpAddress(lb.getSourceIpAddressId()).getAddress();
        final LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList, policyList, hcPolicyList, sourceIp, sslCert, lb.getLbProtocol());
        _rules.add(loadBalancing);
    }
    return visitor.visit(this);
}
Also used : LoadBalancerDao(com.cloud.network.dao.LoadBalancerDao) LbSslCert(com.cloud.network.lb.LoadBalancingRule.LbSslCert) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) LoadBalancingRulesManager(com.cloud.network.lb.LoadBalancingRulesManager) Ip(com.cloud.utils.net.Ip) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) NetworkModel(com.cloud.network.NetworkModel) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)

Example 10 with LbDestination

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

the class InternalLoadBalancerVMManagerImpl method finalizeLbRulesForIp.

protected void finalizeLbRulesForIp(final Commands cmds, final DomainRouterVO internalLbVm, final Provider provider, final Ip sourceIp, final long guestNtwkId) {
    s_logger.debug("Resending load balancing rules as a part of start for " + internalLbVm);
    final List<ApplicationLoadBalancerRuleVO> lbs = _lbDao.listBySrcIpSrcNtwkId(sourceIp, guestNtwkId);
    final List<LoadBalancingRule> lbRules = new ArrayList<LoadBalancingRule>();
    if (_ntwkModel.isProviderSupportServiceInNetwork(guestNtwkId, Service.Lb, provider)) {
        // Re-apply load balancing rules
        for (final ApplicationLoadBalancerRuleVO lb : lbs) {
            final List<LbDestination> dstList = _lbMgr.getExistingDestinations(lb.getId());
            final List<LbStickinessPolicy> policyList = _lbMgr.getStickinessPolicies(lb.getId());
            final List<LbHealthCheckPolicy> hcPolicyList = _lbMgr.getHealthCheckPolicies(lb.getId());
            final 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 Intenrnal LB vm" + internalLbVm + " start.");
    if (!lbRules.isEmpty()) {
        createApplyLoadBalancingRulesCommands(lbRules, internalLbVm, cmds, guestNtwkId);
    }
}
Also used : ApplicationLoadBalancerRuleVO(org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) ArrayList(java.util.ArrayList) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination)

Aggregations

LbDestination (com.cloud.network.lb.LoadBalancingRule.LbDestination)31 ArrayList (java.util.ArrayList)20 LoadBalancerTO (com.cloud.agent.api.to.LoadBalancerTO)16 LbStickinessPolicy (com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy)16 LoadBalancingRule (com.cloud.network.lb.LoadBalancingRule)15 LbHealthCheckPolicy (com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)15 Ip (com.cloud.utils.net.Ip)14 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)13 LoadBalancerConfigCommand (com.cloud.agent.api.routing.LoadBalancerConfigCommand)12 LbSslCert (com.cloud.network.lb.LoadBalancingRule.LbSslCert)9 Nic (com.cloud.vm.Nic)9 PublicIp (com.cloud.network.addr.PublicIp)7 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)6 Network (com.cloud.network.Network)6 DataCenterVO (com.cloud.dc.DataCenterVO)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 HealthCheckLBConfigAnswer (com.cloud.agent.api.routing.HealthCheckLBConfigAnswer)4 NicTO (com.cloud.agent.api.to.NicTO)4 HostVO (com.cloud.host.HostVO)4 NetworkModel (com.cloud.network.NetworkModel)4