Search in sources :

Example 71 with LoadBalancerVO

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

the class GlobalLoadBalancingRulesServiceImplTest method runAssignToGlobalLoadBalancerRuleTest.

void runAssignToGlobalLoadBalancerRuleTest() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTest");
    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.Active);
    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));
    Field sourceIpAddressId = LoadBalancerVO.class.getSuperclass().getDeclaredField("sourceIpAddressId");
    sourceIpAddressId.setAccessible(true);
    sourceIpAddressId.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);
    IPAddressVO ip = new IPAddressVO(new Ip("10.1.1.1"), 1, 1, 1, true);
    when(gslbServiceImpl._ipAddressDao.findById(new Long(1))).thenReturn(ip);
    try {
        gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd);
    } catch (Exception e) {
        s_logger.info("exception in testing runAssignToGlobalLoadBalancerRuleTest message: " + e.toString());
    }
}
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) Ip(com.cloud.utils.net.Ip) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) AccountVO(com.cloud.user.AccountVO) Field(java.lang.reflect.Field) NetworkVO(com.cloud.network.dao.NetworkVO) IPAddressDao(com.cloud.network.dao.IPAddressDao) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) NetworkDao(com.cloud.network.dao.NetworkDao) Matchers.anyLong(org.mockito.Matchers.anyLong) AccountManager(com.cloud.user.AccountManager) IPAddressVO(com.cloud.network.dao.IPAddressVO) RegionDao(org.apache.cloudstack.region.dao.RegionDao)

Example 72 with LoadBalancerVO

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

the class GlobalLoadBalancingRulesServiceImplTest method runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb.

void runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb");
    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);
    RemoveFromGlobalLoadBalancerRuleCmd removeFromGslbCmd = new RemoveFromGlobalLoadBalancerRuleCmdExtn();
    Class<?> _class = removeFromGslbCmd.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(removeFromGslbCmd, new Long(1));
    GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", "test-domain", "roundrobin", "sourceip", "tcp", 1, 1, 1, GlobalLoadBalancerRule.State.Active);
    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));
    Field idField = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
    idField.setAccessible(true);
    idField.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(removeFromGslbCmd, 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.removeFromGlobalLoadBalancerRule(removeFromGslbCmd);
    } catch (InvalidParameterValueException e) {
        Assert.assertTrue(e.getMessage().contains("not assigned to global load balancer rule"));
    }
}
Also used : 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) RemoveFromGlobalLoadBalancerRuleCmd(org.apache.cloudstack.api.command.user.region.ha.gslb.RemoveFromGlobalLoadBalancerRuleCmd) 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)

Example 73 with LoadBalancerVO

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

the class ExternalDeviceUsageManagerImpl method updateExternalLoadBalancerNetworkUsageStats.

@Override
public void updateExternalLoadBalancerNetworkUsageStats(long loadBalancerRuleId) {
    LoadBalancerVO lb = _loadBalancerDao.findById(loadBalancerRuleId);
    if (lb == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Cannot update usage stats, LB rule is not found");
        }
        return;
    }
    long networkId = lb.getNetworkId();
    Network network = _networkDao.findById(networkId);
    if (network == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Cannot update usage stats, Network is not found");
        }
        return;
    }
    ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
    if (lbDeviceVO == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Cannot update usage stats,  No external LB device found");
        }
        return;
    }
    // Get network stats from the external load balancer
    ExternalNetworkResourceUsageAnswer lbAnswer = null;
    HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
    if (externalLoadBalancer != null) {
        ExternalNetworkResourceUsageCommand cmd = new ExternalNetworkResourceUsageCommand();
        lbAnswer = (ExternalNetworkResourceUsageAnswer) _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
        if (lbAnswer == null || !lbAnswer.getResult()) {
            String details = (lbAnswer != null) ? lbAnswer.getDetails() : "details unavailable";
            String msg = "Unable to get external load balancer stats for network" + networkId + " due to: " + details + ".";
            s_logger.error(msg);
            return;
        }
    }
    long accountId = lb.getAccountId();
    AccountVO account = _accountDao.findById(accountId);
    if (account == null) {
        s_logger.debug("Skipping stats update for external LB for account with ID " + accountId);
        return;
    }
    String publicIp = _networkModel.getIp(lb.getSourceIpAddressId()).getAddress().addr();
    DataCenterVO zone = _dcDao.findById(network.getDataCenterId());
    String statsEntryIdentifier = "account " + account.getAccountName() + ", zone " + zone.getName() + ", network ID " + networkId + ", host ID " + externalLoadBalancer.getName();
    long newCurrentBytesSent = 0;
    long newCurrentBytesReceived = 0;
    if (publicIp != null) {
        long[] bytesSentAndReceived = null;
        statsEntryIdentifier += ", public IP: " + publicIp;
        boolean inline = _networkModel.isNetworkInlineMode(network);
        if (externalLoadBalancer.getType().equals(Host.Type.ExternalLoadBalancer) && inline) {
            // Look up stats for the guest IP address that's mapped to the public IP address
            InlineLoadBalancerNicMapVO mapping = _inlineLoadBalancerNicMapDao.findByPublicIpAddress(publicIp);
            if (mapping != null) {
                NicVO nic = _nicDao.findById(mapping.getNicId());
                String loadBalancingIpAddress = nic.getIPv4Address();
                bytesSentAndReceived = lbAnswer.ipBytes.get(loadBalancingIpAddress);
                if (bytesSentAndReceived != null) {
                    bytesSentAndReceived[0] = 0;
                }
            }
        } else {
            bytesSentAndReceived = lbAnswer.ipBytes.get(publicIp);
        }
        if (bytesSentAndReceived == null) {
            s_logger.debug("Didn't get an external network usage answer for public IP " + publicIp);
        } else {
            newCurrentBytesSent += bytesSentAndReceived[0];
            newCurrentBytesReceived += bytesSentAndReceived[1];
        }
        commitStats(networkId, externalLoadBalancer, accountId, publicIp, zone, statsEntryIdentifier, newCurrentBytesSent, newCurrentBytesReceived);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) NetworkExternalLoadBalancerVO(com.cloud.network.dao.NetworkExternalLoadBalancerVO) ExternalNetworkResourceUsageCommand(com.cloud.agent.api.ExternalNetworkResourceUsageCommand) InlineLoadBalancerNicMapVO(com.cloud.network.dao.InlineLoadBalancerNicMapVO) AccountVO(com.cloud.user.AccountVO) HostVO(com.cloud.host.HostVO) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) NicVO(com.cloud.vm.NicVO)

Example 74 with LoadBalancerVO

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

the class CertServiceTest method runDeleteSslCertBoundCert.

@Test
public void runDeleteSslCertBoundCert() throws NoSuchFieldException, IllegalAccessException {
    TransactionLegacy.open("runDeleteSslCertBoundCert");
    final CertServiceImpl certService = new CertServiceImpl();
    // setting mock objects
    final long certId = 1;
    certService._accountMgr = Mockito.mock(AccountManager.class);
    final Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    when(certService._accountMgr.getAccount(anyLong())).thenReturn(account);
    certService._domainDao = Mockito.mock(DomainDao.class);
    final DomainVO domain = new DomainVO("networkdomain", 1L, 1L, "networkdomain");
    when(certService._domainDao.findByIdIncludingRemoved(anyLong())).thenReturn(domain);
    certService._sslCertDao = Mockito.mock(SslCertDao.class);
    when(certService._sslCertDao.remove(anyLong())).thenReturn(true);
    when(certService._sslCertDao.findById(anyLong())).thenReturn(new SslCertVO());
    // rule holding the cert
    certService._lbCertDao = Mockito.mock(LoadBalancerCertMapDao.class);
    final List<LoadBalancerCertMapVO> lbMapList = new ArrayList<>();
    lbMapList.add(new LoadBalancerCertMapVO());
    certService._lbCertDao = Mockito.mock(LoadBalancerCertMapDao.class);
    when(certService._lbCertDao.listByCertId(anyLong())).thenReturn(lbMapList);
    certService._entityMgr = Mockito.mock(EntityManager.class);
    when(certService._entityMgr.findById(eq(LoadBalancerVO.class), nullable(Long.class))).thenReturn(new LoadBalancerVO());
    // creating the command
    final DeleteSslCertCmd deleteCmd = new DeleteSslCertCmdExtn();
    final Class<?> klazz = deleteCmd.getClass().getSuperclass();
    final Field certField = klazz.getDeclaredField("id");
    certField.setAccessible(true);
    certField.set(deleteCmd, certId);
    try {
        certService.deleteSslCert(deleteCmd);
        Assert.fail("Delete with a cert id bound to a lb should fail");
    } catch (final Exception e) {
        Assert.assertTrue(e.getMessage().contains("Certificate in use by a loadbalancer"));
    }
}
Also used : Account(com.cloud.user.Account) SslCertDao(com.cloud.network.dao.SslCertDao) LoadBalancerCertMapDao(com.cloud.network.dao.LoadBalancerCertMapDao) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LoadBalancerCertMapVO(com.cloud.network.dao.LoadBalancerCertMapVO) AccountVO(com.cloud.user.AccountVO) IOException(java.io.IOException) DomainVO(com.cloud.domain.DomainVO) Field(java.lang.reflect.Field) EntityManager(com.cloud.utils.db.EntityManager) SslCertVO(com.cloud.network.dao.SslCertVO) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DomainDao(com.cloud.domain.dao.DomainDao) AccountManager(com.cloud.user.AccountManager) DeleteSslCertCmd(org.apache.cloudstack.api.command.user.loadbalancer.DeleteSslCertCmd) Test(org.junit.Test)

Example 75 with LoadBalancerVO

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

the class LoadBalancingRulesManagerImpl method listLoadBalancerInstances.

@Override
public Pair<List<? extends UserVm>, List<String>> listLoadBalancerInstances(ListLoadBalancerRuleInstancesCmd cmd) throws PermissionDeniedException {
    Account caller = CallContext.current().getCallingAccount();
    Long loadBalancerId = cmd.getId();
    Boolean applied = cmd.isApplied();
    if (applied == null) {
        applied = Boolean.TRUE;
    }
    LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId);
    if (loadBalancer == null) {
        return null;
    }
    _accountMgr.checkAccess(caller, null, true, loadBalancer);
    List<UserVmVO> loadBalancerInstances = new ArrayList<UserVmVO>();
    List<String> serviceStates = new ArrayList<String>();
    List<LoadBalancerVMMapVO> vmLoadBalancerMappings = null;
    vmLoadBalancerMappings = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId);
    if (vmLoadBalancerMappings == null) {
        String msg = "no VM Loadbalancer Mapping found";
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    Map<Long, String> vmServiceState = new HashMap<Long, String>(vmLoadBalancerMappings.size());
    List<Long> appliedInstanceIdList = new ArrayList<Long>();
    if ((vmLoadBalancerMappings != null) && !vmLoadBalancerMappings.isEmpty()) {
        for (LoadBalancerVMMapVO vmLoadBalancerMapping : vmLoadBalancerMappings) {
            appliedInstanceIdList.add(vmLoadBalancerMapping.getInstanceId());
            vmServiceState.put(vmLoadBalancerMapping.getInstanceId(), vmLoadBalancerMapping.getState());
        }
    }
    List<UserVmVO> userVms = _vmDao.listVirtualNetworkInstancesByAcctAndNetwork(loadBalancer.getAccountId(), loadBalancer.getNetworkId());
    for (UserVmVO userVm : userVms) {
        // an unknown state, skip it
        switch(userVm.getState()) {
            case Destroyed:
            case Expunging:
            case Error:
            case Unknown:
                continue;
        }
        boolean isApplied = appliedInstanceIdList.contains(userVm.getId());
        if ((isApplied && applied) || (!isApplied && !applied)) {
            loadBalancerInstances.add(userVm);
            serviceStates.add(vmServiceState.get(userVm.getId()));
        }
    }
    return new Pair<List<? extends UserVm>, List<String>>(loadBalancerInstances, serviceStates);
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) Pair(com.cloud.utils.Pair)

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