Search in sources :

Example 6 with StaticNatRuleTO

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

the class CommandSetupHelper method createApplyStaticNatRulesCommands.

public void createApplyStaticNatRulesCommands(final List<? extends StaticNatRule> rules, final VirtualRouter router, final Commands cmds, final long guestNetworkId) {
    final List<StaticNatRuleTO> rulesTO = new ArrayList<>();
    if (rules != null) {
        for (final StaticNatRule rule : rules) {
            final IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
            final StaticNatRuleTO ruleTO = new StaticNatRuleTO(rule, null, sourceIp.getAddress().addr(), rule.getDestIpAddress());
            rulesTO.add(ruleTO);
        }
    }
    final SetStaticNatRulesCommand cmd = new SetStaticNatRulesCommand(rulesTO, router.getVpcId());
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
    final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
    cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, zone.getNetworkType().toString());
    cmds.addCommand(cmd);
}
Also used : StaticNatRuleTO(com.cloud.legacymodel.to.StaticNatRuleTO) SetStaticNatRulesCommand(com.cloud.legacymodel.communication.command.SetStaticNatRulesCommand) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) StaticNatRule(com.cloud.legacymodel.network.StaticNatRule)

Example 7 with StaticNatRuleTO

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

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(), getStringSrcPortRange(rule), rule.getDstIp());
        rules.add(staticNatRule);
    }
    final StaticNatRules staticNatRules = new StaticNatRules(rules);
    return generateConfigItems(staticNatRules);
}
Also used : StaticNatRuleTO(com.cloud.legacymodel.to.StaticNatRuleTO) SetStaticNatRulesCommand(com.cloud.legacymodel.communication.command.SetStaticNatRulesCommand) StaticNatRules(com.cloud.legacymodel.network.rules.StaticNatRules) StaticNatRule(com.cloud.legacymodel.network.rules.StaticNatRule) LinkedList(java.util.LinkedList)

Example 8 with StaticNatRuleTO

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

the class CommandSetupHelper method createApplyStaticNatCommands.

public void createApplyStaticNatCommands(final List<? extends StaticNat> rules, final VirtualRouter router, final Commands cmds) {
    final List<StaticNatRuleTO> rulesTO = new ArrayList<>();
    if (rules != null) {
        for (final StaticNat rule : rules) {
            final IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
            final StaticNatRuleTO ruleTO = new StaticNatRuleTO(0, sourceIp.getAddress().addr(), null, null, rule.getDestIpAddress(), null, null, null, rule.isForRevoke(), false);
            rulesTO.add(ruleTO);
        }
    }
    final SetStaticNatRulesCommand cmd = new SetStaticNatRulesCommand(rulesTO, router.getVpcId());
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
    final Zone zone = zoneRepository.findById(router.getDataCenterId()).orElse(null);
    cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, zone.getNetworkType().toString());
    cmds.addCommand(cmd);
}
Also used : StaticNatRuleTO(com.cloud.legacymodel.to.StaticNatRuleTO) SetStaticNatRulesCommand(com.cloud.legacymodel.communication.command.SetStaticNatRulesCommand) Zone(com.cloud.db.model.Zone) ArrayList(java.util.ArrayList) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) StaticNat(com.cloud.network.rules.StaticNat)

Example 9 with StaticNatRuleTO

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

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.legacymodel.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.legacymodel.communication.answer.ConfigureStaticNatRulesOnLogicalRouterAnswer)

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