Search in sources :

Example 66 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO in project cosmic by MissionCriticalCloud.

the class UpdateLoadBalancerTest method testValidateRuleBeforeUpdateLB.

@Test
public void testValidateRuleBeforeUpdateLB() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
    final LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, null, 0L, 0L, domainId, null, 60000, 60000);
    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);
    final 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)

Example 67 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO 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 68 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO 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 69 with LoadBalancerVO

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

the class LoadBalanceRuleHandler method handleCreateLoadBalancerRuleWithLock.

private LoadBalancer handleCreateLoadBalancerRuleWithLock(final CreateLoadBalancerRuleCmd lb, final Account account, final long networkId) throws InsufficientAddressCapacityException, NetworkRuleConflictException {
    Long ipId = null;
    boolean newIp = false;
    List<LoadBalancerVO> existingLbs = findExistingLoadBalancers(lb.getName(), lb.getSourceIpAddressId(), lb.getAccountId(), lb.getDomainId(), lb.getSourcePortStart());
    if (existingLbs == null) {
        existingLbs = findExistingLoadBalancers(lb.getName(), lb.getSourceIpAddressId(), lb.getAccountId(), lb.getDomainId(), null);
        if (existingLbs == null) {
            if (lb.getSourceIpAddressId() != null) {
                throwExceptionIfSuppliedlLbNameIsNotAssociatedWithIpAddress(lb);
            } else {
                s_logger.debug("Could not find any existing frontend ips for this account for this LB rule, acquiring a new frontent IP for ELB");
                final PublicIp ip = allocDirectIp(account, networkId);
                ipId = ip.getId();
                newIp = true;
            }
        } else {
            ipId = existingLbs.get(0).getSourceIpAddressId();
            s_logger.debug("ELB: Found existing frontend ip for this account for this LB rule " + ipId);
        }
    } else {
        s_logger.warn("ELB: Found existing load balancers matching requested new LB");
        throw new NetworkRuleConflictException("ELB: Found existing load balancers matching requested new LB");
    }
    final IPAddressVO ipAddr = _ipAddressDao.findById(ipId);
    LoadBalancer result = null;
    try {
        lb.setSourceIpAddressId(ipId);
        result = _lbMgr.createPublicLoadBalancer(lb.getXid(), lb.getName(), lb.getDescription(), lb.getSourcePortStart(), lb.getDefaultPortStart(), ipId.longValue(), lb.getProtocol(), lb.getAlgorithm(), false, CallContext.current(), lb.getLbProtocol(), true);
    } catch (final NetworkRuleConflictException e) {
        s_logger.warn("Failed to create LB rule, not continuing with ELB deployment");
        if (newIp) {
            releaseIp(ipId, CallContext.current().getCallingUserId(), account);
        }
        throw e;
    }
    DomainRouterVO elbVm = null;
    if (existingLbs == null) {
        elbVm = findElbVmWithCapacity(ipAddr);
        if (elbVm == null) {
            elbVm = deployLoadBalancerVM(networkId, ipAddr);
            if (elbVm == null) {
                final Network network = _networkModel.getNetwork(networkId);
                s_logger.warn("Failed to deploy a new ELB vm for ip " + ipAddr + " in network " + network + "lb name=" + lb.getName());
                if (newIp) {
                    releaseIp(ipId, CallContext.current().getCallingUserId(), account);
                }
            }
        }
    } else {
        final ElasticLbVmMapVO elbVmMap = _elbVmMapDao.findOneByIp(ipId);
        if (elbVmMap != null) {
            elbVm = _routerDao.findById(elbVmMap.getElbVmId());
        }
    }
    if (elbVm == null) {
        s_logger.warn("No ELB VM can be found or deployed");
        s_logger.warn("Deleting LB since we failed to deploy ELB VM");
        _lbDao.remove(result.getId());
        return null;
    }
    final ElasticLbVmMapVO mapping = new ElasticLbVmMapVO(ipId, elbVm.getId(), result.getId());
    _elbVmMapDao.persist(mapping);
    return result;
}
Also used : ElasticLbVmMapVO(com.cloud.network.ElasticLbVmMapVO) PublicIp(com.cloud.network.addr.PublicIp) Network(com.cloud.network.Network) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LoadBalancer(com.cloud.network.rules.LoadBalancer) IPAddressVO(com.cloud.network.dao.IPAddressVO) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 70 with LoadBalancerVO

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

the class GlobalLoadBalancingRulesServiceImplTest method runAssignToGlobalLoadBalancerRuleTestRevokedState.

void runAssignToGlobalLoadBalancerRuleTestRevokedState() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTestRevokedState");
    GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl();
    gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class);
    gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class);
    gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class);
    gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class);
    gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class);
    gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class);
    gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class);
    gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class);
    gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class);
    gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class);
    AssignToGlobalLoadBalancerRuleCmd assignCmd = new AssignToGlobalLoadBalancerRuleCmdExtn();
    Class<?> _class = assignCmd.getClass().getSuperclass();
    Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account);
    Field gslbRuleId = _class.getDeclaredField("id");
    gslbRuleId.setAccessible(true);
    gslbRuleId.set(assignCmd, new Long(1));
    GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Revoke);
    when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
    LoadBalancerVO lbRule = new LoadBalancerVO();
    lbRule.setState(FirewallRule.State.Active);
    Field networkIdField = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
    networkIdField.setAccessible(true);
    networkIdField.set(lbRule, new Long(1));
    when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule);
    Field lbRules = _class.getDeclaredField("loadBalancerRulesIds");
    lbRules.setAccessible(true);
    List<Long> lbRuleIds = new ArrayList<Long>();
    lbRuleIds.add(new Long(1));
    lbRules.set(assignCmd, lbRuleIds);
    NetworkVO networkVo = new NetworkVO();
    Field dcID = NetworkVO.class.getDeclaredField("dataCenterId");
    dcID.setAccessible(true);
    dcID.set(networkVo, new Long(1));
    when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo);
    try {
        gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd);
    } catch (InvalidParameterValueException e) {
        Assert.assertTrue(e.getMessage().contains("revoked state"));
    }
}
Also used : AssignToGlobalLoadBalancerRuleCmd(org.apache.cloudstack.api.command.user.region.ha.gslb.AssignToGlobalLoadBalancerRuleCmd) ConfigurationDao(org.apache.cloudstack.framework.config.dao.ConfigurationDao) Account(com.cloud.user.Account) LoadBalancerDao(com.cloud.network.dao.LoadBalancerDao) AgentManager(com.cloud.agent.AgentManager) RulesManager(com.cloud.network.rules.RulesManager) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) AccountVO(com.cloud.user.AccountVO) Field(java.lang.reflect.Field) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkVO(com.cloud.network.dao.NetworkVO) IPAddressDao(com.cloud.network.dao.IPAddressDao) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) NetworkDao(com.cloud.network.dao.NetworkDao) Matchers.anyLong(org.mockito.Matchers.anyLong) AccountManager(com.cloud.user.AccountManager) RegionDao(org.apache.cloudstack.region.dao.RegionDao)

Aggregations

LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)96 ArrayList (java.util.ArrayList)45 ActionEvent (com.cloud.event.ActionEvent)31 Account (com.cloud.user.Account)30 DB (com.cloud.utils.db.DB)30 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)29 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)27 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)23 LoadBalancerVMMapVO (com.cloud.network.dao.LoadBalancerVMMapVO)23 InvalidParameterException (java.security.InvalidParameterException)22 FirewallRule (com.cloud.network.rules.FirewallRule)21 Ip (com.cloud.utils.net.Ip)18 List (java.util.List)17 NetworkVO (com.cloud.network.dao.NetworkVO)15 TransactionStatus (com.cloud.utils.db.TransactionStatus)14 IPAddressVO (com.cloud.network.dao.IPAddressVO)13 LoadBalancerDao (com.cloud.network.dao.LoadBalancerDao)13 LbDestination (com.cloud.network.lb.LoadBalancingRule.LbDestination)13 LbHealthCheckPolicy (com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)13 HashMap (java.util.HashMap)13