Search in sources :

Example 26 with LbDestination

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

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

the class ElasticLoadBalancerManagerImpl method createApplyLoadBalancingRulesCommands.

private void createApplyLoadBalancingRulesCommands(List<LoadBalancingRule> rules, DomainRouterVO elbVm, Commands cmds, long guestNetworkId) {
    /* XXX: cert */
    LoadBalancerTO[] lbs = new LoadBalancerTO[rules.size()];
    int i = 0;
    for (LoadBalancingRule rule : rules) {
        boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke));
        String protocol = rule.getProtocol();
        String algorithm = rule.getAlgorithm();
        String elbIp = rule.getSourceIp().addr();
        int srcPort = rule.getSourcePortStart();
        String uuid = rule.getUuid();
        List<LbDestination> destinations = rule.getDestinations();
        LoadBalancerTO lb = new LoadBalancerTO(uuid, elbIp, srcPort, protocol, algorithm, revoked, false, false, destinations);
        lbs[i++] = lb;
    }
    NetworkOffering offering = _networkOfferingDao.findById(guestNetworkId);
    String maxconn = null;
    if (offering.getConcurrentConnections() == null) {
        maxconn = _configDao.getValue(Config.NetworkLBHaproxyMaxConn.key());
    } else {
        maxconn = offering.getConcurrentConnections().toString();
    }
    LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lbs, elbVm.getPublicIpAddress(), _nicDao.getIpAddress(guestNetworkId, elbVm.getId()), elbVm.getPrivateIpAddress(), null, null, maxconn, offering.isKeepAliveEnabled());
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, elbVm.getPrivateIpAddress());
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, elbVm.getInstanceName());
    // FIXME: why are we setting attributes directly? Ick!! There should be accessors and
    // the constructor should set defaults.
    cmd.lbStatsVisibility = _configDao.getValue(Config.NetworkLBHaproxyStatsVisbility.key());
    cmd.lbStatsUri = _configDao.getValue(Config.NetworkLBHaproxyStatsUri.key());
    cmd.lbStatsAuth = _configDao.getValue(Config.NetworkLBHaproxyStatsAuth.key());
    cmd.lbStatsPort = _configDao.getValue(Config.NetworkLBHaproxyStatsPort.key());
    cmds.addCommand(cmd);
}
Also used : NetworkOffering(com.cloud.offering.NetworkOffering) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Example 28 with LbDestination

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

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

the class NetscalerElement method getElasticLBRulesHealthCheck.

public List<LoadBalancerTO> getElasticLBRulesHealthCheck(Network network, List<LoadBalancingRule> loadBalancingRules) throws ResourceUnavailableException {
    HealthCheckLBConfigAnswer answer = null;
    if (loadBalancingRules == null || loadBalancingRules.isEmpty()) {
        return null;
    }
    String errMsg = null;
    ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
    if (lbDeviceVO == null) {
        s_logger.warn("There is no external load balancer device assigned to this network either network is not implement are already shutdown so just returning");
        return null;
    }
    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, null, rule.getHealthCheckPolicies(), rule.getLbSslCert(), rule.getLbProtocol());
            loadBalancersToApply.add(loadBalancer);
        }
    }
    if (loadBalancersToApply.size() > 0) {
        int numLoadBalancersForCommand = loadBalancersToApply.size();
        LoadBalancerTO[] loadBalancersForCommand = loadBalancersToApply.toArray(new LoadBalancerTO[numLoadBalancersForCommand]);
        HealthCheckLBConfigCommand cmd = new HealthCheckLBConfigCommand(loadBalancersForCommand, network.getId());
        HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
        answer = (HealthCheckLBConfigAnswer) _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
        return answer.getLoadBalancers();
    }
    return null;
}
Also used : LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) ArrayList(java.util.ArrayList) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) HealthCheckLBConfigCommand(com.cloud.agent.api.routing.HealthCheckLBConfigCommand) HostVO(com.cloud.host.HostVO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) HealthCheckLBConfigAnswer(com.cloud.agent.api.routing.HealthCheckLBConfigAnswer)

Example 30 with LbDestination

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

the class ExternalLoadBalancerDeviceManagerImpl method getLBHealthChecks.

@Override
public List<LoadBalancerTO> getLBHealthChecks(Network network, List<LoadBalancingRule> loadBalancingRules) throws ResourceUnavailableException {
    // Find the external load balancer in this zone
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    if (loadBalancingRules == null || loadBalancingRules.isEmpty()) {
        return null;
    }
    HostVO externalLoadBalancer = null;
    if (isNccServiceProvider(network)) {
        externalLoadBalancer = getNetScalerControlCenterForNetwork(network);
    } else {
        ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
        if (lbDeviceVO == null) {
            s_logger.warn("There is no external load balancer device assigned to this network either network is not implement are already shutdown so just returning");
            return null;
        } else {
            externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
        }
    }
    boolean externalLoadBalancerIsInline = _networkMgr.isNetworkInlineMode(network);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("External load balancer was asked to apply LB rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands.");
        return null;
    }
    List<LoadBalancerTO> loadBalancersToApply = new ArrayList<LoadBalancerTO>();
    List<MappingState> mappingStates = new ArrayList<MappingState>();
    for (final LoadBalancingRule rule : loadBalancingRules) {
        boolean revoked = (FirewallRule.State.Revoke.equals(rule.getState()));
        String protocol = rule.getProtocol();
        String algorithm = rule.getAlgorithm();
        String uuid = rule.getUuid();
        String srcIp = rule.getSourceIp().addr();
        int srcPort = rule.getSourcePortStart();
        List<LbDestination> destinations = rule.getDestinations();
        if (externalLoadBalancerIsInline) {
            long sourceIpId = _networkModel.getPublicIpAddress(rule.getSourceIp().addr(), network.getDataCenterId()).getId();
            MappingNic nic = getLoadBalancingIpNic(zone, network, sourceIpId, revoked, null);
            mappingStates.add(nic.getState());
            Nic loadBalancingIpNic = nic.getNic();
            if (loadBalancingIpNic == null) {
                continue;
            }
            // Change the source IP address for the load balancing rule to
            // be the load balancing IP address
            srcIp = loadBalancingIpNic.getIPv4Address();
        }
        if ((destinations != null && !destinations.isEmpty()) || !rule.isAutoScaleConfig()) {
            boolean inline = _networkMgr.isNetworkInlineMode(network);
            LoadBalancerTO loadBalancer = new LoadBalancerTO(uuid, srcIp, srcPort, protocol, algorithm, revoked, false, inline, destinations, rule.getStickinessPolicies(), rule.getHealthCheckPolicies(), rule.getLbSslCert(), rule.getLbProtocol());
            loadBalancersToApply.add(loadBalancer);
        }
    }
    try {
        if (loadBalancersToApply.size() > 0) {
            int numLoadBalancersForCommand = loadBalancersToApply.size();
            LoadBalancerTO[] loadBalancersForCommand = loadBalancersToApply.toArray(new LoadBalancerTO[numLoadBalancersForCommand]);
            HealthCheckLBConfigCommand cmd = new HealthCheckLBConfigCommand(loadBalancersForCommand, network.getId());
            long guestVlanTag = Integer.parseInt(BroadcastDomainType.getValue(network.getBroadcastUri()));
            cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, String.valueOf(guestVlanTag));
            HealthCheckLBConfigAnswer answer = (HealthCheckLBConfigAnswer) _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
            // easySend will return null on error
            return answer == null ? null : answer.getLoadBalancers();
        }
    } catch (Exception ex) {
        s_logger.error("Exception Occured ", ex);
    }
    // null return is handled by clients
    return null;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) ArrayList(java.util.ArrayList) Nic(com.cloud.vm.Nic) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) HealthCheckLBConfigCommand(com.cloud.agent.api.routing.HealthCheckLBConfigCommand) HostVO(com.cloud.host.HostVO) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) HealthCheckLBConfigAnswer(com.cloud.agent.api.routing.HealthCheckLBConfigAnswer)

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