}
private LoadBalancerConfiguration transform(Node loadBalancerNode) {
LoadBalancerConfiguration configuration = new LoadBalancerConfiguration();
if (loadBalancerNode == null || (!loadBalancerNode.getName().equals(Constants.CONF_ELEMENT_LOADBALANCER))) {
throw new InvalidConfigurationException("loadbalancer node was not found");
}
// Set load balancer properties
String defaultAlgorithm = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_ALGORITHM);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_ALGORITHM, defaultAlgorithm, "loadbalancer");
configuration.setDefaultAlgorithmName(defaultAlgorithm);
String failOver = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_FAILOVER);
if (StringUtils.isNotBlank(failOver)) {
configuration.setFailOverEnabled(Boolean.parseBoolean(failOver));
}
String sessionAffinity = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_SESSION_AFFINITY);
if (StringUtils.isNotBlank(sessionAffinity)) {
configuration.setSessionAffinityEnabled(Boolean.parseBoolean(sessionAffinity));
}
String endpointTimeout = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_ENDPOINT_TIMEOUT);
if (StringUtils.isNotBlank(endpointTimeout)) {
configuration.setEndpointTimeout(Long.parseLong(endpointTimeout));
} else {
// Endpoint timeout is not found, set default value
configuration.setEndpointTimeout(Constants.DEFAULT_ENDPOINT_TIMEOUT);
if(log.isWarnEnabled()) {
log.warn(String.format("Endpoint timeout not found, using default: %d", configuration.getEndpointTimeout()));
}
}
String sessionTimeout = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_SESSION_TIMEOUT);
if (StringUtils.isNotBlank(sessionTimeout)) {
configuration.setSessionTimeout(Long.parseLong(sessionTimeout));
} else {
// Session timeout is not found, set default value
configuration.setSessionTimeout(Constants.DEFAULT_SESSION_TIMEOUT);
if(log.isWarnEnabled()) {
log.warn(String.format("Session timeout not found, using default: %d", configuration.getSessionTimeout()));
}
}
String topologyEventListenerEnabled = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TOPOLOGY_EVENT_LISTENER);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_TOPOLOGY_EVENT_LISTENER, topologyEventListenerEnabled, Constants.CONF_ELEMENT_LOADBALANCER);
configuration.setTopologyEventListenerEnabled(Boolean.parseBoolean(topologyEventListenerEnabled));
if(configuration.isTopologyEventListenerEnabled()) {
String topologyMemberIpType = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TOPOLOGY_MEMBER_IP_TYPE);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_TOPOLOGY_MEMBER_IP_TYPE, topologyMemberIpType, Constants.CONF_ELEMENT_LOADBALANCER);
configuration.setTopologyMemberIpType(transformMemberIpType(topologyMemberIpType));
}
String statsPublisherEnabled = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_CEP_STATS_PUBLISHER);
if (StringUtils.isNotBlank(statsPublisherEnabled)) {
configuration.setCepStatsPublisherEnabled(Boolean.parseBoolean(statsPublisherEnabled));
}
String multiTenancyEnabled = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_MULTI_TENANCY);
if (StringUtils.isNotBlank(multiTenancyEnabled)) {
configuration.setMultiTenancyEnabled(Boolean.parseBoolean(multiTenancyEnabled));
}
// Read mb ip and port
if (configuration.isTopologyEventListenerEnabled() || configuration.isMultiTenancyEnabled()) {
String mbIp = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_MB_IP);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_MB_IP, mbIp, "loadbalancer");
configuration.setMbIp(mbIp);
String mbPort = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_MB_PORT);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_MB_PORT, mbPort, "loadbalancer");
configuration.setMbPort(Integer.parseInt(mbPort));
}
// Read topology service filter and topology cluster filter
if (configuration.isTopologyEventListenerEnabled()) {
String serviceFilter = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TOPOLOGY_SERVICE_FILTER);
if (StringUtils.isNotBlank(serviceFilter)) {
configuration.setTopologyServiceFilter(serviceFilter);
}
String clusterFilter = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TOPOLOGY_CLUSTER_FILTER);
if (StringUtils.isNotBlank(clusterFilter)) {
configuration.setTopologyClusterFilter(clusterFilter);
}
String memberFilter = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TOPOLOGY_MEMBER_FILTER);
if (StringUtils.isNotBlank(memberFilter)) {
configuration.setTopologyMemberFilter(memberFilter);
}
}
// Read cep ip and port if cep stats publisher is enabled
if (configuration.isCepStatsPublisherEnabled()) {
String cepIp = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_CEP_IP);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_CEP_IP, cepIp, "loadbalancer");
configuration.setCepIp(cepIp);
String cepPort = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_CEP_PORT);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_CEP_PORT, cepPort, "loadbalancer");
configuration.setCepPort(Integer.parseInt(cepPort));
String networkPartitionId = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_NETWORK_PARTITION_ID);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_NETWORK_PARTITION_ID, networkPartitionId, "loadbalancer");
configuration.setNetworkPartitionId(networkPartitionId);
}
if (configuration.isMultiTenancyEnabled()) {
String tenantIdentifierStr = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TENANT_IDENTIFIER);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_TENANT_IDENTIFIER, tenantIdentifierStr, "loadbalancer");
if (tenantIdentifierStr.equals(Constants.CONF_PROPERTY_VALUE_TENANT_ID)) {
configuration.setTenantIdentifier(TenantIdentifier.TenantId);
} else if (tenantIdentifierStr.equals(Constants.CONF_PROPERTY_VALUE_TENANT_DOMAIN)) {
configuration.setTenantIdentifier(TenantIdentifier.TenantDomain);
} else {
throw new InvalidConfigurationException(String.format("Tenant identifier %s is not valid", tenantIdentifierStr));
}
String tenantIdentifierRegex = loadBalancerNode.getProperty(Constants.CONF_PROPERTY_TENANT_IDENTIFIER_REGEX);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_TENANT_IDENTIFIER_REGEX, tenantIdentifierRegex, "loadbalancer");
try {
Pattern.compile(tenantIdentifierRegex);
} catch (Exception e) {
throw new InvalidConfigurationException(String.format("Invalid tenant identifier regular expression: %s", tenantIdentifierRegex), e);
}
configuration.setTenantIdentifierRegex(tenantIdentifierRegex);
}
Node algorithmsNode = loadBalancerNode.findChildNodeByName(Constants.CONF_ELEMENT_ALGORITHMS);
validateRequiredNode(loadBalancerNode, Constants.CONF_ELEMENT_ALGORITHMS);
for (Node algorithmNode : algorithmsNode.getChildNodes()) {
String className = algorithmNode.getProperty(Constants.CONF_PROPERTY_CLASS_NAME);
validateRequiredPropertyInNode(Constants.CONF_PROPERTY_CLASS_NAME, className, "algorithm", algorithmNode.getName());
Algorithm algorithm = new Algorithm(algorithmNode.getName(), className);
configuration.addAlgorithm(algorithm);
}
if (!configuration.isTopologyEventListenerEnabled()) {
Node servicesNode = loadBalancerNode.findChildNodeByName(Constants.CONF_ELEMENT_SERVICES);
validateRequiredNode(servicesNode, Constants.CONF_ELEMENT_SERVICES);
for (Node serviceNode : servicesNode.getChildNodes()) {
ServiceType serviceType = ServiceType.SingleTenant;
String multiTenant = serviceNode.getProperty(Constants.CONF_PROPERTY_MULTI_TENANT);
if (StringUtils.isNotBlank(multiTenant) && (Boolean.parseBoolean(multiTenant))) {
serviceType = ServiceType.MultiTenant;
}
Service service = new Service(serviceNode.getName(), serviceType);
Node clustersNode = serviceNode.findChildNodeByName(Constants.CONF_ELEMENT_CLUSTERS);
for (Node clusterNode : clustersNode.getChildNodes()) {
String clusterId = clusterNode.getName();
Cluster cluster = new Cluster(service.getServiceName(), clusterId, null, null);
String tenantRange = clusterNode.getProperty(Constants.CONF_PROPERTY_TENANT_RANGE);
if (StringUtils.isNotBlank(tenantRange)) {
if (service.getServiceType() != ServiceType.MultiTenant) {
throw new InvalidConfigurationException(String.format("%s property is not valid for non multi-tenant service cluster: [service] %s [cluster] %s",
Constants.CONF_PROPERTY_TENANT_RANGE, service.getServiceName(), cluster.getClusterId()));
}
cluster.setTenantRange(tenantRange);
}