use of com.cloud.legacymodel.communication.answer.CreateLogicalRouterAnswer in project cosmic by MissionCriticalCloud.
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"));
}
use of com.cloud.legacymodel.communication.answer.CreateLogicalRouterAnswer 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, 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);
}
}
use of com.cloud.legacymodel.communication.answer.CreateLogicalRouterAnswer in project cosmic by MissionCriticalCloud.
the class NiciraNvpElement method implement.
@Override
public boolean implement(final Network network, final NetworkOffering offering, final DeployDestination dest, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
s_logger.debug("entering NiciraNvpElement implement function for network " + network.getDisplayText() + " (state " + network.getState() + ")");
if (!canHandle(network, Network.Service.Connectivity)) {
return false;
}
if (network.getBroadcastUri() == null) {
s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid");
return false;
}
final List<NiciraNvpDeviceVO> devices = this.niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
if (devices.isEmpty()) {
s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId());
return false;
}
final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
final HostVO niciraNvpHost = this.hostDao.findById(niciraNvpDevice.getHostId());
this.hostDao.loadDetails(niciraNvpHost);
final Account owner = context.getAccount();
// Implement SourceNat immediately as we have al the info already
if (this.networkModel.isProviderSupportServiceInNetwork(network.getId(), Network.Service.SourceNat, Network.Provider.NiciraNvp)) {
s_logger.debug("Apparently we are supposed to provide SourceNat on this network");
final PublicIp sourceNatIp = this.ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(owner, network);
final String publicCidr = sourceNatIp.getAddress().addr() + "/" + NetUtils.getCidrSize(sourceNatIp.getVlanNetmask());
final String internalCidr = network.getGateway() + "/" + network.getCidr().split("/")[1];
// assuming a vlan:
String vtag = sourceNatIp.getVlanTag();
BroadcastDomainType tiep = null;
try {
tiep = BroadcastDomainType.getTypeOf(vtag);
} catch (final URISyntaxException use) {
throw new CloudRuntimeException("vlantag for sourceNatIp is not valid: " + vtag, use);
}
if (tiep == BroadcastDomainType.Vlan) {
vtag = BroadcastDomainType.Vlan.getValueFrom(BroadcastDomainType.fromString(vtag));
} else if (!(tiep == BroadcastDomainType.UnDecided || tiep == BroadcastDomainType.Native)) {
throw new CloudRuntimeException("only vlans are supported for sourceNatIp, at this moment: " + vtag);
}
final long vlanid = Vlan.UNTAGGED.equals(vtag) ? 0 : Long.parseLong(vtag);
final CreateLogicalRouterCommand cmd = new CreateLogicalRouterCommand(niciraNvpHost.getDetail("l3gatewayserviceuuid"), vlanid, BroadcastDomainType.getValue(network.getBroadcastUri()), "router-" + network.getDisplayText(), publicCidr, sourceNatIp.getGateway(), internalCidr, context.getDomain().getName() + "-" + context.getAccount().getAccountName());
final CreateLogicalRouterAnswer answer = (CreateLogicalRouterAnswer) this.agentMgr.easySend(niciraNvpHost.getId(), cmd);
if (answer.getResult() == false) {
s_logger.error("Failed to create Logical Router for network " + network.getDisplayText());
return false;
}
// Store the uuid so we can easily find it during cleanup
final NiciraNvpRouterMappingVO routermapping = new NiciraNvpRouterMappingVO(answer.getLogicalRouterUuid(), network.getId());
this.niciraNvpRouterMappingDao.persist(routermapping);
}
return true;
}
use of com.cloud.legacymodel.communication.answer.CreateLogicalRouterAnswer in project cosmic by MissionCriticalCloud.
the class NiciraNvpResourceTest method testCreateLogicalRouter.
@Test
public void testCreateLogicalRouter() 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);
final CreateLogicalRouterCommand clrc = new CreateLogicalRouterCommand("aaaaa", 50, "bbbbb", "lrouter", "publiccidr", "nexthop", "internalcidr", "owner");
final CreateLogicalRouterAnswer clra = (CreateLogicalRouterAnswer) resource.executeRequest(clrc);
assertTrue(clra.getResult());
assertTrue("ccccc".equals(clra.getLogicalRouterUuid()));
verify(nvpApi, atLeast(1)).createLogicalRouterNatRule((String) any(), (NatRule) any());
}
use of com.cloud.legacymodel.communication.answer.CreateLogicalRouterAnswer in project cosmic by MissionCriticalCloud.
the class NiciraNvpResourceTest method testCreateLogicalRouterApiException.
@Test
public void testCreateLogicalRouterApiException() throws ConfigurationException, NiciraNvpApiException {
resource.configure("NiciraNvpResource", parameters);
when(nvpApi.createLogicalRouter((LogicalRouter) 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());
}
Aggregations