Search in sources :

Example 11 with NatRule

use of com.cloud.network.nicira.NatRule in project cloudstack by apache.

the class NiciraNvpResourceTest method testGenerateStaticNatRulePair.

@Test
public void testGenerateStaticNatRulePair() {
    final NatRule[] rules = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11");
    assertTrue("DestinationNatRule".equals(rules[0].getType()));
    assertTrue("SourceNatRule".equals(rules[1].getType()));
    final DestinationNatRule dnr = (DestinationNatRule) rules[0];
    assertTrue(dnr.getToDestinationIpAddress().equals("10.10.10.10"));
    assertTrue(dnr.getToDestinationPort() == null);
    assertTrue(dnr.getMatch().getDestinationIpAddresses().equals("11.11.11.11"));
    final SourceNatRule snr = (SourceNatRule) rules[1];
    assertTrue(snr.getToSourceIpAddressMin().equals("11.11.11.11") && snr.getToSourceIpAddressMax().equals("11.11.11.11"));
    assertTrue(snr.getToSourcePort() == null);
    assertTrue(snr.getMatch().getSourceIpAddresses().equals("10.10.10.10"));
}
Also used : DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) NatRule(com.cloud.network.nicira.NatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Example 12 with NatRule

use of com.cloud.network.nicira.NatRule in project cloudstack by apache.

the class NiciraNvpConfigurePortForwardingRulesCommandWrapper method execute.

@Override
public Answer execute(final ConfigurePortForwardingRulesOnLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) {
    final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
    try {
        final List<NatRule> existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.getLogicalRouterUuid());
        for (final PortForwardingRuleTO rule : command.getRules()) {
            if (rule.isAlreadyAdded() && !rule.revoked()) {
                // Don't need to do anything
                continue;
            }
            if (rule.getDstPortRange()[0] != rule.getDstPortRange()[1] || rule.getSrcPortRange()[0] != rule.getSrcPortRange()[1]) {
                return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(command, false, "Nicira NVP doesn't support port ranges for port forwarding");
            }
            final NatRule[] rulepair = niciraNvpResource.generatePortForwardingRulePair(rule.getDstIp(), rule.getDstPortRange(), rule.getSrcIp(), rule.getSrcPortRange(), rule.getProtocol());
            NatRule incoming = null;
            NatRule outgoing = null;
            for (final NatRule storedRule : existingRules) {
                if (storedRule.equalsIgnoreUuid(rulepair[1])) {
                    // The outgoing rule exists
                    outgoing = storedRule;
                    s_logger.debug("Found matching outgoing rule " + outgoing.getUuid());
                    if (incoming != null) {
                        break;
                    }
                } else if (storedRule.equalsIgnoreUuid(rulepair[0])) {
                    // The incoming rule exists
                    incoming = storedRule;
                    s_logger.debug("Found matching incoming rule " + incoming.getUuid());
                    if (outgoing != null) {
                        break;
                    }
                }
            }
            if (incoming != null && outgoing != null) {
                if (rule.revoked()) {
                    s_logger.debug("Deleting incoming rule " + incoming.getUuid());
                    niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), incoming.getUuid());
                    s_logger.debug("Deleting outgoing rule " + outgoing.getUuid());
                    niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), outgoing.getUuid());
                }
            } else {
                if (rule.revoked()) {
                    s_logger.warn("Tried deleting a rule that does not exist, " + rule.getSrcIp() + " -> " + rule.getDstIp());
                    break;
                }
                rulepair[0] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0]);
                s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[0]));
                try {
                    rulepair[1] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[1]);
                    s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[1]));
                } catch (final NiciraNvpApiException ex) {
                    s_logger.warn("NiciraNvpApiException during create call, rolling back previous create");
                    niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0].getUuid());
                    // Rethrow the original exception
                    throw ex;
                }
            }
        }
        return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(command, true, command.getRules().size() + " PortForwarding rules applied");
    } catch (final NiciraNvpApiException e) {
        final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
        retryUtility.addRetry(command, NUM_RETRIES);
        return retryUtility.retry(command, ConfigurePortForwardingRulesOnLogicalRouterAnswer.class, e);
    }
}
Also used : PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) NiciraNvpApi(com.cloud.network.nicira.NiciraNvpApi) NatRule(com.cloud.network.nicira.NatRule) CommandRetryUtility(com.cloud.network.utils.CommandRetryUtility) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) ConfigurePortForwardingRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer)

Example 13 with NatRule

use of com.cloud.network.nicira.NatRule in project cloudstack by apache.

the class NiciraNvpConfigureStaticNatRulesCommandWrapper method execute.

@Override
public Answer execute(final ConfigureStaticNatRulesOnLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) {
    final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
    try {
        final List<NatRule> existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.getLogicalRouterUuid());
        for (final StaticNatRuleTO rule : command.getRules()) {
            final NatRule[] rulepair = niciraNvpResource.generateStaticNatRulePair(rule.getDstIp(), rule.getSrcIp());
            NatRule incoming = null;
            NatRule outgoing = null;
            for (final NatRule storedRule : existingRules) {
                if (storedRule.equalsIgnoreUuid(rulepair[1])) {
                    // The outgoing rule exists
                    outgoing = storedRule;
                    s_logger.debug("Found matching outgoing rule " + outgoing.getUuid());
                    if (incoming != null) {
                        break;
                    }
                } else if (storedRule.equalsIgnoreUuid(rulepair[0])) {
                    // The incoming rule exists
                    incoming = storedRule;
                    s_logger.debug("Found matching incoming rule " + incoming.getUuid());
                    if (outgoing != null) {
                        break;
                    }
                }
            }
            if (incoming != null && outgoing != null) {
                if (rule.revoked()) {
                    s_logger.debug("Deleting incoming rule " + incoming.getUuid());
                    niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), incoming.getUuid());
                    s_logger.debug("Deleting outgoing rule " + outgoing.getUuid());
                    niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), outgoing.getUuid());
                }
            } else {
                if (rule.revoked()) {
                    s_logger.warn("Tried deleting a rule that does not exist, " + rule.getSrcIp() + " -> " + rule.getDstIp());
                    break;
                }
                rulepair[0] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0]);
                s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[0]));
                try {
                    rulepair[1] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[1]);
                    s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[1]));
                } catch (final NiciraNvpApiException ex) {
                    s_logger.debug("Failed to create SourceNatRule, rolling back DestinationNatRule");
                    niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0].getUuid());
                    // Rethrow original exception
                    throw ex;
                }
            }
        }
        return new ConfigureStaticNatRulesOnLogicalRouterAnswer(command, true, command.getRules().size() + " StaticNat rules applied");
    } catch (final NiciraNvpApiException e) {
        final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
        retryUtility.addRetry(command, NUM_RETRIES);
        return retryUtility.retry(command, ConfigureStaticNatRulesOnLogicalRouterAnswer.class, e);
    }
}
Also used : StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) NiciraNvpApi(com.cloud.network.nicira.NiciraNvpApi) NatRule(com.cloud.network.nicira.NatRule) CommandRetryUtility(com.cloud.network.utils.CommandRetryUtility) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) ConfigureStaticNatRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer)

Example 14 with NatRule

use of com.cloud.network.nicira.NatRule in project cloudstack by apache.

the class NiciraNvpResource method generateStaticNatRulePair.

public NatRule[] generateStaticNatRulePair(final String insideIp, final String outsideIp) {
    final NatRule[] rulepair = new NatRule[2];
    rulepair[0] = new DestinationNatRule();
    rulepair[0].setType("DestinationNatRule");
    rulepair[0].setOrder(100);
    rulepair[1] = new SourceNatRule();
    rulepair[1].setType("SourceNatRule");
    rulepair[1].setOrder(100);
    Match m = new Match();
    m.setDestinationIpAddresses(outsideIp);
    rulepair[0].setMatch(m);
    ((DestinationNatRule) rulepair[0]).setToDestinationIpAddress(insideIp);
    // create matching snat rule
    m = new Match();
    m.setSourceIpAddresses(insideIp);
    rulepair[1].setMatch(m);
    ((SourceNatRule) rulepair[1]).setToSourceIpAddressMin(outsideIp);
    ((SourceNatRule) rulepair[1]).setToSourceIpAddressMax(outsideIp);
    return rulepair;
}
Also used : DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) NatRule(com.cloud.network.nicira.NatRule) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Match(com.cloud.network.nicira.Match)

Aggregations

NatRule (com.cloud.network.nicira.NatRule)14 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)12 SourceNatRule (com.cloud.network.nicira.SourceNatRule)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 ConfigurePortForwardingRulesOnLogicalRouterAnswer (com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer)6 PortForwardingRuleTO (com.cloud.agent.api.to.PortForwardingRuleTO)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 ConfigurePortForwardingRulesOnLogicalRouterCommand (com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand)5 ConfigureStaticNatRulesOnLogicalRouterAnswer (com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer)5 StaticNatRuleTO (com.cloud.agent.api.to.StaticNatRuleTO)5 ConfigureStaticNatRulesOnLogicalRouterCommand (com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand)4 NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)4 NiciraNvpApi (com.cloud.network.nicira.NiciraNvpApi)2 CommandRetryUtility (com.cloud.network.utils.CommandRetryUtility)2 UUID (java.util.UUID)2 Match (com.cloud.network.nicira.Match)1