ExternalLoadBalancerDeviceManagerImpl extends AdapterBase implements ExternalLoadBalancerDeviceManager, ResourceStateAdapter { @Override public List<LoadBalancerTO> getLBHealthChecks(Network network, List<LoadBalancingRule> loadBalancingRules) throws ResourceUnavailableException { long zoneId = network.getDataCenterId(); DataCenterVO zone = _dcDao.findById(zoneId); if (loadBalancingRules == null || loadBalancingRules.isEmpty()) { return null; } ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network); if (lbDeviceVO == null) { s_logger.warn("There is no external load balancer device assigned to this network either network is not implement are already shutdown so just returning"); return null; } HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId()); boolean externalLoadBalancerIsInline = _networkMgr.isNetworkInlineMode(network); if (network.getState() == Network.State.Allocated) { s_logger.debug("External load balancer was asked to apply LB rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands."); return null; } List<LoadBalancerTO> loadBalancersToApply = new ArrayList<LoadBalancerTO>(); List<MappingState> mappingStates = new ArrayList<MappingState>(); for (final LoadBalancingRule rule : loadBalancingRules) { boolean revoked = (FirewallRule.State.Revoke.equals(rule.getState())); String protocol = rule.getProtocol(); String algorithm = rule.getAlgorithm(); String uuid = rule.getUuid(); String srcIp = rule.getSourceIp().addr(); int srcPort = rule.getSourcePortStart(); List<LbDestination> destinations = rule.getDestinations(); if (externalLoadBalancerIsInline) { long sourceIpId = _networkModel.getPublicIpAddress(rule.getSourceIp().addr(), network.getDataCenterId()).getId(); MappingNic nic = getLoadBalancingIpNic(zone, network, sourceIpId, revoked, null); mappingStates.add(nic.getState()); Nic loadBalancingIpNic = nic.getNic(); if (loadBalancingIpNic == null) { continue; } srcIp = loadBalancingIpNic.getIPv4Address(); } if ((destinations != null && !destinations.isEmpty()) || !rule.isAutoScaleConfig()) { boolean inline = _networkMgr.isNetworkInlineMode(network); LoadBalancerTO loadBalancer = new LoadBalancerTO(uuid, srcIp, srcPort, protocol, algorithm, revoked, false, inline, destinations, rule.getStickinessPolicies(), rule.getHealthCheckPolicies(), rule.getLbSslCert(), rule.getLbProtocol()); loadBalancersToApply.add(loadBalancer); } } try { if (loadBalancersToApply.size() > 0) { int numLoadBalancersForCommand = loadBalancersToApply.size(); LoadBalancerTO[] loadBalancersForCommand = loadBalancersToApply.toArray(new LoadBalancerTO[numLoadBalancersForCommand]); HealthCheckLBConfigCommand cmd = new HealthCheckLBConfigCommand(loadBalancersForCommand); long guestVlanTag = Integer.parseInt(BroadcastDomainType.getValue(network.getBroadcastUri())); cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, String.valueOf(guestVlanTag)); HealthCheckLBConfigAnswer answer = (HealthCheckLBConfigAnswer) _agentMgr .easySend(externalLoadBalancer.getId(), cmd); return answer == null ? null : answer.getLoadBalancers(); } } catch (Exception ex) { s_logger.error("Exception Occured ", ex); } return null; }  @Override @DB ExternalLoadBalancerDeviceVO addExternalLoadBalancer(long physicalNetworkId, String url, String username, String password, final String deviceName,
        ServerResource resource, final boolean gslbProvider, final boolean exclusiveGslbProivider,
        final String gslbSitePublicIp, final String gslbSitePrivateIp); @Override boolean deleteExternalLoadBalancer(long hostId); @Override List<Host> listExternalLoadBalancers(long physicalNetworkId, String deviceName); ExternalLoadBalancerResponse createExternalLoadBalancerResponse(Host externalLoadBalancer); String getExternalLoadBalancerResourceGuid(long physicalNetworkId, String deviceName, String ip); @Override ExternalLoadBalancerDeviceVO getExternalLoadBalancerForNetwork(Network network); void setExternalLoadBalancerForNetwork(Network network, long externalLBDeviceID); @Override ExternalLoadBalancerDeviceVO findSuitableLoadBalancerForNetwork(Network network, boolean dedicatedLb); @Override boolean applyLoadBalancerRules(Network network, List<LoadBalancingRule> loadBalancingRules); @Override boolean manageGuestNetworkWithExternalLoadBalancer(boolean add, Network guestConfig); @Override boolean configure(String name, Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override HostVO createHostVOForConnectedAgent(HostVO host, StartupCommand[] cmd); @Override HostVO createHostVOForDirectConnectAgent(HostVO host, StartupCommand[] startup, ServerResource resource, Map<String, String> details, List<String> hostTags); @Override DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForceDeleteStorage); @Override List<LoadBalancerTO> getLBHealthChecks(Network network, List<LoadBalancingRule> loadBalancingRules);  }
@Test public void getLBHealthChecks() throws ResourceUnavailableException, URISyntaxException { setupLBHealthChecksMocks(); HealthCheckLBConfigAnswer answer = Mockito .mock(HealthCheckLBConfigAnswer.class); Mockito.when(answer.getLoadBalancers()).thenReturn( Collections.<LoadBalancerTO> emptyList()); Mockito.when( _agentMgr.easySend(Mockito.anyLong(), Mockito.any(Command.class))).thenReturn(answer); Assert.assertNotNull(externalLoadBalancerDeviceManager .getLBHealthChecks(network, Arrays.asList(rule))); }
@Test public void getLBHealthChecksNullAnswer() throws ResourceUnavailableException, URISyntaxException { setupLBHealthChecksMocks(); Mockito.when( _agentMgr.easySend(Mockito.anyLong(), Mockito.any(Command.class))).thenReturn(null); Assert.assertNull(externalLoadBalancerDeviceManager .getLBHealthChecks(network, Arrays.asList(rule))); }