Search in sources :

Example 11 with StaticNat

use of com.cloud.network.rules.StaticNat in project cloudstack by apache.

the class NiciraNvpElement method applyStaticNats.

/**
     * From interface StaticNatServiceProvider
     */
@Override
public boolean applyStaticNats(Network network, List<? extends StaticNat> rules) throws ResourceUnavailableException {
    if (!canHandle(network, Service.StaticNat)) {
        return false;
    }
    List<NiciraNvpDeviceVO> devices = niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
    if (devices.isEmpty()) {
        s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId());
        return false;
    }
    NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
    HostVO niciraNvpHost = hostDao.findById(niciraNvpDevice.getHostId());
    NiciraNvpRouterMappingVO routermapping = niciraNvpRouterMappingDao.findByNetworkId(network.getId());
    if (routermapping == null) {
        s_logger.error("No logical router uuid found for network " + network.getDisplayText());
        return false;
    }
    List<StaticNatRuleTO> staticNatRules = new ArrayList<StaticNatRuleTO>();
    for (StaticNat rule : rules) {
        IpAddress sourceIp = networkModel.getIp(rule.getSourceIpAddressId());
        // Force the nat rule into the StaticNatRuleTO, no use making a new TO object
        // we only need the source and destination ip. Unfortunately no mention if a rule
        // is new.
        StaticNatRuleTO ruleTO = new StaticNatRuleTO(1, sourceIp.getAddress().addr(), MIN_PORT, MAX_PORT, rule.getDestIpAddress(), MIN_PORT, MAX_PORT, "any", rule.isForRevoke(), false);
        staticNatRules.add(ruleTO);
    }
    ConfigureStaticNatRulesOnLogicalRouterCommand cmd = new ConfigureStaticNatRulesOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), staticNatRules);
    ConfigureStaticNatRulesOnLogicalRouterAnswer answer = (ConfigureStaticNatRulesOnLogicalRouterAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
    return answer.getResult();
}
Also used : StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand) NiciraNvpRouterMappingVO(com.cloud.network.NiciraNvpRouterMappingVO) NiciraNvpDeviceVO(com.cloud.network.NiciraNvpDeviceVO) ArrayList(java.util.ArrayList) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) ConfigureStaticNatRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer) HostVO(com.cloud.host.HostVO) StaticNat(com.cloud.network.rules.StaticNat)

Example 12 with StaticNat

use of com.cloud.network.rules.StaticNat 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 13 with StaticNat

use of com.cloud.network.rules.StaticNat 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)

Aggregations

StaticNat (com.cloud.network.rules.StaticNat)13 ArrayList (java.util.ArrayList)10 IpAddress (com.cloud.network.IpAddress)6 PublicIpAddress (com.cloud.network.PublicIpAddress)6 StaticNatRuleTO (com.cloud.agent.api.to.StaticNatRuleTO)5 HostVO (com.cloud.host.HostVO)5 SetStaticNatRulesCommand (com.cloud.agent.api.routing.SetStaticNatRulesCommand)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 IPAddressVO (com.cloud.network.dao.IPAddressVO)4 Answer (com.cloud.agent.api.Answer)3 PublicIp (com.cloud.network.addr.PublicIp)3 StaticNatImpl (com.cloud.network.rules.StaticNatImpl)3 Ip (com.cloud.utils.net.Ip)3 DataCenterVO (com.cloud.dc.DataCenterVO)2 VlanVO (com.cloud.dc.VlanVO)2 Network (com.cloud.network.Network)2 CiscoVnmcControllerVO (com.cloud.network.cisco.CiscoVnmcControllerVO)2 NetworkAsa1000vMapVO (com.cloud.network.cisco.NetworkAsa1000vMapVO)2 StaticNatServiceProvider (com.cloud.network.element.StaticNatServiceProvider)2 Test (org.junit.Test)2