Search in sources :

Example 1 with RouterPort

use of org.midonet.client.resource.RouterPort in project cloudstack by apache.

the class MidoNetElementTest method testImplement.

/*
     * Test the standard case of implement with no errors.
     */
public void testImplement() {
    //mock
    MidonetApi api = mock(MidonetApi.class, RETURNS_DEEP_STUBS);
    //mockAccountDao
    AccountDao mockAccountDao = mock(AccountDao.class);
    AccountVO mockAccountVO = mock(AccountVO.class);
    when(mockAccountDao.findById(anyLong())).thenReturn(mockAccountVO);
    when(mockAccountVO.getUuid()).thenReturn("1");
    MidoNetElement elem = new MidoNetElement();
    elem.setMidonetApi(api);
    elem.setAccountDao(mockAccountDao);
    //mockRPort
    RouterPort mockRPort = mock(RouterPort.class);
    when(mockRPort.getId()).thenReturn(UUID.fromString("550e8400-e29b-41d4-a716-446655440000"));
    //mockBPort
    BridgePort mockBPort = mock(BridgePort.class);
    when(mockBPort.link(any(UUID.class))).thenReturn(mockBPort);
    //mockPort
    Port mockPort = mock(Port.class);
    ResourceCollection<Port> peerPorts = new ResourceCollection<Port>(new ArrayList<Port>());
    peerPorts.add(mockPort);
    //mockBridge
    Bridge mockBridge = mock(Bridge.class, RETURNS_DEEP_STUBS);
    when(api.addBridge().tenantId(anyString()).name(anyString()).create()).thenReturn(mockBridge);
    when(mockBridge.addInteriorPort().create()).thenReturn(mockBPort);
    when(mockBridge.getPeerPorts()).thenReturn(peerPorts);
    //mockRouter
    Router mockRouter = mock(Router.class, RETURNS_DEEP_STUBS);
    when(api.addRouter().tenantId(anyString()).name(anyString()).create()).thenReturn(mockRouter);
    when(mockRouter.addInteriorRouterPort().create()).thenReturn(mockRPort);
    //mockNetwork
    Network mockNetwork = mock(Network.class);
    when(mockNetwork.getAccountId()).thenReturn((long) 1);
    when(mockNetwork.getGateway()).thenReturn("1.2.3.4");
    when(mockNetwork.getCidr()).thenReturn("1.2.3.0/24");
    when(mockNetwork.getId()).thenReturn((long) 2);
    when(mockNetwork.getBroadcastDomainType()).thenReturn(Networks.BroadcastDomainType.Mido);
    when(mockNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Public);
    boolean result = false;
    try {
        result = elem.implement(mockNetwork, null, null, null);
    } catch (ConcurrentOperationException e) {
        fail(e.getMessage());
    } catch (InsufficientCapacityException e) {
        fail(e.getMessage());
    } catch (ResourceUnavailableException e) {
        fail(e.getMessage());
    }
    assertEquals(result, true);
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) RouterPort(org.midonet.client.resource.RouterPort) BridgePort(org.midonet.client.resource.BridgePort) Port(org.midonet.client.resource.Port) Router(org.midonet.client.resource.Router) AccountDao(com.cloud.user.dao.AccountDao) AccountVO(com.cloud.user.AccountVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) MidonetApi(org.midonet.client.MidonetApi) Network(com.cloud.network.Network) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) UUID(java.util.UUID) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) RouterPort(org.midonet.client.resource.RouterPort) Bridge(org.midonet.client.resource.Bridge) ResourceCollection(org.midonet.client.resource.ResourceCollection)

Example 2 with RouterPort

use of org.midonet.client.resource.RouterPort in project cloudstack by apache.

the class MidoNetElement method deleteGuestNetworkRouters.

private void deleteGuestNetworkRouters(Network network) {
    String accountUuid = getAccountUuid(network);
    boolean isVpc = getIsVpc(network);
    long id = getRouterId(network, isVpc);
    Router tenantRouter = getGuestNetworkRouter(id, accountUuid, isVpc);
    // Delete any peer ports corresponding to this router
    for (Port peerPort : tenantRouter.getPeerPorts((new MultivaluedMapImpl()))) {
        if (peerPort != null && peerPort instanceof RouterPort) {
            RouterPort checkPort = (RouterPort) peerPort;
            if (checkPort.getType().equals("ExteriorRouter")) {
                checkPort.vifId(null).update();
            } else if (checkPort.getType().equals("InteriorRouter")) {
                checkPort.unlink();
            }
            checkPort.delete();
        } else if (peerPort != null && peerPort instanceof BridgePort) {
            BridgePort checkPort = (BridgePort) peerPort;
            if (checkPort.getType().equals("ExteriorBridge")) {
                checkPort.vifId(null).update();
            } else if (checkPort.getType().equals("InteriorBridge")) {
                checkPort.unlink();
            }
            checkPort.delete();
        }
    }
    if (tenantRouter != null) {
        // Remove all peer ports if any exist
        for (RouterPort p : tenantRouter.getPorts(new MultivaluedMapImpl())) {
            if (p.getType().equals("ExteriorRouter")) {
                // Set VIF ID to null
                p.vifId(null).update();
            // the port might have some chains associated with it
            }
            if (p.getType().equals("InteriorRouter")) {
                p.unlink();
            }
            // Delete port
            p.delete();
        }
        // Remove inbound and outbound filter chains
        String accountIdStr = String.valueOf(accountUuid);
        String routerName = getRouterName(isVpc, id);
        RuleChain pre = api.getChain(tenantRouter.getInboundFilterId());
        RuleChain preFilter = getChain(accountIdStr, routerName, RuleChainCode.TR_PREFILTER);
        RuleChain preNat = getChain(accountIdStr, routerName, RuleChainCode.TR_PRENAT);
        RuleChain post = api.getChain(tenantRouter.getOutboundFilterId());
        pre.delete();
        preFilter.delete();
        preNat.delete();
        post.delete();
        // Remove routes
        for (Route r : tenantRouter.getRoutes(new MultivaluedMapImpl())) {
            r.delete();
        }
        tenantRouter.delete();
    }
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) RuleChain(org.midonet.client.resource.RuleChain) RouterPort(org.midonet.client.resource.RouterPort) BridgePort(org.midonet.client.resource.BridgePort) Port(org.midonet.client.resource.Port) Router(org.midonet.client.resource.Router) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) RouterPort(org.midonet.client.resource.RouterPort) Route(org.midonet.client.resource.Route)

Example 3 with RouterPort

use of org.midonet.client.resource.RouterPort in project cloudstack by apache.

the class MidoNetElement method applyStaticNats.

/**
     * From interface StaticNatServiceProvider
     */
@Override
public boolean applyStaticNats(Network network, List<? extends StaticNat> rules) throws ResourceUnavailableException {
    s_logger.debug("applyStaticNats called with network: " + network.toString());
    if (!midoInNetwork(network)) {
        return false;
    }
    if (!canHandle(network, Service.StaticNat)) {
        return false;
    }
    boolean resources = false;
    Router tenantRouter = null;
    Router providerRouter = null;
    RouterPort[] ports = null;
    RouterPort tenantUplink = null;
    RouterPort providerDownlink = null;
    RuleChain preFilter = null;
    RuleChain preNat = null;
    RuleChain post = null;
    String accountIdStr = getAccountUuid(network);
    String networkUUIDStr = String.valueOf(network.getId());
    for (StaticNat rule : rules) {
        IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
        String sourceIpAddr = sourceIp.getAddress().addr();
        if (resources == false) {
            tenantRouter = getOrCreateGuestNetworkRouter(network);
            providerRouter = api.getRouter(_providerRouterId);
            ports = getOrCreateProviderRouterPorts(tenantRouter, providerRouter);
            tenantUplink = ports[0];
            providerDownlink = ports[1];
            boolean isVpc = getIsVpc(network);
            long id = getRouterId(network, isVpc);
            String routerName = getRouterName(isVpc, id);
            preFilter = getChain(accountIdStr, routerName, RuleChainCode.TR_PREFILTER);
            preNat = getChain(accountIdStr, routerName, RuleChainCode.TR_PRENAT);
            post = api.getChain(tenantRouter.getOutboundFilterId());
            resources = true;
        }
        if (rule.isForRevoke()) {
            removeMidonetStaticNAT(preFilter, preNat, post, sourceIpAddr, rule.getDestIpAddress(), providerRouter);
        } else {
            addMidonetStaticNAT(preFilter, preNat, post, sourceIpAddr, rule.getDestIpAddress(), tenantUplink, providerDownlink, providerRouter, network);
        }
    }
    return true;
}
Also used : RuleChain(org.midonet.client.resource.RuleChain) Router(org.midonet.client.resource.Router) IpAddress(com.cloud.network.IpAddress) PublicIpAddress(com.cloud.network.PublicIpAddress) RouterPort(org.midonet.client.resource.RouterPort) StaticNat(com.cloud.network.rules.StaticNat)

Example 4 with RouterPort

use of org.midonet.client.resource.RouterPort in project cloudstack by apache.

the class MidoNetElement method cleanBridge.

private void cleanBridge(Bridge br) {
    for (Port peerPort : br.getPeerPorts()) {
        if (peerPort != null && peerPort instanceof RouterPort) {
            RouterPort checkPort = (RouterPort) peerPort;
            if (checkPort.getType().equals("ExteriorRouter")) {
                checkPort.vifId(null).update();
            } else if (checkPort.getType().equals("InteriorRouter")) {
                checkPort.unlink();
            }
            checkPort.delete();
        }
    }
    for (BridgePort p : br.getPorts()) {
        if (p.getType().equals("ExteriorBridge")) {
            // Set VIF ID to null
            p.vifId(null).update();
        }
        if (p.getType().equals("InteriorBridge")) {
            p.unlink();
        }
        // Delete port
        p.delete();
    }
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) RouterPort(org.midonet.client.resource.RouterPort) BridgePort(org.midonet.client.resource.BridgePort) Port(org.midonet.client.resource.Port) RouterPort(org.midonet.client.resource.RouterPort)

Example 5 with RouterPort

use of org.midonet.client.resource.RouterPort in project cloudstack by apache.

the class MidoNetElement method prepare.

@Override
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    s_logger.debug("prepare called with network: " + network.toString() + " nic: " + nic.toString() + " vm: " + vm.toString());
    if (!midoInNetwork(network)) {
        return false;
    }
    if (nic.getTrafficType() == Networks.TrafficType.Guest && !canHandle(network, Service.StaticNat)) {
        return false;
    }
    if (nic.getTrafficType() == Networks.TrafficType.Guest || nic.getTrafficType() == Networks.TrafficType.Public && nic.getBroadcastType() == Networks.BroadcastDomainType.Mido) {
        Bridge netBridge = getOrCreateNetworkBridge(network);
        if (nic.getTrafficType() == Networks.TrafficType.Public && vm.getVirtualMachine().getType() != VirtualMachine.Type.DomainRouter) {
            // Get provider router
            Router providerRouter = api.getRouter(_providerRouterId);
            Port[] ports = getOrCreatePublicBridgePorts(nic, netBridge, providerRouter);
            RouterPort providerDownlink = (RouterPort) ports[1];
            // Set route from router to bridge for this particular IP. Prepare
            // is called in both starting a new VM and restarting a VM, so the
            // NIC may
            boolean routeExists = false;
            for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
                String ip4 = route.getDstNetworkAddr();
                if (ip4 != null && ip4.equals(nic.getIPv4Address())) {
                    routeExists = true;
                    break;
                }
            }
            if (!routeExists) {
                providerRouter.addRoute().type("Normal").weight(100).srcNetworkAddr("0.0.0.0").srcNetworkLength(0).dstNetworkAddr(nic.getIPv4Address()).dstNetworkLength(32).nextHopPort(providerDownlink.getId()).nextHopGateway(null).create();
            }
        }
        // Add port on bridge
        // returns wrapper resource of port
        BridgePort newPort = netBridge.addExteriorPort().create();
        // Set MidoNet port VIF ID to UUID of nic
        UUID nicUUID = getNicUUID(nic);
        newPort.vifId(nicUUID).update();
    }
    return true;
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) RouterPort(org.midonet.client.resource.RouterPort) BridgePort(org.midonet.client.resource.BridgePort) Port(org.midonet.client.resource.Port) Router(org.midonet.client.resource.Router) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) UUID(java.util.UUID) Bridge(org.midonet.client.resource.Bridge) RouterPort(org.midonet.client.resource.RouterPort) Route(org.midonet.client.resource.Route)

Aggregations

RouterPort (org.midonet.client.resource.RouterPort)10 BridgePort (org.midonet.client.resource.BridgePort)7 Port (org.midonet.client.resource.Port)6 Router (org.midonet.client.resource.Router)6 RuleChain (org.midonet.client.resource.RuleChain)5 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)4 PublicIpAddress (com.cloud.network.PublicIpAddress)3 Route (org.midonet.client.resource.Route)3 IpAddress (com.cloud.network.IpAddress)2 UUID (java.util.UUID)2 Bridge (org.midonet.client.resource.Bridge)2 PortForwardingRuleTO (com.cloud.agent.api.to.PortForwardingRuleTO)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 Network (com.cloud.network.Network)1 FirewallRule (com.cloud.network.rules.FirewallRule)1 PortForwardingRule (com.cloud.network.rules.PortForwardingRule)1 StaticNat (com.cloud.network.rules.StaticNat)1 AccountVO (com.cloud.user.AccountVO)1