Search in sources :

Example 1 with StaticNatRuleTO

use of com.cloud.agent.api.to.StaticNatRuleTO in project cloudstack by apache.

the class SetStaticNatRulesConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final SetStaticNatRulesCommand command = (SetStaticNatRulesCommand) cmd;
    final LinkedList<StaticNatRule> rules = new LinkedList<>();
    for (final StaticNatRuleTO rule : command.getRules()) {
        final StaticNatRule staticNatRule = new StaticNatRule(rule.revoked(), rule.getProtocol(), rule.getSrcIp(), rule.getStringSrcPortRange(), rule.getDstIp());
        rules.add(staticNatRule);
    }
    final StaticNatRules staticNatRules = new StaticNatRules(rules);
    return generateConfigItems(staticNatRules);
}
Also used : StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) SetStaticNatRulesCommand(com.cloud.agent.api.routing.SetStaticNatRulesCommand) StaticNatRules(com.cloud.agent.resource.virtualnetwork.model.StaticNatRules) StaticNatRule(com.cloud.agent.resource.virtualnetwork.model.StaticNatRule) LinkedList(java.util.LinkedList)

Example 2 with StaticNatRuleTO

use of com.cloud.agent.api.to.StaticNatRuleTO in project cloudstack by apache.

the class JuniperSrxResource method execute.

private Answer execute(SetStaticNatRulesCommand cmd, int numRetries) {
    StaticNatRuleTO[] allRules = cmd.getRules();
    Map<String, ArrayList<FirewallRuleTO>> activeRules = getActiveRules(allRules);
    Map<String, String> vlanTagMap = getVlanTagMap(allRules);
    try {
        openConfiguration();
        Set<String> ipPairs = activeRules.keySet();
        for (String ipPair : ipPairs) {
            String[] ipPairComponents = ipPair.split("-");
            String publicIp = ipPairComponents[0];
            String privateIp = ipPairComponents[1];
            List<FirewallRuleTO> activeRulesForIpPair = activeRules.get(ipPair);
            Long publicVlanTag = getVlanTag(vlanTagMap.get(publicIp));
            // Delete the existing static NAT rule for this IP pair
            removeStaticNatRule(publicVlanTag, publicIp, privateIp);
            if (activeRulesForIpPair.size() > 0) {
                // If there are active FirewallRules for this IP pair, add the static NAT rule and open the specified port ranges
                addStaticNatRule(publicVlanTag, publicIp, privateIp, activeRulesForIpPair);
            }
        }
        commitConfiguration();
        return new Answer(cmd);
    } catch (ExecutionException e) {
        s_logger.error(e);
        closeConfiguration();
        if (numRetries > 0 && refreshSrxConnection()) {
            int numRetriesRemaining = numRetries - 1;
            s_logger.debug("Retrying SetPortForwardingRulesCommand. Number of retries remaining: " + numRetriesRemaining);
            return execute(cmd, numRetriesRemaining);
        } else {
            return new Answer(cmd, e);
        }
    }
}
Also used : StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) ArrayList(java.util.ArrayList) FirewallRuleTO(com.cloud.agent.api.to.FirewallRuleTO) Answer(com.cloud.agent.api.Answer) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) ExternalNetworkResourceUsageAnswer(com.cloud.agent.api.ExternalNetworkResourceUsageAnswer) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 3 with StaticNatRuleTO

use of com.cloud.agent.api.to.StaticNatRuleTO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method applyStaticNatRules.

public boolean applyStaticNatRules(Network network, List<? extends StaticNat> rules) throws ResourceUnavailableException {
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    ExternalFirewallDeviceVO fwDeviceVO = getExternalFirewallForNetwork(network);
    HostVO externalFirewall = _hostDao.findById(fwDeviceVO.getHostId());
    assert (externalFirewall != null);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("External firewall was asked to apply firewall rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands.");
        return true;
    }
    List<StaticNatRuleTO> staticNatRules = new ArrayList<StaticNatRuleTO>();
    for (StaticNat rule : rules) {
        IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
        Vlan vlan = _vlanDao.findById(sourceIp.getVlanId());
        StaticNatRuleTO ruleTO = new StaticNatRuleTO(0, vlan.getVlanTag(), sourceIp.getAddress().addr(), -1, -1, rule.getDestIpAddress(), -1, -1, "any", rule.isForRevoke(), false);
        staticNatRules.add(ruleTO);
    }
    sendStaticNatRules(staticNatRules, zone, externalFirewall.getId());
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) ArrayList(java.util.ArrayList) Vlan(com.cloud.dc.Vlan) HostVO(com.cloud.host.HostVO) StaticNat(com.cloud.network.rules.StaticNat)

Example 4 with StaticNatRuleTO

use of com.cloud.agent.api.to.StaticNatRuleTO in project cloudstack by apache.

the class HypervDirectConnectResource method execute.

protected Answer execute(final SetStaticNatRulesCommand cmd) {
    if (cmd.getVpcId() != null) {
    //return SetVPCStaticNatRules(cmd);
    }
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource SetFirewallRuleCommand: " + s_gson.toJson(cmd));
    }
    String args = null;
    final String[] results = new String[cmd.getRules().length];
    int i = 0;
    boolean endResult = true;
    for (final StaticNatRuleTO rule : cmd.getRules()) {
        // 1:1 NAT needs instanceip;publicip;domrip;op
        args = rule.revoked() ? " -D " : " -A ";
        args += " -l " + rule.getSrcIp();
        args += " -r " + rule.getDstIp();
        if (rule.getProtocol() != null) {
            args += " -P " + rule.getProtocol().toLowerCase();
        }
        args += " -d " + rule.getStringSrcPortRange();
        args += " -G ";
        try {
            final String controlIp = getRouterSshControlIp(cmd);
            final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "/root/firewall.sh " + args);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Executing script on domain router " + controlIp + ": /root/firewall.sh " + args);
            }
            if (!result.first()) {
                s_logger.error("SetStaticNatRulesCommand failure on setting one rule. args: " + args);
                results[i++] = "Failed";
                endResult = false;
            } else {
                results[i++] = null;
            }
        } catch (final Throwable e) {
            s_logger.error("SetStaticNatRulesCommand (args: " + args + ") failed on setting one rule due to " + e.getMessage());
            results[i++] = "Failed";
            endResult = false;
        }
    }
    return new SetStaticNatRulesAnswer(cmd, results, endResult);
}
Also used : StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) SetStaticNatRulesAnswer(com.cloud.agent.api.routing.SetStaticNatRulesAnswer)

Example 5 with StaticNatRuleTO

use of com.cloud.agent.api.to.StaticNatRuleTO in project cloudstack by apache.

the class NiciraNvpResourceTest method testConfigureStaticNatRulesOnLogicalRouterRollback.

@Test
public void testConfigureStaticNatRulesOnLogicalRouterRollback() throws ConfigurationException, NiciraNvpApiException {
    resource.configure("NiciraNvpResource", parameters);
    /*
         * StaticNat Outside IP: 11.11.11.11 Inside IP: 10.10.10.10
         */
    // Mock the command
    final ConfigureStaticNatRulesOnLogicalRouterCommand cmd = mock(ConfigureStaticNatRulesOnLogicalRouterCommand.class);
    final StaticNatRuleTO rule = new StaticNatRuleTO(1, "11.11.11.11", null, null, "10.10.10.10", null, null, null, false, false);
    final List<StaticNatRuleTO> rules = new ArrayList<StaticNatRuleTO>();
    rules.add(rule);
    when(cmd.getRules()).thenReturn(rules);
    when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa");
    // Mock the api create calls
    final NatRule[] rulepair = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11");
    rulepair[0].setUuid(UUID.randomUUID());
    rulepair[1].setUuid(UUID.randomUUID());
    when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule) any())).thenReturn(rulepair[0]).thenThrow(new NiciraNvpApiException());
    // Mock the api find call
    @SuppressWarnings("unchecked") final List<NatRule> storedRules = Collections.EMPTY_LIST;
    when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules);
    final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) resource.executeRequest(cmd);
    assertFalse(a.getResult());
    verify(nvpApi, atLeastOnce()).deleteLogicalRouterNatRule(eq("aaaaa"), eq(rulepair[0].getUuid()));
}
Also used : ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) ArrayList(java.util.ArrayList) NatRule(com.cloud.network.nicira.NatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) ConfigureStaticNatRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer) Test(org.junit.Test)

Aggregations

StaticNatRuleTO (com.cloud.agent.api.to.StaticNatRuleTO)21 ArrayList (java.util.ArrayList)17 SetStaticNatRulesCommand (com.cloud.agent.api.routing.SetStaticNatRulesCommand)8 Answer (com.cloud.agent.api.Answer)7 Test (org.junit.Test)7 ConfigureStaticNatRulesOnLogicalRouterAnswer (com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer)6 ConfigureStaticNatRulesOnLogicalRouterCommand (com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand)5 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)5 IpAddress (com.cloud.network.IpAddress)5 PublicIpAddress (com.cloud.network.PublicIpAddress)5 NatRule (com.cloud.network.nicira.NatRule)5 StaticNat (com.cloud.network.rules.StaticNat)5 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)4 SourceNatRule (com.cloud.network.nicira.SourceNatRule)4 ExecutionException (com.cloud.utils.exception.ExecutionException)4 ExternalNetworkResourceUsageAnswer (com.cloud.agent.api.ExternalNetworkResourceUsageAnswer)3 MaintainAnswer (com.cloud.agent.api.MaintainAnswer)3 ReadyAnswer (com.cloud.agent.api.ReadyAnswer)3 SetStaticNatRulesAnswer (com.cloud.agent.api.routing.SetStaticNatRulesAnswer)3 DataCenterVO (com.cloud.dc.DataCenterVO)3