Search in sources :

Example 6 with PortForwardingRuleTO

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

the class ConfigHelperTest method generateSetPortForwardingRulesVpcCommand.

protected SetPortForwardingRulesVpcCommand generateSetPortForwardingRulesVpcCommand() {
    final List<PortForwardingRuleTO> pfRules = new ArrayList<>();
    pfRules.add(new PortForwardingRuleTO(1, "64.1.1.10", 22, 80, "10.10.1.10", 22, 80, "TCP", false, false));
    pfRules.add(new PortForwardingRuleTO(2, "64.1.1.11", 8080, 8080, "10.10.1.11", 8080, 8080, "UDP", true, false));
    final SetPortForwardingRulesVpcCommand cmd = new SetPortForwardingRulesVpcCommand(pfRules);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, this.ROUTERNAME);
    assertEquals(cmd.getAnswersCount(), 2);
    return cmd;
}
Also used : PortForwardingRuleTO(com.cloud.legacymodel.to.PortForwardingRuleTO) SetPortForwardingRulesVpcCommand(com.cloud.legacymodel.communication.command.SetPortForwardingRulesVpcCommand) ArrayList(java.util.ArrayList)

Example 7 with PortForwardingRuleTO

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

the class CommandSetupHelper method createApplyPortForwardingRulesCommands.

public void createApplyPortForwardingRulesCommands(final List<? extends PortForwardingRule> rules, final VirtualRouter router, final Commands cmds, final long guestNetworkId) {
    final List<PortForwardingRuleTO> rulesTO = new ArrayList<>();
    if (rules != null) {
        for (final PortForwardingRule rule : rules) {
            final IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
            final PortForwardingRuleTO ruleTO = new PortForwardingRuleTO(rule, null, sourceIp.getAddress().addr());
            rulesTO.add(ruleTO);
        }
    }
    final SetPortForwardingRulesCommand cmd;
    if (router.getVpcId() != null) {
        cmd = new SetPortForwardingRulesVpcCommand(rulesTO);
    } else {
        cmd = new SetPortForwardingRulesCommand(rulesTO);
    }
    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 : PortForwardingRuleTO(com.cloud.legacymodel.to.PortForwardingRuleTO) SetPortForwardingRulesCommand(com.cloud.legacymodel.communication.command.SetPortForwardingRulesCommand) Zone(com.cloud.db.model.Zone) SetPortForwardingRulesVpcCommand(com.cloud.legacymodel.communication.command.SetPortForwardingRulesVpcCommand) ArrayList(java.util.ArrayList) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) PortForwardingRule(com.cloud.legacymodel.network.PortForwardingRule)

Example 8 with PortForwardingRuleTO

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

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

Example 9 with PortForwardingRuleTO

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

the class HAProxyConfigurator method getRulesForPool.

private List<String> getRulesForPool(final String poolName, final List<PortForwardingRuleTO> fwRules) {
    final PortForwardingRuleTO firstRule = fwRules.get(0);
    final String publicIP = firstRule.getSrcIp();
    final int publicPort = firstRule.getSrcPortRange()[0];
    final List<String> result = new ArrayList<>();
    // Add line like this: "listen  65_37_141_30-80 65.37.141.30:80"
    StringBuilder sb = new StringBuilder();
    sb.append("listen ").append(poolName).append(" ").append(publicIP).append(":").append(publicPort);
    result.add(sb.toString());
    sb = new StringBuilder();
    // FIXME sb.append("\t").append("balance ").append(algorithm);
    result.add(sb.toString());
    if (publicPort == NetUtils.HTTP_PORT) {
        sb = new StringBuilder();
        sb.append("\t").append("mode http");
        result.add(sb.toString());
        sb = new StringBuilder();
        sb.append("\t").append("option httpclose");
        result.add(sb.toString());
    }
    int i = 0;
    for (final PortForwardingRuleTO rule : fwRules) {
        // Add line like this: "server  65_37_141_30-80_3 10.1.1.4:80 check"
        if (rule.revoked()) {
            continue;
        }
        sb = new StringBuilder();
        sb.append("\t").append("server ").append(poolName).append("_").append(Integer.toString(i++)).append(" ").append(rule.getDstIp()).append(":").append(rule.getDstPortRange()[0]).append(" check");
        result.add(sb.toString());
    }
    result.add(blankLine);
    return result;
}
Also used : PortForwardingRuleTO(com.cloud.legacymodel.to.PortForwardingRuleTO) ArrayList(java.util.ArrayList)

Example 10 with PortForwardingRuleTO

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

the class HAProxyConfigurator method generateConfiguration.

@Override
public String[] generateConfiguration(final List<PortForwardingRuleTO> fwRules) {
    // Group the rules by publicip:publicport
    final Map<String, List<PortForwardingRuleTO>> pools = new HashMap<>();
    for (final PortForwardingRuleTO rule : fwRules) {
        final StringBuilder sb = new StringBuilder();
        final String poolName = sb.append(rule.getSrcIp().replace(".", "_")).append('-').append(rule.getSrcPortRange()[0]).toString();
        if (!rule.revoked()) {
            List<PortForwardingRuleTO> fwList = pools.get(poolName);
            if (fwList == null) {
                fwList = new ArrayList<>();
                pools.put(poolName, fwList);
            }
            fwList.add(rule);
        }
    }
    final List<String> result = new ArrayList<>();
    result.addAll(Arrays.asList(globalSection));
    result.add(blankLine);
    result.addAll(Arrays.asList(defaultsSection));
    result.add(blankLine);
    if (pools.isEmpty()) {
        // HAproxy cannot handle empty listen / frontend or backend, so add a dummy listener on port 9
        result.addAll(Arrays.asList(defaultListen));
    }
    result.add(blankLine);
    for (final Map.Entry<String, List<PortForwardingRuleTO>> e : pools.entrySet()) {
        final List<String> poolRules = getRulesForPool(e.getKey(), e.getValue());
        result.addAll(poolRules);
    }
    return result.toArray(new String[result.size()]);
}
Also used : PortForwardingRuleTO(com.cloud.legacymodel.to.PortForwardingRuleTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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