Search in sources :

Example 31 with LoadBalancerVO

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

the class LoadBalancingRulesManagerImpl method applyLBStickinessPolicy.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_LB_STICKINESSPOLICY_CREATE, eventDescription = "Apply Stickinesspolicy to load balancer ", async = true)
public boolean applyLBStickinessPolicy(CreateLBStickinessPolicyCmd cmd) {
    boolean success = true;
    FirewallRule.State backupState = null;
    long oldStickinessPolicyId = 0;
    LoadBalancerVO loadBalancer = _lbDao.findById(cmd.getLbRuleId());
    if (loadBalancer == null) {
        throw new InvalidParameterException("Invalid Load balancer Id:" + cmd.getLbRuleId());
    }
    List<LBStickinessPolicyVO> stickinessPolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId(cmd.getLbRuleId(), false);
    for (LBStickinessPolicyVO stickinessPolicy : stickinessPolicies) {
        if (stickinessPolicy.getId() == cmd.getEntityId()) {
            backupState = loadBalancer.getState();
            loadBalancer.setState(FirewallRule.State.Add);
            _lbDao.persist(loadBalancer);
        } else {
            oldStickinessPolicyId = stickinessPolicy.getId();
            stickinessPolicy.setRevoke(true);
            _lb2stickinesspoliciesDao.persist(stickinessPolicy);
        }
    }
    try {
        applyLoadBalancerConfig(cmd.getLbRuleId());
    } catch (ResourceUnavailableException e) {
        s_logger.warn("Unable to apply Stickiness policy to the lb rule: " + cmd.getLbRuleId() + " because resource is unavaliable:", e);
        if (isRollBackAllowedForProvider(loadBalancer)) {
            loadBalancer.setState(backupState);
            _lbDao.persist(loadBalancer);
            deleteLBStickinessPolicy(cmd.getEntityId(), false);
            s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " lb state rolback while creating sticky policy");
        } else {
            deleteLBStickinessPolicy(cmd.getEntityId(), false);
            if (oldStickinessPolicyId != 0) {
                LBStickinessPolicyVO stickinessPolicy = _lb2stickinesspoliciesDao.findById(oldStickinessPolicyId);
                stickinessPolicy.setRevoke(false);
                _lb2stickinesspoliciesDao.persist(stickinessPolicy);
                try {
                    if (backupState.equals(FirewallRule.State.Active))
                        applyLoadBalancerConfig(cmd.getLbRuleId());
                } catch (ResourceUnavailableException e1) {
                    s_logger.info("[ignored] applying load balancer config.", e1);
                } finally {
                    loadBalancer.setState(backupState);
                    _lbDao.persist(loadBalancer);
                }
            }
        }
        success = false;
    }
    return success;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO) FirewallRule(com.cloud.network.rules.FirewallRule) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 32 with LoadBalancerVO

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

the class LoadBalancingRulesManagerImpl method deleteLoadBalancerRule.

@Override
@ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_DELETE, eventDescription = "deleting load balancer", async = true)
public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply) {
    CallContext ctx = CallContext.current();
    Account caller = ctx.getCallingAccount();
    LoadBalancerVO rule = _lbDao.findById(loadBalancerId);
    if (rule == null) {
        throw new InvalidParameterValueException("Unable to find load balancer rule " + loadBalancerId);
    }
    _accountMgr.checkAccess(caller, null, true, rule);
    boolean result = deleteLoadBalancerRule(loadBalancerId, apply, caller, ctx.getCallingUserId(), true);
    if (!result) {
        throw new CloudRuntimeException("Unable to remove load balancer rule " + loadBalancerId);
    }
    return result;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) CallContext(org.apache.cloudstack.context.CallContext) ActionEvent(com.cloud.event.ActionEvent)

Example 33 with LoadBalancerVO

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

the class LoadBalancingRulesManagerImpl method searchForLBStickinessPolicies.

@Override
public List<LBStickinessPolicyVO> searchForLBStickinessPolicies(ListLBStickinessPoliciesCmd cmd) throws PermissionDeniedException {
    Account caller = CallContext.current().getCallingAccount();
    Long loadBalancerId = cmd.getLbRuleId();
    Long stickinessId = cmd.getId();
    boolean forDisplay = cmd.getDisplay();
    LoadBalancerVO loadBalancer = null;
    if (loadBalancerId == null) {
        loadBalancer = findLbByStickinessId(stickinessId);
    } else {
        loadBalancer = _lbDao.findById(loadBalancerId);
    }
    if (loadBalancer == null) {
        return null;
    }
    _accountMgr.checkAccess(caller, null, true, loadBalancer);
    List<LBStickinessPolicyVO> sDbpolicies = _lb2stickinesspoliciesDao.listByLoadBalancerIdAndDisplayFlag(loadBalancer.getId(), forDisplay);
    return sDbpolicies;
}
Also used : Account(com.cloud.user.Account) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LBStickinessPolicyVO(com.cloud.network.dao.LBStickinessPolicyVO)

Example 34 with LoadBalancerVO

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

the class AssignLoadBalancerTest method testVmIdAlreadyExist.

@Test(expected = InvalidParameterValueException.class)
public void testVmIdAlreadyExist() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
    AssignToLoadBalancerRuleCmd assignLbRuleCmd = Mockito.mock(AssignToLoadBalancerRuleCmd.class);
    Map<Long, List<String>> vmIdIpMap = new HashMap<Long, List<String>>();
    List<String> secIp = new ArrayList<String>();
    secIp.add("10.1.1.175");
    vmIdIpMap.put(1L, secIp);
    List<Long> vmIds = new ArrayList<Long>();
    vmIds.add(2L);
    LoadBalancerVO lbVO = new LoadBalancerVO("1", "L1", "Lbrule", 1, 22, 22, "rb", 204, 0, 0, "tcp");
    LoadBalancerDao lbDao = Mockito.mock(LoadBalancerDao.class);
    LoadBalancerVMMapDao lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
    UserVmDao userVmDao = Mockito.mock(UserVmDao.class);
    NicSecondaryIpDao nicSecIpDao = Mockito.mock(NicSecondaryIpDao.class);
    LoadBalancerVMMapVO lbVmMapVO = new LoadBalancerVMMapVO(1L, 1L, "10.1.1.175", false);
    _lbMgr._lbDao = lbDao;
    _lbMgr._lb2VmMapDao = lb2VmMapDao;
    _lbMgr._vmDao = userVmDao;
    _lbMgr._nicSecondaryIpDao = nicSecIpDao;
    _lbvmMapList = new ArrayList<>();
    _lbvmMapList.add(lbVmMapVO);
    _lbMgr._rulesMgr = _rulesMgr;
    _lbMgr._networkModel = _networkModel;
    when(lbDao.findById(anyLong())).thenReturn(lbVO);
    when(userVmDao.findById(anyLong())).thenReturn(Mockito.mock(UserVmVO.class));
    when(lb2VmMapDao.listByLoadBalancerId(anyLong(), anyBoolean())).thenReturn(_lbvmMapList);
    when(nicSecIpDao.findByIp4AddressAndNicId(anyString(), anyLong())).thenReturn(null);
    _lbMgr.assignToLoadBalancer(1L, null, vmIdIpMap);
}
Also used : LoadBalancerDao(com.cloud.network.dao.LoadBalancerDao) UserVmVO(com.cloud.vm.UserVmVO) NicSecondaryIpDao(com.cloud.vm.dao.NicSecondaryIpDao) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) Matchers.anyString(org.mockito.Matchers.anyString) AssignToLoadBalancerRuleCmd(org.apache.cloudstack.api.command.user.loadbalancer.AssignToLoadBalancerRuleCmd) Matchers.anyLong(org.mockito.Matchers.anyLong) LoadBalancerVMMapDao(com.cloud.network.dao.LoadBalancerVMMapDao) ArrayList(java.util.ArrayList) List(java.util.List) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) UserVmDao(com.cloud.vm.dao.UserVmDao) Test(org.junit.Test)

Example 35 with LoadBalancerVO

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

the class UpdateLoadBalancerTest method testValidateRuleBeforeUpdateLB.

@Test
public void testValidateRuleBeforeUpdateLB() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
    LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, null, 0L, 0L, domainId, null);
    when(lbDao.findById(anyLong())).thenReturn(lb);
    when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
    when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
    when(lbServiceProvider.validateLBRule(any(Network.class), any(LoadBalancingRule.class))).thenReturn(true);
    when(lbDao.update(anyLong(), eq(lb))).thenReturn(true);
    _lbMgr.updateLoadBalancerRule(updateLbRuleCmd);
    InOrder inOrder = Mockito.inOrder(lbServiceProvider, lbDao);
    inOrder.verify(lbServiceProvider).validateLBRule(any(Network.class), any(LoadBalancingRule.class));
    inOrder.verify(lbDao).update(anyLong(), eq(lb));
}
Also used : PublicIpAddress(com.cloud.network.PublicIpAddress) NetworkVO(com.cloud.network.dao.NetworkVO) InOrder(org.mockito.InOrder) Network(com.cloud.network.Network) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) Test(org.junit.Test)

Aggregations

LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)56 ArrayList (java.util.ArrayList)29 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)22 Account (com.cloud.user.Account)19 ActionEvent (com.cloud.event.ActionEvent)17 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)17 DB (com.cloud.utils.db.DB)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)15 CallContext (org.apache.cloudstack.context.CallContext)12 LoadBalancerVMMapVO (com.cloud.network.dao.LoadBalancerVMMapVO)11 FirewallRule (com.cloud.network.rules.FirewallRule)11 Ip (com.cloud.utils.net.Ip)11 InvalidParameterException (java.security.InvalidParameterException)11 NetworkVO (com.cloud.network.dao.NetworkVO)10 Network (com.cloud.network.Network)9 IPAddressVO (com.cloud.network.dao.IPAddressVO)9 LoadBalancerDao (com.cloud.network.dao.LoadBalancerDao)9 TransactionStatus (com.cloud.utils.db.TransactionStatus)9 List (java.util.List)9 LbHealthCheckPolicy (com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)8