Search in sources :

Example 1 with Match

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

the class NiciraNvpCreateLogicalRouterCommandWrapper method execute.

@Override
public Answer execute(final CreateLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) {
    final String routerName = command.getName();
    final String gatewayServiceUuid = command.getGatewayServiceUuid();
    final String logicalSwitchUuid = command.getLogicalSwitchUuid();
    final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
    tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
    final String publicNetworkNextHopIp = command.getPublicNextHop();
    final String publicNetworkIpAddress = command.getPublicIpCidr();
    final String internalNetworkAddress = command.getInternalIpCidr();
    s_logger.debug("Creating a logical router with external ip " + publicNetworkIpAddress + " and internal ip " + internalNetworkAddress + "on gateway service " + gatewayServiceUuid);
    final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
    try {
        // Create the Router
        LogicalRouter lrc = new LogicalRouter();
        lrc.setDisplayName(niciraNvpResource.truncate(routerName, NAME_MAX_LEN));
        lrc.setTags(tags);
        lrc.setRoutingConfig(new SingleDefaultRouteImplicitRoutingConfig(new RouterNextHop(publicNetworkNextHopIp)));
        lrc = niciraNvpApi.createLogicalRouter(lrc);
        // store the switchport for rollback
        LogicalSwitchPort lsp = null;
        try {
            // Create the outside port for the router
            LogicalRouterPort lrpo = new LogicalRouterPort();
            lrpo.setAdminStatusEnabled(true);
            lrpo.setDisplayName(niciraNvpResource.truncate(routerName + "-outside-port", NAME_MAX_LEN));
            lrpo.setTags(tags);
            final List<String> outsideIpAddresses = new ArrayList<String>();
            outsideIpAddresses.add(publicNetworkIpAddress);
            lrpo.setIpAddresses(outsideIpAddresses);
            lrpo = niciraNvpApi.createLogicalRouterPort(lrc.getUuid(), lrpo);
            // Attach the outside port to the gateway service on the correct VLAN
            final L3GatewayAttachment attachment = new L3GatewayAttachment(gatewayServiceUuid);
            if (command.getVlanId() != 0) {
                attachment.setVlanId(command.getVlanId());
            }
            niciraNvpApi.updateLogicalRouterPortAttachment(lrc.getUuid(), lrpo.getUuid(), attachment);
            // Create the inside port for the router
            LogicalRouterPort lrpi = new LogicalRouterPort();
            lrpi.setAdminStatusEnabled(true);
            lrpi.setDisplayName(niciraNvpResource.truncate(routerName + "-inside-port", NAME_MAX_LEN));
            lrpi.setTags(tags);
            final List<String> insideIpAddresses = new ArrayList<String>();
            insideIpAddresses.add(internalNetworkAddress);
            lrpi.setIpAddresses(insideIpAddresses);
            lrpi = niciraNvpApi.createLogicalRouterPort(lrc.getUuid(), lrpi);
            // Create the inside port on the lswitch
            lsp = new LogicalSwitchPort(niciraNvpResource.truncate(routerName + "-inside-port", NAME_MAX_LEN), tags, true);
            lsp = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lsp);
            // Attach the inside router port to the lswitch port with a PatchAttachment
            niciraNvpApi.updateLogicalRouterPortAttachment(lrc.getUuid(), lrpi.getUuid(), new PatchAttachment(lsp.getUuid()));
            // Attach the inside lswitch port to the router with a PatchAttachment
            niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, lsp.getUuid(), new PatchAttachment(lrpi.getUuid()));
            // Setup the source nat rule
            final SourceNatRule snr = new SourceNatRule();
            snr.setToSourceIpAddressMin(publicNetworkIpAddress.split("/")[0]);
            snr.setToSourceIpAddressMax(publicNetworkIpAddress.split("/")[0]);
            final Match match = new Match();
            match.setSourceIpAddresses(internalNetworkAddress);
            snr.setMatch(match);
            snr.setOrder(200);
            niciraNvpApi.createLogicalRouterNatRule(lrc.getUuid(), snr);
        } catch (final NiciraNvpApiException e) {
            // this will also take care of any router ports and rules
            try {
                niciraNvpApi.deleteLogicalRouter(lrc.getUuid());
                if (lsp != null) {
                    niciraNvpApi.deleteLogicalSwitchPort(logicalSwitchUuid, lsp.getUuid());
                }
            } catch (final NiciraNvpApiException ex) {
            }
            throw e;
        }
        return new CreateLogicalRouterAnswer(command, true, "Logical Router created (uuid " + lrc.getUuid() + ")", lrc.getUuid());
    } catch (final NiciraNvpApiException e) {
        final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
        retryUtility.addRetry(command, NUM_RETRIES);
        return retryUtility.retry(command, CreateLogicalRouterAnswer.class, e);
    }
}
Also used : LogicalRouterPort(com.cloud.network.nicira.LogicalRouterPort) ArrayList(java.util.ArrayList) LogicalRouter(com.cloud.network.nicira.LogicalRouter) RouterNextHop(com.cloud.network.nicira.RouterNextHop) L3GatewayAttachment(com.cloud.network.nicira.L3GatewayAttachment) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) SingleDefaultRouteImplicitRoutingConfig(com.cloud.network.nicira.SingleDefaultRouteImplicitRoutingConfig) Match(com.cloud.network.nicira.Match) PatchAttachment(com.cloud.network.nicira.PatchAttachment) NiciraNvpTag(com.cloud.network.nicira.NiciraNvpTag) NiciraNvpApi(com.cloud.network.nicira.NiciraNvpApi) CommandRetryUtility(com.cloud.network.utils.CommandRetryUtility) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) SourceNatRule(com.cloud.network.nicira.SourceNatRule) LogicalSwitchPort(com.cloud.network.nicira.LogicalSwitchPort)

Example 2 with Match

use of com.cloud.network.nicira.Match in project cosmic by MissionCriticalCloud.

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 3 with Match

use of com.cloud.network.nicira.Match in project cosmic by MissionCriticalCloud.

the class NiciraNvpResource method natRuleToString.

public String natRuleToString(final NatRule rule) {
    final StringBuilder natRuleStr = new StringBuilder();
    natRuleStr.append("Rule ");
    natRuleStr.append(rule.getUuid());
    natRuleStr.append(" (");
    natRuleStr.append(rule.getType());
    natRuleStr.append(") :");
    final Match m = rule.getMatch();
    natRuleStr.append("match (");
    natRuleStr.append(m.getProtocol());
    natRuleStr.append(" ");
    natRuleStr.append(m.getSourceIpAddresses());
    natRuleStr.append(" [");
    natRuleStr.append(m.getSourcePort());
    natRuleStr.append(" ] -> ");
    natRuleStr.append(m.getDestinationIpAddresses());
    natRuleStr.append(" [");
    natRuleStr.append(m.getDestinationPort());
    natRuleStr.append(" ]) -->");
    if ("SourceNatRule".equals(rule.getType())) {
        natRuleStr.append(((SourceNatRule) rule).getToSourceIpAddressMin());
        natRuleStr.append("-");
        natRuleStr.append(((SourceNatRule) rule).getToSourceIpAddressMax());
        natRuleStr.append(" [");
        natRuleStr.append(((SourceNatRule) rule).getToSourcePort());
        natRuleStr.append(" ])");
    } else {
        natRuleStr.append(((DestinationNatRule) rule).getToDestinationIpAddress());
        natRuleStr.append(" [");
        natRuleStr.append(((DestinationNatRule) rule).getToDestinationPort());
        natRuleStr.append(" ])");
    }
    return natRuleStr.toString();
}
Also used : Match(com.cloud.network.nicira.Match)

Example 4 with Match

use of com.cloud.network.nicira.Match in project cosmic by MissionCriticalCloud.

the class NiciraNvpCreateLogicalRouterCommandWrapper method execute.

@Override
public Answer execute(final CreateLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) {
    final String routerName = command.getName();
    final String gatewayServiceUuid = command.getGatewayServiceUuid();
    final String logicalSwitchUuid = command.getLogicalSwitchUuid();
    final List<NiciraNvpTag> tags = new ArrayList<>();
    tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
    final String publicNetworkNextHopIp = command.getPublicNextHop();
    final String publicNetworkIpAddress = command.getPublicIpCidr();
    final String internalNetworkAddress = command.getInternalIpCidr();
    s_logger.debug("Creating a logical router with external ip " + publicNetworkIpAddress + " and internal ip " + internalNetworkAddress + "on gateway service " + gatewayServiceUuid);
    final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
    try {
        // Create the Router
        LogicalRouter lrc = new LogicalRouter();
        lrc.setDisplayName(niciraNvpResource.truncate(routerName, NAME_MAX_LEN));
        lrc.setTags(tags);
        lrc.setRoutingConfig(new SingleDefaultRouteImplicitRoutingConfig(new RouterNextHop(publicNetworkNextHopIp)));
        lrc = niciraNvpApi.createLogicalRouter(lrc);
        // store the switchport for rollback
        LogicalSwitchPort lsp = null;
        try {
            // Create the outside port for the router
            LogicalRouterPort lrpo = new LogicalRouterPort();
            lrpo.setAdminStatusEnabled(true);
            lrpo.setDisplayName(niciraNvpResource.truncate(routerName + "-outside-port", NAME_MAX_LEN));
            lrpo.setTags(tags);
            final List<String> outsideIpAddresses = new ArrayList<>();
            outsideIpAddresses.add(publicNetworkIpAddress);
            lrpo.setIpAddresses(outsideIpAddresses);
            lrpo = niciraNvpApi.createLogicalRouterPort(lrc.getUuid(), lrpo);
            // Attach the outside port to the gateway service on the correct VLAN
            final L3GatewayAttachment attachment = new L3GatewayAttachment(gatewayServiceUuid);
            if (command.getVlanId() != 0) {
                attachment.setVlanId(command.getVlanId());
            }
            niciraNvpApi.updateLogicalRouterPortAttachment(lrc.getUuid(), lrpo.getUuid(), attachment);
            // Create the inside port for the router
            LogicalRouterPort lrpi = new LogicalRouterPort();
            lrpi.setAdminStatusEnabled(true);
            lrpi.setDisplayName(niciraNvpResource.truncate(routerName + "-inside-port", NAME_MAX_LEN));
            lrpi.setTags(tags);
            final List<String> insideIpAddresses = new ArrayList<>();
            insideIpAddresses.add(internalNetworkAddress);
            lrpi.setIpAddresses(insideIpAddresses);
            lrpi = niciraNvpApi.createLogicalRouterPort(lrc.getUuid(), lrpi);
            // Create the inside port on the lswitch
            lsp = new LogicalSwitchPort(niciraNvpResource.truncate(routerName + "-inside-port", NAME_MAX_LEN), tags, true, null);
            lsp = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lsp);
            // Attach the inside router port to the lswitch port with a PatchAttachment
            niciraNvpApi.updateLogicalRouterPortAttachment(lrc.getUuid(), lrpi.getUuid(), new PatchAttachment(lsp.getUuid()));
            // Attach the inside lswitch port to the router with a PatchAttachment
            niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, lsp.getUuid(), new PatchAttachment(lrpi.getUuid()));
            // Setup the source nat rule
            final SourceNatRule snr = new SourceNatRule();
            snr.setToSourceIpAddressMin(publicNetworkIpAddress.split("/")[0]);
            snr.setToSourceIpAddressMax(publicNetworkIpAddress.split("/")[0]);
            final Match match = new Match();
            match.setSourceIpAddresses(internalNetworkAddress);
            snr.setMatch(match);
            snr.setOrder(200);
            niciraNvpApi.createLogicalRouterNatRule(lrc.getUuid(), snr);
        } catch (final NiciraNvpApiException e) {
            // this will also take care of any router ports and rules
            try {
                niciraNvpApi.deleteLogicalRouter(lrc.getUuid());
                if (lsp != null) {
                    niciraNvpApi.deleteLogicalSwitchPort(logicalSwitchUuid, lsp.getUuid());
                }
            } catch (final NiciraNvpApiException ex) {
            }
            throw e;
        }
        return new CreateLogicalRouterAnswer(command, true, "Logical Router created (uuid " + lrc.getUuid() + ")", lrc.getUuid());
    } catch (final NiciraNvpApiException e) {
        final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
        retryUtility.addRetry(command, NUM_RETRIES);
        return retryUtility.retry(command, CreateLogicalRouterAnswer.class, e);
    }
}
Also used : LogicalRouterPort(com.cloud.network.nicira.LogicalRouterPort) ArrayList(java.util.ArrayList) LogicalRouter(com.cloud.network.nicira.LogicalRouter) RouterNextHop(com.cloud.network.nicira.RouterNextHop) L3GatewayAttachment(com.cloud.network.nicira.L3GatewayAttachment) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) SingleDefaultRouteImplicitRoutingConfig(com.cloud.network.nicira.SingleDefaultRouteImplicitRoutingConfig) Match(com.cloud.network.nicira.Match) PatchAttachment(com.cloud.network.nicira.PatchAttachment) NiciraNvpTag(com.cloud.network.nicira.NiciraNvpTag) NiciraNvpApi(com.cloud.network.nicira.NiciraNvpApi) CommandRetryUtility(com.cloud.network.utils.CommandRetryUtility) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) SourceNatRule(com.cloud.network.nicira.SourceNatRule) LogicalSwitchPort(com.cloud.network.nicira.LogicalSwitchPort)

Example 5 with Match

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

the class NiciraNvpResource method natRuleToString.

public String natRuleToString(final NatRule rule) {
    final StringBuilder natRuleStr = new StringBuilder();
    natRuleStr.append("Rule ");
    natRuleStr.append(rule.getUuid());
    natRuleStr.append(" (");
    natRuleStr.append(rule.getType());
    natRuleStr.append(") :");
    final Match m = rule.getMatch();
    natRuleStr.append("match (");
    natRuleStr.append(m.getProtocol());
    natRuleStr.append(" ");
    natRuleStr.append(m.getSourceIpAddresses());
    natRuleStr.append(" [");
    natRuleStr.append(m.getSourcePort());
    natRuleStr.append(" ] -> ");
    natRuleStr.append(m.getDestinationIpAddresses());
    natRuleStr.append(" [");
    natRuleStr.append(m.getDestinationPort());
    natRuleStr.append(" ]) -->");
    if ("SourceNatRule".equals(rule.getType())) {
        natRuleStr.append(((SourceNatRule) rule).getToSourceIpAddressMin());
        natRuleStr.append("-");
        natRuleStr.append(((SourceNatRule) rule).getToSourceIpAddressMax());
        natRuleStr.append(" [");
        natRuleStr.append(((SourceNatRule) rule).getToSourcePort());
        natRuleStr.append(" ])");
    } else {
        natRuleStr.append(((DestinationNatRule) rule).getToDestinationIpAddress());
        natRuleStr.append(" [");
        natRuleStr.append(((DestinationNatRule) rule).getToDestinationPort());
        natRuleStr.append(" ])");
    }
    return natRuleStr.toString();
}
Also used : Match(com.cloud.network.nicira.Match)

Aggregations

Match (com.cloud.network.nicira.Match)6 SourceNatRule (com.cloud.network.nicira.SourceNatRule)4 CreateLogicalRouterAnswer (com.cloud.agent.api.CreateLogicalRouterAnswer)2 DestinationNatRule (com.cloud.network.nicira.DestinationNatRule)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 NatRule (com.cloud.network.nicira.NatRule)2 NiciraNvpApi (com.cloud.network.nicira.NiciraNvpApi)2 NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)2 NiciraNvpTag (com.cloud.network.nicira.NiciraNvpTag)2 PatchAttachment (com.cloud.network.nicira.PatchAttachment)2 RouterNextHop (com.cloud.network.nicira.RouterNextHop)2 SingleDefaultRouteImplicitRoutingConfig (com.cloud.network.nicira.SingleDefaultRouteImplicitRoutingConfig)2 CommandRetryUtility (com.cloud.network.utils.CommandRetryUtility)2 ArrayList (java.util.ArrayList)2