Search in sources :

Example 1 with MidonetApi

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

use of org.midonet.client.MidonetApi in project cloudstack by apache.

the class MidoNetElementTest method testAddDhcpEntry.

/*
     * Test the standard case of addDhcpEntry with no errors.
     */
public void testAddDhcpEntry() {
    //mockMgmt
    MidonetApi api = mock(MidonetApi.class, RETURNS_DEEP_STUBS);
    //mockDhcpHost
    DhcpHost mockDhcpHost = mock(DhcpHost.class);
    //mockHostCollection
    ResourceCollection<DhcpHost> hosts = new ResourceCollection<DhcpHost>(new ArrayList<DhcpHost>());
    //mockDhcpSubnet
    DhcpSubnet mockSub = mock(DhcpSubnet.class);
    when(mockSub.addDhcpHost()).thenReturn(mockDhcpHost);
    when(mockSub.getDhcpHosts()).thenReturn(hosts);
    //mockSubnetCollection
    ResourceCollection mockSubnetCollection = mock(ResourceCollection.class);
    when(mockSubnetCollection.get(anyInt())).thenReturn(mockSub);
    //mockBridge
    Bridge mockBridge = mock(Bridge.class);
    when(api.addBridge().tenantId(anyString()).name(anyString()).create()).thenReturn(mockBridge);
    when(mockBridge.getDhcpSubnets()).thenReturn(mockSubnetCollection);
    //mockRouter
    Router mockRouter = mock(Router.class);
    when(api.addRouter().tenantId(anyString()).name(anyString()).create()).thenReturn(mockRouter);
    //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.Guest);
    //mockAccountDao
    AccountDao mockAccountDao = mock(AccountDao.class);
    AccountVO mockAccountVO = mock(AccountVO.class);
    when(mockAccountDao.findById(anyLong())).thenReturn(mockAccountVO);
    when(mockAccountVO.getUuid()).thenReturn("1");
    //mockNic
    NicProfile mockNic = mock(NicProfile.class);
    when(mockNic.getIPv4Address()).thenReturn("10.10.10.170");
    when(mockNic.getMacAddress()).thenReturn("02:00:73:3e:00:01");
    when(mockNic.getName()).thenReturn("Fake Name");
    //mockVm
    @SuppressWarnings("unchecked") VirtualMachineProfile mockVm = mock(VirtualMachineProfile.class);
    when(mockVm.getType()).thenReturn(VirtualMachine.Type.User);
    MidoNetElement elem = new MidoNetElement();
    elem.setMidonetApi(api);
    elem.setAccountDao(mockAccountDao);
    boolean result = false;
    try {
        result = elem.addDhcpEntry(mockNetwork, mockNic, mockVm, 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 : Router(org.midonet.client.resource.Router) AccountDao(com.cloud.user.dao.AccountDao) NicProfile(com.cloud.vm.NicProfile) DhcpHost(org.midonet.client.resource.DhcpHost) AccountVO(com.cloud.user.AccountVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) MidonetApi(org.midonet.client.MidonetApi) DhcpSubnet(org.midonet.client.resource.DhcpSubnet) Network(com.cloud.network.Network) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) Bridge(org.midonet.client.resource.Bridge) ResourceCollection(org.midonet.client.resource.ResourceCollection)

Example 3 with MidonetApi

use of org.midonet.client.MidonetApi in project cloudstack by apache.

the class MidoNetElement method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    super.configure(name, params);
    String routerIdValue = _configDao.getValue(Config.MidoNetProviderRouterId.key());
    if (routerIdValue != null)
        _providerRouterId = UUID.fromString(routerIdValue);
    String value = _configDao.getValue(Config.MidoNetAPIServerAddress.key());
    if (value == null) {
        throw new ConfigurationException("Could not find midonet API location in config");
    }
    if (this.api == null) {
        s_logger.info("midonet API server address is  " + value);
        setMidonetApi(new MidonetApi(value));
        this.api.enableLogging();
    }
    return true;
}
Also used : ConfigurationException(javax.naming.ConfigurationException) MidonetApi(org.midonet.client.MidonetApi)

Example 4 with MidonetApi

use of org.midonet.client.MidonetApi in project cloudstack by apache.

the class MidoNetVifDriver method plug.

@Override
public LibvirtVMDef.InterfaceDef plug(NicTO nic, String guestOsType, String nicAdapter) throws InternalErrorException, LibvirtException {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("nic=" + nic);
    }
    LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef();
    String trafficLabel = nic.getName();
    if (nic.getBroadcastType() == Networks.BroadcastDomainType.Mido && (nic.getType() == Networks.TrafficType.Guest || nic.getType() == Networks.TrafficType.Public)) {
        /*
            * create the tap.
            */
        String tapName = addTap();
        /*
            * grab the tenant id and the network id from the Broadcast URI.
            * We need to pluck the values out of the String. The string
            * should look like "mido://[tenant_id].[bridge_name]"
            */
        MultivaluedMap qNet = new MultivaluedMapImpl();
        String nicAuthority = nic.getBroadcastUri().getAuthority();
        String tenantId = nicAuthority.split("\\.")[0];
        qNet.add("tenant_id", tenantId);
        String url = nicAuthority.split("\\.")[1];
        String netName = url.split(":")[0];
        MidonetApi api = new MidonetApi(_midoApiLocation);
        api.enableLogging();
        for (Bridge b : api.getBridges(qNet)) {
            if (b.getName().equals(netName)) {
                for (BridgePort p : b.getPorts()) {
                    UUID pvif = p.getVifId();
                    if (pvif != null && p.getVifId().toString().equals(nic.getUuid())) {
                        getMyHost(api).addHostInterfacePort().interfaceName(tapName).portId(p.getId()).create();
                        break;
                    }
                }
            }
        }
        intf.defEthernet(tapName, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), "");
    } else {
        throw new InternalErrorException("Only NICs of BroadcastDomain type Mido are supported by the MidoNetVifDriver");
    }
    return intf;
}
Also used : BridgePort(org.midonet.client.resource.BridgePort) LibvirtVMDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) InternalErrorException(com.cloud.exception.InternalErrorException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UUID(java.util.UUID) Bridge(org.midonet.client.resource.Bridge) MidonetApi(org.midonet.client.MidonetApi)

Aggregations

MidonetApi (org.midonet.client.MidonetApi)4 Bridge (org.midonet.client.resource.Bridge)3 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)2 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)2 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)2 Network (com.cloud.network.Network)2 AccountVO (com.cloud.user.AccountVO)2 AccountDao (com.cloud.user.dao.AccountDao)2 UUID (java.util.UUID)2 BridgePort (org.midonet.client.resource.BridgePort)2 ResourceCollection (org.midonet.client.resource.ResourceCollection)2 Router (org.midonet.client.resource.Router)2 InternalErrorException (com.cloud.exception.InternalErrorException)1 LibvirtVMDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef)1 NicProfile (com.cloud.vm.NicProfile)1 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)1 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)1 ConfigurationException (javax.naming.ConfigurationException)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 DhcpHost (org.midonet.client.resource.DhcpHost)1