Network Configuration using ovirt-engine Java SDK

The ovirt-engine can be accessed via the ovirt-engine-sdk-java to perform any rest-api based  action. The ovirt-engine Java SDK is Java 7 compliant and simple to use.

In the following example I’ll demonstrate creation of ‘bond0’ on top of ‘eth4’ and ‘eth5’ as its slaves, and on top of ‘bond0’, a tagged network ‘vlan200’ will be added.

However, the example makes a use of the 3.0 compliant API which is simpler on the one hand, but suffers of few disadvantages:

  • Does not support complex actions (i.e. multiple networks configuration at once)
  • Does not support advanced features:
    1. Multiple default gateways
    2. Synchronize network no host with its logical network definition
    3. Multiple default gateways
    4. Default route
    5. QoS (well… the other api neither)
import org.ovirt.engine.sdk.Api;
import org.ovirt.engine.sdk.decorators.HostNICs;
import org.ovirt.engine.sdk.entities.Bonding;
import org.ovirt.engine.sdk.entities.HostNIC;
import org.ovirt.engine.sdk.entities.Network;
import org.ovirt.engine.sdk.entities.Option;
import org.ovirt.engine.sdk.entities.Options;
import org.ovirt.engine.sdk.entities.Slaves;

public class CreateBondExample {

    public static void main(String[] args) throws Exception {

        try (Api api = new Api("http://localhost:8080/api",
                "admin@internal",
                "1",
                null, null, null, null, null, null, true)) {

            HostNICs hostNics = api.getHosts().get("venus-vdsb").getHostNics();

            HostNIC bond = new HostNIC();
            bond.setName("bond0");

            // add slaves and bonding options
            Bonding bonding = new Bonding();
            addSlaves(bonding);
            addOptions(bonding);
            bond.setBonding(bonding);

            // add network to be configured on top of the slave
            Network net = new Network();
            net.setName("vlan200");
            bond.setNetwork(net);

            // add the bond
            hostNics.add(bond);
        }

    }

    /**
     * Adds "BONDING_OPTS='miimon=100 mode=1 primary=eth4'" in /etc/sysconfig/network-scripts/ifcfg-bond0
     */
    private static void addOptions(Bonding bonding) {
        Options options = new Options();
        options.getOptions().add(createOption("miimon", "100"));
        options.getOptions().add(createOption("mode", "1"));
        options.getOptions().add(createOption("primary", "eth4"));
        bonding.setOptions(options);
    }

    public static Option createOption(String name, String value) {
        Option option = new Option();
        option.setName(name);
        option.setValue(value);
        return option;
    }

    /**
     *
     * eth4 ---|
     *         |--- bond0
     * eth5 ---|
     *
     */
    public static void addSlaves(Bonding bonding) {
        Slaves slaves = new Slaves();
        HostNIC slave1 = new HostNIC();
        slave1.setName("eth4");
        HostNIC slave2 = new HostNIC();
        slave2.setName("eth5");
        slaves.getSlaves().add(slave1);
        slaves.getSlaves().add(slave2);
        bonding.setSlaves(slaves);
    }
}

After executing the following, the bond device will be created, as long with the vlan device which will be named as ‘bond0.200’, according to the network’s vlan-id.

In order to update the configured network on the nic, the referred updated device should be the device on which the network is configured (‘bond0.200’ in this case). The following example will demonstrate setting a static IP address for ‘vlan200’ network on top of ‘bond0’:

import org.ovirt.engine.sdk.Api;
import org.ovirt.engine.sdk.decorators.HostNIC;
import org.ovirt.engine.sdk.entities.IP;
import org.ovirt.engine.sdk.entities.Network;

public class UpdateVlanNetworkOverBondExample {

    public static void main(String[] args) throws Exception {

        try (Api api = new Api("http://localhost:8080/api",
                "admin@internal",
                "1",
                null, null, null, null, null, null, true)) {

            HostNIC bondNetwork = api.getHosts().get("venus-vdsb").getHostNics().get("bond0.200");

            // add network to be configured on top of the slave
            Network net = new Network();
            net.setName("vlan200");
            IP ip = new IP();
            ip.setAddress("192.168.1.151");
            ip.setNetmask("255.255.255.0");
            bondNetwork.setIp(ip);
            bondNetwork.setBootProtocol("static");
            bondNetwork.setNetwork(net);

            // update the network
            bondNetwork.update();
        }
    }
}

And last example will demonstrate attaching network ‘red’ to ‘eth4’ interface:

import org.ovirt.engine.sdk.Api;
import org.ovirt.engine.sdk.decorators.HostNIC;
import org.ovirt.engine.sdk.entities.Action;
import org.ovirt.engine.sdk.entities.Network;

public class AddNetworkToNic {

    public static void main(String[] args) throws Exception {

        try (Api api = new Api("http://localhost:8080/api",
                "admin@internal",
                "1",
                null, null, null, null, null, null, true)) {

            HostNIC nic = api.getHosts().get("venus-vdsb").getHostNics().get("eth4");

            // add network to nic
            Network net = new Network();
            net.setName("red");
            nic.setNetwork(net);
            Action action = new Action();
            action.setNetwork(net);

            // update the network
            nic.attach(action);
        }
    }
}

However the above could be simplified by using the ‘SetupNetworks’ API which was introduced before and demonstrated by the Python SDK. An example using the Java SDK will be published in the next post.

Leave a comment