Search in sources :

Example 1 with StaticNatRuleTO

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

the class NiciraNvpElement method applyStaticNats.

/**
 * From interface StaticNatServiceProvider
 */
@Override
public boolean applyStaticNats(final Network network, final List<? extends StaticNat> rules) throws ResourceUnavailableException {
    if (!canHandle(network, Network.Service.StaticNat)) {
        return false;
    }
    final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
    if (devices.isEmpty()) {
        s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId());
        return false;
    }
    final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
    final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId());
    final NiciraNvpRouterMappingVO routermapping = this.niciraNvpRouterMappingDao.findByNetworkId(network.getId());
    if (routermapping == null) {
        s_logger.error("No logical router uuid found for network " + network.getDisplayText());
        return false;
    }
    final List<StaticNatRuleTO> staticNatRules = new ArrayList<>();
    for (final StaticNat rule : rules) {
        final IpAddress sourceIp = this.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.
        final 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);
    }
    final ConfigureStaticNatRulesOnLogicalRouterCommand cmd = new ConfigureStaticNatRulesOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), staticNatRules);
    final ConfigureStaticNatRulesOnLogicalRouterAnswer answer = (ConfigureStaticNatRulesOnLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd);
    return answer.getResult();
}
Also used : StaticNatRuleTO(com.cloud.legacymodel.to.StaticNatRuleTO) ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.legacymodel.communication.command.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.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer) HostVO(com.cloud.host.HostVO) StaticNat(com.cloud.network.rules.StaticNat)

Example 2 with StaticNatRuleTO

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

the class NiciraNvpResourceTest method testConfigureStaticNatRulesOnLogicalRouterRemoveRules.

@Test
public void testConfigureStaticNatRulesOnLogicalRouterRemoveRules() 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, true, false);
    final List<StaticNatRuleTO> 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.generateStaticNatRulePair("10.10.10.10", "11.11.11.11");
    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 ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) 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 : ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.legacymodel.communication.command.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.legacymodel.to.StaticNatRuleTO) 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) ConfigureStaticNatRulesOnLogicalRouterAnswer(com.cloud.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer) Test(org.junit.Test)

Example 3 with StaticNatRuleTO

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

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<>();
    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
    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.legacymodel.communication.command.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.legacymodel.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.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer) Test(org.junit.Test)

Example 4 with StaticNatRuleTO

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

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<>();
    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 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 : ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.legacymodel.communication.command.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.legacymodel.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.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Example 5 with StaticNatRuleTO

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

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<>();
    rules.add(rule);
    when(cmd.getRules()).thenReturn(rules);
    when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa");
    // Mock the api find call
    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 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 : ConfigureStaticNatRulesOnLogicalRouterCommand(com.cloud.legacymodel.communication.command.ConfigureStaticNatRulesOnLogicalRouterCommand) StaticNatRuleTO(com.cloud.legacymodel.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.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer) SourceNatRule(com.cloud.network.nicira.SourceNatRule) Test(org.junit.Test)

Aggregations

StaticNatRuleTO (com.cloud.legacymodel.to.StaticNatRuleTO)9 ArrayList (java.util.ArrayList)7 ConfigureStaticNatRulesOnLogicalRouterAnswer (com.cloud.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer)6 ConfigureStaticNatRulesOnLogicalRouterCommand (com.cloud.legacymodel.communication.command.ConfigureStaticNatRulesOnLogicalRouterCommand)5 NatRule (com.cloud.network.nicira.NatRule)5 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)4 SourceNatRule (com.cloud.network.nicira.SourceNatRule)4 Test (org.junit.Test)4 SetStaticNatRulesCommand (com.cloud.legacymodel.communication.command.SetStaticNatRulesCommand)3 IpAddress (com.cloud.network.IpAddress)3 PublicIpAddress (com.cloud.network.PublicIpAddress)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 Zone (com.cloud.db.model.Zone)2 NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)2 StaticNat (com.cloud.network.rules.StaticNat)2 HostVO (com.cloud.host.HostVO)1 StaticNatRule (com.cloud.legacymodel.network.StaticNatRule)1 StaticNatRule (com.cloud.legacymodel.network.rules.StaticNatRule)1 StaticNatRules (com.cloud.legacymodel.network.rules.StaticNatRules)1 NiciraNvpDeviceVO (com.cloud.network.NiciraNvpDeviceVO)1