Search in sources :

Example 11 with SourceNatRule

use of com.cloud.network.nicira.SourceNatRule 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)

Example 12 with SourceNatRule

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

the class NiciraNvpResourceTest method testConfigureStaticNatRulesOnLogicalRouterExistingRules.

@Test
public void testConfigureStaticNatRulesOnLogicalRouterExistingRules() 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]).thenReturn(rulepair[1]);
    // Mock the api find call
    final List<NatRule> storedRules = Arrays.asList(rulepair);
    when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules);
    final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) resource.executeRequest(cmd);
    assertTrue(a.getResult());
    verify(nvpApi, never()).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() {

        @Override
        public boolean matches(final NatRule argument) {
            final NatRule rule = argument;
            if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule) rule).getToDestinationIpAddress().equals("10.10.10.10")) {
                return true;
            }
            if (rule.getType().equals("SourceNatRule") && ((SourceNatRule) rule).getToSourceIpAddressMin().equals("11.11.11.11")) {
                return true;
            }
            return false;
        }
    }));
}
Also used : ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) ArgumentMatcher(org.mockito.ArgumentMatcher) ArrayList(java.util.ArrayList) NatRule(com.cloud.network.nicira.NatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) ConfigureStaticNatRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Example 13 with SourceNatRule

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

the class NiciraNvpResourceTest method testConfigurePortForwardingRulesOnLogicalRouterExistingRules.

@Test
public void testConfigurePortForwardingRulesOnLogicalRouterExistingRules() throws ConfigurationException, NiciraNvpApiException {
    resource.configure("NiciraNvpResource", parameters);
    /*
         * StaticNat Outside IP: 11.11.11.11 Inside IP: 10.10.10.10
         */
    // Mock the command
    final ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = mock(ConfigurePortForwardingRulesOnLogicalRouterCommand.class);
    final PortForwardingRuleTO rule = new PortForwardingRuleTO(1, "11.11.11.11", 80, 80, "10.10.10.10", 8080, 8080, "tcp", false, true);
    final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>();
    rules.add(rule);
    when(cmd.getRules()).thenReturn(rules);
    when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa");
    // Mock the api create calls
    final NatRule[] rulepair = resource.generatePortForwardingRulePair("10.10.10.10", new int[] { 8080, 8080 }, "11.11.11.11", new int[] { 80, 80 }, "tcp");
    rulepair[0].setUuid(UUID.randomUUID());
    rulepair[1].setUuid(UUID.randomUUID());
    when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule) any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]);
    // Mock the api find call
    final List<NatRule> storedRules = Arrays.asList(rulepair);
    when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules);
    final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) resource.executeRequest(cmd);
    assertTrue(a.getResult());
    verify(nvpApi, never()).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() {

        @Override
        public boolean matches(final NatRule rule) {
            if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule) rule).getToDestinationIpAddress().equals("10.10.10.10")) {
                return true;
            }
            if (rule.getType().equals("SourceNatRule") && ((SourceNatRule) rule).getToSourceIpAddressMin().equals("11.11.11.11")) {
                return true;
            }
            return false;
        }
    }));
}
Also used : DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) ConfigurePortForwardingRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand) ArgumentMatcher(org.mockito.ArgumentMatcher) ArrayList(java.util.ArrayList) NatRule(com.cloud.network.nicira.NatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) ConfigurePortForwardingRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Example 14 with SourceNatRule

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

the class NiciraNvpResourceTest method testGeneratePortForwardingRulePair.

@Test
public void testGeneratePortForwardingRulePair() {
    final NatRule[] rules = resource.generatePortForwardingRulePair("10.10.10.10", new int[] { 8080, 8080 }, "11.11.11.11", new int[] { 80, 80 }, "tcp");
    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() == 8080);
    assertTrue(dnr.getMatch().getDestinationIpAddresses().equals("11.11.11.11"));
    assertTrue(dnr.getMatch().getDestinationPort() == 80);
    assertTrue(dnr.getMatch().getEthertype().equals("IPv4") && dnr.getMatch().getProtocol() == 6);
    final SourceNatRule snr = (SourceNatRule) rules[1];
    assertTrue(snr.getToSourceIpAddressMin().equals("11.11.11.11") && snr.getToSourceIpAddressMax().equals("11.11.11.11"));
    assertTrue(snr.getToSourcePort() == 80);
    assertTrue(snr.getMatch().getSourceIpAddresses().equals("10.10.10.10"));
    assertTrue(snr.getMatch().getSourcePort() == 8080);
    assertTrue(snr.getMatch().getEthertype().equals("IPv4") && rules[1].getMatch().getProtocol() == 6);
}
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 15 with SourceNatRule

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

the class NiciraNvpResourceTest method testConfigureStaticNatRulesOnLogicalRouter.

@Test
public void testConfigureStaticNatRulesOnLogicalRouter() 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 find call
    @SuppressWarnings("unchecked") final List<NatRule> storedRules = Collections.EMPTY_LIST;
    when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules);
    // 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]).thenReturn(rulepair[1]);
    final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) resource.executeRequest(cmd);
    assertTrue(a.getResult());
    verify(nvpApi, atLeast(2)).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() {

        @Override
        public boolean matches(final NatRule argument) {
            final NatRule rule = argument;
            if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule) rule).getToDestinationIpAddress().equals("10.10.10.10")) {
                return true;
            }
            if (rule.getType().equals("SourceNatRule") && ((SourceNatRule) rule).getToSourceIpAddressMin().equals("11.11.11.11")) {
                return true;
            }
            return false;
        }
    }));
}
Also used : ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) ArgumentMatcher(org.mockito.ArgumentMatcher) ArrayList(java.util.ArrayList) NatRule(com.cloud.network.nicira.NatRule) SourceNatRule(com.cloud.network.nicira.SourceNatRule) DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) ConfigureStaticNatRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Aggregations

SourceNatRule (com.cloud.network.nicira.SourceNatRule)16 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)14 NatRule (com.cloud.network.nicira.NatRule)14 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 ArgumentMatcher (org.mockito.ArgumentMatcher)8 ConfigurePortForwardingRulesOnLogicalRouterAnswer (com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer)4 ConfigurePortForwardingRulesOnLogicalRouterCommand (com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand)4 ConfigureStaticNatRulesOnLogicalRouterAnswer (com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer)4 ConfigureStaticNatRulesOnLogicalRouterCommand (com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand)4 PortForwardingRuleTO (com.cloud.agent.api.to.PortForwardingRuleTO)4 StaticNatRuleTO (com.cloud.agent.api.to.StaticNatRuleTO)4 Match (com.cloud.network.nicira.Match)4 CreateLogicalRouterAnswer (com.cloud.agent.api.CreateLogicalRouterAnswer)2 L3GatewayAttachment (com.cloud.network.nicira.L3GatewayAttachment)2 LogicalRouter (com.cloud.network.nicira.LogicalRouter)2 LogicalRouterPort (com.cloud.network.nicira.LogicalRouterPort)2 LogicalSwitchPort (com.cloud.network.nicira.LogicalSwitchPort)2 NiciraNvpApi (com.cloud.network.nicira.NiciraNvpApi)2 NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)2