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));
}
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());
}
}
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;
}
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;
}
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"));
}
}
Aggregations