Search in sources :

Example 1 with SiteLoadBalancerConfig

use of com.cloud.agent.api.routing.SiteLoadBalancerConfig in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method applyGlobalLoadBalancerRuleConfig.

private boolean applyGlobalLoadBalancerRuleConfig(long gslbRuleId, boolean revoke) throws ResourceUnavailableException {
    GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId);
    assert (gslbRule != null);
    String lbMethod = gslbRule.getAlgorithm();
    String persistenceMethod = gslbRule.getPersistence();
    String serviceType = gslbRule.getServiceType();
    // each Gslb rule will have a FQDN, formed from the domain name associated with the gslb rule
    // and the deployment DNS name configured in global config parameter 'cloud.dns.name'
    String domainName = gslbRule.getGslbDomain();
    String providerDnsName = _globalConfigDao.getValue(Config.CloudDnsName.key());
    String gslbFqdn = domainName + "." + providerDnsName;
    GlobalLoadBalancerConfigCommand gslbConfigCmd = new GlobalLoadBalancerConfigCommand(gslbFqdn, lbMethod, persistenceMethod, serviceType, gslbRuleId, revoke);
    // list of the physical network participating in global load balancing
    List<Pair<Long, Long>> gslbSiteIds = new ArrayList<Pair<Long, Long>>();
    // map of the zone and info corresponding to the load balancer configured in the zone
    Map<Long, SiteLoadBalancerConfig> zoneSiteLoadbalancerMap = new HashMap<Long, SiteLoadBalancerConfig>();
    List<GlobalLoadBalancerLbRuleMapVO> gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId);
    assert (gslbLbMapVos != null && !gslbLbMapVos.isEmpty());
    for (GlobalLoadBalancerLbRuleMapVO gslbLbMapVo : gslbLbMapVos) {
        // get the zone in which load balancer rule is deployed
        LoadBalancerVO loadBalancer = _lbDao.findById(gslbLbMapVo.getLoadBalancerId());
        Network network = _networkDao.findById(loadBalancer.getNetworkId());
        long dataCenterId = network.getDataCenterId();
        long physicalNetworkId = network.getPhysicalNetworkId();
        gslbSiteIds.add(new Pair<Long, Long>(dataCenterId, physicalNetworkId));
        IPAddressVO ip = _ipAddressDao.findById(loadBalancer.getSourceIpAddressId());
        SiteLoadBalancerConfig siteLb = new SiteLoadBalancerConfig(gslbLbMapVo.isRevoke(), serviceType, ip.getAddress().addr(), Integer.toString(loadBalancer.getDefaultPortStart()), dataCenterId);
        GslbServiceProvider gslbProvider = lookupGslbServiceProvider();
        if (gslbProvider == null) {
            throw new CloudRuntimeException("No GSLB provider is available");
        }
        siteLb.setGslbProviderPublicIp(gslbProvider.getZoneGslbProviderPublicIp(dataCenterId, physicalNetworkId));
        siteLb.setGslbProviderPrivateIp(gslbProvider.getZoneGslbProviderPrivateIp(dataCenterId, physicalNetworkId));
        siteLb.setWeight(gslbLbMapVo.getWeight());
        zoneSiteLoadbalancerMap.put(network.getDataCenterId(), siteLb);
    }
    // to the corresponding GSLB service provider in that zone
    for (Pair<Long, Long> zoneId : gslbSiteIds) {
        List<SiteLoadBalancerConfig> slbs = new ArrayList<SiteLoadBalancerConfig>();
        // set site as 'local' for the site in that zone
        for (Pair<Long, Long> innerLoopZoneId : gslbSiteIds) {
            SiteLoadBalancerConfig siteLb = zoneSiteLoadbalancerMap.get(innerLoopZoneId.first());
            siteLb.setLocal(zoneId.first() == innerLoopZoneId.first());
            slbs.add(siteLb);
        }
        gslbConfigCmd.setSiteLoadBalancers(slbs);
        gslbConfigCmd.setForRevoke(revoke);
        // revoke GSLB configuration completely on the site GSLB provider for the sites that no longer
        // are participants of a GSLB rule
        SiteLoadBalancerConfig siteLb = zoneSiteLoadbalancerMap.get(zoneId.first());
        if (siteLb.forRevoke()) {
            gslbConfigCmd.setForRevoke(true);
        }
        try {
            lookupGslbServiceProvider().applyGlobalLoadBalancerRule(zoneId.first(), zoneId.second(), gslbConfigCmd);
        } catch (ResourceUnavailableException | NullPointerException e) {
            String msg = "Failed to configure GSLB rule in the zone " + zoneId.first() + " due to " + e.getMessage();
            s_logger.warn(msg);
            throw new CloudRuntimeException(msg);
        }
    }
    return true;
}
Also used : GlobalLoadBalancerConfigCommand(com.cloud.agent.api.routing.GlobalLoadBalancerConfigCommand) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) SiteLoadBalancerConfig(com.cloud.agent.api.routing.SiteLoadBalancerConfig) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) Pair(com.cloud.utils.Pair)

Example 2 with SiteLoadBalancerConfig

use of com.cloud.agent.api.routing.SiteLoadBalancerConfig in project cloudstack by apache.

the class NetscalerResource method execute.

private Answer execute(final GlobalLoadBalancerConfigCommand gslbCmd, final int numRetries) {
    final String lbMethod = gslbCmd.getLoadBalancerMethod();
    final String persistenceType = gslbCmd.getPersistenceType();
    final String serviceType = gslbCmd.getServiceType();
    final boolean forRevoke = gslbCmd.isForRevoke();
    final long gslbId = gslbCmd.getGslbId();
    final List<SiteLoadBalancerConfig> sites = gslbCmd.getSiteDetails();
    final String domainName = gslbCmd.getDomainName();
    final String vserverName = GSLB.generateVirtualServerName(domainName);
    try {
        if (!forRevoke) {
            // check if the global load balancer rule is being added
            // Add a GSLB virtual server
            GSLB.createVirtualServer(_netscalerService, vserverName, lbMethod, persistenceType, gslbId, serviceType);
            if (sites != null) {
                // check if there are any sites that are participating in global load balancing
                for (final SiteLoadBalancerConfig site : sites) {
                    final String sitePrivateIP = site.getGslbProviderPrivateIp();
                    final String sitePublicIP = site.getGslbProviderPublicIp();
                    final String servicePublicIp = site.getServicePublicIp();
                    final String servicePublicPort = site.getServicePort();
                    final String siteName = GSLB.generateUniqueSiteName(sitePrivateIP, sitePublicIP, site.getDataCenterId());
                    // Add/Delete GSLB local and remote sites that are part of GSLB virtual server
                    if (!site.forRevoke()) {
                        final String siteType = site.isLocal() ? "LOCAL" : "REMOTE";
                        if (GSLB.getSiteObject(_netscalerService, siteName) != null) {
                            GSLB.updateSite(_netscalerService, siteType, siteName, site.getGslbProviderPrivateIp(), site.getGslbProviderPublicIp());
                        } else {
                            GSLB.createSite(_netscalerService, siteName, siteType, site.getGslbProviderPrivateIp(), site.getGslbProviderPublicIp());
                        }
                    }
                    // Add/Delete GSLB service corresponding the service running on each site
                    final String serviceName = GSLB.generateUniqueServiceName(siteName, servicePublicIp, servicePublicPort);
                    final String monitorName = GSLB.generateGslbServiceMonitorName(servicePublicIp);
                    if (!site.forRevoke()) {
                        // create a 'gslbservice' object
                        GSLB.createService(_netscalerService, serviceName, site.getServiceType(), servicePublicIp, servicePublicPort, siteName);
                        // Bind 'gslbservice' service object to GSLB virtual server
                        GSLB.createVserverServiceBinding(_netscalerService, serviceName, vserverName, site.getWeight());
                        // create a monitor for the service running on the site
                        GSLB.createGslbServiceMonitor(_netscalerService, servicePublicIp, serviceName);
                        // bind the monitor to the GSLB service
                        GSLB.createGslbServiceGslbMonitorBinding(_netscalerService, monitorName, serviceName);
                    } else {
                        // delete GSLB service and GSLB monitor binding
                        GSLB.deleteGslbServiceGslbMonitorBinding(_netscalerService, monitorName, serviceName);
                        // Unbind GSLB service with GSLB virtual server
                        GSLB.deleteVserverServiceBinding(_netscalerService, serviceName, vserverName);
                        GSLB.getServiceObject(_netscalerService, serviceName);
                        GSLB.deleteService(_netscalerService, serviceName);
                        // delete the GSLB service monitor
                        GSLB.deleteGslbServiceMonitor(_netscalerService, monitorName);
                    }
                    if (site.forRevoke()) {
                        // delete the site if its for revoke
                        GSLB.deleteSite(_netscalerService, siteName);
                    }
                }
            }
            // Bind GSLB vserver to domain
            GSLB.createVserverDomainBinding(_netscalerService, vserverName, domainName);
        } else {
            // global load balancer rule is being deleted, so clean up all objects created
            // remove binding between virtual server and the domain name
            GSLB.deleteVserverDomainBinding(_netscalerService, vserverName, domainName);
            if (sites != null) {
                for (final SiteLoadBalancerConfig site : sites) {
                    final String sitePrivateIP = site.getGslbProviderPrivateIp();
                    final String sitePublicIP = site.getGslbProviderPublicIp();
                    final String servicePublicIp = site.getServicePublicIp();
                    final String servicePublicPort = site.getServicePort();
                    final String siteName = GSLB.generateUniqueSiteName(sitePrivateIP, sitePublicIP, site.getDataCenterId());
                    final String serviceName = GSLB.generateUniqueServiceName(siteName, servicePublicIp, servicePublicPort);
                    final String monitorName = GSLB.generateGslbServiceMonitorName(servicePublicIp);
                    // delete GSLB service and GSLB monitor binding
                    GSLB.deleteGslbServiceGslbMonitorBinding(_netscalerService, monitorName, serviceName);
                    // remove binding between virtual server and services
                    GSLB.deleteVserverServiceBinding(_netscalerService, serviceName, vserverName);
                    // delete service object
                    GSLB.deleteService(_netscalerService, serviceName);
                    // delete GSLB site object
                    GSLB.deleteSite(_netscalerService, siteName);
                    // delete the GSLB service monitor
                    GSLB.deleteGslbServiceMonitor(_netscalerService, monitorName);
                }
            }
            // delete GSLB virtual server
            GSLB.deleteVirtualServer(_netscalerService, vserverName);
        }
        saveConfiguration();
    } catch (final Exception e) {
        final String errMsg = "Failed to apply GSLB configuration due to " + e.getMessage();
        if (shouldRetry(numRetries)) {
            return retry(gslbCmd, numRetries);
        }
        return new GlobalLoadBalancerConfigAnswer(false, errMsg);
    }
    return new GlobalLoadBalancerConfigAnswer(true, "Successfully applied GSLB configuration.");
}
Also used : GlobalLoadBalancerConfigAnswer(com.cloud.agent.api.routing.GlobalLoadBalancerConfigAnswer) ExecutionException(com.cloud.utils.exception.ExecutionException) IOException(java.io.IOException) ConfigurationException(javax.naming.ConfigurationException) SiteLoadBalancerConfig(com.cloud.agent.api.routing.SiteLoadBalancerConfig)

Aggregations

SiteLoadBalancerConfig (com.cloud.agent.api.routing.SiteLoadBalancerConfig)2 GlobalLoadBalancerConfigAnswer (com.cloud.agent.api.routing.GlobalLoadBalancerConfigAnswer)1 GlobalLoadBalancerConfigCommand (com.cloud.agent.api.routing.GlobalLoadBalancerConfigCommand)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 Network (com.cloud.network.Network)1 IPAddressVO (com.cloud.network.dao.IPAddressVO)1 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)1 Pair (com.cloud.utils.Pair)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ExecutionException (com.cloud.utils.exception.ExecutionException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConfigurationException (javax.naming.ConfigurationException)1