use of com.cloud.network.nicira.NiciraNvpTag 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);
}
}
use of com.cloud.network.nicira.NiciraNvpTag in project cloudstack by apache.
the class NiciraNvpCreateLogicalSwitchCommandWrapper method execute.
@Override
public Answer execute(final CreateLogicalSwitchCommand command, final NiciraNvpResource niciraNvpResource) {
final NiciraNvpUtilities niciraNvpUtilities = niciraNvpResource.getNiciraNvpUtilities();
LogicalSwitch logicalSwitch = niciraNvpUtilities.createLogicalSwitch();
logicalSwitch.setDisplayName(niciraNvpResource.truncate("lswitch-" + command.getName(), NiciraNvpResource.NAME_MAX_LEN));
logicalSwitch.setPortIsolationEnabled(false);
// Set transport binding
final List<TransportZoneBinding> ltzb = new ArrayList<TransportZoneBinding>();
ltzb.add(new TransportZoneBinding(command.getTransportUuid(), command.getTransportType()));
logicalSwitch.setTransportZones(ltzb);
// Tags set to scope cs_account and account name
final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
logicalSwitch.setTags(tags);
try {
final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
logicalSwitch = niciraNvpApi.createLogicalSwitch(logicalSwitch);
final String switchUuid = logicalSwitch.getUuid();
return new CreateLogicalSwitchAnswer(command, true, "Logicalswitch " + switchUuid + " created", switchUuid);
} catch (final NiciraNvpApiException e) {
final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
retryUtility.addRetry(command, NUM_RETRIES);
return retryUtility.retry(command, CreateLogicalSwitchAnswer.class, e);
}
}
use of com.cloud.network.nicira.NiciraNvpTag 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);
}
}
use of com.cloud.network.nicira.NiciraNvpTag 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");
}
use of com.cloud.network.nicira.NiciraNvpTag in project cloudstack by apache.
the class NiciraNvpConfigureSharedNetworkVlanIdCommandWrapper method execute.
@Override
public Answer execute(ConfigureSharedNetworkVlanIdCommand command, NiciraNvpResource niciraNvpResource) {
final String logicalSwitchUuid = command.getLogicalSwitchUuid();
final String l2GatewayServiceUuid = command.getL2GatewayServiceUuid();
long vlanId = command.getVlanId();
final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
final long networkId = command.getNetworkId();
s_logger.debug("Connecting Logical Switch " + logicalSwitchUuid + " to L2 Gateway Service " + l2GatewayServiceUuid + ", vlan id " + vlanId + " network " + networkId);
final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
s_logger.debug("Creating Logical Switch Port in Logical Switch " + logicalSwitchUuid);
LogicalSwitchPort lSwitchPort = null;
try {
lSwitchPort = new LogicalSwitchPort();
lSwitchPort.setAdminStatusEnabled(true);
lSwitchPort.setDisplayName(niciraNvpResource.truncate(networkId + "-l2Gateway-port", NAME_MAX_LEN));
lSwitchPort.setTags(tags);
lSwitchPort = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lSwitchPort);
} catch (NiciraNvpApiException e) {
s_logger.warn("Could not create Logical Switch Port on Logical Switch " + logicalSwitchUuid + " due to: " + e.getMessage() + ", retrying");
return handleException(e, command, niciraNvpResource);
}
s_logger.debug("Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") successfully created in Logical Switch " + logicalSwitchUuid);
s_logger.debug("Attaching Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") on VLAN " + command.getVlanId() + " using L2GatewayAttachment");
try {
final L2GatewayAttachment attachment = new L2GatewayAttachment(l2GatewayServiceUuid);
if (command.getVlanId() != 0) {
attachment.setVlanId(command.getVlanId());
}
niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, lSwitchPort.getUuid(), attachment);
} catch (NiciraNvpApiException e) {
s_logger.warn("Could not attach Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") to Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") due to: " + e.getMessage() + ", errorCode: " + e.getErrorCode());
cleanup(logicalSwitchUuid, lSwitchPort, niciraNvpApi);
return handleException(e, command, niciraNvpResource);
}
s_logger.debug("Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ") successfully attached on VLAN " + command.getVlanId() + " using L2GatewayAttachment");
s_logger.debug("Successfully connected Logical Switch " + logicalSwitchUuid + " to L2 Gateway Service " + l2GatewayServiceUuid + ", vlan id " + vlanId + ", network " + networkId + ", through Logical Switch Port " + lSwitchPort.getUuid() + " (" + lSwitchPort.getDisplayName() + ")");
return new ConfigureSharedNetworkVlanIdAnswer(command, true, "OK");
}
Aggregations