Search in sources :

Example 21 with LoadBalancerVO

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

the class GlobalLoadBalancingRulesServiceImplTest method runRemoveFromGlobalLoadBalancerRuleTest.

void runRemoveFromGlobalLoadBalancerRuleTest() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runRemoveFromGlobalLoadBalancerRuleTest");
    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);
    List<GslbServiceProvider> mockGslbProviders = new ArrayList<GslbServiceProvider>();
    mockGslbProviders.add(Mockito.mock(GslbServiceProvider.class));
    gslbServiceImpl._gslbProviders = mockGslbProviders;
    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));
    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(removeFromGslbCmd, lbRuleIds);
    NetworkVO networkVo = new NetworkVO();
    Field dcID = NetworkVO.class.getDeclaredField("dataCenterId");
    dcID.setAccessible(true);
    dcID.set(networkVo, new Long(1));
    Field phyNetworkId = NetworkVO.class.getDeclaredField("physicalNetworkId");
    phyNetworkId.setAccessible(true);
    phyNetworkId.set(networkVo, new Long(200));
    when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo);
    GlobalLoadBalancerLbRuleMapVO gslbLbMap = new GlobalLoadBalancerLbRuleMapVO(1, 1, 1);
    List<GlobalLoadBalancerLbRuleMapVO> listSslbLbMap = new ArrayList<GlobalLoadBalancerLbRuleMapVO>();
    listSslbLbMap.add(gslbLbMap);
    when(gslbServiceImpl._gslbLbMapDao.listByGslbRuleId(new Long(1))).thenReturn(listSslbLbMap);
    when(gslbServiceImpl._gslbLbMapDao.findByGslbRuleIdAndLbRuleId(new Long(1), new Long(1))).thenReturn(gslbLbMap);
    IPAddressVO ip = new IPAddressVO(new Ip("10.1.1.1"), 1, 1, 1, true);
    when(gslbServiceImpl._ipAddressDao.findById(new Long(1))).thenReturn(ip);
    gslbServiceImpl.removeFromGlobalLoadBalancerRule(removeFromGslbCmd);
}
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) Ip(com.cloud.utils.net.Ip) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) AccountVO(com.cloud.user.AccountVO) Field(java.lang.reflect.Field) 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) IPAddressVO(com.cloud.network.dao.IPAddressVO) RegionDao(org.apache.cloudstack.region.dao.RegionDao)

Example 22 with LoadBalancerVO

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

the class GlobalLoadBalancingRulesServiceImplTest method runAssignToGlobalLoadBalancerRuleTestSameZoneLb.

void runAssignToGlobalLoadBalancerRuleTestSameZoneLb() throws Exception {
    TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTestSameZoneLb");
    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", 3, "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, 3, 1, GlobalLoadBalancerRule.State.Active);
    when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
    LoadBalancerVO lbRule1 = new LoadBalancerVO();
    lbRule1.setState(FirewallRule.State.Active);
    Field networkIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
    Field accountIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("accountId");
    Field domainIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("domainId");
    networkIdField1.setAccessible(true);
    accountIdField1.setAccessible(true);
    domainIdField1.setAccessible(true);
    networkIdField1.set(lbRule1, new Long(1));
    accountIdField1.set(lbRule1, new Long(3));
    domainIdField1.set(lbRule1, new Long(1));
    Field idField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
    idField1.setAccessible(true);
    idField1.set(lbRule1, new Long(1));
    LoadBalancerVO lbRule2 = new LoadBalancerVO();
    lbRule2.setState(FirewallRule.State.Active);
    Field networkIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
    Field accountIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("accountId");
    Field domainIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("domainId");
    networkIdField2.setAccessible(true);
    accountIdField2.setAccessible(true);
    domainIdField2.setAccessible(true);
    networkIdField2.set(lbRule2, new Long(1));
    accountIdField2.set(lbRule2, new Long(3));
    domainIdField2.set(lbRule2, new Long(1));
    Field idField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
    idField2.setAccessible(true);
    idField2.set(lbRule2, new Long(2));
    when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule1);
    when(gslbServiceImpl._lbDao.findById(new Long(2))).thenReturn(lbRule2);
    Field lbRules = _class.getDeclaredField("loadBalancerRulesIds");
    lbRules.setAccessible(true);
    List<Long> lbRuleIds = new ArrayList<Long>();
    lbRuleIds.add(new Long(1));
    lbRuleIds.add(new Long(2));
    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) {
        s_logger.info(e.getMessage());
        Assert.assertTrue(e.getMessage().contains("Load balancer rule specified should be in unique zone"));
    }
}
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)

Example 23 with LoadBalancerVO

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

the class UpdateLoadBalancerTest method testRuleNotValidated.

@Test(expected = InvalidParameterValueException.class)
public void testRuleNotValidated() 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(false);
    _lbMgr.updateLoadBalancerRule(updateLbRuleCmd);
}
Also used : PublicIpAddress(com.cloud.network.PublicIpAddress) NetworkVO(com.cloud.network.dao.NetworkVO) Network(com.cloud.network.Network) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) Test(org.junit.Test)

Example 24 with LoadBalancerVO

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

the class AssignLoadBalancerTest method tesSecIpNotSetToVm.

@Test(expected = InvalidParameterValueException.class)
public void tesSecIpNotSetToVm() 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);
    _lbMgr._lbDao = lbDao;
    _lbMgr._lb2VmMapDao = lb2VmMapDao;
    _lbMgr._vmDao = userVmDao;
    _lbMgr._nicSecondaryIpDao = nicSecIpDao;
    _lbvmMapList = new ArrayList<>();
    _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) UserVmDao(com.cloud.vm.dao.UserVmDao) Test(org.junit.Test)

Example 25 with LoadBalancerVO

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

the class CertServiceImpl method deleteSslCert.

@DB
@Override
@ActionEvent(eventType = EventTypes.EVENT_LB_CERT_DELETE, eventDescription = "Deleting a certificate to cloudstack", async = false)
public void deleteSslCert(final DeleteSslCertCmd deleteSslCertCmd) {
    Preconditions.checkNotNull(deleteSslCertCmd);
    final CallContext ctx = CallContext.current();
    final Account caller = ctx.getCallingAccount();
    final Long certId = deleteSslCertCmd.getId();
    final SslCertVO certVO = _sslCertDao.findById(certId);
    if (certVO == null) {
        throw new InvalidParameterValueException("Invalid certificate id: " + certId);
    }
    _accountMgr.checkAccess(caller, SecurityChecker.AccessType.OperateEntry, true, certVO);
    final List<LoadBalancerCertMapVO> lbCertRule = _lbCertDao.listByCertId(certId);
    if (lbCertRule != null && !lbCertRule.isEmpty()) {
        String lbUuids = "";
        for (final LoadBalancerCertMapVO rule : lbCertRule) {
            final LoadBalancerVO lb = _entityMgr.findById(LoadBalancerVO.class, rule.getLbId());
            lbUuids += " " + lb.getUuid();
        }
        throw new CloudRuntimeException("Certificate in use by a loadbalancer(s)" + lbUuids);
    }
    _sslCertDao.remove(certId);
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SslCertVO(com.cloud.network.dao.SslCertVO) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LoadBalancerCertMapVO(com.cloud.network.dao.LoadBalancerCertMapVO) CallContext(org.apache.cloudstack.context.CallContext) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

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