Search in sources :

Example 6 with LoadBalancerVMMapVO

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

the class LoadBalancingRulesManagerImpl method listLbVmIpAddress.

@Override
public List<String> listLbVmIpAddress(long id, long vmId) {
    List<LoadBalancerVMMapVO> listLbvmMapVo = _lb2VmMapDao.listByLoadBalancerIdAndVmId(id, vmId);
    List<String> vmIps = new ArrayList<String>();
    for (LoadBalancerVMMapVO lbVmVo : listLbvmMapVo) {
        vmIps.add(lbVmVo.getInstanceIp());
    }
    return vmIps;
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO)

Example 7 with LoadBalancerVMMapVO

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

the class LoadBalancingRulesManagerImpl method searchForLoadBalancers.

@Override
public Pair<List<? extends LoadBalancer>, Integer> searchForLoadBalancers(ListLoadBalancerRulesCmd cmd) {
    Long ipId = cmd.getPublicIpId();
    Long zoneId = cmd.getZoneId();
    Long id = cmd.getId();
    String name = cmd.getLoadBalancerRuleName();
    String keyword = cmd.getKeyword();
    Long instanceId = cmd.getVirtualMachineId();
    Long networkId = cmd.getNetworkId();
    Map<String, String> tags = cmd.getTags();
    Boolean forDisplay = cmd.getDisplay();
    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(LoadBalancerVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<LoadBalancerVO> 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.LIKE);
    sb.and("sourceIpAddress", sb.entity().getSourceIpAddressId(), SearchCriteria.Op.EQ);
    sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    sb.and("scheme", sb.entity().getScheme(), SearchCriteria.Op.EQ);
    sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
    if (instanceId != null) {
        SearchBuilder<LoadBalancerVMMapVO> lbVMSearch = _lb2VmMapDao.createSearchBuilder();
        lbVMSearch.and("instanceId", lbVMSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
        sb.join("lbVMSearch", lbVMSearch, sb.entity().getId(), lbVMSearch.entity().getLoadBalancerId(), JoinBuilder.JoinType.INNER);
    }
    if (zoneId != null) {
        SearchBuilder<IPAddressVO> ipSearch = _ipAddressDao.createSearchBuilder();
        ipSearch.and("zoneId", ipSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
        sb.join("ipSearch", ipSearch, sb.entity().getSourceIpAddressId(), ipSearch.entity().getId(), JoinBuilder.JoinType.INNER);
    }
    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<LoadBalancerVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<LoadBalancerVO> 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 (ipId != null) {
        sc.setParameters("sourceIpAddress", ipId);
    }
    if (instanceId != null) {
        sc.setJoinParameters("lbVMSearch", "instanceId", instanceId);
    }
    if (zoneId != null) {
        sc.setJoinParameters("ipSearch", "zoneId", zoneId);
    }
    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 (forDisplay != null) {
        sc.setParameters("display", forDisplay);
    }
    //list only Public load balancers using this command
    sc.setParameters("scheme", Scheme.Public);
    Pair<List<LoadBalancerVO>, Integer> result = _lbDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends LoadBalancer>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) ResourceTagVO(com.cloud.tags.ResourceTagVO) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) Ternary(com.cloud.utils.Ternary) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 8 with LoadBalancerVMMapVO

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

the class LoadBalancingRulesManagerImpl method applyLoadBalancerRules.

@DB
protected boolean applyLoadBalancerRules(List<LoadBalancerVO> lbs, boolean updateRulesInDB) throws ResourceUnavailableException {
    List<LoadBalancingRule> rules = new ArrayList<LoadBalancingRule>();
    for (LoadBalancerVO lb : lbs) {
        rules.add(getLoadBalancerRuleToApply(lb));
    }
    if (!applyLbRules(rules, false)) {
        s_logger.debug("LB rules are not completely applied");
        return false;
    }
    if (updateRulesInDB) {
        for (final LoadBalancerVO lb : lbs) {
            boolean checkForReleaseElasticIp = Transaction.execute(new TransactionCallback<Boolean>() {

                @Override
                public Boolean doInTransaction(TransactionStatus status) {
                    boolean checkForReleaseElasticIp = false;
                    if (lb.getState() == FirewallRule.State.Revoke) {
                        removeLBRule(lb);
                        s_logger.debug("LB " + lb.getId() + " is successfully removed");
                        checkForReleaseElasticIp = true;
                    } else if (lb.getState() == FirewallRule.State.Add) {
                        lb.setState(FirewallRule.State.Active);
                        s_logger.debug("LB rule " + lb.getId() + " state is set to Active");
                        _lbDao.persist(lb);
                    }
                    // remove LB-Vm mappings that were state to revoke
                    List<LoadBalancerVMMapVO> lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lb.getId(), true);
                    List<Long> instanceIds = new ArrayList<Long>();
                    for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) {
                        instanceIds.add(lbVmMap.getInstanceId());
                        _lb2VmMapDao.remove(lb.getId(), lbVmMap.getInstanceId(), lbVmMap.getInstanceIp(), null);
                        s_logger.debug("Load balancer rule id " + lb.getId() + " is removed for vm " + lbVmMap.getInstanceId() + " instance ip " + lbVmMap.getInstanceIp());
                    }
                    if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) {
                        lb.setState(FirewallRule.State.Add);
                        _lbDao.persist(lb);
                        s_logger.debug("LB rule " + lb.getId() + " state is set to Add as there are no more active LB-VM mappings");
                    }
                    // remove LB-Stickiness policy mapping that were state to revoke
                    List<LBStickinessPolicyVO> stickinesspolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(lb.getId(), true);
                    if (!stickinesspolicies.isEmpty()) {
                        _lb2stickinesspoliciesDao.remove(lb.getId(), true);
                        s_logger.debug("Load balancer rule id " + lb.getId() + " is removed stickiness policies");
                    }
                    // remove LB-HealthCheck policy mapping that were state to
                    // revoke
                    List<LBHealthCheckPolicyVO> healthCheckpolicies = _lb2healthcheckDao.listByLoadBalancerId(lb.getId(), true);
                    if (!healthCheckpolicies.isEmpty()) {
                        _lb2healthcheckDao.remove(lb.getId(), true);
                        s_logger.debug("Load balancer rule id " + lb.getId() + " is removed health check monitors policies");
                    }
                    LoadBalancerCertMapVO lbCertMap = _lbCertMapDao.findByLbRuleId(lb.getId());
                    if (lbCertMap != null && lbCertMap.isRevoke()) {
                        _lbCertMapDao.remove(lbCertMap.getId());
                        s_logger.debug("Load balancer rule id " + lb.getId() + " removed certificate mapping");
                    }
                    return checkForReleaseElasticIp;
                }
            });
            if (checkForReleaseElasticIp && lb.getSourceIpAddressId() != null) {
                boolean success = true;
                long count = _firewallDao.countRulesByIpId(lb.getSourceIpAddressId());
                if (count == 0) {
                    try {
                        success = handleSystemLBIpRelease(lb);
                    } catch (Exception ex) {
                        s_logger.warn("Failed to release system ip as a part of lb rule " + lb + " deletion due to exception ", ex);
                        success = false;
                    } finally {
                        if (!success) {
                            s_logger.warn("Failed to release system ip as a part of lb rule " + lb + " deletion");
                        }
                    }
                }
            }
            // VPC, unassign it from the network
            if (lb.getSourceIpAddressId() != null) {
                IpAddress ip = _ipAddressDao.findById(lb.getSourceIpAddressId());
                _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), lb.getNetworkId());
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) LoadBalancerCertMapVO(com.cloud.network.dao.LoadBalancerCertMapVO) 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) ArrayList(java.util.ArrayList) List(java.util.List) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) IpAddress(com.cloud.network.IpAddress) DB(com.cloud.utils.db.DB)

Example 9 with LoadBalancerVMMapVO

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

the class LoadBalancingRulesManagerImpl method removeFromLoadBalancerInternal.

private boolean removeFromLoadBalancerInternal(long loadBalancerId, List<Long> instanceIds, boolean rollBack, Map<Long, List<String>> vmIdIpMap) {
    CallContext caller = CallContext.current();
    LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(loadBalancerId));
    if (loadBalancer == null) {
        throw new InvalidParameterException("Invalid load balancer value: " + loadBalancerId);
    }
    _accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer);
    if (instanceIds == null && vmIdIpMap.isEmpty()) {
        throw new InvalidParameterValueException("Both instanceids and vmidipmap  can't be null");
    }
    // instanceIds and vmIdipmap is passed
    if (instanceIds != null && !vmIdIpMap.isEmpty()) {
        for (long instanceId : instanceIds) {
            if (!vmIdIpMap.containsKey(instanceId)) {
                vmIdIpMap.put(instanceId, null);
            }
        }
    }
    //only instanceids list passed
    if (instanceIds != null && vmIdIpMap.isEmpty()) {
        vmIdIpMap = new HashMap<Long, List<String>>();
        for (long instanceId : instanceIds) {
            vmIdIpMap.put(instanceId, null);
        }
    }
    boolean success = false;
    FirewallRule.State backupState = loadBalancer.getState();
    Set<Long> vmIds = vmIdIpMap.keySet();
    try {
        loadBalancer.setState(FirewallRule.State.Add);
        _lbDao.persist(loadBalancer);
        for (long instanceId : vmIds) {
            List<String> lbVmIps = vmIdIpMap.get(instanceId);
            if (lbVmIps == null || lbVmIps.isEmpty()) {
                List<LoadBalancerVMMapVO> lbVms = _lb2VmMapDao.listByLoadBalancerIdAndVmId(loadBalancerId, instanceId);
                if (lbVms == null) {
                    throw new InvalidParameterValueException("The instance id: " + instanceId + " is not configured " + " for LB rule id " + loadBalancerId);
                }
                for (LoadBalancerVMMapVO lbvm : lbVms) {
                    lbvm.setRevoke(true);
                    _lb2VmMapDao.persist(lbvm);
                }
                s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + instanceId);
            } else {
                for (String vmIp : lbVmIps) {
                    LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmIdVmIp(loadBalancerId, instanceId, vmIp);
                    if (map == null) {
                        throw new InvalidParameterValueException("The instance id: " + instanceId + " is not configured " + " for LB rule id " + loadBalancerId);
                    }
                    map.setRevoke(true);
                    _lb2VmMapDao.persist(map);
                    s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + instanceId + ", vmip " + vmIp);
                }
            }
        }
        if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) {
            // For autoscaled loadbalancer, the rules need not be applied,
            // meaning the call need not reach the resource layer.
            // We can consider the job done and only need to remove the
            // rules in DB
            _lb2VmMapDao.remove(loadBalancer.getId(), instanceIds, null);
            return true;
        }
        if (!applyLoadBalancerConfig(loadBalancerId)) {
            s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for vms " + instanceIds);
            CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + instanceIds);
            ex.addProxyObject(loadBalancer.getUuid(), "loadBalancerId");
            throw ex;
        }
        success = true;
    } catch (ResourceUnavailableException e) {
        if (rollBack && isRollBackAllowedForProvider(loadBalancer)) {
            for (long instanceId : vmIds) {
                List<String> lbVmIps = vmIdIpMap.get(instanceId);
                if (lbVmIps == null || lbVmIps.isEmpty()) {
                    LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmId(loadBalancerId, instanceId);
                    map.setRevoke(false);
                    _lb2VmMapDao.persist(map);
                    s_logger.debug("LB Rollback rule id: " + loadBalancerId + ",while removing vmId " + instanceId);
                } else {
                    for (String vmIp : lbVmIps) {
                        LoadBalancerVMMapVO map = _lb2VmMapDao.findByLoadBalancerIdAndVmIdVmIp(loadBalancerId, instanceId, vmIp);
                        map.setRevoke(true);
                        _lb2VmMapDao.persist(map);
                        s_logger.debug("LB Rollback rule id: " + loadBalancerId + ",while removing vmId " + instanceId + ", vmip " + vmIp);
                    }
                }
            }
            loadBalancer.setState(backupState);
            _lbDao.persist(loadBalancer);
            s_logger.debug("LB Rollback rule id: " + loadBalancerId + " while removing vm instances");
        }
        s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
    }
    if (!success) {
        CloudRuntimeException ex = new CloudRuntimeException("Failed to remove specified load balancer rule id for vms " + vmIds);
        ex.addProxyObject(loadBalancer.getUuid(), "loadBalancerId");
        throw ex;
    }
    return success;
}
Also used : LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) CallContext(org.apache.cloudstack.context.CallContext) InvalidParameterException(java.security.InvalidParameterException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ArrayList(java.util.ArrayList) List(java.util.List) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) FirewallRule(com.cloud.network.rules.FirewallRule)

Example 10 with LoadBalancerVMMapVO

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

the class LoadBalancingRulesManagerImpl method getExistingDestinations.

@Override
public List<LbDestination> getExistingDestinations(long lbId) {
    List<LbDestination> dstList = new ArrayList<LbDestination>();
    List<LoadBalancerVMMapVO> lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lbId);
    LoadBalancerVO lb = _lbDao.findById(lbId);
    String dstIp = null;
    for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) {
        UserVm vm = _vmDao.findById(lbVmMap.getInstanceId());
        Nic nic = _nicDao.findByInstanceIdAndNetworkIdIncludingRemoved(lb.getNetworkId(), vm.getId());
        dstIp = lbVmMap.getInstanceIp() == null ? nic.getIPv4Address() : lbVmMap.getInstanceIp();
        LbDestination lbDst = new LbDestination(lb.getDefaultPortStart(), lb.getDefaultPortEnd(), dstIp, lbVmMap.isRevoke());
        dstList.add(lbDst);
    }
    return dstList;
}
Also used : UserVm(com.cloud.uservm.UserVm) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) Nic(com.cloud.vm.Nic) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination)

Aggregations

LoadBalancerVMMapVO (com.cloud.network.dao.LoadBalancerVMMapVO)17 ArrayList (java.util.ArrayList)15 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)11 List (java.util.List)10 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)6 DB (com.cloud.utils.db.DB)6 TransactionStatus (com.cloud.utils.db.TransactionStatus)5 HashMap (java.util.HashMap)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 FirewallRule (com.cloud.network.rules.FirewallRule)4 Account (com.cloud.user.Account)4 ActionEvent (com.cloud.event.ActionEvent)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 Network (com.cloud.network.Network)3 NetworkVO (com.cloud.network.dao.NetworkVO)3 UserVm (com.cloud.uservm.UserVm)3 Pair (com.cloud.utils.Pair)3 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)3 Nic (com.cloud.vm.Nic)3