Search in sources :

Example 1 with DhcpHost

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

the class MidoNetElement method addDhcpEntry.

/**
     * From interface DHCPServiceProvider
     */
@Override
public boolean addDhcpEntry(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
    s_logger.debug("addDhcpEntry called with network: " + network.toString() + " nic: " + nic.toString() + " vm: " + vm.toString());
    if (!this.midoInNetwork(network)) {
        return false;
    }
    if (vm.getType() != VirtualMachine.Type.User) {
        return false;
    }
    // Get MidoNet bridge
    Bridge netBridge = getOrCreateNetworkBridge(network);
    // On bridge, get DHCP subnet (ensure it exists)
    ResourceCollection res = netBridge.getDhcpSubnets();
    DhcpSubnet sub = null;
    if (!res.isEmpty()) {
        sub = (DhcpSubnet) res.get(0);
    } else {
        Pair<String, Integer> cidrInfo = NetUtils.getCidr(network.getCidr());
        sub = netBridge.addDhcpSubnet();
        sub.subnetLength(cidrInfo.second());
        sub.subnetPrefix(cidrInfo.first());
        sub.defaultGateway(network.getGateway());
        List<String> dcs = new ArrayList<String>();
        dcs.add(dest.getDataCenter().getDns1());
        sub.dnsServerAddrs(dcs);
        sub.create();
    }
    // On DHCP subnet, add host using host details
    if (sub == null) {
        s_logger.error("Failed to create DHCP subnet on Midonet bridge");
        return false;
    } else {
        // Check if the host already exists - we may just be restarting an existing VM
        boolean isNewDhcpHost = true;
        for (DhcpHost dhcpHost : sub.getDhcpHosts()) {
            if (dhcpHost.getIpAddr().equals(nic.getIPv4Address())) {
                isNewDhcpHost = false;
                break;
            }
        }
        if (isNewDhcpHost) {
            DhcpHost host = sub.addDhcpHost();
            host.ipAddr(nic.getIPv4Address());
            host.macAddr(nic.getMacAddress());
            // This only sets the cloudstack internal name
            host.name(vm.getHostName());
            host.create();
        }
    }
    return true;
}
Also used : DhcpSubnet(org.midonet.client.resource.DhcpSubnet) ArrayList(java.util.ArrayList) DhcpHost(org.midonet.client.resource.DhcpHost) Bridge(org.midonet.client.resource.Bridge) ResourceCollection(org.midonet.client.resource.ResourceCollection)

Example 2 with DhcpHost

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

Aggregations

Bridge (org.midonet.client.resource.Bridge)2 DhcpHost (org.midonet.client.resource.DhcpHost)2 DhcpSubnet (org.midonet.client.resource.DhcpSubnet)2 ResourceCollection (org.midonet.client.resource.ResourceCollection)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 Network (com.cloud.network.Network)1 AccountVO (com.cloud.user.AccountVO)1 AccountDao (com.cloud.user.dao.AccountDao)1 NicProfile (com.cloud.vm.NicProfile)1 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)1 ArrayList (java.util.ArrayList)1 MidonetApi (org.midonet.client.MidonetApi)1 Router (org.midonet.client.resource.Router)1