Search in sources :

Example 1 with BridgePort

use of org.midonet.client.resource.BridgePort 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 BridgePort

use of org.midonet.client.resource.BridgePort 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 BridgePort

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

the class MidoNetElement method release.

@Override
public boolean release(Network network, NicProfile nic, VirtualMachineProfile vm, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
    s_logger.debug("release called with network: " + network.toString() + " nic: " + nic.toString() + " vm: " + vm.toString());
    if (!midoInNetwork(network)) {
        return false;
    }
    UUID nicUUID = getNicUUID(nic);
    if (nic.getTrafficType() == Networks.TrafficType.Guest || (nic.getTrafficType() == Networks.TrafficType.Public && nic.getBroadcastType() == Networks.BroadcastDomainType.Mido)) {
        // Seems like a good place to remove the port in midonet
        Bridge netBridge = getOrCreateNetworkBridge(network);
        Router providerRouter = api.getRouter(_providerRouterId);
        //remove the routes associated with this IP address
        for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
            String routeDstAddr = route.getDstNetworkAddr();
            if (routeDstAddr != null && routeDstAddr.equals(nic.getIPv4Address())) {
                route.delete();
            }
        }
        for (BridgePort p : netBridge.getPorts()) {
            UUID vifID = p.getVifId();
            if (vifID != null && vifID.equals(nicUUID)) {
                // This is the MidoNet port which corresponds to the NIC we are releasing
                // Set VIF ID to null
                p.vifId(null).update();
                // Delete port
                p.delete();
            }
        }
    }
    return true;
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) Router(org.midonet.client.resource.Router) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) UUID(java.util.UUID) Bridge(org.midonet.client.resource.Bridge) Route(org.midonet.client.resource.Route)

Example 4 with BridgePort

use of org.midonet.client.resource.BridgePort 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 BridgePort

use of org.midonet.client.resource.BridgePort 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

BridgePort (org.midonet.client.resource.BridgePort)8 RouterPort (org.midonet.client.resource.RouterPort)6 Port (org.midonet.client.resource.Port)5 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)4 UUID (java.util.UUID)4 Bridge (org.midonet.client.resource.Bridge)4 Router (org.midonet.client.resource.Router)4 Route (org.midonet.client.resource.Route)3 MidonetApi (org.midonet.client.MidonetApi)2 RuleChain (org.midonet.client.resource.RuleChain)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 LibvirtVMDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef)1 Network (com.cloud.network.Network)1 AccountVO (com.cloud.user.AccountVO)1 AccountDao (com.cloud.user.dao.AccountDao)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 ResourceCollection (org.midonet.client.resource.ResourceCollection)1