Search in sources :

Example 1 with StaticNatServiceProvider

use of com.cloud.network.element.StaticNatServiceProvider in project cloudstack by apache.

the class IpAddressManagerImpl method applyStaticNats.

@Override
public boolean applyStaticNats(List<? extends StaticNat> staticNats, boolean continueOnError, boolean forRevoke) throws ResourceUnavailableException {
    if (staticNats == null || staticNats.size() == 0) {
        s_logger.debug("There are no static nat rules for the network elements");
        return true;
    }
    Network network = _networksDao.findById(staticNats.get(0).getNetworkId());
    boolean success = true;
    // Check if the StaticNat service is supported
    if (!_networkModel.areServicesSupportedInNetwork(network.getId(), Service.StaticNat)) {
        s_logger.debug("StaticNat service is not supported in specified network id");
        return true;
    }
    List<IPAddressVO> userIps = getStaticNatSourceIps(staticNats);
    List<PublicIp> publicIps = new ArrayList<PublicIp>();
    if (userIps != null && !userIps.isEmpty()) {
        for (IPAddressVO userIp : userIps) {
            PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
            publicIps.add(publicIp);
        }
    }
    // association for the network so as to ensure IP is associated before applying rules
    if (checkStaticNatIPAssocRequired(network, false, forRevoke, publicIps)) {
        applyIpAssociations(network, false, continueOnError, publicIps);
    }
    // get provider
    StaticNatServiceProvider element = _networkMgr.getStaticNatProviderForNetwork(network);
    try {
        success = element.applyStaticNats(network, staticNats);
    } catch (ResourceUnavailableException e) {
        if (!continueOnError) {
            throw e;
        }
        s_logger.warn("Problems with " + element.getName() + " but pushing on", e);
        success = false;
    }
    // For revoked static nat IP, set the vm_id to null, indicate it should be revoked
    for (StaticNat staticNat : staticNats) {
        if (staticNat.isForRevoke()) {
            for (PublicIp publicIp : publicIps) {
                if (publicIp.getId() == staticNat.getSourceIpAddressId()) {
                    publicIps.remove(publicIp);
                    IPAddressVO ip = _ipAddressDao.findByIdIncludingRemoved(staticNat.getSourceIpAddressId());
                    // ip can't be null, otherwise something wrong happened
                    ip.setAssociatedWithVmId(null);
                    publicIp = PublicIp.createFromAddrAndVlan(ip, _vlanDao.findById(ip.getVlanId()));
                    publicIps.add(publicIp);
                    break;
                }
            }
        }
    }
    // if the static NAT rules configured on public IP is revoked then, dis-associate IP with static NAT service provider
    if (checkStaticNatIPAssocRequired(network, true, forRevoke, publicIps)) {
        applyIpAssociations(network, true, continueOnError, publicIps);
    }
    return success;
}
Also used : PublicIp(com.cloud.network.addr.PublicIp) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) StaticNat(com.cloud.network.rules.StaticNat)

Example 2 with StaticNatServiceProvider

use of com.cloud.network.element.StaticNatServiceProvider in project cloudstack by apache.

the class ExternalLoadBalancerDeviceManagerImpl method applyStaticNatRuleForInlineLBRule.

private void applyStaticNatRuleForInlineLBRule(DataCenterVO zone, Network network, boolean revoked, String publicIp, String privateIp) throws ResourceUnavailableException {
    List<StaticNat> staticNats = new ArrayList<StaticNat>();
    IPAddressVO ipVO = _ipAddressDao.listByDcIdIpAddress(zone.getId(), publicIp).get(0);
    StaticNatImpl staticNat = new StaticNatImpl(ipVO.getAllocatedToAccountId(), ipVO.getAllocatedInDomainId(), network.getId(), ipVO.getId(), privateIp, revoked);
    staticNats.add(staticNat);
    StaticNatServiceProvider element = _networkMgr.getStaticNatProviderForNetwork(network);
    element.applyStaticNats(network, staticNats);
}
Also used : StaticNatImpl(com.cloud.network.rules.StaticNatImpl) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) ArrayList(java.util.ArrayList) IPAddressVO(com.cloud.network.dao.IPAddressVO) StaticNat(com.cloud.network.rules.StaticNat)

Example 3 with StaticNatServiceProvider

use of com.cloud.network.element.StaticNatServiceProvider in project cloudstack by apache.

the class VpcManagerImpl method applyStaticRoutes.

protected boolean applyStaticRoutes(final List<StaticRouteProfile> routes) throws ResourceUnavailableException {
    if (routes.isEmpty()) {
        s_logger.debug("No static routes to apply");
        return true;
    }
    final Vpc vpc = _vpcDao.findById(routes.get(0).getVpcId());
    s_logger.debug("Applying static routes for vpc " + vpc);
    final String staticNatProvider = _vpcSrvcDao.getProviderForServiceInVpc(vpc.getId(), Service.StaticNat);
    for (final VpcProvider provider : getVpcElements()) {
        if (!(provider instanceof StaticNatServiceProvider && provider.getName().equalsIgnoreCase(staticNatProvider))) {
            continue;
        }
        if (provider.applyStaticRoutes(vpc, routes)) {
            s_logger.debug("Applied static routes for vpc " + vpc);
        } else {
            s_logger.warn("Failed to apply static routes for vpc " + vpc);
            return false;
        }
    }
    return true;
}
Also used : VpcProvider(com.cloud.network.element.VpcProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider)

Aggregations

StaticNatServiceProvider (com.cloud.network.element.StaticNatServiceProvider)3 IPAddressVO (com.cloud.network.dao.IPAddressVO)2 StaticNat (com.cloud.network.rules.StaticNat)2 ArrayList (java.util.ArrayList)2 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 PublicIp (com.cloud.network.addr.PublicIp)1 VpcProvider (com.cloud.network.element.VpcProvider)1 StaticNatImpl (com.cloud.network.rules.StaticNatImpl)1