Search in sources :

Example 11 with PortForwardingRuleTO

use of com.cloud.legacymodel.to.PortForwardingRuleTO in project cosmic by MissionCriticalCloud.

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<>();
    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 Object argument) {
            final NatRule rule = (NatRule) 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 : DestinationNatRule(com.cloud.network.nicira.DestinationNatRule) PortForwardingRuleTO(com.cloud.legacymodel.to.PortForwardingRuleTO) ConfigurePortForwardingRulesOnLogicalRouterCommand(com.cloud.legacymodel.communication.command.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.legacymodel.communication.answer.ConfigurePortForwardingRulesOnLogicalRouterAnswer) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Example 12 with PortForwardingRuleTO

use of com.cloud.legacymodel.to.PortForwardingRuleTO in project cosmic by MissionCriticalCloud.

the class NiciraNvpResourceTest method testConfigurePortForwardingRulesOnLogicalRouterRemoveRules.

@Test
public void testConfigurePortForwardingRulesOnLogicalRouterRemoveRules() 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", true, true);
    final List<PortForwardingRuleTO> rules = new ArrayList<>();
    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");
    final UUID rule0Uuid = UUID.randomUUID();
    final UUID rule1Uuid = UUID.randomUUID();
    rulepair[0].setUuid(rule0Uuid);
    rulepair[1].setUuid(rule1Uuid);
    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, atLeast(2)).deleteLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<UUID>() {

        @Override
        public boolean matches(final Object argument) {
            final UUID uuid = (UUID) argument;
            if (rule0Uuid.equals(uuid) || rule1Uuid.equals(uuid)) {
                return true;
            }
            return false;
        }
    }));
}
Also used : PortForwardingRuleTO(com.cloud.legacymodel.to.PortForwardingRuleTO) ConfigurePortForwardingRulesOnLogicalRouterCommand(com.cloud.legacymodel.communication.command.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) UUID(java.util.UUID) ConfigurePortForwardingRulesOnLogicalRouterAnswer(com.cloud.legacymodel.communication.answer.ConfigurePortForwardingRulesOnLogicalRouterAnswer) Test(org.junit.Test)

Aggregations

PortForwardingRuleTO (com.cloud.legacymodel.to.PortForwardingRuleTO)12 ArrayList (java.util.ArrayList)11 ConfigurePortForwardingRulesOnLogicalRouterAnswer (com.cloud.legacymodel.communication.answer.ConfigurePortForwardingRulesOnLogicalRouterAnswer)7 ConfigurePortForwardingRulesOnLogicalRouterCommand (com.cloud.legacymodel.communication.command.ConfigurePortForwardingRulesOnLogicalRouterCommand)6 NatRule (com.cloud.network.nicira.NatRule)6 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)5 SourceNatRule (com.cloud.network.nicira.SourceNatRule)5 Test (org.junit.Test)5 ArgumentMatcher (org.mockito.ArgumentMatcher)3 SetPortForwardingRulesCommand (com.cloud.legacymodel.communication.command.SetPortForwardingRulesCommand)2 SetPortForwardingRulesVpcCommand (com.cloud.legacymodel.communication.command.SetPortForwardingRulesVpcCommand)2 PortForwardingRule (com.cloud.legacymodel.network.PortForwardingRule)2 IpAddress (com.cloud.network.IpAddress)2 PublicIpAddress (com.cloud.network.PublicIpAddress)2 NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)2 Zone (com.cloud.db.model.Zone)1 HostVO (com.cloud.host.HostVO)1 Vlan (com.cloud.legacymodel.dc.Vlan)1 ForwardingRule (com.cloud.legacymodel.network.rules.ForwardingRule)1 ForwardingRules (com.cloud.legacymodel.network.rules.ForwardingRules)1