Search in sources :

Example 1 with ApplicationLoadBalancerRuleVO

use of org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO 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)

Example 2 with ApplicationLoadBalancerRuleVO

use of org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO in project cloudstack by apache.

the class InternalLbElementTest method verifyValidateLBRule.

//TEST FOR validateLBRule METHOD
@Test
public void verifyValidateLBRule() throws ResourceUnavailableException {
    ApplicationLoadBalancerRuleVO lb = new ApplicationLoadBalancerRuleVO(null, null, 22, 22, "roundrobin", 1L, 1L, 1L, new Ip("10.10.10.1"), 1L, Scheme.Internal);
    lb.setState(FirewallRule.State.Add);
    LoadBalancingRule rule = new LoadBalancingRule(lb, null, null, null, new Ip("10.10.10.1"));
    boolean result = _lbEl.validateLBRule(new NetworkVO(), rule);
    assertTrue("Wrong value is returned by validateLBRule method", result);
}
Also used : ApplicationLoadBalancerRuleVO(org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO) NetworkVO(com.cloud.network.dao.NetworkVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) Test(org.junit.Test)

Example 3 with ApplicationLoadBalancerRuleVO

use of org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO in project cloudstack by apache.

the class ApplicationLoadBalancerManagerImpl method listApplicationLoadBalancers.

@Override
public Pair<List<? extends ApplicationLoadBalancerRule>, Integer> listApplicationLoadBalancers(ListApplicationLoadBalancersCmd cmd) {
    Long id = cmd.getId();
    String name = cmd.getLoadBalancerRuleName();
    String ip = cmd.getSourceIp();
    Long ipNtwkId = cmd.getSourceIpNetworkId();
    String keyword = cmd.getKeyword();
    Scheme scheme = cmd.getScheme();
    Long networkId = cmd.getNetworkId();
    Boolean display = cmd.getDisplay();
    Map<String, String> tags = cmd.getTags();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(ApplicationLoadBalancerRuleVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<ApplicationLoadBalancerRuleVO> sb = _lbDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("sourceIpAddress", sb.entity().getSourceIp(), SearchCriteria.Op.EQ);
    sb.and("sourceIpAddressNetworkId", sb.entity().getSourceIpNetworkId(), SearchCriteria.Op.EQ);
    sb.and("scheme", sb.entity().getScheme(), SearchCriteria.Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
    //list only load balancers having not null sourceIp/sourceIpNtwkId
    sb.and("sourceIpAddress", sb.entity().getSourceIp(), SearchCriteria.Op.NNULL);
    sb.and("sourceIpAddressNetworkId", sb.entity().getSourceIpNetworkId(), SearchCriteria.Op.NNULL);
    if (tags != null && !tags.isEmpty()) {
        SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
        for (int count = 0; count < tags.size(); count++) {
            tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
            tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
            tagSearch.cp();
        }
        tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
        sb.groupBy(sb.entity().getId());
        sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
    }
    SearchCriteria<ApplicationLoadBalancerRuleVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<ApplicationLoadBalancerRuleVO> ssc = _lbDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (name != null) {
        sc.setParameters("name", name);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (ip != null) {
        sc.setParameters("sourceIpAddress", ip);
    }
    if (ipNtwkId != null) {
        sc.setParameters("sourceIpAddressNetworkId", ipNtwkId);
    }
    if (scheme != null) {
        sc.setParameters("scheme", scheme);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (tags != null && !tags.isEmpty()) {
        int count = 0;
        sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.LoadBalancer.toString());
        for (String key : tags.keySet()) {
            sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
            sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
            count++;
        }
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    Pair<List<ApplicationLoadBalancerRuleVO>, Integer> result = _lbDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends ApplicationLoadBalancerRule>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) ApplicationLoadBalancerRuleVO(org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO) Scheme(com.cloud.network.rules.LoadBalancerContainer.Scheme) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) ResourceTagVO(com.cloud.tags.ResourceTagVO) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 4 with ApplicationLoadBalancerRuleVO

use of org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO in project cloudstack by apache.

the class ApplicationLoadBalancerManagerImpl method createApplicationLoadBalancer.

protected ApplicationLoadBalancerRule createApplicationLoadBalancer(String name, String description, Scheme scheme, Network sourceIpNtwk, String sourceIp, int sourcePort, int instancePort, String algorithm, Account lbOwner, Network guestNtwk, Boolean forDisplay) throws NetworkRuleConflictException, InsufficientVirtualNetworkCapacityException {
    //Only Internal scheme is supported in this release
    if (scheme != Scheme.Internal) {
        throw new UnsupportedServiceException("Only scheme of type " + Scheme.Internal + " is supported");
    }
    //1) Validate LB rule's parameters
    validateLbRule(sourcePort, instancePort, algorithm, guestNtwk, scheme);
    //2) Validate source network
    validateSourceIpNtwkForLbRule(sourceIpNtwk, scheme);
    //3) Get source ip address
    Ip sourceIpAddr = getSourceIp(scheme, sourceIpNtwk, sourceIp);
    ApplicationLoadBalancerRuleVO newRule = new ApplicationLoadBalancerRuleVO(name, description, sourcePort, instancePort, algorithm, guestNtwk.getId(), lbOwner.getId(), lbOwner.getDomainId(), sourceIpAddr, sourceIpNtwk.getId(), scheme);
    if (forDisplay != null) {
        newRule.setDisplay(forDisplay);
    }
    //4) Validate Load Balancing rule on the providers
    LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList<LbDestination>(), new ArrayList<LbStickinessPolicy>(), new ArrayList<LbHealthCheckPolicy>(), sourceIpAddr, null, null);
    if (!_lbMgr.validateLbRule(loadBalancing)) {
        throw new InvalidParameterValueException("LB service provider cannot support this rule");
    }
    //5) Persist Load Balancer rule
    return persistLbRule(newRule);
}
Also used : ApplicationLoadBalancerRuleVO(org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) Ip(com.cloud.utils.net.Ip) LbHealthCheckPolicy(com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy) LbStickinessPolicy(com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination)

Example 5 with ApplicationLoadBalancerRuleVO

use of org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO in project cloudstack by apache.

the class ApplicationLoadBalancerManagerImpl method persistLbRule.

@DB
protected ApplicationLoadBalancerRule persistLbRule(final ApplicationLoadBalancerRuleVO newRuleFinal) throws NetworkRuleConflictException {
    boolean success = true;
    ApplicationLoadBalancerRuleVO newRule = null;
    try {
        newRule = Transaction.execute(new TransactionCallbackWithException<ApplicationLoadBalancerRuleVO, NetworkRuleConflictException>() {

            @Override
            public ApplicationLoadBalancerRuleVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException {
                //1) Persist the rule
                ApplicationLoadBalancerRuleVO newRule = _lbDao.persist(newRuleFinal);
                //2) Detect conflicts
                detectLbRulesConflicts(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 " + newRule.getSourceIp().addr() + ", source port " + newRule.getSourcePortStart().intValue() + ", instance port " + newRule.getDefaultPortStart() + " is added successfully.");
                CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId());
                Network ntwk = _networkModel.getNetwork(newRule.getNetworkId());
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, newRule.getAccountId(), ntwk.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), newRule.getUuid());
                return newRule;
            }
        });
        return newRule;
    } catch (Exception e) {
        success = false;
        if (e instanceof NetworkRuleConflictException) {
            throw (NetworkRuleConflictException) e;
        }
        throw new CloudRuntimeException("Unable to add lb rule for ip address " + newRuleFinal.getSourceIpAddressId(), e);
    } finally {
        if (!success && newRule != null) {
            _lbMgr.removeLBRule(newRule);
        }
    }
}
Also used : ApplicationLoadBalancerRuleVO(org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) TransactionStatus(com.cloud.utils.db.TransactionStatus) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Aggregations

ApplicationLoadBalancerRuleVO (org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO)8 LoadBalancingRule (com.cloud.network.lb.LoadBalancingRule)6 Ip (com.cloud.utils.net.Ip)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 ArrayList (java.util.ArrayList)3 UnsupportedServiceException (com.cloud.exception.UnsupportedServiceException)2 Network (com.cloud.network.Network)2 NetworkVO (com.cloud.network.dao.NetworkVO)2 LbDestination (com.cloud.network.lb.LoadBalancingRule.LbDestination)2 LbHealthCheckPolicy (com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)2 LbStickinessPolicy (com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy)2 Account (com.cloud.user.Account)2 Test (org.junit.Test)2 ActionEvent (com.cloud.event.ActionEvent)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)1 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)1 Capability (com.cloud.network.Network.Capability)1 Service (com.cloud.network.Network.Service)1 PublicIp (com.cloud.network.addr.PublicIp)1