Search in sources :

Example 26 with NiciraNvpApiException

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

the class NiciraNvpResourceTest method testCreateLogicalRouterApiExceptionRollbackRouterAndSwitchPort.

@Test
public void testCreateLogicalRouterApiExceptionRollbackRouterAndSwitchPort() throws ConfigurationException, NiciraNvpApiException {
    resource.configure("NiciraNvpResource", parameters);
    final LogicalRouter lrc = mock(LogicalRouter.class);
    final LogicalRouterPort lrp = mock(LogicalRouterPort.class);
    final LogicalSwitchPort lsp = mock(LogicalSwitchPort.class);
    when(lrc.getUuid()).thenReturn("ccccc");
    when(lrp.getUuid()).thenReturn("ddddd").thenReturn("eeeee");
    when(lsp.getUuid()).thenReturn("fffff");
    when(nvpApi.createLogicalRouter((LogicalRouter) any())).thenReturn(lrc);
    when(nvpApi.createLogicalRouterPort(eq("ccccc"), (LogicalRouterPort) any())).thenReturn(lrp);
    when(nvpApi.createLogicalSwitchPort(eq("bbbbb"), (LogicalSwitchPort) any())).thenReturn(lsp);
    when(nvpApi.createLogicalRouterNatRule((String) any(), (NatRule) any())).thenThrow(new NiciraNvpApiException());
    final CreateLogicalRouterCommand clrc = new CreateLogicalRouterCommand("aaaaa", 50, "bbbbb", "lrouter", "publiccidr", "nexthop", "internalcidr", "owner");
    final CreateLogicalRouterAnswer clra = (CreateLogicalRouterAnswer) resource.executeRequest(clrc);
    assertFalse(clra.getResult());
    verify(nvpApi, atLeast(1)).deleteLogicalRouter(eq("ccccc"));
    verify(nvpApi, atLeast(1)).deleteLogicalSwitchPort(eq("bbbbb"), eq("fffff"));
}
Also used : CreateLogicalRouterCommand(com.cloud.agent.api.CreateLogicalRouterCommand) LogicalRouterPort(com.cloud.network.nicira.LogicalRouterPort) LogicalRouter(com.cloud.network.nicira.LogicalRouter) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) LogicalSwitchPort(com.cloud.network.nicira.LogicalSwitchPort) Test(org.junit.Test)

Example 27 with NiciraNvpApiException

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

the class NiciraNvpResourceTest method testConfigurePortForwardingRulesOnLogicalRouterRollback.

@Test
public void testConfigurePortForwardingRulesOnLogicalRouterRollback() 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, false);
    final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>();
    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]).thenThrow(new NiciraNvpApiException());
    // Mock the api find call
    @SuppressWarnings("unchecked") final List<NatRule> storedRules = Collections.EMPTY_LIST;
    when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules);
    final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) resource.executeRequest(cmd);
    assertFalse(a.getResult());
    verify(nvpApi, atLeastOnce()).deleteLogicalRouterNatRule(eq("aaaaa"), eq(rulepair[0].getUuid()));
}
Also used : PortForwardingRuleTO(com.cloud.agent.api.to.PortForwardingRuleTO) ConfigurePortForwardingRulesOnLogicalRouterCommand(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand) 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) ConfigurePortForwardingRulesOnLogicalRouterAnswer(com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer) Test(org.junit.Test)

Example 28 with NiciraNvpApiException

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

the class NiciraNvpUpdateLogicalSwitchPortCommandWrapper method execute.

@Override
public Answer execute(final UpdateLogicalSwitchPortCommand command, final NiciraNvpResource niciraNvpResource) {
    final NiciraNvpUtilities niciraNvpUtilities = niciraNvpResource.getNiciraNvpUtilities();
    final String logicalSwitchUuid = command.getLogicalSwitchUuid();
    final String logicalSwitchPortUuid = command.getLogicalSwitchPortUuid();
    final String attachmentUuid = command.getAttachmentUuid();
    final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
    try {
        // Tags set to scope cs_account and account name
        final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
        tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
        final VifAttachment vifAttachment = niciraNvpUtilities.createVifAttachment(attachmentUuid);
        niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, logicalSwitchPortUuid, vifAttachment);
        return new UpdateLogicalSwitchPortAnswer(command, true, "Attachment for  " + logicalSwitchPortUuid + " updated", logicalSwitchPortUuid);
    } catch (final NiciraNvpApiException e) {
        final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
        retryUtility.addRetry(command, NUM_RETRIES);
        return retryUtility.retry(command, UpdateLogicalSwitchPortAnswer.class, e);
    }
}
Also used : NiciraNvpUtilities(com.cloud.network.resource.NiciraNvpUtilities) UpdateLogicalSwitchPortAnswer(com.cloud.agent.api.UpdateLogicalSwitchPortAnswer) NiciraNvpApi(com.cloud.network.nicira.NiciraNvpApi) NiciraNvpTag(com.cloud.network.nicira.NiciraNvpTag) ArrayList(java.util.ArrayList) CommandRetryUtility(com.cloud.network.utils.CommandRetryUtility) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) VifAttachment(com.cloud.network.nicira.VifAttachment)

Example 29 with NiciraNvpApiException

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

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.agent.api.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.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer)

Example 30 with NiciraNvpApiException

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

the class NiciraNvpConfigureSharedNetworkUuidCommandWrapper method execute.

@Override
public Answer execute(ConfigureSharedNetworkUuidCommand command, NiciraNvpResource niciraNvpResource) {
    final String logicalRouterUuid = command.getLogicalRouterUuid();
    final String logicalSwitchUuid = command.getLogicalSwitchUuid();
    final String portIpAddress = command.getPortIpAddress();
    final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
    tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
    final long networkId = command.getNetworkId();
    final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
    s_logger.debug("Attaching Logical Switch " + logicalSwitchUuid + " on Logical Router " + logicalRouterUuid + " for Shared Network " + networkId);
    //Step 1: Get lSwitch displayName
    s_logger.info("Looking for Logical Switch " + logicalSwitchUuid + " display name");
    String logicalSwitchDisplayName;
    try {
        List<LogicalSwitch> lSwitchList = niciraNvpApi.findLogicalSwitch(logicalSwitchUuid);
        if (lSwitchList != null) {
            if (lSwitchList.size() == 1) {
                logicalSwitchDisplayName = lSwitchList.get(0).getDisplayName();
            } else {
                s_logger.error("More than one Logical Switch found with uuid " + logicalSwitchUuid);
                throw new CloudRuntimeException("More than one Logical Switch found with uuid=" + logicalSwitchUuid);
            }
        } else {
            s_logger.error("Logical Switch " + logicalSwitchUuid + " not found");
            throw new CloudRuntimeException("Logical Switch " + logicalSwitchUuid + " not found");
        }
    } catch (NiciraNvpApiException e) {
        s_logger.warn("Logical Switch " + logicalSwitchUuid + " not found, retrying");
        final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
        retryUtility.addRetry(command, NUM_RETRIES);
        return retryUtility.retry(command, ConfigureSharedNetworkUuidAnswer.class, e);
    } catch (CloudRuntimeException e) {
        s_logger.info("Shared network UUID vlan id failed due to : " + e.getMessage());
        return new ConfigureSharedNetworkUuidAnswer(command, false, e.getMessage());
    }
    s_logger.info("Found display name " + logicalSwitchDisplayName + " for Logical Switch " + logicalSwitchUuid);
    //Step 2: Create lRouterPort
    s_logger.debug("Creating Logical Router Port in Logical Router " + logicalRouterUuid);
    LogicalRouterPort lRouterPort = null;
    try {
        lRouterPort = new LogicalRouterPort();
        lRouterPort.setAdminStatusEnabled(true);
        lRouterPort.setDisplayName(niciraNvpResource.truncate(logicalSwitchDisplayName + "-uplink", NAME_MAX_LEN));
        lRouterPort.setTags(tags);
        final List<String> ipAddresses = new ArrayList<String>();
        ipAddresses.add(portIpAddress);
        lRouterPort.setIpAddresses(ipAddresses);
        lRouterPort = niciraNvpApi.createLogicalRouterPort(logicalRouterUuid, lRouterPort);
    } catch (NiciraNvpApiException e) {
        s_logger.warn("Could not create Logical Router Port on Logical Router " + logicalRouterUuid + " due to: " + e.getMessage() + ", retrying");
        return handleException(e, command, niciraNvpResource);
    }
    s_logger.debug("Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") successfully created in Logical Router " + logicalRouterUuid);
    //Step 3: Create lSwitchPort
    s_logger.debug("Creating Logical Switch Port in Logical Switch " + logicalSwitchUuid + " (" + logicalSwitchDisplayName + ")");
    LogicalSwitchPort lSwitchPort = null;
    try {
        lSwitchPort = new LogicalSwitchPort(niciraNvpResource.truncate("lrouter-uplink", NAME_MAX_LEN), tags, true);
        lSwitchPort = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lSwitchPort);
    } catch (NiciraNvpApiException e) {
        s_logger.warn("Could not create Logical Switch Port on Logical Switch " + logicalSwitchUuid + " (" + logicalSwitchDisplayName + ")  due to: " + e.getMessage());
        cleanupLRouterPort(logicalRouterUuid, lRouterPort, niciraNvpApi);
        return handleException(e, command, niciraNvpResource);
    }
    s_logger.debug("Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") successfully created in Logical Switch " + logicalSwitchUuid + " (" + logicalSwitchDisplayName + ")");
    //Step 4: Attach lRouterPort to lSwitchPort with a PatchAttachment
    s_logger.debug("Attaching Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") to Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") with a PatchAttachment");
    try {
        niciraNvpApi.updateLogicalRouterPortAttachment(logicalRouterUuid, lRouterPort.getUuid(), new PatchAttachment(lSwitchPort.getUuid()));
    } catch (NiciraNvpApiException e) {
        s_logger.warn("Could not attach Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") to Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") due to: " + e.getMessage() + ", retrying");
        cleanupLRouterPort(logicalRouterUuid, lRouterPort, niciraNvpApi);
        cleanupLSwitchPort(logicalSwitchUuid, lSwitchPort, niciraNvpApi);
        return handleException(e, command, niciraNvpResource);
    }
    s_logger.debug("Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") successfully attached to to Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") with a PatchAttachment");
    //Step 5: Attach lSwitchPort to lRouterPort with a PatchAttachment
    s_logger.debug("Attaching Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") to Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") with a PatchAttachment");
    try {
        niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, lSwitchPort.getUuid(), new PatchAttachment(lRouterPort.getUuid()));
    } catch (NiciraNvpApiException e) {
        s_logger.warn("Could not attach Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") to Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") due to: " + e.getMessage() + ", retrying");
        cleanupLRouterPort(logicalRouterUuid, lRouterPort, niciraNvpApi);
        cleanupLSwitchPort(logicalSwitchUuid, lSwitchPort, niciraNvpApi);
        return handleException(e, command, niciraNvpResource);
    }
    s_logger.debug("Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") successfully attached to to Logical Router Port " + lRouterPort.getUuid() + " (" + lRouterPort.getDisplayName() + ") with a PatchAttachment");
    s_logger.info("Successfully attached Logical Switch " + logicalSwitchUuid + " on Logical Router " + logicalRouterUuid + " for Shared Network " + networkId);
    return new ConfigureSharedNetworkUuidAnswer(command, true, "OK");
}
Also used : ConfigureSharedNetworkUuidAnswer(com.cloud.agent.api.ConfigureSharedNetworkUuidAnswer) LogicalRouterPort(com.cloud.network.nicira.LogicalRouterPort) LogicalSwitch(com.cloud.network.nicira.LogicalSwitch) ArrayList(java.util.ArrayList) PatchAttachment(com.cloud.network.nicira.PatchAttachment) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NiciraNvpTag(com.cloud.network.nicira.NiciraNvpTag) NiciraNvpApi(com.cloud.network.nicira.NiciraNvpApi) CommandRetryUtility(com.cloud.network.utils.CommandRetryUtility) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) LogicalSwitchPort(com.cloud.network.nicira.LogicalSwitchPort)

Aggregations

NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)39 Test (org.junit.Test)23 NiciraNvpApi (com.cloud.network.nicira.NiciraNvpApi)21 CommandRetryUtility (com.cloud.network.utils.CommandRetryUtility)13 LogicalSwitchPort (com.cloud.network.nicira.LogicalSwitchPort)8 ArrayList (java.util.ArrayList)8 LogicalRouterPort (com.cloud.network.nicira.LogicalRouterPort)7 Answer (com.cloud.agent.api.Answer)6 LogicalSwitch (com.cloud.network.nicira.LogicalSwitch)5 NiciraNvpTag (com.cloud.network.nicira.NiciraNvpTag)5 CreateLogicalRouterAnswer (com.cloud.agent.api.CreateLogicalRouterAnswer)4 NatRule (com.cloud.network.nicira.NatRule)4 ConfigurePublicIpsOnLogicalRouterAnswer (com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer)3 ConfigurePublicIpsOnLogicalRouterCommand (com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand)3 CreateLogicalRouterCommand (com.cloud.agent.api.CreateLogicalRouterCommand)3 CreateLogicalSwitchAnswer (com.cloud.agent.api.CreateLogicalSwitchAnswer)3 CreateLogicalSwitchCommand (com.cloud.agent.api.CreateLogicalSwitchCommand)3 CreateLogicalSwitchPortAnswer (com.cloud.agent.api.CreateLogicalSwitchPortAnswer)3 ControlClusterStatus (com.cloud.network.nicira.ControlClusterStatus)3 VifAttachment (com.cloud.network.nicira.VifAttachment)3