Search in sources :

Example 1 with CreateLogicalRouterAnswer

use of com.cloud.agent.api.CreateLogicalRouterAnswer in project cloudstack by apache.

the class NiciraNvpResourceTest method testCreateLogicalRouterApiExceptionRollbackRouter.

@Test
public void testCreateLogicalRouterApiExceptionRollbackRouter() throws ConfigurationException, NiciraNvpApiException {
    resource.configure("NiciraNvpResource", parameters);
    final LogicalRouter lrc = mock(LogicalRouter.class);
    when(lrc.getUuid()).thenReturn("ccccc");
    when(nvpApi.createLogicalRouter((LogicalRouter) any())).thenReturn(lrc);
    when(nvpApi.createLogicalRouterPort(eq("ccccc"), (LogicalRouterPort) 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"));
}
Also used : CreateLogicalRouterCommand(com.cloud.agent.api.CreateLogicalRouterCommand) LogicalRouter(com.cloud.network.nicira.LogicalRouter) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) Test(org.junit.Test)

Example 2 with CreateLogicalRouterAnswer

use of com.cloud.agent.api.CreateLogicalRouterAnswer in project cloudstack by apache.

the class NiciraNvpElement method implement.

@Override
public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    s_logger.debug("entering NiciraNvpElement implement function for network " + network.getDisplayText() + " (state " + network.getState() + ")");
    if (!canHandle(network, Service.Connectivity)) {
        return false;
    }
    if (network.getBroadcastUri() == null) {
        s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid");
        return false;
    }
    List<NiciraNvpDeviceVO> devices = niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
    if (devices.isEmpty()) {
        s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId());
        return false;
    }
    NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
    HostVO niciraNvpHost = hostDao.findById(niciraNvpDevice.getHostId());
    hostDao.loadDetails(niciraNvpHost);
    Account owner = context.getAccount();
    if (network.getGuestType().equals(GuestType.Shared)) {
        //Support Shared Networks
        String lSwitchUuid = BroadcastDomainType.getValue(network.getBroadcastUri());
        String ownerName = context.getDomain().getName() + "-" + context.getAccount().getAccountName();
        return sharedNetworkSupport(network, lSwitchUuid, ownerName, niciraNvpHost);
    } else if (network.getGuestType().equals(GuestType.Isolated) && networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.SourceNat, Provider.NiciraNvp)) {
        // Implement SourceNat immediately as we have al the info already
        s_logger.debug("Apparently we are supposed to provide SourceNat on this network");
        PublicIp sourceNatIp = ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(owner, network);
        String publicCidr = sourceNatIp.getAddress().addr() + "/" + NetUtils.getCidrSize(sourceNatIp.getVlanNetmask());
        String internalCidr = network.getGateway() + "/" + network.getCidr().split("/")[1];
        // assuming a vlan:
        String vtag = sourceNatIp.getVlanTag();
        BroadcastDomainType tiep = null;
        try {
            tiep = BroadcastDomainType.getTypeOf(vtag);
        } catch (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);
        }
        long vlanid = (Vlan.UNTAGGED.equals(vtag)) ? 0 : Long.parseLong(vtag);
        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());
        CreateLogicalRouterAnswer answer = (CreateLogicalRouterAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
        if (answer.getResult() == false) {
            s_logger.error("Failed to create Logical Router for network " + network.getDisplayText());
            return false;
        }
        NiciraNvpRouterMappingVO routermapping = new NiciraNvpRouterMappingVO(answer.getLogicalRouterUuid(), network.getId());
        niciraNvpRouterMappingDao.persist(routermapping);
    }
    return true;
}
Also used : Account(com.cloud.user.Account) CreateLogicalRouterCommand(com.cloud.agent.api.CreateLogicalRouterCommand) BroadcastDomainType(com.cloud.network.Networks.BroadcastDomainType) PublicIp(com.cloud.network.addr.PublicIp) NiciraNvpRouterMappingVO(com.cloud.network.NiciraNvpRouterMappingVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NiciraNvpDeviceVO(com.cloud.network.NiciraNvpDeviceVO) URISyntaxException(java.net.URISyntaxException) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) HostVO(com.cloud.host.HostVO)

Example 3 with CreateLogicalRouterAnswer

use of com.cloud.agent.api.CreateLogicalRouterAnswer 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 4 with CreateLogicalRouterAnswer

use of com.cloud.agent.api.CreateLogicalRouterAnswer in project cloudstack by apache.

the class NiciraNvpElementTest method implementIsolatedNetworkTest.

@Test
public void implementIsolatedNetworkTest() throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException, URISyntaxException {
    final Network network = mock(Network.class);
    when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch);
    when(network.getBroadcastUri()).thenReturn(new URI("lswitch:aaaaa"));
    when(network.getId()).thenReturn(NETWORK_ID);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    when(network.getGuestType()).thenReturn(GuestType.Isolated);
    when(networkModel.isProviderForNetwork(Provider.NiciraNvp, NETWORK_ID)).thenReturn(true);
    when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NiciraNvp)).thenReturn(true);
    final NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
    when(niciraNvpDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NiciraNvpDeviceVO[] { device }));
    when(device.getId()).thenReturn(1L);
    when(device.getHostId()).thenReturn(NICIRA_NVP_HOST_ID);
    HostVO niciraNvpHost = mock(HostVO.class);
    when(niciraNvpHost.getId()).thenReturn(NICIRA_NVP_HOST_ID);
    when(hostDao.findById(NICIRA_NVP_HOST_ID)).thenReturn(niciraNvpHost);
    final NetworkOffering offering = mock(NetworkOffering.class);
    when(offering.getId()).thenReturn(NETWORK_ID);
    when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
    when(offering.getGuestType()).thenReturn(GuestType.Isolated);
    final DeployDestination dest = mock(DeployDestination.class);
    final Domain dom = mock(Domain.class);
    when(dom.getName()).thenReturn("domain");
    final Account acc = mock(Account.class);
    when(acc.getAccountName()).thenReturn("accountname");
    final ReservationContext context = mock(ReservationContext.class);
    when(context.getDomain()).thenReturn(dom);
    when(context.getAccount()).thenReturn(acc);
    //ISOLATED NETWORK
    when(networkModel.isProviderSupportServiceInNetwork(NETWORK_ID, Service.SourceNat, Provider.NiciraNvp)).thenReturn(true);
    PublicIp sourceNatIp = mock(PublicIp.class);
    Ip ip = mock(Ip.class);
    when(ip.addr()).thenReturn("10.0.0.0");
    when(sourceNatIp.getAddress()).thenReturn(ip);
    when(sourceNatIp.getVlanNetmask()).thenReturn("255.255.255.0");
    when(sourceNatIp.getVlanTag()).thenReturn("111");
    when(ipAddressManager.assignSourceNatIpAddressToGuestNetwork(acc, network)).thenReturn(sourceNatIp);
    when(network.getGateway()).thenReturn(NETWORK_GATEWAY);
    when(network.getCidr()).thenReturn(NETWORK_CIDR);
    final CreateLogicalRouterAnswer answer = mock(CreateLogicalRouterAnswer.class);
    when(answer.getResult()).thenReturn(true);
    when(agentManager.easySend(eq(NICIRA_NVP_HOST_ID), (Command) any())).thenReturn(answer);
    assertTrue(element.implement(network, offering, dest, context));
}
Also used : Account(com.cloud.user.Account) NetworkOffering(com.cloud.offering.NetworkOffering) PublicIp(com.cloud.network.addr.PublicIp) DeployDestination(com.cloud.deploy.DeployDestination) Network(com.cloud.network.Network) NiciraNvpDeviceVO(com.cloud.network.NiciraNvpDeviceVO) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) Domain(com.cloud.domain.Domain) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) URI(java.net.URI) HostVO(com.cloud.host.HostVO) ReservationContext(com.cloud.vm.ReservationContext) Test(org.junit.Test)

Example 5 with CreateLogicalRouterAnswer

use of com.cloud.agent.api.CreateLogicalRouterAnswer in project cloudstack by apache.

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());
}
Also used : CreateLogicalRouterCommand(com.cloud.agent.api.CreateLogicalRouterCommand) NiciraNvpApiException(com.cloud.network.nicira.NiciraNvpApiException) CreateLogicalRouterAnswer(com.cloud.agent.api.CreateLogicalRouterAnswer) Test(org.junit.Test)

Aggregations

CreateLogicalRouterAnswer (com.cloud.agent.api.CreateLogicalRouterAnswer)7 CreateLogicalRouterCommand (com.cloud.agent.api.CreateLogicalRouterCommand)5 Test (org.junit.Test)5 LogicalRouter (com.cloud.network.nicira.LogicalRouter)4 NiciraNvpApiException (com.cloud.network.nicira.NiciraNvpApiException)4 LogicalRouterPort (com.cloud.network.nicira.LogicalRouterPort)3 LogicalSwitchPort (com.cloud.network.nicira.LogicalSwitchPort)3 HostVO (com.cloud.host.HostVO)2 NiciraNvpDeviceVO (com.cloud.network.NiciraNvpDeviceVO)2 PublicIp (com.cloud.network.addr.PublicIp)2 Account (com.cloud.user.Account)2 DeployDestination (com.cloud.deploy.DeployDestination)1 Domain (com.cloud.domain.Domain)1 Network (com.cloud.network.Network)1 BroadcastDomainType (com.cloud.network.Networks.BroadcastDomainType)1 NiciraNvpRouterMappingVO (com.cloud.network.NiciraNvpRouterMappingVO)1 L3GatewayAttachment (com.cloud.network.nicira.L3GatewayAttachment)1 Match (com.cloud.network.nicira.Match)1 NiciraNvpApi (com.cloud.network.nicira.NiciraNvpApi)1 NiciraNvpTag (com.cloud.network.nicira.NiciraNvpTag)1